Skip to content

Commit

Permalink
Settings are now checked at proper stage
Browse files Browse the repository at this point in the history
Checking for valid settings should be done before executing logic, to allow for different configurations

Signed-off-by: Brend Smits <[email protected]>
  • Loading branch information
Brend-Smits committed Nov 26, 2021
1 parent db15da5 commit cbab270
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Action/Domain/MediumNewPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public class Post
[JsonPropertyName("content")] public string Content { get; set; }

[JsonPropertyName("contentFormat")] public string ContentFormat { get; set; }
[JsonPropertyName("canonicalUrl")] public string CanonicalUrl { get; set; }
[JsonPropertyName("license")] public string License { get; set; }
}
}
11 changes: 3 additions & 8 deletions Action/Services/ConfigureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public ConfigureService(string[] args)

/// <summary>
/// Configure the application with correct settings.
/// First Set environment variables
/// Command line arguments will then override environment variables
/// </summary>
private void ConfigureApplication(string[] args)
{
Expand Down Expand Up @@ -49,13 +51,6 @@ private void ConfigureApplication(string[] args)
};
Program.Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Program.Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {Program.Settings.IntegrationToken}");

CheckForValidSettings();

// Ensure lower case, API is case sensitive sadly
Program.Settings.License = Program.Settings.License?.ToLower();
Program.Settings.PublishStatus = Program.Settings.PublishStatus?.ToLower();
Program.Settings.ContentFormat = Program.Settings.ContentFormat?.ToLower();
}

/// <summary>
Expand Down Expand Up @@ -97,7 +92,7 @@ public void OverrideSettings(Settings settingsToReplace)
/// <summary>
/// Checks if settings are filled in correctly.
/// </summary>
private void CheckForValidSettings()
public void CheckForValidSettings()
{
if (string.IsNullOrEmpty(Program.Settings.IntegrationToken))
throw new ArgumentNullException(nameof(Program.Settings.IntegrationToken),
Expand Down
11 changes: 10 additions & 1 deletion Action/Services/MediumService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public async Task SubmitNewContentAsync()
await ParseFrontmatter(Program.Settings.Content);
}

_configureService.CheckForValidSettings();

// Ensure lower case, API is case sensitive sadly
Program.Settings.License = Program.Settings.License?.ToLower();
Program.Settings.PublishStatus = Program.Settings.PublishStatus?.ToLower();
Program.Settings.ContentFormat = Program.Settings.ContentFormat?.ToLower();

MediumCreatedPost post = await CreateNewPostUnderPublicationAsync(pub.Id);
SetWorkflowOutputs(post);
}
Expand Down Expand Up @@ -132,7 +139,9 @@ public async Task<MediumCreatedPost> CreateNewPostUnderPublicationAsync(string p
ContentFormat = Program.Settings.ContentFormat,
PublishStatus = Program.Settings.PublishStatus,
Tags = Program.Settings.Tags as string[],
Title = Program.Settings.Title
Title = Program.Settings.Title,
CanonicalUrl = Program.Settings.CanonicalUrl,
License = Program.Settings.License
};
HttpResponseMessage response = await Program.Client.PostAsync($"publications/{publicationId}/posts",
new StringContent(JsonSerializer.Serialize(post), Encoding.UTF8, "application/json"))
Expand Down

0 comments on commit cbab270

Please sign in to comment.