Last active
March 28, 2025 13:30
-
-
Save davidglezz/0bf6cd5f502007822c25788cb976f571 to your computer and use it in GitHub Desktop.
List the top contributions of an organization
This file contains hidden or 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
#!/usr/bin/env node | |
/** | |
* Create top contributors list for a GitHub organization. | |
* Usage: ./gh-org-top-contributors.ts <org-name> | |
*/ | |
const accessToken = process.env.GITHUB_TOKEN || process.env.GH_TOKEN || 'your-token' | |
const headers = { | |
Authorization: `token ${accessToken}`, | |
Accept: 'application/vnd.github.v3+json', | |
} | |
async function* getAll(path) { | |
const perPage = 100 | |
let page = 1 | |
let pageLength | |
do { | |
const url = `https://api.github.com/${path}?per_page=${perPage}&page=${page++}` | |
const data = await fetch(url, { headers }).then(res => res.json()) | |
pageLength = data.length | |
for (const item of data) yield item | |
} while (pageLength === perPage) | |
} | |
async function topContributors(orgName) { | |
const top = new Map() | |
const repos = getAll(`orgs/${orgName}/repos`) | |
for await (const repo of repos) { | |
const contributors = getAll(`repos/${orgName}/${repo.name}/contributors`) | |
for await (const { login: user, contributions } of contributors) { | |
top.set(user, (top.get(user) ?? 0) + contributions) | |
} | |
} | |
return [...top] | |
.filter(([user]) => !user.includes('[bot]')) | |
.toSorted((a, b) => b[1] - a[1]) | |
} | |
const padNum = (num, pad) => num.toString().padStart(pad) | |
const toText = ([user, count], i) => `${padNum(i + 1, 3)}. ${padNum(count, 7)} ${user}` | |
topContributors(process.argv[2]).then(top => { | |
console.log('\nRank Contrib User\n') | |
console.log(top.map(toText).join('\n')) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment