-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathnode-repo.js
More file actions
67 lines (55 loc) · 1.65 KB
/
Copy pathnode-repo.js
File metadata and controls
67 lines (55 loc) · 1.65 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
'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 options.logger.error(err, 'Error retrieving files from GitHub')
}
const filepathsChanged = res.map((fileMeta) => fileMeta.filename)
const labels = resolveLabels(filepathsChanged, options.baseBranch)
updatePrWithLabels(options, labels)
})
}
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 options.logger.error(err, 'Error while editing issue to add labels')
}
options.logger.info(`Added labels: ${labels}`)
})
})
}
function fetchExistingLabels (options, cb) {
githubClient.issues.getIssueLabels({
user: options.owner,
repo: options.repo,
number: options.prId
}, (err, res) => {
if (err) {
options.logger.error(err, 'Error while fetching existing issue labels')
return cb(err)
}
const existingLabels = res.map((labelMeta) => labelMeta.name)
cb(null, existingLabels)
})
}
exports.resolveLabelsThenUpdatePr = resolveLabelsThenUpdatePr