Skip to content

Commit 447cecb

Browse files
raboidmfix22
raboid
authored andcommitted
check for merge method in label
1 parent a342c32 commit 447cecb

File tree

3 files changed

+111
-38
lines changed

3 files changed

+111
-38
lines changed

src/config.js

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const defaultConfig = {
1717
duplicate: CLOSE,
1818
wontfix: CLOSE,
1919
invalid: CLOSE,
20+
'squash when passing': MERGE,
21+
'rebase when passing': MERGE,
2022
'merge when passing': MERGE
2123
},
2224
comments: [],

src/pull/labeled.js

+49-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const ms = require('ms')
2+
const retry = require('async-retry')
23

34
const getId = require('../get-job-id')
45
const getConfig = require('../config')
@@ -20,6 +21,12 @@ const status = {
2021
UNSTABLE: 'unstable' // can merge, but build is failing 🚫
2122
}
2223

24+
const methods = {
25+
MERGE: 'merge',
26+
SQUASH: 'squash',
27+
REBASE: 'rebase'
28+
}
29+
2330
module.exports = queue => async context => {
2431
const ID = getId(context, { action: MERGE })
2532

@@ -50,15 +57,17 @@ module.exports = queue => async context => {
5057
})
5158

5259
if (withMergeableLabels.length) {
53-
return queue
54-
.createJob({
55-
...context.issue({ installation_id: context.payload.installation.id }),
56-
action: MERGE
57-
})
58-
.setId(ID)
59-
.retries(RETRY_HORIZON / RETRY_PERIOD)
60-
.backoff('fixed', RETRY_PERIOD)
61-
.save()
60+
return (
61+
queue
62+
.createJob({
63+
...context.issue({ installation_id: context.payload.installation.id }),
64+
action: MERGE
65+
})
66+
.setId(ID)
67+
//.retries(RETRY_HORIZON / RETRY_PERIOD)
68+
//.backoff('fixed', RETRY_PERIOD)
69+
.save()
70+
)
6271
}
6372

6473
// If closable labels are removed, delete job for this pull
@@ -81,36 +90,40 @@ module.exports.process = robot => async ({ data: { installation_id, owner, repo,
8190
// || pull.mergeable_state === status.HAS_HOOKS
8291
const isMergeable = pull.mergeable && !pull.merged && pull.mergeable_state === status.CLEAN
8392

93+
const method = pull.labels.find(({ name }) => name.match(/rebase/i))
94+
? methods.REBASE
95+
: pull.labels.find(({ name }) => name.match(/squash/i))
96+
? methods.SQUASH
97+
: methods.MERGE
98+
8499
if (isMergeable) {
100+
const payload = {
101+
owner,
102+
repo,
103+
number,
104+
sha: pull.head.sha,
105+
merge_method: method
106+
}
107+
85108
try {
86-
await github.pullRequests.merge({
87-
owner,
88-
repo,
89-
number,
90-
sha: pull.head.sha,
91-
merge_method: 'merge'
92-
/*
93-
commit_title,
94-
commit_message,
95-
*/
96-
})
109+
await github.pullRequests.merge(payload)
97110
} catch (e) {
98-
try {
99-
await github.pullRequests.merge({
100-
owner,
101-
repo,
102-
number,
103-
sha: pull.head.sha,
104-
merge_method: 'rebase'
105-
})
106-
} catch (e) {
107-
await github.pullRequests.merge({
108-
owner,
109-
repo,
110-
number,
111-
sha: pull.head.sha,
112-
merge_method: 'squash'
113-
})
111+
if (method === methods.MERGE) {
112+
payload.merge_method = methods.REBASE
113+
try {
114+
await github.pullRequests.merge(payload)
115+
} catch (e) {
116+
payload.merge_method = methods.SQUASH
117+
await github.pullRequests.merge(payload)
118+
}
119+
} else if (method === methods.REBASE || method === methods.SQUASH) {
120+
payload.merge_method = methods.MERGE
121+
try {
122+
await github.pullRequests.merge(payload)
123+
} catch (e) {
124+
payload.merge_method = method === methods.REBASE ? methods.SQUASH : methods.REBASE
125+
await github.pullRequests.merge(payload)
126+
}
114127
}
115128
}
116129
} else if (pull.mergeable_state === status.DIRTY) {

test/index.test.js

+60-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ labels:
122122
action: comment
123123
message: boop
124124
comment3: comment
125+
'squash when passing':
126+
action: merge
127+
'rebase when passing':
128+
action: merge
125129
126130
comments:
127131
- action: label
@@ -403,7 +407,7 @@ describe('Bot', () => {
403407
})
404408
})
405409

406-
test('Will first try merge, then rebase, then squash', async () => {
410+
test('Will first try merge, then rebase, and then squash', async () => {
407411
github.pullRequests.merge
408412
.mockRejectedValueOnce(new Error('No Merge'))
409413
.mockRejectedValueOnce(new Error('No Rebase'))
@@ -419,7 +423,61 @@ describe('Bot', () => {
419423
)
420424

421425
await wait(2)
422-
;['merge', 'rebase', 'squash'].forEach(merge_method => {
426+
;[('merge', 'rebase', 'squash')].forEach(merge_method => {
427+
expect(github.pullRequests.merge).toHaveBeenCalledWith({
428+
number: 7,
429+
owner: 'mfix22',
430+
repo: 'test-issue-bot',
431+
sha: 0,
432+
merge_method
433+
})
434+
})
435+
})
436+
437+
test('Will first try squash, then merge, and then rebase if label contains "squash"', async () => {
438+
github.pullRequests.merge
439+
.mockRejectedValueOnce(new Error('No Squash'))
440+
.mockRejectedValueOnce(new Error('No Merge'))
441+
.mockResolvedValueOnce()
442+
443+
await robot.receive(
444+
payload({
445+
name: 'pull_request',
446+
threadType: 'pull_request',
447+
labels: ['squash when passing'],
448+
number: 7
449+
})
450+
)
451+
452+
await wait(2)
453+
;['squash', 'merge', 'rebase'].forEach(merge_method => {
454+
expect(github.pullRequests.merge).toHaveBeenCalledWith({
455+
number: 7,
456+
owner: 'mfix22',
457+
repo: 'test-issue-bot',
458+
sha: 0,
459+
merge_method
460+
})
461+
})
462+
})
463+
464+
test('Will first try rebase, then merge, and then squash if label contains "rebase"', async () => {
465+
github.pullRequests.merge
466+
.mockRejectedValueOnce(new Error('No Rebase'))
467+
.mockRejectedValueOnce(new Error('No Merge'))
468+
.mockResolvedValueOnce()
469+
470+
await robot.receive(
471+
payload({
472+
name: 'pull_request',
473+
threadType: 'pull_request',
474+
labels: ['rebase when passing'],
475+
number: 7
476+
})
477+
)
478+
479+
await wait(2)
480+
;['rebase', 'merge', 'squash'].forEach(merge_method => {
423481
expect(github.pullRequests.merge).toHaveBeenCalledWith({
424482
number: 7,
425483
owner: 'mfix22',

0 commit comments

Comments
 (0)