Should fix output problem
This commit is contained in:
@@ -15,7 +15,7 @@ public static class ConfigurationExtension
|
||||
public static IConfigurationBuilder EnsureGiteaConfig(this IConfigurationBuilder builder)
|
||||
{
|
||||
var configuration = builder.Build();
|
||||
var requiredKeys = new[] { "URL", "OWNER", "TYPES", "NAME", "API_TOKEN" };
|
||||
var requiredKeys = new[] { "URL", "OWNER", "TYPES", "NAMES", "API_TOKEN" };
|
||||
|
||||
foreach (var key in requiredKeys)
|
||||
{
|
||||
|
||||
@@ -22,24 +22,75 @@ public class GiteaPackageService(
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<GiteaPackage>> GetPackagesByOwnerAsync(CancellationToken cancellationToken = default)
|
||||
public async Task<IEnumerable<GiteaPackage>> GetPackagesByNameAsync(string name, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var baseUrl = GetBaseUrl();
|
||||
var allPackages = new List<GiteaPackage>();
|
||||
var packages = new List<GiteaPackage>();
|
||||
|
||||
// Parse comma-separated types
|
||||
var types = (configuration["TYPE"] ?? "")
|
||||
var types = (configuration["TYPES"] ?? "")
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
if (types.Length == 0)
|
||||
{
|
||||
logger.LogWarning("No package types configured");
|
||||
return allPackages;
|
||||
return packages;
|
||||
}
|
||||
|
||||
// Parse comma-separated names
|
||||
foreach (var type in types)
|
||||
{
|
||||
var page = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var url = $"{baseUrl}/{type}/{name}?page={page}";
|
||||
logger.LogInformation("Fetching packages from Gitea: {Url}", url);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
AddAuthorizationHeader(request);
|
||||
|
||||
var response = await httpClient.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var fetchedPackages =
|
||||
await response.Content.ReadFromJsonAsync<List<GiteaPackage>>(cancellationToken: cancellationToken)
|
||||
?? [];
|
||||
|
||||
if (fetchedPackages.Count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
packages.AddRange(fetchedPackages);
|
||||
logger.LogInformation("Fetched {Count} packages from page {Page}", fetchedPackages.Count, page);
|
||||
|
||||
page++;
|
||||
}
|
||||
}
|
||||
|
||||
logger.LogInformation("Successfully fetched {Count} packages for name '{Name}'", packages.Count, name);
|
||||
return packages;
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
logger.LogError(ex, "Error fetching packages from Gitea for name '{Name}'", name);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Unexpected error fetching packages for name '{Name}'", name);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<GiteaPackage>> GetPackagesByOwnerAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var allPackages = new List<GiteaPackage>();
|
||||
|
||||
var names = (configuration["NAMES"] ?? "")
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
@@ -49,38 +100,10 @@ public class GiteaPackageService(
|
||||
return allPackages;
|
||||
}
|
||||
|
||||
foreach (var type in types)
|
||||
foreach (var name in names)
|
||||
{
|
||||
foreach (var name in names)
|
||||
{
|
||||
var page = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var url = $"{baseUrl}/{type}/{name}?page={page}";
|
||||
logger.LogInformation("Fetching packages from Gitea: {Url}", url);
|
||||
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
AddAuthorizationHeader(request);
|
||||
|
||||
var response = await httpClient.SendAsync(request, cancellationToken);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var packages =
|
||||
await response.Content.ReadFromJsonAsync<List<GiteaPackage>>(cancellationToken: cancellationToken)
|
||||
?? [];
|
||||
|
||||
if (packages.Count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
allPackages.AddRange(packages);
|
||||
logger.LogInformation("Fetched {Count} packages from page {Page}", packages.Count, page);
|
||||
|
||||
page++;
|
||||
}
|
||||
}
|
||||
var packages = await GetPackagesByNameAsync(name, cancellationToken);
|
||||
allPackages.AddRange(packages);
|
||||
}
|
||||
|
||||
logger.LogInformation("Successfully fetched {Count} packages in total", allPackages.Count);
|
||||
|
||||
@@ -4,6 +4,7 @@ using sh.actions.package_cleanup.Models;
|
||||
|
||||
public interface IGiteaPackageService
|
||||
{
|
||||
Task<IEnumerable<GiteaPackage>> GetPackagesByNameAsync(string name, CancellationToken cancellationToken = default);
|
||||
Task<IEnumerable<GiteaPackage>> GetPackagesByOwnerAsync(CancellationToken cancellationToken = default);
|
||||
Task DeletePackage(GiteaPackage package, CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -6,8 +6,7 @@ public class PackageService(IGiteaPackageService packageService) : IPackageServi
|
||||
{
|
||||
public async Task<List<GiteaPackage>> GetFilteredPackages()
|
||||
{
|
||||
var packages = (await packageService.GetPackagesByOwnerAsync()).ToList();
|
||||
return packages;
|
||||
return (await packageService.GetPackagesByOwnerAsync()).ToList();
|
||||
}
|
||||
|
||||
public List<GiteaPackage> FilterPackagesToDelete(List<GiteaPackage> packages)
|
||||
|
||||
@@ -11,20 +11,22 @@ public class Worker(
|
||||
IGiteaPackageService giteaPackageService
|
||||
) : BackgroundService
|
||||
{
|
||||
private async Task DeletePackages(List<GiteaPackage> packages, CancellationToken cancellationToken = default)
|
||||
private async Task<int> DeletePackages(List<GiteaPackage> packages, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var dryRun = configuration["DRY_RUN"]?.ToLower() == "true";
|
||||
if (dryRun)
|
||||
{
|
||||
logger.LogInformation("Dry run enabled, not deleting {Count} packages", packages.Count);
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
var deletedCount = 0;
|
||||
foreach (var giteaPackage in packages)
|
||||
{
|
||||
try
|
||||
{
|
||||
await giteaPackageService.DeletePackage(giteaPackage, cancellationToken);
|
||||
deletedCount++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -32,25 +34,77 @@ public class Worker(
|
||||
giteaPackage.Name, giteaPackage.Version);
|
||||
}
|
||||
}
|
||||
return deletedCount;
|
||||
}
|
||||
|
||||
private void WriteGitHubOutput(string name, string value)
|
||||
{
|
||||
var githubOutput = configuration["GITHUB_OUTPUT"];
|
||||
if (string.IsNullOrEmpty(githubOutput))
|
||||
{
|
||||
logger.LogDebug("GITHUB_OUTPUT not set, skipping output: {Name}={Value}", name, value);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
File.AppendAllText(githubOutput, $"{name}={value}{Environment.NewLine}");
|
||||
logger.LogInformation("Wrote to GITHUB_OUTPUT: {Name}={Value}", name, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to write to GITHUB_OUTPUT file");
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var packages = await packageService.GetFilteredPackages();
|
||||
logger.LogInformation("Found {Count} packages", packages.Count);
|
||||
// Parse comma-separated names
|
||||
var names = (configuration["NAMES"] ?? "")
|
||||
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
if (names.Length == 0)
|
||||
{
|
||||
logger.LogWarning("No package names configured");
|
||||
WriteGitHubOutput("deleted_packages", "0");
|
||||
WriteGitHubOutput("processed_names", "0");
|
||||
appLifetime.StopApplication();
|
||||
return;
|
||||
}
|
||||
|
||||
var totalDeleted = 0;
|
||||
|
||||
// Process each name separately: collect -> filter -> delete
|
||||
foreach (var name in names)
|
||||
{
|
||||
logger.LogInformation("Processing packages for name '{Name}'", name);
|
||||
|
||||
var packages = (await giteaPackageService.GetPackagesByNameAsync(name, cancellationToken)).ToList();
|
||||
logger.LogInformation("Found {Count} packages for name '{Name}'", packages.Count, name);
|
||||
|
||||
var packagesToDelete = packageService.FilterPackagesToDelete(packages);
|
||||
logger.LogInformation("Found {Count} packages to delete for name '{Name}'", packagesToDelete.Count, name);
|
||||
|
||||
var deletedCount = await DeletePackages(packagesToDelete, cancellationToken);
|
||||
totalDeleted += deletedCount;
|
||||
logger.LogInformation("Deleted {Count} packages for name '{Name}'", deletedCount, name);
|
||||
|
||||
logger.LogInformation("Cleanup finished for name '{Name}'", name);
|
||||
}
|
||||
|
||||
var packagesToDelete = packageService.FilterPackagesToDelete(packages);
|
||||
logger.LogInformation("Found {Count} packages to delete", packagesToDelete.Count);
|
||||
logger.LogInformation("All package names processed successfully");
|
||||
|
||||
await DeletePackages(packagesToDelete, cancellationToken);
|
||||
|
||||
logger.LogInformation("Cleanup finished successfully");
|
||||
// Write outputs to GITHUB_OUTPUT
|
||||
WriteGitHubOutput("deleted_packages", totalDeleted.ToString());
|
||||
WriteGitHubOutput("processed_names", names.Length.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Cleanup failed with an error");
|
||||
WriteGitHubOutput("deleted_packages", "0");
|
||||
WriteGitHubOutput("processed_names", "0");
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user