-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathget_contributors.js
104 lines (98 loc) · 3.35 KB
/
get_contributors.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
require('dotenv').config()
const { Octokit } = require("@octokit/core")
const { throttling } = require("@octokit/plugin-throttling")
module.exports = async function (filepath) {
const unixStyleFilepath = filepath.replace(/\\/g, "/")
const EnhancedOctokit = Octokit.plugin(throttling)
const octokit = new EnhancedOctokit({
auth: process.env.TOKEN,
throttle: {
onRateLimit: (retryAfter, options, octokit, retryCount) => {
if (retryCount <= 1) {
octokit.log.info(`Request quota reached for ${options.method} ${options.url}, retrying after ${retryAfter} seconds`)
return true
}
octokit.log.warn(`Request quota exhausted for ${options.method} ${options.url}`)
return false
},
onSecondaryRateLimit: (retryAfter, options, octokit) => {
octokit.log.warn(`SecondaryRateLimit hit for request ${options.method} ${options.url}, retrying after ${retryAfter} seconds`)
return true
},
},
})
const { repository: { object: { history } } } = await octokit.graphql(
`query ($filepath: String!) {
repository(owner: "InnerSourceCommons", name: "InnerSourceLearningPath") {
object(expression: "main") {
... on Commit {
history(first: 100, path: $filepath) {
totalCount
nodes {
authors(first: 100) {
nodes {
name
user {
name
url
}
}
}
associatedPullRequests(last: 1, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
reviews(first:100) {
nodes {
author {
... on User {
login
name
url
}
}
}
}
}
}
}
}
}
}
}
}`, {
filepath: unixStyleFilepath
}
)
if (history.totalCount > 100) {
// Needs addressing properly, but deferring to avoid merge conflicts with https://github.com/InnerSourceCommons/InnerSourceLearningPath/pull/380
console.warn('This script needs updating to handle >100 commits: ', unixStyleFilepath)
}
const authors = history.nodes.flatMap(({ authors }) => {
return authors.nodes.map(author => {
return {
name: (author.user && author.user.name) || author.name,
url: author.user && author.user.url
}
})
})
const reviewers = history.nodes.flatMap(({ associatedPullRequests }) => {
return associatedPullRequests.nodes.flatMap(({ reviews }) => {
return reviews.nodes.map(({ author }) => {
return {
name: author.name || author.login,
url: author.url
}
})
})
})
return Object.values(
[...authors, ...reviewers].reduce((accumulator, user) => {
// Dedupe users
// Some user objects do not have a URL (can't figure out why).
// In that case, copy over a good URL instead.
const newUser = accumulator[user.name] || user
newUser.url = newUser.url || user.url
accumulator[user.name] = newUser
return accumulator
}, {})
)
}