Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

support all merge methods #89

Merged
merged 1 commit into from
Jan 9, 2024
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
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
Loading