Skip to content

Commit

Permalink
Add support for merge_queue events
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap committed Sep 17, 2024
1 parent 4292bf2 commit 31fe490
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,28 @@ async function action() {
octokit,
shouldAddComment,
`Unknown exit_code input [${exitType}]. Must be one of: ${allowedExitCodes.join(
", ",
)}`,
", "
)}`
);
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
// We use the API rather than read event.json in case earlier steps
// added a label
const labels = (
await octokit.rest.issues.listLabelsOnIssue({
...github.context.repo,
issue_number: github.context.issue.number,
issue_number,
})
).data;

Expand Down
61 changes: 60 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,60 @@ describe("Required Labels", () => {

await action();
});

});

describe("merge_queue", () => {
it("extracts the PR number from the ref if needed", async () => {
restoreTest = mockEvent(
"merge_queue",
{},
{
INPUT_LABELS: "enhancement",
INPUT_MODE: "exactly",
INPUT_COUNT: "1",
GITHUB_TOKEN: "mock-token-here-abc",
},
{
ref: "refs/heads/gh-readonly-queue/main/pr-28-a3c310584587d4b97c2df0cb46fe050cc46a15d6"
},
);

mockLabels(["enhancement", "bug"]);

await action();
expect(core.setOutput).toBeCalledTimes(2);
expect(core.setOutput).toBeCalledWith("status", "success");
expect(core.setOutput).toBeCalledWith("labels", "enhancement");
});

it("prefers the issue number that is set in the payload to extracting from a ref", async () => {
restoreTest = mockEvent(
"merge_queue",
{
issue: {
number: 28,
},
},
{
INPUT_LABELS: "enhancement",
INPUT_MODE: "exactly",
INPUT_COUNT: "1",
GITHUB_TOKEN: "mock-token-here-abc",
},
{
// This would lead to an error as there are no mocks for PR 999
ref: "refs/heads/gh-readonly-queue/main/pr-999-a3c310584587d4b97c2df0cb46fe050cc46a15d6"
},
);

mockLabels(["enhancement", "bug"]);

await action();
expect(core.setOutput).toBeCalledTimes(2);
expect(core.setOutput).toBeCalledWith("status", "success");
expect(core.setOutput).toBeCalledWith("labels", "enhancement");
});
});
});

Expand Down Expand Up @@ -683,8 +737,13 @@ function mockListComments(comments) {
);
}

function mockEvent(eventName, mockPayload, additionalParams = {}) {
function mockEvent(eventName, mockPayload, additionalParams, additionalContext = {}) {
github.context.payload = mockPayload;
github.context.eventName = eventName;

for (const key in additionalContext) {
github.context[key] = additionalContext[key];
}

const params = {
GITHUB_EVENT_NAME: eventName,
Expand Down

0 comments on commit 31fe490

Please sign in to comment.