Skip to content

Commit ef90fa0

Browse files
committed
githubredme
1 parent a277580 commit ef90fa0

File tree

7 files changed

+114
-4
lines changed

7 files changed

+114
-4
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
namespace GithubRelated;
2+
3+
public class GithubReadme
4+
{
5+
6+
public async Task<string> GeneratedBadge(string owner, string repo)
7+
{
8+
var str = "";
9+
str += $"[![GitHub last commit](https://img.shields.io/github/last-commit/{owner}/{repo}?label=updated)](https://github.com/{owner}/{repo})";
10+
str += $"[![Stars](https://img.shields.io/github/stars/{owner}/{repo})](https://github.com/{owner}/{repo}/stargazers)";
11+
str += $"[![Sparkline](https://stars.medv.io/{owner}/{repo}.svg)](https://stars.medv.io/{owner}/{repo})";
12+
str += $"[![Nuget](https://img.shields.io/nuget/v/{repo})](https://www.nuget.org/packages/{repo})";
13+
str += $"[![NuGet Badge](https://buildstats.info/nuget/{repo})](https://www.nuget.org/packages/{repo}/)";
14+
var workflow = await FindLastExecutedWorkflow(owner, repo);
15+
if (!string.IsNullOrWhiteSpace(workflow))
16+
{
17+
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)";
18+
}
19+
return str;
20+
}
21+
22+
23+
private async Task<string> FindLastExecutedWorkflow(string owner, string repo)
24+
{
25+
var client = new HttpClient();
26+
string url = $"https://api.github.com/repos/{owner}/{repo}/actions/workflows";
27+
28+
client.DefaultRequestHeaders.Add("User-Agent", "request");
29+
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
30+
31+
try
32+
{
33+
//var response = await client.GetStringAsync(url);
34+
//var json = JsonSerializer.Deserialize<JsonElement>(response);
35+
var responseStream = await client.GetStreamAsync(url);
36+
var workflowsJson = await JsonSerializer.DeserializeAsync<JsonElement>(responseStream);
37+
var workflows = workflowsJson.GetProperty("workflows").EnumerateArray();
38+
39+
DateTime latestUpdateTime = DateTime.MinValue;
40+
JsonElement latestWorkflow = new JsonElement();
41+
42+
foreach (var workflow in workflows)
43+
{
44+
try
45+
{
46+
var updatedAt = DateTime.Parse(workflow.GetProperty("updated_at").GetString() ?? string.Empty);
47+
var name = latestWorkflow.GetProperty("name").GetString() ?? string.Empty;
48+
if (name.Contains("dependabot"))
49+
continue;
50+
51+
if (updatedAt > latestUpdateTime)
52+
{
53+
latestUpdateTime = updatedAt;
54+
latestWorkflow = workflow;
55+
}
56+
}
57+
catch (Exception e)
58+
{
59+
//do nothing
60+
}
61+
}
62+
63+
if (latestUpdateTime > DateTime.MinValue)
64+
{
65+
return latestWorkflow.GetProperty("name").GetString()?? string.Empty;
66+
}
67+
return string.Empty;
68+
}
69+
catch (HttpRequestException e)
70+
{
71+
return string.Empty;
72+
}
73+
}
74+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using System.Text.Json;

src/Local/LocalAPI/LocalAPI.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyProgrammerAllTool", "MyPr
2121
EndProject
2222
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BrowserTest", "BrowserTest\BrowserTest.csproj", "{6F47CAF4-4687-47B0-9F71-70C12474C882}"
2323
EndProject
24-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlServerDB", "SqlServerDB\SqlServerDB.csproj", "{DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}"
24+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SqlServerDB", "SqlServerDB\SqlServerDB.csproj", "{DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}"
25+
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GithubRelated", "GithubRelated\GithubRelated.csproj", "{ABD0464D-588C-4BAF-BDE8-2576D69979CB}"
2527
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -65,6 +67,10 @@ Global
6567
{DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
6668
{DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
6769
{DBD26DE3-6E25-4664-BA62-B6909E9AF5CA}.Release|Any CPU.Build.0 = Release|Any CPU
70+
{ABD0464D-588C-4BAF-BDE8-2576D69979CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71+
{ABD0464D-588C-4BAF-BDE8-2576D69979CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
72+
{ABD0464D-588C-4BAF-BDE8-2576D69979CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
73+
{ABD0464D-588C-4BAF-BDE8-2576D69979CB}.Release|Any CPU.Build.0 = Release|Any CPU
6874
EndGlobalSection
6975
GlobalSection(SolutionProperties) = preSolution
7076
HideSolutionNode = FALSE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using GithubRelated;
2+
3+
namespace LocalAPI.Controllers;
4+
5+
[ApiController]
6+
[ApiVersion("1.0")]
7+
[Route("api/v{version:apiVersion}/[controller]/[action]")]
8+
[AutoActions(template = TemplateIndicator.AllPostWithRecord, FieldsName = new[] { "*" }, ExcludeFields = new[] { "_logger" })]
9+
10+
public partial class GitHubReadmeController: ControllerBase
11+
{
12+
private readonly GithubReadme githubReadme;
13+
public GitHubReadmeController(GithubReadme githubReadme)
14+
{
15+
this.githubReadme = githubReadme;
16+
}
17+
}
18+

src/Local/LocalAPI/LocalAPI/LocalAPI.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
<PackageReference Include="FluentEmail.Liquid" Version="3.0.2" />
2121
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="8.0.4" />
2222
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
23-
<PackageReference Include="NetCore2Blockly" Version="8.2024.428.1000" />
24-
<PackageReference Include="NetCoreUsefullEndpoints" Version="7.2023.1216.1825" />
23+
<PackageReference Include="NetCore2Blockly" Version="8.2024.503.745" />
24+
<PackageReference Include="NetCoreUsefullEndpoints" Version="8.2024.627.800" />
2525
<PackageReference Include="SkinnyControllersCommon" Version="2023.5.14.2055" />
2626
<PackageReference Include="SkinnyControllersGenerator" Version="2023.5.14.2055" />
2727
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
@@ -33,6 +33,7 @@
3333
</ItemGroup>
3434
<ItemGroup>
3535
<ProjectReference Include="..\BrowserTest\BrowserTest.csproj" />
36+
<ProjectReference Include="..\GithubRelated\GithubRelated.csproj" />
3637
<ProjectReference Include="..\LocalAPIChrome\LocalAPIChrome.csproj" />
3738
<ProjectReference Include="..\LocalAPIEmail\LocalAPIEmail.csproj" />
3839
<ProjectReference Include="..\LocalFS\LocalFS.csproj" />

src/Local/LocalAPI/LocalAPI/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BrowserTest;
2+
using GithubRelated;
23
using LocalAPI.converters;
34
using LocalTools;
45
using Microsoft.Extensions.Options;
@@ -75,7 +76,7 @@ public static async Task<int> Main(string[] args)
7576
builder.Services.AddTransient<WebPagesInteraction>();
7677
builder.Services.AddSingleton<SimpleSqlServer>();
7778
//builder.Services.AddTransient<WebPages>();
78-
79+
builder.Services.AddTransient<GithubReadme>();
7980
//builder.Configuration.GetDebugView();
8081
EmailConfig cfgEmail = new();
8182
builder.Configuration.GetSection("plugins:email").Bind(cfgEmail);

0 commit comments

Comments
 (0)