-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
412 lines (338 loc) · 9.7 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
var request = require('request-promise')
/*
* checks that a string is a valid hex color code without the preceding `#`
*/
var valid_color = /^([0-9A-F]{3}$|[0-9A-F]{6}$)/i
/*
* GitHub api requires a `User-Agent` header.
*/
var header = { 'User-Agent': 'org-labels' }
/*
* expose `Labeler`
*/
module.exports = Labeler
/*
* Initialize with CLI options and GitHub auth
*
* This allows us to not pass this stuff absolutely everwhere.
*
* opts = {
* destructive: <boolean>
* }
}
*/
function Labeler(opts, auth) {
this.opts = opts
this.auth = {
user: auth.token
, pass: 'x-oauth-basic'
}
}
/*
* Setup the prototype functions
*/
var proto = Labeler.prototype
/* actions */
proto.add = add
proto.remove = remove
proto.update = update
proto.rename = rename
proto.standardize = standardize
/* utilities */
proto.handle_repo_labels = handle_repo_labels
proto.get_repos = get_repos
proto.handle_label = handle_label
proto.send_label = send_label
/*
* Adds a label with the specified name and color to all repos in an org.
*/
function* add(args) {
var org = args[0]
var label = args[1]
var color = args[2]
if (!valid_color.test(color))
throw new TypeError('color must be a valid hex color code without the \'#\': 09aF00')
return yield* this.handle_label(org, 'POST', { name: label, color: color },
'done adding labels')
}
/*
* Removes a label with the specified name from all repos in an org.
*/
function* remove(args) {
var org = args[0]
var label = args[1]
return yield* this.handle_label(org, 'DELETE', { name: label, ext: label },
'done removing labels')
}
/*
* Updates an existing label with the specified name to the specified color for all repos in an org.
*/
function* update(args) {
var org = args[0]
var label = args[1]
var color = args[2]
if (!valid_color.test(color))
throw new TypeError('color must be a valid hex color code without the \'#\': 09aF00')
return yield* this.handle_label(org, 'PATCH', { name: label, color: color, ext: label },
'done updating labels')
}
/*
* Renames an existing label with the specified revised name for all repos in an org.
*/
function* rename(args) {
var org = args[0]
var label = args[1]
var new_label = args[2]
return yield* this.handle_label(org, 'PATCH', { name: new_label, ext: label },
'done renaming labels')
}
/*
* Standardizes a json list of labels across all repos in an org.
*
* The json list must reside in a repo at config/github_labels.json
*/
function* standardize(args) {
var org = args[0]
var config_repo = args[1]
// if the config_repo isn't a user/repo path, make it one.
if (!~config_repo.indexOf('/')) {
config_repo = org + '/' + config_repo
}
var res = yield request({
uri : 'https://api.github.com/repos/' + config_repo + '/contents/config/github_labels.json'
, headers: header
, auth : this.auth
, json : true
, resolveWithFullResponse: true
}).catch(log_request_err('error retrieving config from repo:'))
if (!res) process.exit()
// github sends the body (json file) as base64
var config = JSON.parse(new Buffer(res.body.content, 'base64').toString('utf8'))
if (!Array.isArray(config))
throw new Error('error: github_labels.json must be a json array')
// check if the org specifies a single repo via org/repo
if (~org.indexOf('/')) {
var org_and_repo = org.split('/')
var repos = [org_and_repo[1]]
org = org_and_repo[0]
} else {
// if no single repo is specified, do all the repos! \o/
var repos = yield* this.get_repos(org)
}
console.log('checking %d labels across %d repos', config.length, repos.length)
var i = repos.length
var reqs = []
while (i--) {
reqs.push(this.handle_repo_labels(org, repos[i], config, this.opts.destructive))
}
var results = yield reqs
var info = log_results(results)
console.log('%d label updates across %d repos', info.updates, info.repos)
console.log('done standardizing labels')
}
/*
* Handles differences between existing labels and a config list of labels.
*
* returns an array of responses
*/
function* handle_repo_labels(org, repo, config, destructive) {
var uri = 'https://api.github.com/repos/' + org + '/' + repo + '/labels'
var res = yield request({
uri : uri
, headers: header
, method : 'GET'
, json : true
, auth : this.auth
}).catch(log_request_err('error getting labels from a repo:'))
if (!res) return []
var list = compare_labels(config, res, destructive)
var results = []
var i = list.length
while (i--) {
item = list[i]
results.push(request({
uri : uri + (item.method === 'POST' ? '' : '/' + item.name)
, headers: header
, method : item.method
, json : item
, auth : this.auth
, resolveWithFullResponse: true
}))
}
return yield results
}
/*
* Compares two lists of labels and determines the differences.
*
* returns a list of objects containing the needed JSON body and http method.
*/
function compare_labels(config, _existing, destructive) {
var out = []
var i = config.length
// don't splice the actual array
var existing = _existing.slice(0)
while (i--) {
var wanted = config[i]
var next = false
var j = existing.length
var current
while (j--) {
current = existing[j]
if (wanted.name !== current.name) continue
existing.splice(j, 1)
next = {
name : wanted.name
, color : wanted.color
, method: 'PATCH'
}
break
}
if (next && wanted.color === current.color) continue
out.push(next || {
name : wanted.name
, color : wanted.color
, method: 'POST'
})
}
i = existing.length
while (destructive && i--) {
out.push({
name : existing[i].name
, method: 'DELETE'
})
}
return out
}
/*
* Gets information about all of a GitHub organization's repos.
*
* returns a list of repos
*/
function* get_repos(org) {
var repos = []
var page = 0
var last_length = 0
// handle github pagination for orgs with many repos
while (++page) {
var res = yield request({
uri : 'https://api.github.com/users/' + org + '/repos?page=' + page
, headers: header
, auth : this.auth
, json : true
}).catch(log_request_err('error retrieving org\'s repos:'))
if (!res) continue
var i = res.length
while (i--) {
repos.push(res[i].name)
}
// if this page has less repos than the last, then it is the last page.
if (res.length < last_length) break
last_length = res.length
}
console.log('found %d repositories in %s\n', repos.length, org)
return repos
}
/*
* Handles getting repos and sending requests for single-label commands.
*
* See `send_label` for options
*
* returns an array of responses
*/
function* handle_label(org, method, opts, done) {
var repos = yield* this.get_repos(org)
var results = yield* this.send_label(org, repos, opts, method)
var i = results.length
while (i--) {
log_result(results[i], opts.name)
}
if (done) console.log(done)
return yield results
}
/*
* Applies a label via method & options to all repos.
*
* Options can contain:
* - The outgoing json, sent as the entire options.
* - The uri extension.
* - The http method, if not otherwise specified.
*
* returns an array of responses
*/
function* send_label(org, repos, opts, method) {
var arr = []
var i = repos.length
var uri = 'https://api.github.com/repos/' + org + '/'
while (i--) {
arr.push(request({
uri : uri + repos[i] + '/labels' + (opts.ext ? '/' + opts.ext : '')
, headers: header
, method : method || opts.method
, json : opts
, auth : this.auth
, resolveWithFullResponse: true
}))
}
return yield arr
}
/*
* Logs a two-dimensional [][] array of results.results
*
* returns the total number of results
*/
function log_results(results) {
var updates = 0
var repos = []
var i = results.length
while (i--) {
var sub = results[i]
var j = sub.length
while (j--) {
var result = sub[j]
// increment counter on successful request (2XX code)
if (('' + result.statusCode)[0] === "2") {
updates++
if (!~repos.indexOf(result.request.path))
repos.push(result.request.path)
}
log_result(result)
}
}
return { updates: updates, repos: repos.length }
}
/*
* Logs a single response object.
*/
function log_result(result, label) {
label = label || (result.body && result.body.name)
// delete requests to github do not return bodies ..
if (!label) {
var path = result.request.path
label = path.slice(path.lastIndexOf('/') + 1)
}
if (result.statusCode === 422)
console.log('label `' + label + '` already exists at ' + result.request.path)
else if (result.statusCode === 200)
console.log('label `' + label + '` successfully updated at ' + result.request.path)
else if (result.statusCode === 201)
console.log('label `' + label + '` successfully created at ' + result.request.path)
else if (result.statusCode === 204)
console.log('label `' + label + '` successfully deleted from ' + result.request.path)
else {
if (result.request.path) console.log(result.request.path)
console.log('status: ' + result.statusCode)
if (result.body) console.log(result.body)
}
}
/*
* make a generic request error logger with a specified message
*/
function log_request_err(msg) {
return function (err) {
if (err.response.headers['x-ratelimit-remaining']>>0 === 0) {
console.log('Exceded GitHub rate-limit. Bailing.')
return process.exit()
}
console.log(msg, JSON.stringify(err.response.headers) +'\n')
}
}