Skip to content

Commit aa29c21

Browse files
authored
refactor: github_api/pulls:merge (#539)
* refactor: github_api/pulls:merge * add missing awaits * ran lint
1 parent 7c7089b commit aa29c21

File tree

4 files changed

+89
-44
lines changed

4 files changed

+89
-44
lines changed

__fixtures__/unit/helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ module.exports = {
179179
return { status: 204 }
180180
}
181181
},
182-
merge: jest.fn(),
182+
merge: jest.fn().mockReturnValue(options.merge || 'merged'),
183183
get: jest.fn()
184184
},
185185
paginate: jest.fn(async (fn, cb) => {

__tests__/unit/github/api.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,3 +560,47 @@ describe('compareCommits', () => {
560560
}
561561
})
562562
})
563+
564+
describe('checkIfMerged', () => {
565+
test('return correct data if no error', async () => {
566+
let res = await GithubAPI.checkIfMerged(Helper.mockContext())
567+
expect(res).toEqual(true)
568+
569+
res = await GithubAPI.checkIfMerged(Helper.mockContext({ checkIfMerged: false }))
570+
expect(res).toEqual(false)
571+
})
572+
573+
test('that error are re-thrown', async () => {
574+
const context = Helper.mockContext()
575+
context.octokit.pulls.checkIfMerged = jest.fn().mockRejectedValue({ status: 402 })
576+
577+
try {
578+
await GithubAPI.checkIfMerged(context)
579+
// Fail test if above expression doesn't throw anything.
580+
expect(true).toBe(false)
581+
} catch (e) {
582+
expect(e.status).toBe(402)
583+
}
584+
})
585+
})
586+
587+
describe('mergePR', () => {
588+
test('return correct data if no error', async () => {
589+
const res = await GithubAPI.mergePR(Helper.mockContext())
590+
591+
expect(res).toEqual('merged')
592+
})
593+
594+
test('that error are re-thrown', async () => {
595+
const context = Helper.mockContext()
596+
context.octokit.pulls.merge = jest.fn().mockRejectedValue({ status: 402 })
597+
598+
try {
599+
await GithubAPI.mergePR(context)
600+
// Fail test if above expression doesn't throw anything.
601+
expect(true).toBe(false)
602+
} catch (e) {
603+
expect(e.status).toBe(402)
604+
}
605+
})
606+
})

lib/actions/merge.js

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,14 @@
11
const { Action } = require('./action')
2-
const logger = require('../logger')
32
const UnSupportedSettingError = require('../errors/unSupportedSettingError')
43

54
const MERGE_METHOD_OPTIONS = ['merge', 'rebase', 'squash']
65

7-
const checkIfMerged = async (context, prNumber) => {
8-
let status = true
9-
10-
// return can be 204 or 404 only
11-
try {
12-
await context.octokit.pulls.checkIfMerged(
13-
context.repo({ pull_number: prNumber })
14-
)
15-
} catch (err) {
16-
if (err.status === 404) {
17-
status = false
18-
} else {
19-
throw err
20-
}
21-
}
22-
23-
return status
24-
}
25-
266
const mergePR = async (context, prNumber, mergeMethod, actionObj) => {
27-
const isMerged = await checkIfMerged(context, prNumber)
7+
const isMerged = await actionObj.githubAPI.checkIfMerged(context, prNumber)
288
if (!isMerged) {
299
const pullRequest = await context.octokit.pulls.get(context.repo({ pull_number: prNumber }))
3010
if (pullRequest.data.mergeable_state !== 'blocked' && pullRequest.data.state === 'open') {
31-
try {
32-
await context.octokit.pulls.merge(context.repo({ pull_number: prNumber, merge_method: mergeMethod }))
33-
} catch (err) {
34-
// skip on known errors
35-
// 405 === Method not allowed , 409 === Conflict
36-
if (err.status === 405 || err.status === 409) {
37-
// if the error is another required status check, just skip
38-
// no easy way to check if all required status are done
39-
if (err.message.toLowerCase().includes('required status check')) return
40-
41-
const errorLog = {
42-
logType: logger.logTypes.MERGE_FAIL_ERROR,
43-
eventId: context.eventId,
44-
repo: context.payload.repository.full_name,
45-
actionName: actionObj.name
46-
}
47-
48-
actionObj.info(JSON.stringify(errorLog))
49-
} else {
50-
throw err
51-
}
52-
}
11+
await actionObj.githubAPI.mergePR(context, context.repo({ pull_number: prNumber, merge_method: mergeMethod }))
5312
}
5413
}
5514
}

lib/github/api.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,48 @@ class GithubAPI {
370370
return checkCommonError(err, context, callFn)
371371
}
372372
}
373+
374+
static async mergePR (context, callParams) {
375+
const callFn = 'pulls.merge'
376+
debugLog(context, callFn)
377+
378+
try {
379+
return await context.octokit.pulls.merge(callParams)
380+
} catch (err) {
381+
// skip on known errors
382+
// 405 === Method not allowed , 409 === Conflict
383+
if (err.status === 405 || err.status === 409) {
384+
// if the error is another required status check, just skip
385+
// no easy way to check if all required status are done
386+
if (err.message.toLowerCase().includes('required status check')) return
387+
388+
const errorLog = createLog(context, { callFn: callFn, errors: err.toString(), logType: logger.logTypes.MERGE_FAIL_ERROR })
389+
390+
const log = logger.create(`GithubAPI/${callFn}`)
391+
log.info(JSON.stringify(errorLog))
392+
}
393+
394+
return checkCommonError(err, context, callFn)
395+
}
396+
}
397+
398+
static async checkIfMerged (context, pullNumber) {
399+
const callFn = 'pulls.checkIfMerged'
400+
401+
debugLog(context, callFn)
402+
403+
try {
404+
await context.octokit.pulls.checkIfMerged(context.repo({ pull_number: pullNumber }))
405+
// if the above doesn't throw error just return true
406+
return true
407+
} catch (err) {
408+
if (err.status === 404) {
409+
return false
410+
} else {
411+
return checkCommonError(err, context, callFn)
412+
}
413+
}
414+
}
373415
}
374416

375417
module.exports = GithubAPI

0 commit comments

Comments
 (0)