Skip to content

Instantly share code, notes, and snippets.

@7u4
Forked from mattwarren/GitHubAPI-IssuesAndPRs.linq
Created December 21, 2015 03:36
Show Gist options
  • Save 7u4/dd18b8739ec638043026 to your computer and use it in GitHub Desktop.
Save 7u4/dd18b8739ec638043026 to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<NuGetReference>Octokit</NuGetReference>
<NuGetReference>Octokit.Reactive</NuGetReference>
<NuGetReference>Rx-Main</NuGetReference>
<Namespace>Octokit</Namespace>
<Namespace>System.Linq</Namespace>
<Namespace>System.Reactive.Linq</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
</Query>
async Task Main(string[] args)
{
string organisation = "dotnet";
var client = new GitHubClient(new Octokit.ProductHeaderValue("octokit.samples"));
// See https://help.github.com/articles/creating-an-access-token-for-command-line-use/
client.Credentials = new Credentials("<PUT YOUR SECRET KEY HERE!!>");
foreach (var reponame in new [] { "roslyn", "coreclr", "corefx" })
{
var repository = await client.Repository.Get(organisation, reponame);
Console.WriteLine(String.Format("\n{0} - {1} can be found at {2}", organisation, reponame, repository.HtmlUrl));
Console.WriteLine("It currently has {0} watchers and {1} forks", repository.StargazersCount, repository.ForksCount);
Console.WriteLine("It has {0} open issues", repository.OpenIssuesCount); // The API doesn't have ClosedIssuesCount
Console.WriteLine("And GitHub thinks it is a {0} project", repository.Language);
}
var queriesWithResults = new Dictionary<string, List<object>>()
{
{ "type:issue", new List<object>() },
{ "type:pr", new List<object>() },
};
foreach (var item in queriesWithResults)
{
var startDate = new DateTime(2014, 11, 1);
for (var date = startDate; date < DateTime.Now; date = date.AddMonths(1))
{
var endOfMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
var dateString = string.Format("created:{0}..{1}", date.ToString("yyyy-MM-dd"), endOfMonth.ToString("yyyy-MM-dd"));
Console.WriteLine();
var openResults = new Dictionary<string, int>();
var closedResults = new Dictionary<string, int>();
var totalResults = new Dictionary<string, int>();
foreach (var repo in new [] { "roslyn", "coreclr", "corefx" })
{
var baseQuery = dateString + " " + item.Key + " ";
openResults[repo] = await RunQuery(client, organisation, repo, baseQuery + " is:open");
closedResults[repo] = await RunQuery(client, organisation, repo, baseQuery + " is:closed");
totalResults[repo] = await RunQuery(client, organisation, repo, baseQuery);
var type = item.Key == "type:issue" ? "Issues" : (item.Key == "type:pr" ? "Pull Requests" : "ERROR");
Console.WriteLine("{0} - \"{1}\" - {2} -> Open: {3}, Closed: {4}, Total: {5} ({6})",
date.ToString("MMM yyyy"), repo, type, openResults[repo], closedResults[repo],
totalResults[repo], openResults[repo] + closedResults[repo]);
}
item.Value.Add(new
{
Month = date.ToString("MMM yyyy"),
Open = new List<object> {
new {
Roslyn = openResults["roslyn"],
CoreCLR = openResults["coreclr"],
CoreFX = openResults["corefx"]
}
},
Closed = new List<object> {
new {
Roslyn = closedResults["roslyn"],
CoreCLR = closedResults["coreclr"],
CoreFX = closedResults["corefx"]
}
},
Total = new List<object> {
new {
Roslyn = totalResults["roslyn"],
CoreCLR = totalResults["coreclr"],
CoreFX = totalResults["corefx"]
}
},
});
}
}
foreach (var item in queriesWithResults)
{
var type = item.Key == "type:issue" ? "Issues" : (item.Key == "type:pr" ? "Pull Requests" : "ERROR");
Console.WriteLine("\nResults - {0}:", type);
item.Value.Dump();
}
}
async Task<int> RunQuery(GitHubClient client, string org, string repo, string query)
{
var timer = Stopwatch.StartNew();
var issue = new SearchIssuesRequest(query);
issue.Repos.Add(org, repo);
issue.SortField = IssueSearchSort.Created;
issue.Order = SortDirection.Ascending;
issue.PerPage = 1;
issue.Page = 1;
var issues = await client.Search.SearchIssues(issue);
timer.Stop();
// Ensure we aren't hitting the rate-limit, ~2 seconds between API requests
var totalMsecs = (int)timer.Elapsed.TotalMilliseconds;
if (totalMsecs < 2100)
{
//Console.WriteLine("Sleeping {0} ms, to escape the rate-limit", 2100 - totalMsecs);
Thread.Sleep(2100 - totalMsecs);
}
return issues.TotalCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment