GitHub Repository Analyzer [geschlossen]C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 GitHub Repository Analyzer [geschlossen]

Post by Anonymous »

Ich mache einen Github -Repository -Analysator als Übung. Das ist das Ziel: < /p>

Erstellen Sie eine Konsolenanwendung, die ein öffentliches Github-Repository analysiert und aussagekräftige Statistiken und Klassifizierungen liefert. https://github.com/dotnet/runtime

[*]Fetch and display:
  • Name, description, stars, forks, open issues
  • Top 5 contributors (by commits)
  • Commits in the last 7 days
  • Repository languages ​​and their percentages (optional)
  • Pull request stats: open/closed/merged (optional)
  • Issue Statistiken: Durchschnittliche Zeit zum Schließen (optional) < /p>
    < /li>
    < /ul>
    < /li>
    Klassifizieren Sie den Repository -Status als: < /p>

    < /li>
    Beiträge in den letzten 6 Monaten < /p>
    < /li>
    Betreuer reagierte auf Probleme in den letzten 3 Monaten < /p>
    < /li>
    < /ul> < /> < />


    Stagnant: < /P. />No contributions in 6+ months
  • Some issue activity in the last 6 months
[*]Dead:
  • Archived
  • No contributions in 1+ year
  • No maintainer response in 1+ year



Design for Erweiterbarkeit: < /p>
Ihre Implementierung sollte das Hinzufügen neuer Statusklassifizierungen
(e.g., "Veraltet", "experimentell") unterstützen, ohne die Kernlogik zu ändern. AIM
, um ein offenes Prinzip zu befolgen-Code sollte für die Erweiterung geöffnet werden,
für die Änderung geschlossen werden.

Code: Select all

using System;
using System.Net.Http;
using System.Text.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace GitHubRepoAnalyzer
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Enter GitHub repository URL (e.g., https://github.com/dotnet/runtime):");
string url = Console.ReadLine();

var analyzer = new GitHubAnalyzer(new GitHubApiService(), new RepositoryStatusClassifier());
await analyzer.AnalyzeRepository(url);
}
}

public class GitHubAnalyzer
{
private readonly IGitHubApiService _apiService;
private readonly IRepositoryStatusClassifier _classifier;

public GitHubAnalyzer(IGitHubApiService apiService, IRepositoryStatusClassifier classifier)
{
_apiService = apiService;
_classifier = classifier;
}

public async Task AnalyzeRepository(string repoUrl)
{
var repoInfo = await _apiService.FetchRepositoryInfo(repoUrl);
var contributors = await _apiService.GetTopContributors(repoUrl);
var recentCommits = await _apiService.GetCommitsLast7Days(repoUrl);
var status = await _classifier.Classify(repoUrl);

Console.WriteLine("\nRepository Info:");
Console.WriteLine($"Name: {repoInfo.Name}");
Console.WriteLine($"Description: {repoInfo.Description}");
Console.WriteLine($"Stars: {repoInfo.Stars}");
Console.WriteLine($"Forks: {repoInfo.Forks}");
Console.WriteLine($"Open Issues: {repoInfo.OpenIssues}");

Console.WriteLine("\nTop 5 Contributors:");
foreach (var contributor in contributors)
{
Console.WriteLine($"- {contributor.Name}: {contributor.Commits} commits");
}

Console.WriteLine($"\nCommits in last 7 days: {recentCommits}");
Console.WriteLine($"\nRepository Status: {status}");
}
}

public interface IGitHubApiService
{
Task FetchRepositoryInfo(string repoUrl);
Task GetTopContributors(string repoUrl);
Task GetCommitsLast7Days(string repoUrl);
Task IsArchived(string repoUrl);
Task HasRecentContributions(string repoUrl, int months);
Task HasMaintainerActivity(string repoUrl, int months);
}

public class GitHubApiService : IGitHubApiService
{
private readonly HttpClient _client = new HttpClient();

public async Task FetchRepositoryInfo(string repoUrl)
{
var (owner, repo) = ParseRepoUrl(repoUrl);
var json = await _client.GetStringAsync($"https://api.github.com/repos/{owner}/{repo}");
var doc = JsonDocument.Parse(json);
var root = doc.RootElement;

return new RepositoryInfo
{
Name = root.GetProperty("name").GetString(),
Description = root.GetProperty("description").GetString(),
Stars = root.GetProperty("stargazers_count").GetInt32(),
Forks = root.GetProperty("forks_count").GetInt32(),
OpenIssues = root.GetProperty("open_issues_count").GetInt32()
};
}

public async Task GetTopContributors(string repoUrl)
{
var (owner, repo) = ParseRepoUrl(repoUrl);
var json = await _client.GetStringAsync($"https://api.github.com/repos/{owner}/{repo}/contributors");
var doc = JsonDocument.Parse(json);
return doc.RootElement.EnumerateArray()
.Take(5)
.Select(c => new Contributor
{
Name = c.GetProperty("login").GetString(),
Commits = c.GetProperty("contributions").GetInt32()
})
.ToList();
}

public async Task  GetCommitsLast7Days(string repoUrl)
{
var (owner, repo) = ParseRepoUrl(repoUrl);
var since = DateTime.UtcNow.AddDays(-7).ToString("o");
var json = await _client.GetStringAsync($"https://api.github.com/repos/{owner}/{repo}/commits?since={since}");
var doc = JsonDocument.Parse(json);
return doc.RootElement.GetArrayLength();
}

public async Task IsArchived(string repoUrl)
{
var (owner, repo) = ParseRepoUrl(repoUrl);
var json = await _client.GetStringAsync($"https://api.github.com/repos/{owner}/{repo}");
var root = JsonDocument.Parse(json).RootElement;
return root.GetProperty("archived").GetBoolean();
}

public async Task HasRecentContributions(string repoUrl, int months)
{
var (owner, repo) = ParseRepoUrl(repoUrl);
var since = DateTime.UtcNow.AddMonths(-months).ToString("o");
var json = await _client.GetStringAsync($"https://api.github.com/repos/{owner}/{repo}/commits?since={since}");
var doc = JsonDocument.Parse(json);
return doc.RootElement.GetArrayLength() > 0;
}

public async Task HasMaintainerActivity(string repoUrl, int months)
{
var (owner, repo) = ParseRepoUrl(repoUrl);
var since = DateTime.UtcNow.AddMonths(-months).ToString("o");
var json = await _client.GetStringAsync($"https://api.github.com/repos/{owner}/{repo}/issues?since={since}&state=all");
var doc = JsonDocument.Parse(json);
return doc.RootElement.EnumerateArray().Any();
}

private (string owner, string repo) ParseRepoUrl(string url)
{
var parts = new Uri(url).AbsolutePath.Trim('/').Split('/');
return (parts[0], parts[1]);
}
}

public interface IRepositoryStatusClassifier
{
Task Classify(string repoUrl);
}

public class RepositoryStatusClassifier : IRepositoryStatusClassifier
{
private readonly IGitHubApiService _api = new GitHubApiService();

public async Task Classify(string repoUrl)
{
bool archived = await _api.IsArchived(repoUrl);
bool recentContrib = await _api.HasRecentContributions(repoUrl, 6);
bool maintainerActivity = await _api.HasMaintainerActivity(repoUrl, 3);

if (!archived && recentContrib && maintainerActivity)
return "🟢 Live";
if (!archived && !recentContrib && await _api.HasMaintainerActivity(repoUrl, 6))
return "🟡 Stagnant";
if (archived || !await _api.HasRecentContributions(repoUrl, 12))
return "🔴 Dead";

return "⚪ Unknown";
}
}

public class RepositoryInfo
{
public string Name { get; set; }
public string Description { get; set; }
public int Stars { get; set; }
public int Forks { get; set; }
public int OpenIssues { get; set; }
}

public class Contributor
{
public string Name { get; set; }
public int Commits { get; set; }
}
}
Ich möchte ein Feedback und einige helfen dabei, es richtig auszuführen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post