-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f35a71
commit 826f8e2
Showing
7 changed files
with
162 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Actions.Commands; | ||
using FluentAssertions; | ||
|
||
namespace Actions.Tests; | ||
|
||
public class ValidateTagCommandTest | ||
{ | ||
[Theory] | ||
[InlineData("0.1.0", "0.1.0")] | ||
[InlineData("v0.1.0", "0.1.0")] | ||
[InlineData("v10.1.0", "10.1.0")] | ||
public void NormalizeTest(string tag, string expected) | ||
{ | ||
var command = new ValidateTagCommand(); | ||
var actual = command.Normalize(tag); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Theory] | ||
[InlineData("0.1.0", false)] | ||
[InlineData("1.0.0", false)] | ||
[InlineData("1.1.0", false)] | ||
[InlineData("1.2.0", true)] // Current Release Tag is 1.2.0 | ||
[InlineData("1.2.1", true)] | ||
[InlineData("999.0.0", true)] | ||
public async Task ValidateTest(string tag, bool expected) | ||
{ | ||
var command = new ValidateTagCommand(); | ||
var (success, releaseTag) = await command.ValidateTagAsync(tag); | ||
|
||
success.Should().Be(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Actions.Commands; | ||
|
||
public class ValidateTagCommand() | ||
{ | ||
/// <summary> | ||
/// Normalize input tag. If the tag starts with 'v', it will be removed. | ||
/// </summary> | ||
/// <param name="tag"></param> | ||
/// <returns></returns> | ||
public string Normalize(string tag) => tag.StartsWith("v") ? tag.Substring(1) : tag; | ||
|
||
/// <summary> | ||
/// Validate input tag. If the input tag is older than the latest release tag, it will return false. Otherwise, it will return true. | ||
/// </summary> | ||
/// <param name="tag"></param> | ||
/// <returns></returns> | ||
public async Task<(bool result, string releaseTag)> ValidateTagAsync(string tag) | ||
{ | ||
// release_latest=$(gh release list --exclude-drafts --exclude-pre-releases --json tagName,isLatest | jq -c -r ".[] | select(.isLatest == true) | .tagName") | ||
// sorted_latest=$(echo - e "${release_latest}\n${{ steps.trim.outputs.normalized_tag }}" | sort - V | tail - n 1) | ||
var releaseLatests = await "gh release list --exclude-drafts --exclude-pre-releases --json tagName,isLatest"; | ||
var githubReleases = JsonSerializer.Deserialize<GitHubRelease[]>(releaseLatests); | ||
var releaseTag = githubReleases?.SingleOrDefault(x => x.IsLatest)?.TagName; | ||
|
||
if (releaseTag == null) | ||
{ | ||
// no release tag | ||
return (true, ""); | ||
} | ||
|
||
var sortedLatest = new[] { releaseTag, tag }.OrderBy(x => x).Last(); | ||
if (sortedLatest == tag) | ||
{ | ||
// input tag is same or newer than latest tag | ||
return (true, releaseTag); | ||
} | ||
|
||
// input tag is older than latest tag, reverting!! | ||
return (false, releaseTag); | ||
} | ||
|
||
private record GitHubRelease | ||
{ | ||
[JsonPropertyName("tagName")] | ||
public required string TagName { get; init; } | ||
[JsonPropertyName("isLatest")] | ||
public required bool IsLatest { get; init; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters