Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Do not attempt to merge a pull request if the status is blocked #400

Merged
merged 3 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions __tests__/unit/actions/merge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,41 @@ test('check that merge is called if PR has not been merged', async () => {
const merge = new Merge()
const checkIfMerged = false
const context = Helper.mockContext({ checkIfMerged })
context.github.pulls.get.mockReturnValue({ data: { mergeable_state: 'clean', state: 'open' } })
const settings = {}

await merge.afterValidate(context, settings)
expect(context.github.pulls.merge.mock.calls.length).toBe(1)
expect(context.github.pulls.merge.mock.calls[0][0].merge_method).toBe('merge')
})

test('check that merge is not called if PR is in a blocked state', async () => {
const merge = new Merge()
const checkIfMerged = false
const context = Helper.mockContext({ checkIfMerged })
context.github.pulls.get.mockReturnValue({ data: { mergeable_state: 'blocked', state: 'open' } })
const settings = {}

await merge.afterValidate(context, settings)
expect(context.github.pulls.merge.mock.calls.length).toBe(0)
})

test('check that merge is not called if PR is closed', async () => {
const merge = new Merge()
const checkIfMerged = false
const context = Helper.mockContext({ checkIfMerged })
context.github.pulls.get.mockReturnValue({ data: { mergeable_state: 'clean', state: 'closed' } })
const settings = {}

await merge.afterValidate(context, settings)
expect(context.github.pulls.merge.mock.calls.length).toBe(0)
})

test('check that merge is not called if PR has not been merged', async () => {
const merge = new Merge()
const checkIfMerged = true
const context = Helper.mockContext({ checkIfMerged })
context.github.pulls.get.mockReturnValue({ data: { mergeable_state: 'clean', state: 'open' } })
const settings = {}

await merge.afterValidate(context, settings)
Expand All @@ -26,6 +50,7 @@ test('check that merge_method option works correctly', async () => {
const merge = new Merge()
const checkIfMerged = false
const context = Helper.mockContext({ checkIfMerged })
context.github.pulls.get.mockReturnValue({ data: { mergeable_state: 'clean', state: 'open' } })
const settings = {
merge_method: 'squash'
}
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CHANGELOG
=====================================

| October 6, 2020 : Do not attempt to merge a pull request if the status is blocked `#389 <https://github.com/mergeability/mergeable/issues/389>`_
| October 6, 2020 : fix: Fix undefined error with blank validators `#402 <https://github.com/mergeability/mergeable/issues/402>`_
| October 5, 2020 : fix Typo in header of labels action docs and corresponding rst file
| October 4, 2020 : fix Typo in header of title validator docs
Expand Down
57 changes: 33 additions & 24 deletions lib/actions/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,11 @@ const checkIfMerged = async (context, prNumber) => {
return status
}

class Merge extends Action {
constructor () {
super('merge')
this.supportedEvents = [
'pull_request.*'
]

this.supportedSettings = {
merge_method: 'string'
}
}

// there is nothing to do
async beforeValidate () {}

async afterValidate (context, settings, results) {
const prNumber = this.getPayload(context).number
const isMerged = await checkIfMerged(context, prNumber)

if (!isMerged) {
if (settings.merge_method && !MERGE_METHOD_OPTIONS.includes(settings.merge_method)) {
throw new UnSupportedSettingError(`Unknown Merge method, supported options are ${MERGE_METHOD_OPTIONS.join(', ')}`)
}
let mergeMethod = settings.merge_method ? settings.merge_method : 'merge'
const mergePR = async (context, prNumber, mergeMethod) => {
const isMerged = await checkIfMerged(context, prNumber)
if (!isMerged) {
const pullRequest = await context.github.pulls.get(context.repo({ pull_number: prNumber }))
if (pullRequest.data.mergeable_state !== 'blocked' && pullRequest.data.state === 'open') {
try {
await context.github.pulls.merge(context.repo({ pull_number: prNumber, merge_method: mergeMethod }))
} catch (err) {
Expand All @@ -65,4 +46,32 @@ class Merge extends Action {
}
}

class Merge extends Action {
constructor () {
super('merge')
this.supportedEvents = [
'pull_request.*'
]

this.supportedSettings = {
merge_method: 'string'
}
}

// there is nothing to do
async beforeValidate () {}

async afterValidate (context, settings, name, results) {
if (settings.merge_method && !MERGE_METHOD_OPTIONS.includes(settings.merge_method)) {
throw new UnSupportedSettingError(`Unknown Merge method, supported options are ${MERGE_METHOD_OPTIONS.join(', ')}`)
}
let mergeMethod = settings.merge_method ? settings.merge_method : 'merge'
return mergePR(
context,
this.getPayload(context).number,
mergeMethod
)
}
}

module.exports = Merge