-
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
174f772
commit 7fecf9c
Showing
6 changed files
with
155 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using Actions.Commands; | ||
using Zx; | ||
|
||
namespace Actions.Tests; | ||
|
||
public class CreateReleaseCommandTest | ||
{ | ||
[Theory] | ||
[InlineData("test.0.1.0", "Ver.test.0.1.0")] | ||
[InlineData("test.1.0.0", "Ver.test.1.0.0")] | ||
[InlineData("test.10.1.0", "Ver.test.10.1.0")] | ||
public async Task ReleaseTest(string tag, string releaseTitle) | ||
{ | ||
if (Environment.GetEnvironmentVariable("CI") is null) | ||
return; | ||
if (Environment.GetEnvironmentVariable("GH_REPO") is null) | ||
throw new Exception("GH_REPO is not set"); | ||
if (Environment.GetEnvironmentVariable("GH_TOKEN") is null) | ||
throw new Exception("GH_TOKEN is not set"); | ||
|
||
var dir = Path.Combine(Path.GetTempPath(), nameof(ReleaseTest)); | ||
var files = Enumerable.Range(0, 3) | ||
.Select(x => Path.Combine(dir, Path.GetTempFileName())) | ||
.ToArray(); | ||
try | ||
{ | ||
CreateFiles(dir, files); | ||
var command = new CreateReleaseCommand(tag, releaseTitle); | ||
await command.CreateReleaseAsync(); | ||
await command.UploadAssetFiles(files); | ||
} | ||
finally | ||
{ | ||
SafeDeleteDir(dir); | ||
|
||
// clean up release | ||
var list = await $"gh release list"; | ||
var exists = SplitByNewLine(list) | ||
.Where(x => x.Contains("Draft")) | ||
.Where(x => x.Contains("Ver.1.1.0")) | ||
.Any(); | ||
if (exists) | ||
{ | ||
await $"gh release delete {tag} --yes --cleanup-tag"; | ||
} | ||
} | ||
} | ||
|
||
private static string[] SplitByNewLine(string stringsValue) => stringsValue.Split(["\r\n", "\n"], StringSplitOptions.RemoveEmptyEntries); | ||
|
||
private void CreateFiles(string dir, string[] files) | ||
{ | ||
if (!Directory.Exists(dir)) | ||
{ | ||
Directory.CreateDirectory(dir); | ||
foreach (var file in files) | ||
{ | ||
File.WriteAllText(file, ""); | ||
} | ||
} | ||
} | ||
|
||
private void SafeDeleteDir(string dir) | ||
{ | ||
if (Directory.Exists(dir)) | ||
Directory.Delete(dir); | ||
} | ||
} |
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,58 @@ | ||
using Actions.Utils; | ||
using static Actions.Utils.ZxHelper; | ||
|
||
namespace Actions.Commands; | ||
|
||
public class CreateReleaseCommand(string tag, string releaseTitle) | ||
{ | ||
/// <summary> | ||
/// Create GitHub Release | ||
/// </summary> | ||
/// <returns></returns> | ||
/// <exception cref="ActionCommandException"></exception> | ||
public async Task CreateReleaseAsync() | ||
{ | ||
// git tag | ||
using (_ = new GitHubActionsGroupLogger("Create git tag")) | ||
{ | ||
await $"git tag {tag}"; | ||
await $"git push origin {tag}"; | ||
} | ||
|
||
// create release | ||
using (_ = new GitHubActionsGroupLogger("Create Release")) | ||
{ | ||
await $"gh release create {tag} --draft --verify-tag --title \"{releaseTitle}\" --generate-notes"; | ||
// wait a while | ||
await Task.Delay(TimeSpan.FromSeconds(5)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Upload asset files to the release | ||
/// </summary> | ||
/// <param name="tag"></param> | ||
/// <param name="assetPaths"></param> | ||
/// <returns></returns> | ||
public async Task UploadAssetFiles(string[] assetPaths) | ||
{ | ||
foreach (var assetPath in assetPaths) | ||
{ | ||
if (GlobFiles.IsGlobPattern(assetPath)) | ||
{ | ||
// Is Wildcard? | ||
foreach (var file in GlobFiles.EnumerateFiles(assetPath)) | ||
{ | ||
using var _ = new GitHubActionsGroupLogger($"Uploading asset. tag: {tag}. assetPath: {file}"); | ||
await $"gh release upload {tag} \"{EscapeArg(file)}\""; | ||
} | ||
} | ||
else | ||
{ | ||
// Is File? | ||
using var _ = new GitHubActionsGroupLogger($"Uploading asset. tag: {tag}. assetPath: {assetPath}"); | ||
await $"gh release upload {tag} \"{EscapeArg(assetPath)}\""; | ||
} | ||
} | ||
} | ||
} |
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