mirror of
https://git.stillkill.net/Stillkill/GiteaPublish.git
synced 2026-10-22 13:32:19 +01:00
Workflow test
This commit is contained in:
1 parent
518159ea7f
commit
ef25216552
17 files changed
+876
-25
No files matched your search
@@ -3,32 +3,50 @@ using Spectre.Console;
|
||||
|
||||
namespace GiteaPublish.Gitea;
|
||||
|
||||
public class ArchPackageRepo(HttpClient httpClient, IAnsiConsole ansiConsole, BasicAuthModel basicAuth)
|
||||
: IPackageRepo
|
||||
public class ArchPackageRepo : IPackageRepo
|
||||
{
|
||||
public async Task<bool> PublishPackageAsync(string owner, string repository, string filePath)
|
||||
private BasicAuthModel BasicAuth { get; set; } = new BasicAuthModel();
|
||||
|
||||
private readonly IAnsiConsole _console;
|
||||
private readonly HttpClient _httpClient;
|
||||
|
||||
public ArchPackageRepo(HttpClient httpClient, IAnsiConsole ansiConsole)
|
||||
{
|
||||
ansiConsole.MarkupLine($"[blue]Publishing package {filePath} to {owner}/{repository}...[/]");
|
||||
HttpRequestMessage request = new(HttpMethod.Put, $"api/packages/{owner}/arch/{repository}");
|
||||
_console = ansiConsole;
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public async Task<bool> PublishPackageAsync(string owner, string repository, string filePath, string baseUrl)
|
||||
{
|
||||
_httpClient.BaseAddress = new Uri(baseUrl.TrimEnd('/'));
|
||||
_console.MarkupLine($"[blue]Publishing package {filePath} to {owner}/{repository}...[/]");
|
||||
HttpRequestMessage request = new(HttpMethod.Put, $"/api/packages/{owner}/arch/{repository}");
|
||||
request.Content = new ByteArrayContent(await File.ReadAllBytesAsync(filePath));
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", basicAuth.AuthHeader);
|
||||
HttpResponseMessage response = await httpClient.SendAsync(request);
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", BasicAuth.AuthHeader);
|
||||
HttpResponseMessage response = await _httpClient.SendAsync(request);
|
||||
if (response.IsSuccessStatusCode) return true;
|
||||
ansiConsole.MarkupLine($"[red]Failed to publish package: {response.StatusCode}[/]");
|
||||
ansiConsole.MarkupLine($"[red]{(await response.Content.ReadAsStringAsync()).EscapeMarkup()}[/]");
|
||||
_console.MarkupLine($"[red]Failed to publish package: {response.StatusCode}[/]");
|
||||
_console.MarkupLine($"[red]{(await response.Content.ReadAsStringAsync()).EscapeMarkup()}[/]");
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<bool> DeletePackageAsync(string owner, string repository, string architecture, string packageName,
|
||||
string packageVersion)
|
||||
public async Task<bool> DeletePackageAsync(string owner, string repository, string packageName,
|
||||
string packageVersion, string baseUrl)
|
||||
{
|
||||
_httpClient.BaseAddress = new Uri(baseUrl.TrimEnd('/'));
|
||||
HttpRequestMessage request = new(HttpMethod.Delete,
|
||||
$"api/packages/{owner}/arch/{repository}/{architecture}/{packageName}/{packageVersion}");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", basicAuth.AuthHeader);
|
||||
HttpResponseMessage response = await httpClient.SendAsync(request);
|
||||
$"/api/packages/{owner}/arch/{repository}/{packageName}/{packageVersion}");
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", BasicAuth.AuthHeader);
|
||||
HttpResponseMessage response = await _httpClient.SendAsync(request);
|
||||
if (response.IsSuccessStatusCode) return true;
|
||||
ansiConsole.MarkupLine($"[red]Failed to delete package: {response.StatusCode}[/]");
|
||||
ansiConsole.MarkupLine($"[red]{(await response.Content.ReadAsStringAsync()).EscapeMarkup()}[/]");
|
||||
_console.MarkupLine($"[red]Failed to delete package: {response.StatusCode}[/]");
|
||||
_console.MarkupLine($"[red]{(await response.Content.ReadAsStringAsync()).EscapeMarkup()}[/]");
|
||||
return false;
|
||||
}
|
||||
|
||||
public void SetAuthentication(BasicAuthModel basicAuth)
|
||||
{
|
||||
BasicAuth = basicAuth;
|
||||
_console.MarkupLine("[green]Authentication set.[/]");
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,16 @@ public class BasicAuthModel
|
||||
{
|
||||
public string AuthHeader => Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{_username}:{_password}"));
|
||||
|
||||
private readonly string _username;
|
||||
private readonly string _password;
|
||||
private readonly string _username = string.Empty;
|
||||
private readonly string _password = string.Empty;
|
||||
|
||||
public BasicAuthModel(string username, string password)
|
||||
{
|
||||
_username = username;
|
||||
_password = password;
|
||||
}
|
||||
|
||||
public BasicAuthModel()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ public interface IPackageRepo
|
||||
/// <param name="repository">The name of the repository</param>
|
||||
/// <param name="filePath">The path to the package file</param>
|
||||
/// <returns>A bool representing whether the task was successful</returns>
|
||||
public Task<bool> PublishPackageAsync(string owner, string repository, string filePath);
|
||||
public Task<bool> PublishPackageAsync(string owner, string repository, string filePath, string baseUrl);
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -21,5 +21,11 @@ public interface IPackageRepo
|
||||
/// <param name="packageName">The name of the package</param>
|
||||
/// <param name="packageVersion">The version of the package</param>
|
||||
/// <returns>A bool representing whether the task was successful</returns>
|
||||
public Task<bool> DeletePackageAsync(string owner, string repository, string architecture, string packageName, string packageVersion);
|
||||
public Task<bool> DeletePackageAsync(string owner, string architecture, string packageName, string packageVersion, string baseUrl);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the authentication for the package repository. This method should be called before any operations that require authentication (e.g., publishing or deleting packages). The BasicAuthModel contains the necessary information for basic authentication, such as the username and password.
|
||||
/// </summary>
|
||||
/// <param name="basicAuth"></param>
|
||||
public void SetAuthentication(BasicAuthModel basicAuth);
|
||||
}
|
||||
Reference in new issue
Block a user