This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
106 lines (83 loc) · 2.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { Octokit } = require('@octokit/core')
const fetch = require('isomorphic-fetch')
require('dotenv').config()
const authToken = process.env.GITHUB_TOKEN
if (!authToken) {
throw new Error('GITHUB_TOKEN is required')
}
const octokit = new Octokit({
auth: authToken
})
async function main() {
const org = process.argv[2] || process.env.ORG
if (!org) {
throw new Error('Please provide an org name')
}
const repos = await getRepos(org)
const repoContributors = []
for (const { repo, url } of repos) {
const contributors = await getContributors(org, repo)
const totalContributions = contributors.reduce((acc, curr) => acc + curr.contributions, 0)
repoContributors.push({
org,
repo,
url,
contributors,
totalContributions
})
}
const sortedRepoContributors = repoContributors.sort((a, b) => b.totalContributions - a.totalContributions)
const json = {
lastUpdated: Date.now(),
org,
repos: sortedRepoContributors
}
console.log(JSON.stringify(json, null, 2))
}
async function getRepos(org) {
const res = await octokit.request('GET /orgs/{org}/repos', {
org,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
const json = []
for (const item of res.data) {
if (item.private) {
continue
}
if (item.fork) {
continue
}
json.push({
repo: item.name,
url: item.html_url,
})
}
return json
}
async function getContributors(org, repo) {
const res = await octokit.request('GET /repos/{owner}/{repo}/contributors', {
owner: org,
repo: repo,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
const json = []
let sumContributions = 0
for (const item of res.data) {
sumContributions += item.contributions
}
for (const item of res.data) {
json.push({
username: item.login,
avatar: item.avatar_url,
url: item.html_url,
contributions: item.contributions,
percentage: Number(((item.contributions / sumContributions) * 100).toFixed(2))
})
}
return json
}
main().catch(console.error)