Files
actions/sh.actions/sh.actions.package-cleanup/Service/GiteaPackageService.cs
edraft 0332f08194
All checks were successful
Build on push / prepare (push) Successful in 19s
Build on push / build (push) Successful in 23s
Try generic upload
2026-02-14 17:56:57 +01:00

48 lines
1.6 KiB
C#

namespace sh.actions.package_cleanup.Service;
using System.Net.Http.Json;
using Microsoft.Extensions.Logging;
using sh.actions.package_cleanup.Models;
public class GiteaPackageService(HttpClient httpClient, GiteaConfig config, ILogger<GiteaPackageService> logger)
: IGiteaPackageService
{
public async Task<IEnumerable<GiteaPackage>> GetPackagesByOwnerAsync(CancellationToken cancellationToken = default)
{
try
{
var url = $"{config.GiteaUrl.TrimEnd('/')}/api/v1/packages/{config.Owner}";
logger.LogInformation("Fetching packages from Gitea: {Url}", url);
var request = new HttpRequestMessage(HttpMethod.Get, url);
// Add authentication if token is provided
if (!string.IsNullOrEmpty(config.ApiToken))
{
request.Headers.Add("Authorization", $"token {config.ApiToken}");
}
var response = await httpClient.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
var packages =
await response.Content.ReadFromJsonAsync<List<GiteaPackage>>(cancellationToken: cancellationToken)
?? new List<GiteaPackage>();
logger.LogInformation("Successfully fetched {Count} packages", packages.Count);
return packages;
}
catch (HttpRequestException ex)
{
logger.LogError(ex, "Error fetching packages from Gitea");
throw;
}
catch (Exception ex)
{
logger.LogError(ex, "Unexpected error fetching packages");
throw;
}
}
}