Skip to content

Commit 77aacc8

Browse files
davidcelisgr2m
authored andcommitted
feat: support labels (#46)
1 parent e66e1ae commit 77aacc8

4 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

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

1718
## Local setup
1819

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = handlePullRequestChange
22

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

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

27+
async function labelsContainWIP (context) {
28+
const labels = await context.github.issues.getIssueLabels(context.repo({
29+
number: context.payload.pull_request.number
30+
}))
31+
32+
return labels.data.map(label => label.name).some(containsWIP)
33+
}
34+
2735
function containsWIP (string) {
2836
return /\b(wip|do not merge)\b/i.test(string)
2937
}

‎package.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
"semantic-release": "semantic-release"
1919
},
2020
"standard": {
21-
"globals": [ "jest", "it", "expect", "describe" ]
21+
"globals": [
22+
"jest",
23+
"it",
24+
"expect",
25+
"describe"
26+
]
2227
},
2328
"repository": {
2429
"type": "git",

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,34 @@ describe('handlePullRequestChange', () => {
1616
},
1717
pullRequests: {
1818
getCommits: jest.fn().mockReturnValue({ data: [] })
19+
},
20+
issues: {
21+
getIssueLabels: jest.fn().mockReturnValue({ data: [] })
1922
}
2023
}
2124
}
2225
}
2326

27+
const createMockCommitContext = commitMessage => {
28+
const context = createMockContext('Example PR title')
29+
30+
context.github.pullRequests.getCommits = jest.fn().mockReturnValue({
31+
data: [{ commit: { message: commitMessage } }]
32+
})
33+
34+
return context
35+
}
36+
37+
const createMockLabelContext = labelName => {
38+
const context = createMockContext('Example PR title')
39+
40+
context.github.issues.getIssueLabels = jest.fn().mockReturnValue({
41+
data: [{ name: labelName }]
42+
})
43+
44+
return context
45+
}
46+
2447
const pendingStatusObject = {
2548
context: 'WIP',
2649
description: 'work in progress – do not merge!',
@@ -64,4 +87,46 @@ describe('handlePullRequestChange', () => {
6487

6588
expect(context.repo).lastCalledWith(successStatusObject)
6689
})
90+
91+
it('creates pending status if a commit message contains `wip`', async () => {
92+
const context = createMockCommitContext('[wip] foo bar commit message')
93+
await handlePullRequestChange(context)
94+
95+
expect(context.repo).lastCalledWith(pendingStatusObject)
96+
})
97+
98+
it('creates pending status if a commit message contains `do not merge`', async () => {
99+
const context = createMockCommitContext('my DO NOT MERGE commit message')
100+
await handlePullRequestChange(context)
101+
102+
expect(context.repo).lastCalledWith(pendingStatusObject)
103+
})
104+
105+
it('creates success status if a commit message does not contain `wip` or `do not merge`', async () => {
106+
const context = createMockCommitContext('my commit message')
107+
await handlePullRequestChange(context)
108+
109+
expect(context.repo).lastCalledWith(successStatusObject)
110+
})
111+
112+
it('creates pending status if a label contains `wip`', async () => {
113+
const context = createMockLabelContext('WIP')
114+
await handlePullRequestChange(context)
115+
116+
expect(context.repo).lastCalledWith(pendingStatusObject)
117+
})
118+
119+
it('creates pending status if a label contains `do not merge`', async () => {
120+
const context = createMockLabelContext('do not merge')
121+
await handlePullRequestChange(context)
122+
123+
expect(context.repo).lastCalledWith(pendingStatusObject)
124+
})
125+
126+
it('creates success status if a label does not contain `wip` or `do not merge`', async () => {
127+
const context = createMockLabelContext('bug')
128+
await handlePullRequestChange(context)
129+
130+
expect(context.repo).lastCalledWith(successStatusObject)
131+
})
67132
})

0 commit comments

Comments
 (0)