Skip to content

Commit

Permalink
Improve PR property resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalgn committed Oct 8, 2020
1 parent a8d6a65 commit dd7959e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.
37 changes: 7 additions & 30 deletions lib/merge.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const resolvePath = require("object-resolve-path");

const { logger, retry } = require("./common");
const resolvePath = require('object-resolve-path');

const MAYBE_READY = ["clean", "has_hooks", "unknown", "unstable"];
const NOT_READY = ["dirty", "draft"];

const PR_PROPERTY = new RegExp("{pullRequest.([^}]+)}", "g");

async function merge(context, pullRequest) {
if (skipPullRequest(context, pullRequest)) {
return false;
Expand Down Expand Up @@ -299,28 +302,6 @@ 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 @@ -331,13 +312,9 @@ function getCommitMessage(mergeCommitMessage, pullRequest) {
} else if (mergeCommitMessage === "pull-request-title-and-description") {
return pullRequest.title + "\n\n" + pullRequest.body;
} else {
propertiesToArray(pullRequest).forEach(prProp => {
mergeCommitMessage = mergeCommitMessage.replace(
new RegExp(`{pullRequest.${prProp}}`, "g"),
resolvePath(pullRequest, prProp)
);
});
return mergeCommitMessage;
return mergeCommitMessage.replace(PR_PROPERTY, (_, prProp) =>
resolvePath(pullRequest, prProp)
);
}
}

Expand Down
9 changes: 4 additions & 5 deletions test/merge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ 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"};
pr.title = "This is the PR's title";
pr.user = { login: "author" };

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

// WHEN
Expand All @@ -29,8 +29,7 @@ test("MERGE_COMMIT_MESSAGE with nested custom fields", async () => {
// THEN
expect(octokit.pulls.merge).toHaveBeenCalledWith(
expect.objectContaining({
commit_title:
"This is the PR's title @author",
commit_title: "This is the PR's title @author",
commit_message: "",
pull_number: 1,
repo: "repository",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3055,6 +3055,11 @@ object-copy@^0.1.0:
define-property "^0.2.5"
kind-of "^3.0.3"

object-resolve-path@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/object-resolve-path/-/object-resolve-path-1.1.1.tgz#a7f8f93e8a20af80e44217ba7db54316d9d12232"
integrity sha1-p/j5Poogr4DkQhe6fbVDFtnRIjI=

object-visit@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/object-visit/-/object-visit-1.0.1.tgz#f79c4493af0c5377b59fe39d395e41042dd045bb"
Expand Down

0 comments on commit dd7959e

Please sign in to comment.