mirror of
https://git.stillkill.net/Stillkill/GiteaPublish.git
synced 2026-10-22 13:32:19 +01:00
115 lines
4.3 KiB
C#
115 lines
4.3 KiB
C#
using GiteaPublish.Gitea;
|
|
using Spectre.Console;
|
|
using Spectre.Console.Cli;
|
|
|
|
namespace GiteaPublish.Commands.Release;
|
|
|
|
public class PublishReleaseCommand : AsyncCommand<PublishReleaseCommandSettings>
|
|
{
|
|
private readonly ReleaseService _releaseService;
|
|
private readonly IAnsiConsole _console;
|
|
|
|
public PublishReleaseCommand(ReleaseService releaseService, IAnsiConsole console)
|
|
{
|
|
_releaseService = releaseService;
|
|
_console = console;
|
|
}
|
|
|
|
public override async Task<int> ExecuteAsync(CommandContext context, PublishReleaseCommandSettings settings,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
_releaseService.SetAuthorization(settings.GetAuth());
|
|
int releaseId = await _releaseService.CreateReleaseAsync(new CreateReleaseOption(settings), settings.Owner, settings.Repository, settings.BaseUrl);
|
|
if (releaseId != int.MinValue)
|
|
{
|
|
_console.MarkupLine("[green]Release created successfully![/]");
|
|
}
|
|
else
|
|
{
|
|
_console.MarkupLine("[red]Operation Failed.[/]");
|
|
return 1;
|
|
}
|
|
|
|
if (settings.Files.Length == 0)
|
|
{
|
|
_console.MarkupLine("[yellow]No assets to upload.[/]");
|
|
return 0;
|
|
}
|
|
|
|
_console.MarkupLine($"[blue]Uploading {settings.Files.Length} asset(s)...[/]");
|
|
foreach (var file in settings.Files)
|
|
{
|
|
if (!File.Exists(file))
|
|
{
|
|
_console.MarkupLine($"[red]File not found: {file}[/]");
|
|
continue;
|
|
}
|
|
|
|
AddAssetOptions options = new AddAssetOptions(settings.Owner, settings.Repository, releaseId, file);
|
|
|
|
bool assetSuccess =
|
|
await _releaseService.AddAssetAsync(options, settings.BaseUrl);
|
|
if (assetSuccess)
|
|
_console.MarkupLine($"[green]Uploaded asset: {file}[/]");
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public override ValidationResult Validate(CommandContext context, PublishReleaseCommandSettings settings)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(settings.Owner))
|
|
return ValidationResult.Error("Owner is required.");
|
|
if (string.IsNullOrWhiteSpace(settings.Repository))
|
|
return ValidationResult.Error("Repository is required.");
|
|
if (string.IsNullOrWhiteSpace(settings.BaseUrl))
|
|
return ValidationResult.Error("Base URL is required.");
|
|
if (string.IsNullOrWhiteSpace(settings.TagName))
|
|
return ValidationResult.Error("Tag name is required.");
|
|
if (string.IsNullOrWhiteSpace(settings.AuthFile) && (string.IsNullOrWhiteSpace(settings.Username) || string.IsNullOrWhiteSpace(settings.Password)))
|
|
return ValidationResult.Error("Either auth file or username/password must be provided.");
|
|
|
|
return ValidationResult.Success();
|
|
}
|
|
}
|
|
//release publish --auth-file "/home/gitea-package-token.txt" -o Stillkill -r GiteaPublish -b https://git.stillkill.net
|
|
//-e "Release body" -d -n "Release name" -p --tag-message "Tag message" --tag-name "v1.0.0" -tc "master" -f "/home/Downloads/ACTv3.zip" --file "/home/stillkill/Downloads/test.gif"
|
|
|
|
public class PublishReleaseCommandSettings : AuthSettings
|
|
{
|
|
[CommandOption("-o|--owner <OWNER>", isRequired: true)]
|
|
public string Owner { get; set; } = string.Empty;
|
|
|
|
[CommandOption("-r|--repository <REPOSITORY>", isRequired: true)]
|
|
public string Repository { get; set; } = string.Empty;
|
|
|
|
[CommandOption("-b|--base-url <BASE_URL>", isRequired: true)]
|
|
public string BaseUrl { get; set; } = string.Empty;
|
|
|
|
[CommandOption("-e|--body <BODY>")]
|
|
public string? Body { get; set; } = null;
|
|
|
|
[CommandOption("-d|--draft")]
|
|
public bool Draft { get; set; } = false;
|
|
|
|
[CommandOption("-n|--name <NAME>")]
|
|
public string? Name { get; set; } = null;
|
|
|
|
[CommandOption("-p|--prerelease")]
|
|
public bool Prerelease { get; set; } = false;
|
|
|
|
[CommandOption("--tag-message <TAG_MESSAGE>")]
|
|
public string? TagMessage { get; set; } = null;
|
|
|
|
[CommandOption("--tag-name <TAG_NAME>", isRequired: true)]
|
|
public string TagName { get; set; } = string.Empty;
|
|
|
|
[CommandOption("-c|--target-commitish <TARGET_COMMITISH>")]
|
|
public string? TargetCommitish { get; set; } = null;
|
|
|
|
[CommandOption("-f|--file <FILE>")]
|
|
public string[] Files { get; set; } = Array.Empty<string>();
|
|
|
|
|
|
|
|
} |