-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
249 lines (219 loc) · 6.49 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
'use strict'
const graphql = require('./graphql')
const queries = require('./queries')
const csv = require('csv-writer').createArrayCsvStringifier
const exec = require('child_process').exec
const readFileSync = require('fs').readFileSync
const Spinner = require('clui').Spinner
const use = require('nclr').use
const loader = new Spinner('Loading...')
//
// Shell Helpers
//
const shellOut = command =>
new Promise((resolve, reject) =>
exec(command, (err, stdout, _stderr) => {
if (err) {
reject(err)
} else {
resolve(stdout)
}
}))
const gitConfigCommand = 'git config --get remote.origin.url'
const parseGitURLRE = new RegExp('.*github\\.com[:/]([^/]+)\\/(.+)$')
const parseGitURL = url => {
const parse = parseGitURLRE.exec(url.trim())
if (parse[2].endsWith('.git')) {
parse[2] = parse[2].substring(0, parse[2].indexOf('.git'))
}
return parse
}
const getCurrentRepoInfo = () => shellOut(gitConfigCommand)
.then(parseGitURL)
.then(x => { return { user: x[1], repo: x[2] } })
//
// CSV Output
//
const flatten = json => {
const prs = json.prCreators.map(x => ['pr creator'].concat(x))
const prcs = json.prCommentators.map(x => ['pr commentator'].concat(x))
const is = json.issueCreators.map(x => ['issue creator'].concat(x))
const iscs = json.issueCommentators.map(x => ['issue commentator'].concat(x))
const co = json.commitAuthors.map(x => ['commit creator'].concat(x))
const cocs = json.commitCommentators.map(x => ['commit commentator'].concat(x))
return [...prs, ...prcs, ...is, ...iscs, ...co, ...cocs]
}
const toCSV = json => {
const writer = csv({
header: ['TYPE', 'LOGIN', 'NAME']
})
return writer.getHeaderString() +
writer.stringifyRecords(flatten(json))
}
const verifyResultHasKey = (key, query, dryRun) =>
x => {
if (!dryRun && x[key] == null) {
throw new Error(`Bad query: ${key} '${query}' does not exist`)
} else {
return x
}
}
//
// Config File Parsing
//
// API
//
const epochDate = new Date(0)
const prunedFetch = args => graphql.prune(args)
.then(json => queries.timeFilterFullTree(json, args.before, args.after))
/** Returns all contributions to a repo.
* @param token - GitHub auth token
* @param user - Username to whom the repo belongs
* @param repo - repo name
* @param {Date} [before = new Date()] - only return contributions before this timestamp
* @param {Date} [after = new Date(0)] - only return contributions after this timestamp
*/
const repoContributors = ({
token, user, repo, before = new Date(), after = epochDate, debug, dryRun, verbose, commits, reactions, full
}) => {
loader.message(`Getting all contributors from repo ${use('inp', repo)}...`)
loader.start()
const summarise = args =>
graphql.execute(args)
.then(verifyResultHasKey('repository', user + '/' + repo, dryRun))
.then(json => {
if (dryRun) {
return json
} else {
return queries.repoSynopsis({ json, before, after, commits, reactions })
}
})
const qfn = full ? prunedFetch : summarise
loader.stop()
return qfn({
token,
debug,
dryRun,
before,
after,
verbose,
name: `${user}/${repo}`,
query: queries.repository(repo, user, before, after, commits, reactions)
})
}
/** Returns contributions to all repos owned by orgName.
* @param token - GitHub auth token
* @param orgName - Name of organization
* @param {Date} [before = new Date()] - only return contributions before this timestamp
* @param {Date} [after = new Date(0)] - only return contributions after this timestamp
*/
const orgContributors = ({
token, orgName, before = new Date(), after = epochDate, debug, dryRun, verbose, commits, reactions, full
}) => {
loader.message(`Getting all contributors of ${use('inp', orgName)}...`)
loader.start()
const summarise = args =>
graphql.execute(args)
.then(verifyResultHasKey('organization', orgName, dryRun))
.then(json => {
if (dryRun) {
return json
} else {
return queries.orgSynopsis({
json, before, after, commits, reactions
})
}
})
const qfn = full ? prunedFetch : summarise
loader.stop()
return qfn({
token,
debug,
before,
after,
dryRun,
verbose,
name: orgName,
query: queries.orgRepos(orgName, before, after, commits, reactions)
})
}
/** Returns all contributions to repos and orgs specified in `file`
* @param token - GitHub auth token
* @param file - Config file path
*/
const fromConfig = async ({
token, file, commits, reactions, verbose, debug, dryRun, full
}) => {
loader.message(`Getting config from ${use('inp', file)}...`)
loader.start()
const config = JSON.parse(readFileSync(file))
const ght = config.token || token
if (!ght) {
throw new Error('No token specified in config or arguments. Aborting.')
}
loader.message('Getting repo information from the config')
const repoResults = config.repos.map(({ login, repo, before, after }) => {
const afterDate = after ? new Date(after) : new Date(0)
const beforeDate = before ? new Date(before) : new Date()
return repoContributors({
token: ght,
user: login,
repo,
before: beforeDate,
after: afterDate,
commits,
reactions,
full,
debug,
dryRun,
verbose
})
})
loader.message('Getting org information from the config')
const orgResults = config.orgs.map(({ login, before, after }) => {
const afterDate = after ? new Date(after) : new Date(0)
const beforeDate = before ? new Date(before) : new Date()
return orgContributors({
orgName: login,
before: beforeDate,
after: afterDate,
token: ght,
commits,
reactions,
full,
verbose,
debug,
dryRun
})
})
loader.stop()
return {
repos: (await Promise.all(repoResults)).map((result, index) => {
const repo = config.repos[index]
repo.contributions = result
return repo
}),
orgs: (await Promise.all(orgResults)).map((result, index) => {
const org = config.orgs[index]
org.contributions = result
return org
})
}
}
/** Returns the login of the user to whom the given token is registered.
* @param token - GitHub Auth token
*/
const currentUser = token =>
graphql.execute({
token,
query: queries.whoAmI
}).then(queries.cleanWhoAmI)
module.exports = {
toCSV,
fromConfig,
parseGitURL,
getCurrentRepoInfo,
currentUser,
repoContributors,
orgContributors
}