Skip to content

Commit

Permalink
feat: deprecate support for legacy commit status support
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed May 31, 2019
1 parent 669407d commit 9b4a335
Showing 1 changed file with 9 additions and 45 deletions.
54 changes: 9 additions & 45 deletions lib/legacy/handle-pull-request-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,35 @@ module.exports = handlePullRequestChange
const getConfig = require('../app-config')

async function handlePullRequestChange (context) {
const { action, pull_request: pr, repository: repo } = context.payload
const currentStatus = await getCurrentStatus(context)
const labelNames = pr.labels.map(label => label.name)
const isWip = containsWIP(pr.title) || labelNames.some(containsWIP) || await commitsContainWIP(context)
const newStatus = isWip ? 'pending' : 'success'
const { action, pull_request: pr, repository: repo, installation, organization } = context.payload
const newStatus = 'error'
const shortUrl = `${repo.full_name}#${pr.number}`

const hasChange = currentStatus !== newStatus
const log = context.log.child({
name: getConfig().name,
event: context.event,
action,
account: repo.owner.id,
repo: repo.id,
change: hasChange,
wip: isWip,
legacy: true
})

// if status did not change then don’t call .createStatus. Quotas for mutations
// are much more restrictive so we want to avoid them if possible
if (!hasChange) {
return log.info(`😐 ${shortUrl} (legacy)`)
}
const isOrg = !!organization
const targetUrl = isOrg
? `https://github.com/organizations/${organization.login}/settings/installations/${installation.id}/permissions/update`
: `https://github.com/settings/installations/${installation.id}/permissions/update`

try {
await context.github.repos.createStatus(context.repo({
sha: pr.head.sha,
state: newStatus,
target_url: 'https://github.com/apps/wip',
description: isWip ? 'work in progress' : 'ready for review',
target_url: targetUrl,
description: 'Please accept the new permissions',
context: getConfig().name
}))

const logStatus = isWip ? '⏳' : '✅'
log.info(`${logStatus} ${shortUrl} (legacy)`)
log.info(`⛔ ${shortUrl} (legacy)`)
} catch (error) {
try {
// workaround for https://github.com/octokit/rest.js/issues/684
const parsed = JSON.parse(error.message)
for (const key in parsed) {
error[key] = parsed[key]
}
} catch (e) {}

log.error(error)
}
}

async function getCurrentStatus (context) {
const { data: { statuses } } = await context.github.repos.getCombinedStatusForRef(context.repo({
ref: context.payload.pull_request.head.sha
}))

return (statuses.find(status => status.context === getConfig().name) || {}).state
}

async function commitsContainWIP (context) {
const commits = await context.github.pullRequests.listCommits(context.repo({
number: context.payload.pull_request.number
}))

return commits.data.map(element => element.commit.message).some(containsWIP)
}

function containsWIP (string) {
return /\b(wip|do not merge|work in progress)\b/i.test(string)
}

1 comment on commit 9b4a335

@a2425rdl
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hc

Please sign in to comment.