Skip to content

Commit

Permalink
Support for more PR properties in MERGE_COMMIT_MESSAGE (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
bilelomrani1 authored Oct 8, 2020
1 parent 2b9a4d4 commit a8d6a65
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/merge.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { logger, retry } = require("./common");
const resolvePath = require('object-resolve-path');

const MAYBE_READY = ["clean", "has_hooks", "unknown", "unstable"];
const NOT_READY = ["dirty", "draft"];
Expand Down Expand Up @@ -298,6 +299,28 @@ function getMergeMethod(defaultMergeMethod, mergeMethodLabels, pullRequest) {
return defaultMergeMethod;
}

// https://stackoverflow.com/a/53620876
function propertiesToArray(obj) {
const isObject = val =>
typeof val === 'object' && !Array.isArray(val);

const addDelimiter = (a, b) =>
a ? `${a}.${b}` : b;

const paths = (obj = {}, head = '') => {
return Object.entries(obj)
.reduce((product, [key, value]) =>
{
let fullPath = addDelimiter(head, key)
return isObject(value) ?
product.concat(paths(value, fullPath))
: product.concat(fullPath)
}, []);
}

return paths(obj);
}

function getCommitMessage(mergeCommitMessage, pullRequest) {
if (mergeCommitMessage === "automatic") {
return undefined;
Expand All @@ -308,10 +331,10 @@ function getCommitMessage(mergeCommitMessage, pullRequest) {
} else if (mergeCommitMessage === "pull-request-title-and-description") {
return pullRequest.title + "\n\n" + pullRequest.body;
} else {
["number", "title", "body"].forEach(prProp => {
propertiesToArray(pullRequest).forEach(prProp => {
mergeCommitMessage = mergeCommitMessage.replace(
new RegExp(`{pullRequest.${prProp}}`, "g"),
pullRequest[prProp]
resolvePath(pullRequest, prProp)
);
});
return mergeCommitMessage;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@octokit/rest": "^18.0.6",
"argparse": "^2.0.1",
"fs-extra": "^9.0.1",
"object-resolve-path": "^1.1.1",
"tmp": "^0.2.1"
},
"devDependencies": {
Expand Down
26 changes: 26 additions & 0 deletions test/merge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ beforeEach(() => {
};
});

test("MERGE_COMMIT_MESSAGE with nested custom fields", async () => {
// GIVEN
const pr = pullRequest();
pr.title = "This is the PR's title"
pr.user = {login: "author"};

const config = createConfig({
MERGE_COMMIT_MESSAGE: "{pullRequest.title} @{pullRequest.user.login}",
});

// WHEN
expect(await merge({ config, octokit }, pr)).toEqual(true);

// THEN
expect(octokit.pulls.merge).toHaveBeenCalledWith(
expect.objectContaining({
commit_title:
"This is the PR's title @author",
commit_message: "",
pull_number: 1,
repo: "repository",
sha: "2c3b4d5"
})
);
});

test("MERGE_COMMIT_MESSAGE_REGEX can be used to cut PR body", async () => {
// GIVEN
const pr = pullRequest();
Expand Down

0 comments on commit a8d6a65

Please sign in to comment.