Skip to content

Commit

Permalink
Clean up the project for release
Browse files Browse the repository at this point in the history
Signed-off-by: Brend Smits <brend.smits@philips.com>
  • Loading branch information
Brend-Smits committed Oct 29, 2021
1 parent 1c7e949 commit 71b6844
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 99 deletions.
36 changes: 12 additions & 24 deletions Action/Domain/MediumCreatedPost.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace PostMediumGitHubAction.Domain
{
public class MediumCreatedPost
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("id")] public string Id { get; set; }

[JsonPropertyName("title")]
public string Title { get; set; }
[JsonPropertyName("title")] public string Title { get; set; }

[JsonPropertyName("authorId")]
public string AuthorId { get; set; }
[JsonPropertyName("authorId")] public string AuthorId { get; set; }

[JsonPropertyName("url")]
public string Url { get; set; }
[JsonPropertyName("url")] public string Url { get; set; }

[JsonPropertyName("canonicalUrl")]
public string CanonicalUrl { get; set; }
[JsonPropertyName("canonicalUrl")] public string CanonicalUrl { get; set; }

[JsonPropertyName("publishStatus")]
public string PublishStatus { get; set; }
[JsonPropertyName("publishStatus")] public string PublishStatus { get; set; }

[JsonPropertyName("license")]
public string License { get; set; }
[JsonPropertyName("license")] public string License { get; set; }

[JsonPropertyName("licenseUrl")]
public string LicenseUrl { get; set; }
[JsonPropertyName("licenseUrl")] public string LicenseUrl { get; set; }

[JsonPropertyName("tags")]
public List<string> Tags { get; set; }
[JsonPropertyName("tags")] public List<string> Tags { get; set; }

[JsonPropertyName("publicationId")]
public string PublicationId { get; set; }
[JsonPropertyName("publicationId")] public string PublicationId { get; set; }
}
}
}
1 change: 0 additions & 1 deletion Action/Domain/MediumError.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace PostMediumGitHubAction.Domain
Expand Down
1 change: 0 additions & 1 deletion Action/Domain/MediumPublication.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace PostMediumGitHubAction.Domain
Expand Down
6 changes: 3 additions & 3 deletions Action/PostMediumGitHubAction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="DotNetEnv" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="DotNetEnv" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.11.1" />
</ItemGroup>

</Project>
5 changes: 1 addition & 4 deletions Action/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using CommandLine;
using DotNetEnv;
using PostMediumGitHubAction.Services;

Expand Down
29 changes: 13 additions & 16 deletions Action/Services/ConfigureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,32 @@ public ConfigureService(string[] args)
{
ConfigureApplication(args);
}

/// <summary>
/// Configure the application with correct settings.
/// Configure the application with correct settings.
/// </summary>
private void ConfigureApplication(string[] args)
{
Program.Settings.File = Environment.GetEnvironmentVariable(nameof(Program.Settings.File));
Program.Settings.Content = Environment.GetEnvironmentVariable(nameof(Program.Settings.Content));
Program.Settings.ContentFormat = Environment.GetEnvironmentVariable(nameof(Program.Settings.ContentFormat));
Program.Settings.CanonicalUrl = Environment.GetEnvironmentVariable(nameof(Program.Settings.CanonicalUrl));
Program.Settings.IntegrationToken = Environment.GetEnvironmentVariable(nameof(Program.Settings.IntegrationToken));
Program.Settings.IntegrationToken =
Environment.GetEnvironmentVariable(nameof(Program.Settings.IntegrationToken));
Program.Settings.License = Environment.GetEnvironmentVariable(nameof(Program.Settings.License));
Program.Settings.NotifyFollowers =
Convert.ToBoolean(Environment.GetEnvironmentVariable(nameof(Program.Settings.NotifyFollowers)));
Program.Settings.PublicationId =
Environment.GetEnvironmentVariable(nameof(Program.Settings.PublicationId));
Program.Settings.PublicationName = Environment.GetEnvironmentVariable(nameof(Program.Settings.PublicationName));
Program.Settings.PublicationName =
Environment.GetEnvironmentVariable(nameof(Program.Settings.PublicationName));
Program.Settings.PublishStatus = Environment.GetEnvironmentVariable(nameof(Program.Settings.PublishStatus));
Program.Settings.Tags = Environment.GetEnvironmentVariable(nameof(Program.Settings.Tags))?.Split(',');
Program.Settings.Title = Environment.GetEnvironmentVariable(nameof(Program.Settings.Title));

// Command Line arguments will overwrite environment file.
if (args.Length > 0)
{
Parser.Default.ParseArguments<Settings>(args).WithParsed(s =>
{
Program.Settings = s;
Console.WriteLine(s.IntegrationToken);
});
}
Parser.Default.ParseArguments<Settings>(args).WithParsed(s => { Program.Settings = s; });

Program.Client = new HttpClient
{
Expand All @@ -57,30 +54,30 @@ private void ConfigureApplication(string[] args)
}

/// <summary>
/// Checks if settings are filled in correctly.
/// Checks if settings are filled in correctly.
/// </summary>
private void CheckForValidSettings()
{
if (string.IsNullOrEmpty(Program.Settings.IntegrationToken))
throw new ArgumentNullException(nameof(Program.Settings.IntegrationToken),
$"The {nameof(Program.Settings.IntegrationToken)} parameter was not set successfully.");

if (string.IsNullOrEmpty(Program.Settings.PublicationId) &&
string.IsNullOrEmpty(Program.Settings.PublicationName))
throw new ArgumentNullException(nameof(Program.Settings.PublicationId),
"Either the parameter PublicationId or PublicationName should be filled in.");

if (string.IsNullOrEmpty(Program.Settings.Title))
throw new ArgumentNullException(nameof(Program.Settings.Title),
$"The {nameof(Program.Settings.Title)} parameter was not set successfully.");

if (string.IsNullOrEmpty(Program.Settings.ContentFormat))
throw new ArgumentNullException(nameof(Program.Settings.ContentFormat),
$"The {nameof(Program.Settings.ContentFormat)} parameter was not set successfully.");

if (string.IsNullOrEmpty(Program.Settings.File) && string.IsNullOrEmpty(Program.Settings.Content))
throw new ArgumentNullException(nameof(Program.Settings.Content),
"Either the parameter Content or File should be filled in.");
}
}
}
}
18 changes: 10 additions & 8 deletions Action/Services/MediumService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public async Task SubmitNewContentAsync()
SetWorkflowOutputs(post);
}

/// <summary>
/// Retrieves current authenticated user
/// </summary>
/// <summary>
/// Retrieves current authenticated user
/// </summary>
/// <returns>Medium User</returns>
public async Task<User> GetCurrentMediumUserAsync()
{
Expand Down Expand Up @@ -97,9 +97,10 @@ await response.Content.ReadAsByteArrayAsync())
.RootElement.EnumerateObject().First().Value.ToString());
throw new Exception("Something went wrong when posting: " + errors[0].Message);
}

MediumCreatedPost createdPostData = JsonSerializer.Deserialize<MediumCreatedPost>(
JsonDocument.Parse(
await response.Content.ReadAsByteArrayAsync())
await response.Content.ReadAsByteArrayAsync())
.RootElement.EnumerateObject().First().Value.ToString());
return createdPostData;
}
Expand All @@ -116,11 +117,12 @@ public async Task<string> ReadFileFromPath(string filePath)
return result;
}

/// <summary>
/// <summary>
/// Sets the output variables in the GitHub workflow
/// See: https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#setting-an-output-parameter
/// </summary>
/// <param name="post">Newly created post that is used to set output variables</param>
/// See:
/// https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions#setting-an-output-parameter
/// </summary>
/// <param name="post">Newly created post that is used to set output variables</param>
/// <returns></returns>
public void SetWorkflowOutputs(MediumCreatedPost post)
{
Expand Down
57 changes: 35 additions & 22 deletions Action/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,59 @@
namespace PostMediumGitHubAction
{
public class Settings
{
{
[Option('e', "title", Required = true, HelpText = "The post's title.")]
public string Title { get; set; }

[Option('a', "tags", Required = true, HelpText = "The post’s tags.", Separator = ',')]
public IEnumerable<string> Tags { get; set; }
public IEnumerable<string> Tags { get; set; }

[Option('u', "canonical-url", Required = false, HelpText = "The canonical URL of the post. " +
"If canonicalUrl was not specified in the creation of the post, this field will not be present.")]
public string CanonicalUrl { get; set; }
"If canonicalUrl was not specified in the creation of the post, this field will not be present.")]
public string CanonicalUrl { get; set; }

[Option('s', "publish-status", Required = false, HelpText = "The publish status of the post. Valid values are: Draft, Public or Unlisted")]
public string PublishStatus { get; set; } = "draft";

[Option('s', "publish-status", Required = false,
HelpText = "The publish status of the post. Valid values are: Draft, Public or Unlisted")]
public string PublishStatus { get; set; } = "draft";

[Option('l', "license", Required = false, HelpText = "The license of the post. Valid values are all-rights-reserved, " +
"cc-40-by, cc-40-by-sa, cc-40-by-nd, cc-40-by-nc, cc-40-by-nc-nd, cc-40-by-nc-sa, cc-40-zero, public-domain.")]
public string License { get; set; } = "all-rights-reserved";

[Option('p', "publication-id", Required = false, HelpText = "The id of the publication the post is being created under. If you do not know the Id, use PublicationName")]
[Option('l', "license", Required = false, HelpText =
"The license of the post. Valid values are all-rights-reserved, " +
"cc-40-by, cc-40-by-sa, cc-40-by-nd, cc-40-by-nc, cc-40-by-nc-nd, cc-40-by-nc-sa, cc-40-zero, public-domain.")]
public string License { get; set; } = "all-rights-reserved";

[Option('p', "publication-id", Required = false,
HelpText =
"The id of the publication the post is being created under. If you do not know the Id, use PublicationName")]
public string PublicationId { get; set; }

[Option('n', "publication-name", Required = false, HelpText = "The name of the publication the post is being created under. Either PublicationName of PublicationId should be set.")]
[Option('n', "publication-name", Required = false,
HelpText =
"The name of the publication the post is being created under. Either PublicationName of PublicationId should be set.")]
public string PublicationName { get; set; }

[Option('f', "notify-followers", Required = false, HelpText = "Whether to notify followers that the user has published.")]
[Option('f', "notify-followers", Required = false,
HelpText = "Whether to notify followers that the user has published.")]
public bool NotifyFollowers { get; set; } = false;

[Option('o', "content-format", Required = true, HelpText = "The format of the \"content\" field. There are two valid values, \"html\", and \"markdown\"")]
public string ContentFormat { get; set; }
[Option('o', "content-format", Required = true,
HelpText = "The format of the \"content\" field. There are two valid values, \"html\", and \"markdown\"")]
public string ContentFormat { get; set; }

[Option('c', "content", Required = false, HelpText = "The body of the post, in a valid, semantic, HTML fragment, or Markdown. " +
"Further markups may be supported in the future. " +
"If you want your title to appear on the post page, you must also include it as part of the post content.")]
public string Content { get; set; }
[Option('c', "content", Required = false, HelpText =
"The body of the post, in a valid, semantic, HTML fragment, or Markdown. " +
"Further markups may be supported in the future. " +
"If you want your title to appear on the post page, you must also include it as part of the post content.")]
public string Content { get; set; }

[Option('i', "file", Required = false, HelpText = "Path to the file that should be read and used as content. If this is set, this will overrule the Content property.")]
public string File { get; set; }
[Option('i', "file", Required = false,
HelpText =
"Path to the file that should be read and used as content. If this is set, this will overrule the Content property.")]
public string File { get; set; }

[Option('t', "integration-token", Required = true, HelpText = "The token that should be used to authenticate in the API. Token can be retrieved at https://medium.com/me/settings under \"Integration Token\"")]
[Option('t', "integration-token", Required = true,
HelpText =
"The token that should be used to authenticate in the API. Token can be retrieved at https://medium.com/me/settings under \"Integration Token\"")]
public string IntegrationToken { get; set; }
}
}
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<p align="right">(<a href="#top">back to top</a>)</p>

## State
> Work in progress, might not be functional yet
> Functional, but work in progress. Use at your own risk.
## Usage

Expand All @@ -50,37 +50,35 @@ The easiest way to use this action is to add the following into your workflow fi
Tags: "test,tag"
Title: "Test post via GH Actions to Medium"
```
## Inputs
| parameter | description | required | default |
| - | - | - | - |
| IntegrationToken | Medium's Integration Token. Token can be retrieved at medium.com, settings section, under 'Integration Token.' | `true` | |
| Content | Content to add in the post, can be either HTML or Markdown. Use either this parameter, or the file parameter. | `false` | |
| ContentFormat | The format of the content field. There are two valid values, html, and markdown. | `true` | |
| File | Absolute path to the file to use as content, can be either HTML or Markdown. Use either this parameter, or the content parameter. | `false` | |
| PublishStatus | Post's status. Valid values are 'draft', 'public', or 'unlisted'. | `false` | draft |
| NotifyFollowers | Whether to notify followers that the user has published. | `false` | false |
| License | Post's license. Valid values are 'all-rights-reserved', 'cc-40-by', 'cc-40-by-sa', 'cc-40-by-nd', 'cc-40-by-nc', 'cc-40-by-nc-nd', 'cc-40-by-nc-sa', 'cc-40-zero', 'public-domain'. | `false` | all-rights-reserved |
| PublicationName | The name of the publication the post is being created under. Either PublicationName of PublicationId should be set. | `false` | |
| PublicationId | The id of the publication the post is being created under. If you do not know the Id, use PublicationName. | `false` | |
| CanonicalUrl | The canonical URL of the post. If canonicalUrl was not specified in the creation of the post, this field will not be present. | `false` | |
| Tags | The post’s tags. Provide a comma separated string without spaces. | `true` | |
| Title | The post's title. | `true` | |
| integration_token | Medium's Integration Token. Token can be retrieved at medium.com, settings section, under 'Integration Token.' | `true` | |
| content | Content to add in the post, can be either HTML or Markdown. Use either this parameter, or the file parameter. | `false` | |
| content_format | The format of the content field. There are two valid values, html, and markdown. | `true` | |
| file | Absolute path to the file to use as content, can be either HTML or Markdown. Use either this parameter, or the content parameter. | `false` | |
| publish_status | Post's status. Valid values are 'draft', 'public', or 'unlisted'. | `false` | draft |
| notify_followers | Whether to notify followers that the user has published. | `false` | false |
| license | Post's license. Valid values are 'all-rights-reserved', 'cc-40-by', 'cc-40-by-sa', 'cc-40-by-nd', 'cc-40-by-nc', 'cc-40-by-nc-nd', 'cc-40-by-nc-sa', 'cc-40-zero', 'public-domain'. | `false` | all-rights-reserved |
| publication_name | The name of the publication the post is being created under. Either PublicationName of PublicationId should be set. | `false` | |
| publication_id | The id of the publication the post is being created under. If you do not know the Id, use PublicationName. | `false` | |
| canonical_url | The canonical URL of the post. If canonicalUrl was not specified in the creation of the post, this field will not be present. | `false` | |
| tags | The post’s tags. Provide a comma separated string without spaces. | `true` | |
| title | The post's title. | `true` | |


## Outputs

| parameter | description |
| - | - |
| id | ID of the Medium post. |
| authorId | Author ID of the post creator. |
| canonicalUrl | Canonical URL of the post. |
| author_id | Author ID of the post creator. |
| canonical_url | Canonical URL of the post. |
| license | License of the post, can be empty at times. |
| licenseUrl | License url of the post, Medium uses this under the hood. |
| publicationId | Id of the publication which the post is created under. |
| publicationStatus | Publication status of the post. |
| license_url | License url of the post, Medium uses this under the hood. |
| publication_id | Id of the publication which the post is created under. |
| publication_status | Publication status of the post. |
| title | Title of the post. |
| tags | Tags of the post, comma separated. |
| url | URL to the Medium post. |
Expand Down

0 comments on commit 71b6844

Please sign in to comment.