-
Notifications
You must be signed in to change notification settings - Fork 161
/
index.js
115 lines (100 loc) · 2.69 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
var mapLimit = require('map-limit')
var request = require('request')
var colors = {
'duplicate': 'ededed',
'greenkeeper': 'ededed',
'starter': 'ffc0cb',
'Priority: Critical': 'ee0701',
'Priority: High': 'd93f0b',
'Priority: Low': '0e8a16',
'Priority: Medium': 'fbca04',
'Status: Abandoned': '000000',
'Status: Available': 'c2e0c6',
'Status: Blocked': 'ee0701',
'Status: In Progress': 'cccccc',
'Status: On Hold': 'e99695',
'Status: Proposal': 'd4c5f9',
'Status: Review Needed': 'fbca04',
'Type: Bug': 'ee0701',
'Type: Documentation': '5319e7',
'Type: Enhancement': '1d76db',
'Type: Maintenance': 'fbca04',
'Type: Question': 'cc317c'
}
module.exports = githubStandardLabels
// https://developer.github.com/v3/issues/labels/
function githubStandardLabels (opts, cb) {
var username = opts.username
var github = opts.github
var repo = opts.repo
var auth = github.token + ':x-oauth-basic@'
var uri = 'https://' + auth +
'api.github.com/repos/' +
username + '/' + repo + '/labels'
var reqOpts = {
uri: uri,
headers: { 'User-Agent': github.user }
}
var labels = null
var operations = [
getLabels,
cleanLabels,
createLabels
]
mapLimit(operations, 1, iterator, cb)
function iterator (fn, cb) {
fn(cb)
}
function getLabels (done) {
request(reqOpts, function (err, res, body) {
if (err) return done(err)
if (res.statusCode !== 200) {
return done(new Error('non-200 statusCode received. ' + body))
}
if (!body) return done(new Error('no body returned'))
try {
labels = JSON.parse(body)
} catch (e) {
return done(e)
}
done()
})
}
function cleanLabels (done) {
mapLimit(labels, 1, iterator, done)
function iterator (label, done) {
var opts = {
uri: uri + '/' + label.name,
headers: { 'User-Agent': github.user }
}
request.del(opts, function (err, res, body) {
if (err) return done(err)
if (res.statusCode !== 204) {
return done(new Error('non-204 statusCode received. ' + body))
}
done()
})
}
}
function createLabels (done) {
mapLimit(Object.keys(colors), 1, iterator, done)
function iterator (name, done) {
var color = colors[name]
var opts = {
uri: uri,
headers: { 'User-Agent': github.user }
}
var req = request.post(opts, function (err, res, body) {
if (err) return done(err)
if (res.statusCode !== 201) {
return done(new Error('non-201 statusCode received. ' + body))
}
done()
})
req.end(JSON.stringify({
name: name,
color: color
}))
}
}
}