feat: implement SyncService for filtering and logging asset releases

This commit is contained in:
mahdium committed 2026-11-03 18:11:30 +03:30
1 parent 9c4fd7e624
commit 7fa501266e
5 files changed
+61 -13

No files matched your search

+1 -1
View File
@@ -65,7 +65,7 @@ public partial class GithubReleaseResponseDto
public required string Body { get; set; }
[JsonPropertyName("reactions")]
public required Reactions Reactions { get; set; }
public Reactions? Reactions { get; set; }
}
public partial class Asset
+3 -2
View File
@@ -12,13 +12,14 @@ var repositoryConfig = await RepoConfigLoader.LoadConfigAsync(
builder.Services.AddSingleton(repositoryConfig);
builder.Services.AddSingleton<GithubService>();
builder.Services.AddSingleton<SyncService>();
var app = builder.Build();
using var scope = app.Services.CreateScope();
await scope.ServiceProvider
.GetRequiredService<GithubService>()
.FetchRepoReleasesAsync("SagerNet","sing-box");
.GetRequiredService<SyncService>()
.Sync();
await app.RunAsync();
+4 -4
View File
@@ -15,8 +15,8 @@ public class RepoSyncConfig
public required string Name { get; set; }
public required string GithubUrl { get; set; }
public required string ForgejoUrl { get; set; }
public required string IncludeRegex { get; set; }
public required string ExcludeRegex { get; set; }
public required string IncludeAssetsRegex { get; set; }
public required string ExcludeAssetsRegex { get; set; }
}
public abstract class RepoConfigLoader()
@@ -46,8 +46,8 @@ public abstract class RepoConfigLoader()
Name = table.Key.ToString(),
GithubUrl = GetRequiredString(fields, "GithubUrl"),
ForgejoUrl = GetRequiredString(fields, "ForgejoUrl"),
IncludeRegex = GetRequiredString(fields, "IncludeRegex"),
ExcludeRegex = GetRequiredString(fields, "ExcludeRegex")
IncludeAssetsRegex = GetRequiredString(fields, "IncludeAssetsRegex"),
ExcludeAssetsRegex = GetRequiredString(fields, "ExcludeAssetsRegex")
});
}
}
+49 -2
View File
@@ -1,6 +1,53 @@
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
namespace ForgejoReleaseSync.Services;
public class SyncService
public class SyncService(
AppConfig config,
RepositoryConfig repositoryConfig,
GithubService githubService,
ILogger<SyncService> logger)
{
private readonly AppConfig _appConfig = config;
private readonly GithubService _githubService = githubService;
public async Task Sync()
{
logger.LogInformation("Starting Sync Service");
logger.LogInformation($"loaded {repositoryConfig.Repositories.Count} repositories");
foreach (var repo in repositoryConfig.Repositories)
{
var repoOwner = repo.GithubUrl.Split('/')[^2];
var repoName = repo.GithubUrl.Split('/').Last();
var includeRegex = new Regex(repo.IncludeAssetsRegex);
var excludeRegex = new Regex(repo.ExcludeAssetsRegex);
var releases = await _githubService.FetchRepoReleasesAsync(repoOwner, repoName);
var lastRelease = releases.LastOrDefault(r => r.Prerelease == false);
if (lastRelease is null)
continue;
var acceptableAssets = lastRelease.Assets
.Where(asset =>
includeRegex.IsMatch(asset.Name) &&
!excludeRegex.IsMatch(asset.Name))
.ToArray();
logger.LogInformation(
"Repo {Owner}/{Name}: release: {ReleaseName} has {ReleaseCount} assets with matching assets",
repoOwner,
repoName,
lastRelease.Name,
acceptableAssets.Length);
foreach (var acceptableAsset in acceptableAssets)
{
logger.LogInformation(acceptableAsset.Name);
}
}
}
}
+4 -4
View File
@@ -1,11 +1,11 @@
[[repo1]]
GithubUrl = "https://github.com/owner/repo1"
ForgejoUrl = "https://forgejo.example.com/owner/repo1"
IncludeRegex = ".*"
ExcludeRegex = "^$"
IncludeAssetsRegex = ".*"
ExcludeAssetsRegex = "^$"
[[repo2]]
GithubUrl = "https://github.com/owner/repo2"
ForgejoUrl = "https://forgejo.example.com/owner/repo2"
IncludeRegex = "^v.*"
ExcludeRegex = ".*-beta.*"
IncludeAssetsRegex = "^v.*"
ExcludeAssetsRegex = ".*-beta.*"