Created
March 21, 2019 14:29
-
-
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.
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
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