From 7fa501266e3ce621147203cd8e9c5243b665bbee Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Date: Tue, 3 Nov 2026 18:11:30 +0330 Subject: [PATCH] feat: implement SyncService for filtering and logging asset releases --- DTOs/GithubReleaseResponseDto.cs | 2 +- Program.cs | 5 ++-- Services/RepositoryConfig.cs | 8 ++--- Services/SyncService.cs | 51 ++++++++++++++++++++++++++++++-- config.example.toml | 8 ++--- 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/DTOs/GithubReleaseResponseDto.cs b/DTOs/GithubReleaseResponseDto.cs index 1786501..2c80bf7 100644 --- a/DTOs/GithubReleaseResponseDto.cs +++ b/DTOs/GithubReleaseResponseDto.cs @@ -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 diff --git a/Program.cs b/Program.cs index 8ddd258..a1f0555 100644 --- a/Program.cs +++ b/Program.cs @@ -12,13 +12,14 @@ var repositoryConfig = await RepoConfigLoader.LoadConfigAsync( builder.Services.AddSingleton(repositoryConfig); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); var app = builder.Build(); using var scope = app.Services.CreateScope(); await scope.ServiceProvider - .GetRequiredService() - .FetchRepoReleasesAsync("SagerNet","sing-box"); + .GetRequiredService() + .Sync(); await app.RunAsync(); \ No newline at end of file diff --git a/Services/RepositoryConfig.cs b/Services/RepositoryConfig.cs index a42500b..83c14fe 100644 --- a/Services/RepositoryConfig.cs +++ b/Services/RepositoryConfig.cs @@ -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") }); } } diff --git a/Services/SyncService.cs b/Services/SyncService.cs index 57ba7d7..197bac0 100644 --- a/Services/SyncService.cs +++ b/Services/SyncService.cs @@ -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 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); + } + } + } } \ No newline at end of file diff --git a/config.example.toml b/config.example.toml index f88f487..b6ae0d2 100644 --- a/config.example.toml +++ b/config.example.toml @@ -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.*" \ No newline at end of file +IncludeAssetsRegex = "^v.*" +ExcludeAssetsRegex = ".*-beta.*" \ No newline at end of file