Skip to content

Instantly share code, notes, and snippets.

@fredeil
Last active September 21, 2018 12:19
Show Gist options
  • Save fredeil/489d6d17af92c34a1486e9fea9ab5cd2 to your computer and use it in GitHub Desktop.
Save fredeil/489d6d17af92c34a1486e9fea9ab5cd2 to your computer and use it in GitHub Desktop.

Github Organization Contribution counter

A small C# program for counting all users contributions to master branch for every repository in the given organization.

Disclaimer: If your organization is huge, you should probably not use this as this will exhaust your GitHub API limit.

How to run:

Edit MY_ORGANIZATION with the name of the organization which you want to count, and edit MY_GITHUB_TOKEN with your GitHub token which has access to repositories.

Using .NET Core CLI, run:

dotnet run

Will output something like this;

user1: 1337
user2: 1002
user3: 860
...
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Octokit" Version="0.32.0" />
</ItemGroup>
</Project>
using Octokit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace GithubContributionCounter
{
class Program
{
public static async Task Main(string[] args)
{
const string MY_ORGANIZATION = "";
const string MY_GITHUB_TOKEN = "";
var credentials = new Credentials(MY_GITHUB_TOKEN);
var githubClient = new GitHubClient(new ProductHeaderValue("GithubContributionCounter"))
{
Credentials = credentials
};
var orgRepos = await githubClient.Repository.GetAllForOrg(MY_ORGANIZATION);
var orgMembers = await githubClient.Organization.Member.GetAll(MY_ORGANIZATION);
var contributions = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
foreach (var item in orgMembers)
{
if (!contributions.ContainsKey(item.Login))
{
contributions.Add(item.Login, 0);
}
}
foreach (var item in orgRepos)
{
var repoContributors = await githubClient.Repository.GetAllContributors(MY_ORGANIZATION, item.Name);
foreach (var items in repoContributors)
{
contributions.TryGetValue(items.Login, out int currentCount);
contributions[items.Login] = currentCount + items.Contributions;
}
}
var results = from pair in contributions
orderby pair.Value descending
select pair;
// Display results.
foreach (var pair in results)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment