Skip to content
Merged
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
53 changes: 39 additions & 14 deletions packages/junior-github/src/webhooks/check-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,37 @@ function isCheckSuiteRepositoryPullRequest(
return pullRequest.base.repo.id === repositoryId;
}

/**
* GitHub's merge queue runs a PR's checks on
* `gh-readonly-queue/<base>/pr-<N>-<sha>` and attaches no PR to that suite.
* A failed queue run blocks PR N, so it goes to PR N like a failed PR suite.
* A passing one needs nothing, so it stays a repository event.
*/
const MERGE_QUEUE_BRANCH = /^gh-readonly-queue\/.+\/pr-(\d+)-[0-9a-f]{40}$/;

/** Same-repository PRs of a check suite, or the PR of a failed queue run. */
function checkSuiteTargetPullRequests(
data: z.infer<typeof checkSuiteWebhookSchema>,
): Array<{ headRef?: string | undefined; number: number }> {
const suite = data.check_suite;
const attached = suite.pull_requests
.filter((pullRequest) =>
isCheckSuiteRepositoryPullRequest(pullRequest, data.repository.id),
)
.map((pullRequest) => ({
headRef: pullRequest.head?.ref,
number: pullRequest.number,
}));
if (
attached.length > 0 ||
checkSuiteEventType(suite.conclusion) !== "pull_request.checks.failed"
) {
return attached;
}
const queued = MERGE_QUEUE_BRANCH.exec(suite.head_branch ?? "")?.[1];
return queued ? [{ number: Number(queued) }] : [];
}

type CheckSuiteEventType =
| "pull_request.checks.failed"
| "pull_request.checks.passed"
Expand Down Expand Up @@ -527,11 +558,9 @@ export function normalizeCheckSuiteEvents(
repo,
suiteConclusion: conclusion,
} as const;
const sameRepoPullRequests = suite.pull_requests.filter((pullRequest) =>
isCheckSuiteRepositoryPullRequest(pullRequest, repository.id),
);
const targetPullRequests = checkSuiteTargetPullRequests(parsed.data);
// No same-repo PR (common on main): one repository event with headBranch.
if (sameRepoPullRequests.length === 0) {
if (targetPullRequests.length === 0) {
return eventTypes.map((type) =>
buildCheckSuiteEvent({
...shared,
Expand All @@ -541,10 +570,10 @@ export function normalizeCheckSuiteEvents(
);
}
return eventTypes.flatMap((type) =>
sameRepoPullRequests.flatMap((pullRequest) => {
targetPullRequests.flatMap((pullRequest) => {
const facts = options?.pullRequestFactsByNumber?.[pullRequest.number];
const pullRequestHeadBranch =
checkSuiteHeadBranch(pullRequest.head?.ref) ?? headBranch;
checkSuiteHeadBranch(pullRequest.headRef) ?? headBranch;
return pullRequestTargets(
buildCheckSuiteEvent({
...shared,
Expand Down Expand Up @@ -603,9 +632,7 @@ export function parseCheckSuitePublishTargets(body: unknown):
const identifiers = new Set<string>([
gitHubRepositoryResource({ repo }).identifier,
]);
for (const pullRequest of parsed.data.check_suite.pull_requests) {
if (!isCheckSuiteRepositoryPullRequest(pullRequest, repository.id))
continue;
for (const pullRequest of checkSuiteTargetPullRequests(parsed.data)) {
identifiers.add(
gitHubPullRequestResource({ number: pullRequest.number, repo })
.identifier,
Expand Down Expand Up @@ -680,11 +707,9 @@ export function parseCheckSuiteFactsTarget(
const repository = parsed.data.repository;
const pullRequestNumbers = [
...new Set(
parsed.data.check_suite.pull_requests
.filter((pullRequest) =>
isCheckSuiteRepositoryPullRequest(pullRequest, repository.id),
)
.map((pullRequest) => pullRequest.number),
checkSuiteTargetPullRequests(parsed.data).map(
(pullRequest) => pullRequest.number,
),
),
];
const loadFailingChecks =
Expand Down
38 changes: 38 additions & 0 deletions packages/junior-github/tests/webhook-outcomes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,44 @@ describe("GitHub webhook events", () => {
]);
});

it("sends a failed merge queue suite to the queued pull request", () => {
const queueSuite = (conclusion: string) => ({
action: "completed",
repository: checkSuiteRepository("getsentry/junior"),
check_suite: {
app: { name: "GitHub Actions" },
conclusion,
head_branch:
"gh-readonly-queue/main/pr-4703-9cc58e1d1f2a3b4c5d6e7f8091a2b3c4d5e6f708",
head_sha: "abcdef1234567890",
id: 77,
pull_requests: [],
},
});
const failed = queueSuite("failure");
expect(parseCheckSuitePublishTargets(failed)?.identifiers).toContain(
"getsentry/junior#4703",
);
expect(
normalizeGitHubEvents({
body: failed,
deliveryId: "delivery-queue",
eventName: "check_suite",
}).map((event) => [event.identifier, event.eventType]),
).toEqual([
["getsentry/junior#4703", "pull_request.checks.failed"],
["getsentry/junior", "pull_request.checks.failed"],
]);
// A passing queue run needs no fix, so it stays a repository event.
expect(
normalizeGitHubEvents({
body: queueSuite("success"),
deliveryId: "delivery-queue-green",
eventName: "check_suite",
}).map((event) => event.identifier),
).toEqual(["getsentry/junior"]);
});

it("attaches headBranch from the check suite webhook without a pull request API load", () => {
expect(needsCheckSuitePullRequestFacts(["headBranch"])).toBe(false);
expect(
Expand Down
Loading