From 2d6181bd68b22f1e7f2c08154aec3974cf6caf3f Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 12 Feb 2018 23:15:13 +0100 Subject: [PATCH] feat: detect WIP commit messages (#32) thanks @Raul6469 & @HeeL --- lib/handle-pull-request-change.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/handle-pull-request-change.js b/lib/handle-pull-request-change.js index cc32e1de6..46093de7c 100644 --- a/lib/handle-pull-request-change.js +++ b/lib/handle-pull-request-change.js @@ -1,17 +1,29 @@ module.exports = handlePullRequestChange async function handlePullRequestChange (robot, context) { - const title = context.payload.pull_request.title - const isWip = /\b(wip|do not merge)\b/i.test(title) + const {title, html_url, head} = context.payload.pull_request + const isWip = containsWIP(title) || await commitsContainWIP(context) const status = isWip ? 'pending' : 'success' - console.log(`Updating PR "${title}" (${context.payload.pull_request.html_url}): ${status}`) + console.log('Updating PR "%s" (%s): %s', title, html_url, status) context.github.repos.createStatus(context.repo({ - sha: context.payload.pull_request.head.sha, + sha: head.sha, state: status, target_url: 'https://github.com/apps/wip', description: isWip ? 'work in progress – do not merge!' : 'ready for review', context: 'WIP' })) } + +async function commitsContainWIP (context) { + const commits = await context.github.pullRequests.getCommits(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)\b/i.test(string) +}