-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a277580
commit ef90fa0
Showing
7 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 += $"[](https://github.com/{owner}/{repo})"; | ||
str += $"[](https://github.com/{owner}/{repo}/stargazers)"; | ||
str += $"[](https://stars.medv.io/{owner}/{repo})"; | ||
str += $"[](https://www.nuget.org/packages/{repo})"; | ||
str += $"[](https://www.nuget.org/packages/{repo}/)"; | ||
var workflow = await FindLastExecutedWorkflow(owner, repo); | ||
if (!string.IsNullOrWhiteSpace(workflow)) | ||
{ | ||
str += $"[](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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using System.Text.Json; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Local/LocalAPI/LocalAPI/Controllers/GitHubReadmeController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters