-
-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathnode-repo.js
More file actions
70 lines (57 loc) · 1.74 KB
/
node-repo.js
File metadata and controls
70 lines (57 loc) · 1.74 KB
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
'use strict'
const githubClient = require('./github-client')
const resolveLabels = require('./node-labels').resolveLabels
function resolveLabelsThenUpdatePr (options) {
githubClient.pullRequests.getFiles({
user: options.owner,
repo: options.repo,
number: options.prId
}, (err, res) => {
if (err) {
return console.error(`! ${prInfoStr(options)} Error retrieving files`, err)
}
const filepathsChanged = res.map((fileMeta) => fileMeta.filename)
updatePrWithLabels(options, resolveLabels(filepathsChanged))
})
}
function updatePrWithLabels (options, labels) {
// no need to request github if we didn't resolve any labels
if (!labels.length) {
return
}
fetchExistingLabels(options, (err, existingLabels) => {
if (err) {
return
}
const mergedLabels = labels.concat(existingLabels)
githubClient.issues.edit({
user: options.owner,
repo: options.repo,
number: options.prId,
labels: mergedLabels
}, (err) => {
if (err) {
return console.error(`! ${prInfoStr(options)} Error while adding labels`, err)
}
console.log(`! ${prInfoStr(options)} Added labels: ${labels}`)
})
})
}
function fetchExistingLabels (options, cb) {
githubClient.issues.getIssueLabels({
user: options.owner,
repo: options.repo,
number: options.prId
}, (err, res) => {
if (err) {
console.error(`! ${prInfoStr(options)} Error while fetching existing labels`, err)
return cb(err)
}
const existingLabels = res.map((labelMeta) => labelMeta.name)
cb(null, existingLabels)
})
}
function prInfoStr (options) {
return `${options.owner}/${options.repo}/#${options.prId}`
}
exports.resolveLabelsThenUpdatePr = resolveLabelsThenUpdatePr