Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for merge_queue events #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading