From 84ca23b7c2a29968b79f547f9c0090b573032259 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Sat, 7 Dec 2024 21:37:47 +0100 Subject: [PATCH] fix: merge-queue support (#85) --- index.js | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index dbe57f2..d4b09ed 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,7 @@ async function action() { let providedLabels = core.getInput("labels", { required: true }); + core.debug(`gather labels: ${providedLabels}`); if (labelsAreRegex) { // If labels are regex they must be provided as new line delimited providedLabels = providedLabels.split("\n"); @@ -43,6 +44,16 @@ async function action() { // Remove any empty labels providedLabels = providedLabels.filter((r) => r); + let issue_number = github.context.issue.number; + + if (github.context.eventName === "merge_group") { + // Parse out of the ref for merge queue + // e.g. refs/heads/gh-readonly-queue/main/pr-17-a3c310584587d4b97c2df0cb46fe050cc46a15d6 + const lastPart = github.context.ref.split("/").pop(); + issue_number = lastPart.match(/pr-(\d+)-/)[1]; + core.info(`merge_group event detected and issue_number parsed as ${issue_number}`); + } + const allowedModes = ["exactly", "minimum", "maximum"]; if (!allowedModes.includes(mode)) { await exitWithError( @@ -52,6 +63,7 @@ async function action() { `Unknown mode input [${mode}]. Must be one of: ${allowedModes.join( ", ", )}`, + issue_number, ); return; } @@ -64,21 +76,13 @@ async function action() { shouldAddComment, `Unknown exit_code input [${exitType}]. Must be one of: ${allowedExitCodes.join( ", " - )}` + )}`, + issue_number, ); return; } - let issue_number = github.context.issue.number; - - if (!issue_number && github.context.eventName == "merge_queue") { - // Parse out of the ref for merge queue - // e.g. refs/heads/gh-readonly-queue/main/pr-17-a3c310584587d4b97c2df0cb46fe050cc46a15d6 - const lastPart = github.context.ref.split("/").pop(); - issue_number = lastPart.match(/pr-(\d+)-/)[1]; - } - - // Fetch the labels using the API + core.debug(`fetch the labels for ${issue_number} using the API`); // We use the API rather than read event.json in case earlier steps // added a label const labels = ( @@ -107,7 +111,7 @@ async function action() { ); } - // Is there an error? + core.debug(`detect errors...`); let errorMode; if (mode === "exactly" && intersection.length !== count) { errorMode = "exactly"; @@ -117,7 +121,7 @@ async function action() { errorMode = "at most"; } - // If so, add a comment (if enabled) and fail the run + core.debug(`if so, add a comment (if enabled) and fail the run...`); if (errorMode !== undefined) { const comment = core.getInput("message"); const errorMessage = tmpl(comment, { @@ -128,15 +132,15 @@ async function action() { applied: appliedLabels.join(", "), }); - await exitWithError(exitType, octokit, shouldAddComment, errorMessage); + await exitWithError(exitType, octokit, shouldAddComment, errorMessage, issue_number); return; } - // Remove the comment if it exists + core.debug(`remove the comment if it exists...`); if (shouldAddComment) { const { data: existing } = await octokit.rest.issues.listComments({ ...github.context.repo, - issue_number: github.context.issue.number, + issue_number: issue_number, }); const generatedComment = existing.find((c) => @@ -163,19 +167,19 @@ function tmpl(t, o) { }); } -async function exitWithError(exitType, octokit, shouldAddComment, message) { +async function exitWithError(exitType, octokit, shouldAddComment, message, issue_number) { if (shouldAddComment) { // Is there an existing comment? const { data: existing } = await octokit.rest.issues.listComments({ ...github.context.repo, - issue_number: github.context.issue.number, + issue_number: issue_number, }); const generatedComment = existing.find((c) => c.body.includes(matchToken)); const params = { ...github.context.repo, - issue_number: github.context.issue.number, + issue_number: issue_number, body: `${matchToken}${message}`, };