Skip to content

Commit

Permalink
support all merge methods (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
robandpdx authored Jan 9, 2024
1 parent 7f1f3e7 commit 370507b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
10 changes: 9 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,19 @@ module.exports = (app) => {
// Merge PR, if configured to do so
if (process.env.MERGE_PR == 'true') {
console.log(`Merging PR`);
let mergeMethod;
if (context.payload.pull_request.base.repo.allow_merge_commit == true) {
mergeMethod = 'merge';
} else if (context.payload.pull_request.base.repo.allow_squash_merge == true) {
mergeMethod = 'squash';
} else {
mergeMethod = 'rebase';
}
await context.octokit.rest.pulls.merge({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: context.payload.pull_request.number,
//merge_method: "squash"
merge_method: mergeMethod
}).then(response => {
console.log(`PR merged`);
}).catch(error => {
Expand Down
71 changes: 68 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ const payloadPrLabeled = {
number: 1,
url: "https://api.github.com/repos/robandpdx/superbigmono/pulls/1",
html_url: "https://github.com/robandpdx/superbigmono/pull/1",
merged: false
merged: false,
base: {
repo: {
allow_merge_commit: true,
allow_squash_merge: true,
allow_rebase_merge: true
}
}
},
organization: {
login: "robandpdx",
Expand Down Expand Up @@ -60,7 +67,14 @@ const payloadPrLabeledByBot = {
number: 1,
url: "https://api.github.com/repos/robandpdx/superbigmono/pulls/1",
html_url: "https://github.com/robandpdx/superbigmono/pull/1",
merged: false
merged: false,
base: {
repo: {
allow_merge_commit: true,
allow_squash_merge: true,
allow_rebase_merge: true
}
}
},
organization: {
login: "robandpdx",
Expand Down Expand Up @@ -285,6 +299,8 @@ test.after.each(() => {
delete process.env.SLACK_MESSAGE_FILE;
delete process.env.EMERGENCY_LABEL_PERMANENT;
delete process.env.AUTHORIZED_TEAM;
payloadPrLabeled.payload.pull_request.base.repo.allow_merge_commit = true;
payloadPrLabeled.payload.pull_request.base.repo.allow_squash_merge = true;
});

// This test sends a payload that is not an emergency label
Expand Down Expand Up @@ -445,7 +461,56 @@ test("recieves pull_request.labeled event, merge the PR", async function () {

// mock the request to add approval to the pr
const mock = nock("https://api.github.com")
.put("/repos/robandpdx/superbigmono/pulls/1/merge").reply(200);
.put("/repos/robandpdx/superbigmono/pulls/1/merge",
(requestBody) => {
assert.equal(requestBody.merge_method, "merge");
return true;
}
).reply(200);

await probot.receive(payloadPrLabeled);
assert.equal(mock.pendingMocks(), []);
});

// This test will only merge the PR, using squash merge
test("recieves pull_request.labeled event, squash merge the PR", async function () {
process.env.APPROVE_PR = 'false';
process.env.CREATE_ISSUE = 'false';
process.env.MERGE_PR = 'true';
process.env.SLACK_NOTIFY = 'false';
payloadPrLabeled.payload.pull_request.base.repo.allow_merge_commit = false;

// mock the request to add approval to the pr
const mock = nock("https://api.github.com")
.put("/repos/robandpdx/superbigmono/pulls/1/merge",
(requestBody) => {
assert.equal(requestBody.merge_method, "squash");
return true;
}
).reply(200);


await probot.receive(payloadPrLabeled);
assert.equal(mock.pendingMocks(), []);
});

// This test will only merge the PR, using rebase merge
test("recieves pull_request.labeled event, rebase merge the PR", async function () {
process.env.APPROVE_PR = 'false';
process.env.CREATE_ISSUE = 'false';
process.env.MERGE_PR = 'true';
process.env.SLACK_NOTIFY = 'false';
payloadPrLabeled.payload.pull_request.base.repo.allow_merge_commit = false;
payloadPrLabeled.payload.pull_request.base.repo.allow_squash_merge = false;

// mock the request to add approval to the pr
const mock = nock("https://api.github.com")
.put("/repos/robandpdx/superbigmono/pulls/1/merge",
(requestBody) => {
assert.equal(requestBody.merge_method, "rebase");
return true;
}
).reply(200);

await probot.receive(payloadPrLabeled);
assert.equal(mock.pendingMocks(), []);
Expand Down

0 comments on commit 370507b

Please sign in to comment.