Added package cleanup
This commit is contained in:
@@ -10,22 +10,33 @@ public class GiteaPackageService(
|
||||
HttpClient httpClient)
|
||||
: IGiteaPackageService
|
||||
{
|
||||
private string GetBaseUrl() =>
|
||||
$"{configuration["URL"]?.TrimEnd('/')}/packages/{configuration["OWNER"]}/{configuration["TYPE"]}/{configuration["NAME"]}";
|
||||
|
||||
private void AddAuthorizationHeader(HttpRequestMessage request)
|
||||
{
|
||||
var token = configuration["API_TOKEN"];
|
||||
if (!string.IsNullOrEmpty(token))
|
||||
{
|
||||
request.Headers.Add("Authorization", $"token {token}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<GiteaPackage>> GetPackagesByOwnerAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var baseUrl =
|
||||
$"{configuration["URL"]?.TrimEnd('/')}/packages/{configuration["OWNER"]}/{configuration["TYPE"]}/{configuration["NAME"]}";
|
||||
var baseUrl = GetBaseUrl();
|
||||
var allPackages = new List<GiteaPackage>();
|
||||
var page = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var url = $"{baseUrl}?page={page}";
|
||||
|
||||
logger.LogInformation("Fetching packages from Gitea: {Url}", url);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
request.Headers.Add("Authorization", $"token {configuration["API_TOKEN"]}");
|
||||
AddAuthorizationHeader(request);
|
||||
|
||||
var response = await httpClient.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
@@ -46,7 +57,6 @@ public class GiteaPackageService(
|
||||
}
|
||||
|
||||
logger.LogInformation("Successfully fetched {Count} packages in total", allPackages.Count);
|
||||
|
||||
return allPackages;
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
@@ -65,30 +75,30 @@ public class GiteaPackageService(
|
||||
{
|
||||
try
|
||||
{
|
||||
var url =
|
||||
$"{configuration["URL"]?.TrimEnd('/')}/packages/{configuration["OWNER"]}/{package.Type}/{package.Name}/{package.Version}";
|
||||
logger.LogInformation("Deleting package {PackageName} (ID: {PackageId}) from Gitea: {Url}", package.Name,
|
||||
package.Id, url);
|
||||
var baseUrl = GetBaseUrl();
|
||||
var url = $"{baseUrl}/{package.Version}";
|
||||
logger.LogInformation("Deleting package {PackageName} (ID: {PackageId}) from Gitea",
|
||||
package.Name, package.Id);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Delete, url);
|
||||
request.Headers.Add("Authorization", $"token {configuration["API_TOKEN"]}");
|
||||
AddAuthorizationHeader(request);
|
||||
|
||||
var response = await httpClient.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
logger.LogInformation("Successfully deleted package {PackageName} (ID: {PackageId})", package.Name,
|
||||
package.Id);
|
||||
logger.LogInformation("Successfully deleted package {PackageName} (ID: {PackageId})",
|
||||
package.Name, package.Id);
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
logger.LogError(ex, "Error deleting package {PackageName} (ID: {PackageId}) from Gitea", package.Name,
|
||||
package.Id);
|
||||
logger.LogError(ex, "Error deleting package {PackageName} (ID: {PackageId}) from Gitea",
|
||||
package.Name, package.Id);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Unexpected error deleting package {PackageName} (ID: {PackageId})", package.Name,
|
||||
package.Id);
|
||||
logger.LogError(ex, "Unexpected error deleting package {PackageName} (ID: {PackageId})",
|
||||
package.Name, package.Id);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user