Some improvements
This commit is contained in:
@@ -1,19 +1,42 @@
|
|||||||
name: "package cleanup"
|
name: "package cleanup"
|
||||||
description: "Cleans up old packages and versions"
|
description: "Cleans up old packages and versions"
|
||||||
|
|
||||||
|
inputs:
|
||||||
|
url:
|
||||||
|
description: "Server URL"
|
||||||
|
required: true
|
||||||
|
names:
|
||||||
|
description: "Names of packages"
|
||||||
|
required: true
|
||||||
|
owner:
|
||||||
|
description: "Owner of the package"
|
||||||
|
required: true
|
||||||
|
types:
|
||||||
|
description: "Types of packages (e.g. Container, PyPi, NuGet)"
|
||||||
|
required: false
|
||||||
|
default: "Container,PyPi,NuGet"
|
||||||
|
api_token:
|
||||||
|
description: "API token for authentication"
|
||||||
|
required: true
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: "composite"
|
using: "composite"
|
||||||
steps:
|
steps:
|
||||||
- name: Install set-version tool
|
- name: Download and test package-cleanup tool
|
||||||
shell: bash
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
dotnet tool install \
|
curl -OJ https://git.sh-edraft.de/api/packages/sh-edraft.de/generic/package-cleanup/package-cleanup-linux-x64
|
||||||
--tool-path .tools \
|
|
||||||
ShEdraft.SetVersion \
|
|
||||||
--version 1.0.0 \
|
|
||||||
--add-source https://git.sh-edraft.de/api/packages/sh-edraft.de/nuget/index.json
|
|
||||||
|
|
||||||
- name: Run set-version
|
# Make executable
|
||||||
|
chmod +x package-cleanup-linux-x64
|
||||||
|
|
||||||
|
- name: Run package-cleanup
|
||||||
shell: bash
|
shell: bash
|
||||||
|
env:
|
||||||
|
URL: ${{ inputs.url }}
|
||||||
|
OWNER: ${{ inputs.owner }}
|
||||||
|
TYPES: ${{ inputs.types }}
|
||||||
|
NAMES: ${{ inputs.names }}
|
||||||
|
API_TOKEN: ${{ inputs.api_token }}
|
||||||
run: |
|
run: |
|
||||||
./.tools/set-version --suffix dev
|
./package-cleanup-linux-x64
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ public static class ConfigurationExtension
|
|||||||
public static IConfigurationBuilder EnsureGiteaConfig(this IConfigurationBuilder builder)
|
public static IConfigurationBuilder EnsureGiteaConfig(this IConfigurationBuilder builder)
|
||||||
{
|
{
|
||||||
var configuration = builder.Build();
|
var configuration = builder.Build();
|
||||||
var requiredKeys = new[] { "URL", "OWNER", "TYPE", "NAME", "API_TOKEN" };
|
var requiredKeys = new[] { "URL", "OWNER", "TYPES", "NAME", "API_TOKEN" };
|
||||||
|
|
||||||
foreach (var key in requiredKeys)
|
foreach (var key in requiredKeys)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public class GiteaPackageService(
|
|||||||
: IGiteaPackageService
|
: IGiteaPackageService
|
||||||
{
|
{
|
||||||
private string GetBaseUrl() =>
|
private string GetBaseUrl() =>
|
||||||
$"{configuration["URL"]?.TrimEnd('/')}/packages/{configuration["OWNER"]}/{configuration["TYPE"]}/{configuration["NAME"]}";
|
$"{configuration["URL"]?.TrimEnd('/')}/packages/{configuration["OWNER"]}";
|
||||||
|
|
||||||
private void AddAuthorizationHeader(HttpRequestMessage request)
|
private void AddAuthorizationHeader(HttpRequestMessage request)
|
||||||
{
|
{
|
||||||
@@ -28,32 +28,59 @@ public class GiteaPackageService(
|
|||||||
{
|
{
|
||||||
var baseUrl = GetBaseUrl();
|
var baseUrl = GetBaseUrl();
|
||||||
var allPackages = new List<GiteaPackage>();
|
var allPackages = new List<GiteaPackage>();
|
||||||
var page = 1;
|
|
||||||
|
|
||||||
while (true)
|
// Parse comma-separated types
|
||||||
|
var types = (configuration["TYPE"] ?? "")
|
||||||
|
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
|
|
||||||
|
if (types.Length == 0)
|
||||||
{
|
{
|
||||||
var url = $"{baseUrl}?page={page}";
|
logger.LogWarning("No package types configured");
|
||||||
logger.LogInformation("Fetching packages from Gitea: {Url}", url);
|
return allPackages;
|
||||||
|
}
|
||||||
|
|
||||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
// Parse comma-separated names
|
||||||
AddAuthorizationHeader(request);
|
var names = (configuration["NAMES"] ?? "")
|
||||||
|
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||||
|
|
||||||
var response = await httpClient.SendAsync(request, cancellationToken);
|
if (names.Length == 0)
|
||||||
response.EnsureSuccessStatusCode();
|
{
|
||||||
|
logger.LogWarning("No package names configured");
|
||||||
|
return allPackages;
|
||||||
|
}
|
||||||
|
|
||||||
var packages =
|
foreach (var type in types)
|
||||||
await response.Content.ReadFromJsonAsync<List<GiteaPackage>>(cancellationToken: cancellationToken)
|
{
|
||||||
?? [];
|
foreach (var name in names)
|
||||||
|
|
||||||
if (packages.Count == 0)
|
|
||||||
{
|
{
|
||||||
break;
|
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++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
allPackages.AddRange(packages);
|
|
||||||
logger.LogInformation("Fetched {Count} packages from page {Page}", packages.Count, page);
|
|
||||||
|
|
||||||
page++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.LogInformation("Successfully fetched {Count} packages in total", allPackages.Count);
|
logger.LogInformation("Successfully fetched {Count} packages in total", allPackages.Count);
|
||||||
@@ -76,7 +103,7 @@ public class GiteaPackageService(
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var baseUrl = GetBaseUrl();
|
var baseUrl = GetBaseUrl();
|
||||||
var url = $"{baseUrl}/{package.Version}";
|
var url = $"{baseUrl}/{package.Type}/{package.Name}/{package.Version}";
|
||||||
logger.LogInformation("Deleting package {PackageName} (ID: {PackageId}) from Gitea",
|
logger.LogInformation("Deleting package {PackageName} (ID: {PackageId}) from Gitea",
|
||||||
package.Name, package.Id);
|
package.Name, package.Id);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user