Skip to content

Instantly share code, notes, and snippets.

@nuitsjp
Created March 21, 2019 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuitsjp/fa80d74b6d847e8a39489a9a0ee8ec2f to your computer and use it in GitHub Desktop.
Save nuitsjp/fa80d74b6d847e8a39489a9a0ee8ec2f to your computer and use it in GitHub Desktop.
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