From ef90fa0049cb87e8589d8df56d13a6c9491d2dd1 Mon Sep 17 00:00:00 2001 From: Andrei Ignat Date: Tue, 23 Jul 2024 20:45:11 +0300 Subject: [PATCH] githubredme --- .../LocalAPI/GithubRelated/GithubReadme.cs | 74 +++++++++++++++++++ .../GithubRelated/GithubRelated.csproj | 9 +++ src/Local/LocalAPI/GithubRelated/globals.cs | 1 + src/Local/LocalAPI/LocalAPI.sln | 8 +- .../Controllers/GitHubReadmeController.cs | 18 +++++ src/Local/LocalAPI/LocalAPI/LocalAPI.csproj | 5 +- src/Local/LocalAPI/LocalAPI/Program.cs | 3 +- 7 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 src/Local/LocalAPI/GithubRelated/GithubReadme.cs create mode 100644 src/Local/LocalAPI/GithubRelated/GithubRelated.csproj create mode 100644 src/Local/LocalAPI/GithubRelated/globals.cs create mode 100644 src/Local/LocalAPI/LocalAPI/Controllers/GitHubReadmeController.cs diff --git a/src/Local/LocalAPI/GithubRelated/GithubReadme.cs b/src/Local/LocalAPI/GithubRelated/GithubReadme.cs new file mode 100644 index 00000000..abd2706c --- /dev/null +++ b/src/Local/LocalAPI/GithubRelated/GithubReadme.cs @@ -0,0 +1,74 @@ +namespace GithubRelated; + +public class GithubReadme +{ + + public async Task GeneratedBadge(string owner, string repo) + { + var str = ""; + str += $"[![GitHub last commit](https://img.shields.io/github/last-commit/{owner}/{repo}?label=updated)](https://github.com/{owner}/{repo})"; + str += $"[![Stars](https://img.shields.io/github/stars/{owner}/{repo})](https://github.com/{owner}/{repo}/stargazers)"; + str += $"[![Sparkline](https://stars.medv.io/{owner}/{repo}.svg)](https://stars.medv.io/{owner}/{repo})"; + str += $"[![Nuget](https://img.shields.io/nuget/v/{repo})](https://www.nuget.org/packages/{repo})"; + str += $"[![NuGet Badge](https://buildstats.info/nuget/{repo})](https://www.nuget.org/packages/{repo}/)"; + var workflow = await FindLastExecutedWorkflow(owner, repo); + if (!string.IsNullOrWhiteSpace(workflow)) + { + str += $"[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/{owner}/{repo}/{workflow}?label={workflow})](https://github.com/{owner}/{repo}/actions/workflows/{workflow}.yml)"; + } + return str; + } + + + private async Task FindLastExecutedWorkflow(string owner, string repo) + { + var client = new HttpClient(); + string url = $"https://api.github.com/repos/{owner}/{repo}/actions/workflows"; + + client.DefaultRequestHeaders.Add("User-Agent", "request"); + client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); + + try + { + //var response = await client.GetStringAsync(url); + //var json = JsonSerializer.Deserialize(response); + var responseStream = await client.GetStreamAsync(url); + var workflowsJson = await JsonSerializer.DeserializeAsync(responseStream); + var workflows = workflowsJson.GetProperty("workflows").EnumerateArray(); + + DateTime latestUpdateTime = DateTime.MinValue; + JsonElement latestWorkflow = new JsonElement(); + + foreach (var workflow in workflows) + { + try + { + var updatedAt = DateTime.Parse(workflow.GetProperty("updated_at").GetString() ?? string.Empty); + var name = latestWorkflow.GetProperty("name").GetString() ?? string.Empty; + if (name.Contains("dependabot")) + continue; + + if (updatedAt > latestUpdateTime) + { + latestUpdateTime = updatedAt; + latestWorkflow = workflow; + } + } + catch (Exception e) + { + //do nothing + } + } + + if (latestUpdateTime > DateTime.MinValue) + { + return latestWorkflow.GetProperty("name").GetString()?? string.Empty; + } + return string.Empty; + } + catch (HttpRequestException e) + { + return string.Empty; + } + } +} diff --git a/src/Local/LocalAPI/GithubRelated/GithubRelated.csproj b/src/Local/LocalAPI/GithubRelated/GithubRelated.csproj new file mode 100644 index 00000000..fa71b7ae --- /dev/null +++ b/src/Local/LocalAPI/GithubRelated/GithubRelated.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/src/Local/LocalAPI/GithubRelated/globals.cs b/src/Local/LocalAPI/GithubRelated/globals.cs new file mode 100644 index 00000000..eefd5409 --- /dev/null +++ b/src/Local/LocalAPI/GithubRelated/globals.cs @@ -0,0 +1 @@ +global using System.Text.Json; diff --git a/src/Local/LocalAPI/LocalAPI.sln b/src/Local/LocalAPI/LocalAPI.sln index ea65062d..aef997b3 100644 --- a/src/Local/LocalAPI/LocalAPI.sln +++ b/src/Local/LocalAPI/LocalAPI.sln @@ -21,7 +21,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyProgrammerAllTool", "MyPr EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BrowserTest", "BrowserTest\BrowserTest.csproj", "{6F47CAF4-4687-47B0-9F71-70C12474C882}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlServerDB", "SqlServerDB\SqlServerDB.csproj", "{DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SqlServerDB", "SqlServerDB\SqlServerDB.csproj", "{DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GithubRelated", "GithubRelated\GithubRelated.csproj", "{ABD0464D-588C-4BAF-BDE8-2576D69979CB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -65,6 +67,10 @@ Global {DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}.Debug|Any CPU.Build.0 = Debug|Any CPU {DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}.Release|Any CPU.ActiveCfg = Release|Any CPU {DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}.Release|Any CPU.Build.0 = Release|Any CPU + {ABD0464D-588C-4BAF-BDE8-2576D69979CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ABD0464D-588C-4BAF-BDE8-2576D69979CB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ABD0464D-588C-4BAF-BDE8-2576D69979CB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ABD0464D-588C-4BAF-BDE8-2576D69979CB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Local/LocalAPI/LocalAPI/Controllers/GitHubReadmeController.cs b/src/Local/LocalAPI/LocalAPI/Controllers/GitHubReadmeController.cs new file mode 100644 index 00000000..7b6367b6 --- /dev/null +++ b/src/Local/LocalAPI/LocalAPI/Controllers/GitHubReadmeController.cs @@ -0,0 +1,18 @@ +using GithubRelated; + +namespace LocalAPI.Controllers; + +[ApiController] +[ApiVersion("1.0")] +[Route("api/v{version:apiVersion}/[controller]/[action]")] +[AutoActions(template = TemplateIndicator.AllPostWithRecord, FieldsName = new[] { "*" }, ExcludeFields = new[] { "_logger" })] + +public partial class GitHubReadmeController: ControllerBase +{ + private readonly GithubReadme githubReadme; + public GitHubReadmeController(GithubReadme githubReadme) + { + this.githubReadme = githubReadme; + } +} + diff --git a/src/Local/LocalAPI/LocalAPI/LocalAPI.csproj b/src/Local/LocalAPI/LocalAPI/LocalAPI.csproj index e354f199..fcb88eb6 100644 --- a/src/Local/LocalAPI/LocalAPI/LocalAPI.csproj +++ b/src/Local/LocalAPI/LocalAPI/LocalAPI.csproj @@ -20,8 +20,8 @@ - - + + @@ -33,6 +33,7 @@ + diff --git a/src/Local/LocalAPI/LocalAPI/Program.cs b/src/Local/LocalAPI/LocalAPI/Program.cs index d153a52a..b560a15a 100644 --- a/src/Local/LocalAPI/LocalAPI/Program.cs +++ b/src/Local/LocalAPI/LocalAPI/Program.cs @@ -1,4 +1,5 @@ using BrowserTest; +using GithubRelated; using LocalAPI.converters; using LocalTools; using Microsoft.Extensions.Options; @@ -75,7 +76,7 @@ public static async Task Main(string[] args) builder.Services.AddTransient(); builder.Services.AddSingleton(); //builder.Services.AddTransient(); - + builder.Services.AddTransient(); //builder.Configuration.GetDebugView(); EmailConfig cfgEmail = new(); builder.Configuration.GetSection("plugins:email").Bind(cfgEmail);