Skip to content

Commit

Permalink
githubredme
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatandrei committed Jul 23, 2024
1 parent a277580 commit ef90fa0
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 4 deletions.
74 changes: 74 additions & 0 deletions src/Local/LocalAPI/GithubRelated/GithubReadme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace GithubRelated;

public class GithubReadme
{

public async Task<string> 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<string> 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<JsonElement>(response);
var responseStream = await client.GetStreamAsync(url);
var workflowsJson = await JsonSerializer.DeserializeAsync<JsonElement>(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;
}
}
}
9 changes: 9 additions & 0 deletions src/Local/LocalAPI/GithubRelated/GithubRelated.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions src/Local/LocalAPI/GithubRelated/globals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using System.Text.Json;
8 changes: 7 additions & 1 deletion src/Local/LocalAPI/LocalAPI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/Local/LocalAPI/LocalAPI/Controllers/GitHubReadmeController.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

5 changes: 3 additions & 2 deletions src/Local/LocalAPI/LocalAPI/LocalAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<PackageReference Include="FluentEmail.Liquid" Version="3.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="8.0.4" />
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="NetCore2Blockly" Version="8.2024.428.1000" />
<PackageReference Include="NetCoreUsefullEndpoints" Version="7.2023.1216.1825" />
<PackageReference Include="NetCore2Blockly" Version="8.2024.503.745" />
<PackageReference Include="NetCoreUsefullEndpoints" Version="8.2024.627.800" />
<PackageReference Include="SkinnyControllersCommon" Version="2023.5.14.2055" />
<PackageReference Include="SkinnyControllersGenerator" Version="2023.5.14.2055" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand All @@ -33,6 +33,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BrowserTest\BrowserTest.csproj" />
<ProjectReference Include="..\GithubRelated\GithubRelated.csproj" />
<ProjectReference Include="..\LocalAPIChrome\LocalAPIChrome.csproj" />
<ProjectReference Include="..\LocalAPIEmail\LocalAPIEmail.csproj" />
<ProjectReference Include="..\LocalFS\LocalFS.csproj" />
Expand Down
3 changes: 2 additions & 1 deletion src/Local/LocalAPI/LocalAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BrowserTest;
using GithubRelated;
using LocalAPI.converters;
using LocalTools;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -75,7 +76,7 @@ public static async Task<int> Main(string[] args)
builder.Services.AddTransient<WebPagesInteraction>();
builder.Services.AddSingleton<SimpleSqlServer>();
//builder.Services.AddTransient<WebPages>();

builder.Services.AddTransient<GithubReadme>();
//builder.Configuration.GetDebugView();
EmailConfig cfgEmail = new();
builder.Configuration.GetSection("plugins:email").Bind(cfgEmail);
Expand Down

0 comments on commit ef90fa0

Please sign in to comment.