diff --git a/lib/legacy/handle-pull-request-change.js b/lib/legacy/handle-pull-request-change.js index e8309eb68..06e71f595 100644 --- a/lib/legacy/handle-pull-request-change.js +++ b/lib/legacy/handle-pull-request-change.js @@ -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) -}