Skip to content

Commit

Permalink
fix: merge-queue support (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v authored Dec 7, 2024
1 parent 31fe490 commit 84ca23b
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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(
Expand All @@ -52,6 +63,7 @@ async function action() {
`Unknown mode input [${mode}]. Must be one of: ${allowedModes.join(
", ",
)}`,
issue_number,
);
return;
}
Expand All @@ -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 = (
Expand Down Expand Up @@ -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";
Expand All @@ -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, {
Expand All @@ -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) =>
Expand All @@ -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}`,
};

Expand Down

0 comments on commit 84ca23b

Please sign in to comment.