-
Notifications
You must be signed in to change notification settings - Fork 41
/
collect-commit-labels.js
56 lines (44 loc) · 1.48 KB
/
collect-commit-labels.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
'use strict'
import { auth } from './auth.js'
import ghissues from 'ghissues'
import async from 'async'
export async function collectCommitLabels (list) {
const sublist = list.filter((commit) => {
return typeof commit.ghIssue === 'number' && commit.ghUser && commit.ghProject
})
if (!sublist.length) {
return
}
const authData = await auth()
const cache = {}
const q = async.queue((commit, next) => {
function onFetch (err, issue) {
if (err) {
console.error('Error fetching issue #%s: %s', commit.ghIssue, err.message)
return next()
}
if (issue.labels) {
commit.labels = issue.labels.map((label) => label.name)
}
next()
}
if (commit.ghUser === 'iojs') {
commit.ghUser = 'nodejs' // forcibly rewrite as the GH API doesn't do it for us
}
// To prevent multiple simultaneous requests for the same issue
// from hitting the network at the same time, immediately assign a Promise
// to the cache that all commits with the same ghIssue value will use.
const key = `${commit.ghUser}/${commit.ghProject}#${commit.ghIssue}`
cache[key] = cache[key] || new Promise((resolve, reject) => {
ghissues.get(authData, commit.ghUser, commit.ghProject, commit.ghIssue, (err, issue) => {
if (err) {
return reject(err)
}
resolve(issue)
})
})
cache[key].then((val) => onFetch(null, val), (err) => onFetch(err))
}, 15)
q.push(sublist)
await q.drain()
}