Skip to content

Commit 76070f4

Browse files
committed
feat: Don’t set status unless changed
1 parent 5770f04 commit 76070f4

1 file changed

Lines changed: 30 additions & 15 deletions

File tree

‎lib/handle-pull-request-change.js‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
module.exports = handlePullRequestChange
22

33
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)`)
1315
}
1416

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
2134
}))
35+
36+
return (statuses.find(status => status.context === 'WIP') || {}).state
2237
}
2338

2439
async function commitsContainWIP (context) {

0 commit comments

Comments
 (0)