Skip to content

Commit

Permalink
New option MERGE_ERROR_FAIL
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalgn committed Mar 24, 2022
1 parent b31ac51 commit 8d853da
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 34 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ The following merge options are supported:
be deleted. This is not the list of GitHub's protected branches, which are never
deleted, but an additional list of branches to protect. The default value is `""`.

- `MERGE_ERROR_FAIL`: Set this to `true` to have the action exit with error code `1`
when the pull request could not be merged successfully during a run.

The following update options are supported:

- `UPDATE_LABELS`: The labels that need to be present for a pull request to be
Expand Down
40 changes: 24 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,11 @@ const logger = {
info: (...str) => log("INFO ", str),

error: (...str) => {
if (str.length === 1) {
if (str[0] instanceof Error) {
if (logger.level === "trace" || logger.level === "debug") {
log(null, [str[0].stack || str[0]]);
} else {
log("ERROR", [str[0].message || str[0]]);
}
if (str.length === 1 && str[0] instanceof Error) {
if (logger.level === "trace" || logger.level === "debug") {
log(null, [str[0].stack || str[0]]);
} else {
log("ERROR", [str[0].message || str[0]]);
}
} else {
log("ERROR", str);
Expand Down Expand Up @@ -663,6 +661,7 @@ function createConfig(env = {}) {
const mergeDeleteBranchFilter = parseArray(env.MERGE_DELETE_BRANCH_FILTER);
const mergeMethodLabels = parseLabelMethods(env.MERGE_METHOD_LABELS);
const mergeMethodLabelRequired = env.MERGE_METHOD_LABEL_REQUIRED === "true";
const mergeErrorFail = env.MERGE_ERROR_FAIL === "true";

const updateLabels = parseMergeLabels(env.UPDATE_LABELS, "automerge");
const updateMethod = env.UPDATE_METHOD || "merge";
Expand All @@ -688,6 +687,7 @@ function createConfig(env = {}) {
mergeRequiredApprovals,
mergeDeleteBranch,
mergeDeleteBranchFilter,
mergeErrorFail,
updateLabels,
updateMethod,
updateRetries,
Expand Down Expand Up @@ -753,11 +753,6 @@ function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

function fail(message) {
logger.error(message);
process.exit(1);
}

module.exports = {
ClientError,
TimeoutError,
Expand Down Expand Up @@ -1011,7 +1006,8 @@ async function merge(context, pullRequest, approvalCount) {
mergeFilterAuthor,
mergeRemoveLabels,
mergeRetries,
mergeRetrySleep
mergeRetrySleep,
mergeErrorFail
}
} = context;

Expand Down Expand Up @@ -1054,6 +1050,7 @@ async function merge(context, pullRequest, approvalCount) {
mergeMethod,
mergeRetries,
mergeRetrySleep,
mergeErrorFail,
commitMessage
);
if (!merged) {
Expand Down Expand Up @@ -1234,7 +1231,7 @@ function skipPullRequest(context, pullRequest, approvalCount) {
function waitUntilReady(pullRequest, context) {
const {
octokit,
config: { mergeRetries, mergeRetrySleep }
config: { mergeRetries, mergeRetrySleep, mergeErrorFail }
} = context;

return retry(
Expand All @@ -1245,7 +1242,7 @@ function waitUntilReady(pullRequest, context) {
const pr = await getPullRequest(octokit, pullRequest);
return checkReady(pr, context);
},
() => fail(`PR not ready to be merged after ${mergeRetries} tries`)
() => failOrInfo(mergeRetries, mergeErrorFail)
);
}

Expand Down Expand Up @@ -1290,6 +1287,7 @@ function tryMerge(
mergeMethod,
mergeRetries,
mergeRetrySleep,
mergeErrorFail,
commitMessage
) {
return retry(
Expand All @@ -1310,10 +1308,20 @@ function tryMerge(
commitMessage
);
},
() => fail(`PR could not be merged after ${mergeRetries} tries`)
() => failOrInfo(mergeRetries, mergeErrorFail)
);
}

function failOrInfo(mergeRetries, mergeErrorFail) {
const message = `PR not ready to be merged after ${mergeRetries} tries`;
if (mergeErrorFail) {
logger.error(message);
process.exit(1);
} else {
logger.info(message);
}
}

function getMergeMethod(defaultMergeMethod, mergeMethodLabels, pullRequest) {
const foundMergeMethodLabels = pullRequest.labels.flatMap(l =>
mergeMethodLabels.filter(ml => ml.label === l.name)
Expand Down
3 changes: 2 additions & 1 deletion it/it.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ async function main() {
MERGE_REQUIRED_APPROVALS: "0",
MERGE_REMOVE_LABELS: "it-merge",
MERGE_RETRIES: "3",
MERGE_RETRY_SLEEP: "2000"
MERGE_RETRY_SLEEP: "2000",
MERGE_ERROR_FAIL: "true"
});

const context = { token, octokit, config };
Expand Down
19 changes: 7 additions & 12 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ const logger = {
info: (...str) => log("INFO ", str),

error: (...str) => {
if (str.length === 1) {
if (str[0] instanceof Error) {
if (logger.level === "trace" || logger.level === "debug") {
log(null, [str[0].stack || str[0]]);
} else {
log("ERROR", [str[0].message || str[0]]);
}
if (str.length === 1 && str[0] instanceof Error) {
if (logger.level === "trace" || logger.level === "debug") {
log(null, [str[0].stack || str[0]]);
} else {
log("ERROR", [str[0].message || str[0]]);
}
} else {
log("ERROR", str);
Expand Down Expand Up @@ -173,6 +171,7 @@ function createConfig(env = {}) {
const mergeDeleteBranchFilter = parseArray(env.MERGE_DELETE_BRANCH_FILTER);
const mergeMethodLabels = parseLabelMethods(env.MERGE_METHOD_LABELS);
const mergeMethodLabelRequired = env.MERGE_METHOD_LABEL_REQUIRED === "true";
const mergeErrorFail = env.MERGE_ERROR_FAIL === "true";

const updateLabels = parseMergeLabels(env.UPDATE_LABELS, "automerge");
const updateMethod = env.UPDATE_METHOD || "merge";
Expand All @@ -198,6 +197,7 @@ function createConfig(env = {}) {
mergeRequiredApprovals,
mergeDeleteBranch,
mergeDeleteBranchFilter,
mergeErrorFail,
updateLabels,
updateMethod,
updateRetries,
Expand Down Expand Up @@ -263,11 +263,6 @@ function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

function fail(message) {
logger.error(message);
process.exit(1);
}

module.exports = {
ClientError,
TimeoutError,
Expand Down
21 changes: 17 additions & 4 deletions lib/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ async function merge(context, pullRequest, approvalCount) {
mergeFilterAuthor,
mergeRemoveLabels,
mergeRetries,
mergeRetrySleep
mergeRetrySleep,
mergeErrorFail
}
} = context;

Expand Down Expand Up @@ -78,6 +79,7 @@ async function merge(context, pullRequest, approvalCount) {
mergeMethod,
mergeRetries,
mergeRetrySleep,
mergeErrorFail,
commitMessage
);
if (!merged) {
Expand Down Expand Up @@ -258,7 +260,7 @@ function skipPullRequest(context, pullRequest, approvalCount) {
function waitUntilReady(pullRequest, context) {
const {
octokit,
config: { mergeRetries, mergeRetrySleep }
config: { mergeRetries, mergeRetrySleep, mergeErrorFail }
} = context;

return retry(
Expand All @@ -269,7 +271,7 @@ function waitUntilReady(pullRequest, context) {
const pr = await getPullRequest(octokit, pullRequest);
return checkReady(pr, context);
},
() => fail(`PR not ready to be merged after ${mergeRetries} tries`)
() => failOrInfo(mergeRetries, mergeErrorFail)
);
}

Expand Down Expand Up @@ -314,6 +316,7 @@ function tryMerge(
mergeMethod,
mergeRetries,
mergeRetrySleep,
mergeErrorFail,
commitMessage
) {
return retry(
Expand All @@ -334,10 +337,20 @@ function tryMerge(
commitMessage
);
},
() => fail(`PR could not be merged after ${mergeRetries} tries`)
() => failOrInfo(mergeRetries, mergeErrorFail)
);
}

function failOrInfo(mergeRetries, mergeErrorFail) {
const message = `PR not ready to be merged after ${mergeRetries} tries`;
if (mergeErrorFail) {
logger.error(message);
process.exit(1);
} else {
logger.info(message);
}
}

function getMergeMethod(defaultMergeMethod, mergeMethodLabels, pullRequest) {
const foundMergeMethodLabels = pullRequest.labels.flatMap(l =>
mergeMethodLabels.filter(ml => ml.label === l.name)
Expand Down
4 changes: 4 additions & 0 deletions test/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test("createConfig", () => {
mergeCommitMessageRegex: "",
mergeDeleteBranch: false,
mergeDeleteBranchFilter: [],
mergeErrorFail: false,
mergeRetries: 3,
mergeRetrySleep: 5000,
mergeRequiredApprovals: 0,
Expand Down Expand Up @@ -59,6 +60,7 @@ test("createConfig with arbitrary pull request (as string)", () => {
mergeCommitMessageRegex: "",
mergeDeleteBranch: false,
mergeDeleteBranchFilter: [],
mergeErrorFail: false,
mergeRetries: 3,
mergeRetrySleep: 5000,
mergeRequiredApprovals: 0,
Expand Down Expand Up @@ -99,6 +101,7 @@ test("createConfig with arbitrary pull request (as number)", () => {
mergeCommitMessageRegex: "",
mergeDeleteBranch: false,
mergeDeleteBranchFilter: [],
mergeErrorFail: false,
mergeRetries: 3,
mergeRetrySleep: 5000,
mergeRequiredApprovals: 0,
Expand Down Expand Up @@ -139,6 +142,7 @@ test("createConfig with arbitrary pull request in another repo", () => {
mergeCommitMessageRegex: "",
mergeDeleteBranch: false,
mergeDeleteBranchFilter: [],
mergeErrorFail: false,
mergeRetries: 3,
mergeRetrySleep: 5000,
mergeRequiredApprovals: 0,
Expand Down
6 changes: 5 additions & 1 deletion test/merge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ test("Unmergeable pull request fails action with non-zero exit code", async () =
// GIVEN
const pr = pullRequest();
pr.mergeable_state = "blocked";
const config = createConfig();
const config = createConfig({ MERGE_ERROR_FAIL: "true" });
octokit.pulls.get = async () => ({ data: pr });

// Reduce retry wait period to 1ms to prevent test timeout
Expand All @@ -287,10 +287,14 @@ test("Unmergeable pull request fails action with non-zero exit code", async () =
`process.exit was called with status code: ${statusCode}`
);
});

try {
await merge({ config, octokit }, pr, 0);
} catch (e) {
expect(e).toEqual(new Error("process.exit was called with status code: 1"));
expect(mockExit).toHaveBeenCalledWith(1);
return;
}

throw new Error("process.exit was not called!");
});

0 comments on commit 8d853da

Please sign in to comment.