Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Count commits to a specific owner's repository for a specific time period.
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Linq;
using System.Threading.Tasks;
public static class GitHubCommitCounter
{
public static async Task Count(string owner, DateTime minDateTime)
{
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("User-Agent", "C# HttpClient");
var repositories =
JArray
.Parse(await httpClient.GetStringAsync($"https://api.github.com/users/{owner}/repos"))
.Select(x => (string)x["name"]);
foreach (var repository in repositories)
{
var json = await httpClient.GetStringAsync($"https://api.github.com/repos/{owner}/{repository}/commits");
var count = JArray.Parse(json)
.Where(x => (string)x.SelectToken("author.login") == owner
&& minDateTime < DateTime.Parse((string)x.SelectToken("commit.author.date")))
.Count();
if (0 < count)
Console.WriteLine($"{repository}:{count}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.