Skip to content

Commit

Permalink
feat: support labels (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcelis authored and gr2m committed Feb 26, 2018
1 parent e66e1ae commit 77aacc8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

- Install the app on your GitHub Repositories: [github.com/apps/wip](https://github.com/apps/wip)
- When creating a pull request that you don’t want to be merged, simply add the
word "wip" or "do not merge" (not case-sensitive) somewhere in the pull request title
word "wip" or "do not merge" (not case-sensitive) somewhere in the pull request title,
or add a label containing "wip"
- The WIP bot will set/update the status of the pull request depending on the
pull requests title.
pull request's title or labels.

## Local setup

Expand Down
10 changes: 9 additions & 1 deletion lib/handle-pull-request-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = handlePullRequestChange

async function handlePullRequestChange (context) {
const {title, html_url: htmlUrl, head} = context.payload.pull_request
const isWip = containsWIP(title) || await commitsContainWIP(context)
const isWip = containsWIP(title) || await commitsContainWIP(context) || await labelsContainWIP(context)
const status = isWip ? 'pending' : 'success'

console.log(`Updating PR "${title}" (${htmlUrl}): ${status}`)
Expand All @@ -24,6 +24,14 @@ async function commitsContainWIP (context) {
return commits.data.map(element => element.commit.message).some(containsWIP)
}

async function labelsContainWIP (context) {
const labels = await context.github.issues.getIssueLabels(context.repo({
number: context.payload.pull_request.number
}))

return labels.data.map(label => label.name).some(containsWIP)
}

function containsWIP (string) {
return /\b(wip|do not merge)\b/i.test(string)
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
"semantic-release": "semantic-release"
},
"standard": {
"globals": [ "jest", "it", "expect", "describe" ]
"globals": [
"jest",
"it",
"expect",
"describe"
]
},
"repository": {
"type": "git",
Expand Down
65 changes: 65 additions & 0 deletions test/unit/lib/handle-pull-request-change.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,34 @@ describe('handlePullRequestChange', () => {
},
pullRequests: {
getCommits: jest.fn().mockReturnValue({ data: [] })
},
issues: {
getIssueLabels: jest.fn().mockReturnValue({ data: [] })
}
}
}
}

const createMockCommitContext = commitMessage => {
const context = createMockContext('Example PR title')

context.github.pullRequests.getCommits = jest.fn().mockReturnValue({
data: [{ commit: { message: commitMessage } }]
})

return context
}

const createMockLabelContext = labelName => {
const context = createMockContext('Example PR title')

context.github.issues.getIssueLabels = jest.fn().mockReturnValue({
data: [{ name: labelName }]
})

return context
}

const pendingStatusObject = {
context: 'WIP',
description: 'work in progress – do not merge!',
Expand Down Expand Up @@ -64,4 +87,46 @@ describe('handlePullRequestChange', () => {

expect(context.repo).lastCalledWith(successStatusObject)
})

it('creates pending status if a commit message contains `wip`', async () => {
const context = createMockCommitContext('[wip] foo bar commit message')
await handlePullRequestChange(context)

expect(context.repo).lastCalledWith(pendingStatusObject)
})

it('creates pending status if a commit message contains `do not merge`', async () => {
const context = createMockCommitContext('my DO NOT MERGE commit message')
await handlePullRequestChange(context)

expect(context.repo).lastCalledWith(pendingStatusObject)
})

it('creates success status if a commit message does not contain `wip` or `do not merge`', async () => {
const context = createMockCommitContext('my commit message')
await handlePullRequestChange(context)

expect(context.repo).lastCalledWith(successStatusObject)
})

it('creates pending status if a label contains `wip`', async () => {
const context = createMockLabelContext('WIP')
await handlePullRequestChange(context)

expect(context.repo).lastCalledWith(pendingStatusObject)
})

it('creates pending status if a label contains `do not merge`', async () => {
const context = createMockLabelContext('do not merge')
await handlePullRequestChange(context)

expect(context.repo).lastCalledWith(pendingStatusObject)
})

it('creates success status if a label does not contain `wip` or `do not merge`', async () => {
const context = createMockLabelContext('bug')
await handlePullRequestChange(context)

expect(context.repo).lastCalledWith(successStatusObject)
})
})

0 comments on commit 77aacc8

Please sign in to comment.