|
1 | 1 | module.exports = handlePullRequestChange |
2 | 2 |
|
3 | 3 | async function handlePullRequestChange (context) { |
4 | | - const {pull_request: {title, head, number, labels}, repository} = context.payload |
5 | | - const labelNames = labels.map(label => label.name) |
6 | | - const isWip = containsWIP(title) || labelNames.some(containsWIP) || await commitsContainWIP(context) |
7 | | - const status = isWip ? 'pending' : 'success' |
8 | | - |
9 | | - if (context.payload.repository.private) { |
10 | | - console.log(`${repository.owner.login}/<private>#${number} — ${status}`) |
11 | | - } else { |
12 | | - console.log(`${repository.full_name}#${number} "${title}" — ${status}`) |
| 4 | + const {pull_request: pr} = context.payload |
| 5 | + const currentStatus = await getCurrentStatus(context) |
| 6 | + const labelNames = pr.labels.map(label => label.name) |
| 7 | + const isWip = containsWIP(pr.title) || labelNames.some(containsWIP) || await commitsContainWIP(context) |
| 8 | + const newStatus = isWip ? 'pending' : 'success' |
| 9 | + const logLabel = `${pr.html_url} "${pr.title}"` |
| 10 | + |
| 11 | + // if status did not change then don’t call .createStatus. Quotas for mutations |
| 12 | + // are much more restrictive so we want to avoid them if possible |
| 13 | + if (currentStatus === newStatus) { |
| 14 | + return console.log(`${logLabel} — ${currentStatus} (unchanged)`) |
13 | 15 | } |
14 | 16 |
|
15 | | - context.github.repos.createStatus(context.repo({ |
16 | | - sha: head.sha, |
17 | | - state: status, |
18 | | - target_url: 'https://github.com/apps/wip', |
19 | | - description: isWip ? 'work in progress – do not merge!' : 'ready for review', |
20 | | - context: 'WIP' |
| 17 | + try { |
| 18 | + await context.github.repos.createStatus(context.repo({ |
| 19 | + sha: pr.head.sha, |
| 20 | + state: newStatus, |
| 21 | + target_url: 'https://github.com/apps/wip', |
| 22 | + description: isWip ? 'work in progress' : 'ready for review', |
| 23 | + context: 'WIP' |
| 24 | + })) |
| 25 | + console.log(`${logLabel} — ${newStatus}`) |
| 26 | + } catch (error) { |
| 27 | + console.log(`${logLabel} — ${error}`) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +async function getCurrentStatus (context) { |
| 32 | + const {data: {statuses}} = await context.github.repos.getCombinedStatusForRef(context.repo({ |
| 33 | + ref: context.payload.pull_request.head.sha |
21 | 34 | })) |
| 35 | + |
| 36 | + return (statuses.find(status => status.context === 'WIP') || {}).state |
22 | 37 | } |
23 | 38 |
|
24 | 39 | async function commitsContainWIP (context) { |
|
0 commit comments