diff --git a/packages/mergebot/README.md b/packages/mergebot/README.md index 48805151b4..3a6a1ef8a1 100644 --- a/packages/mergebot/README.md +++ b/packages/mergebot/README.md @@ -99,7 +99,7 @@ To create fixtures of a current PR: ```sh # To create a fixture for PR 43161 -pnpm run create-fixture -- 43161 +pnpm run create-fixture 43161 ``` Then you can work against these fixtures offline with: diff --git a/packages/mergebot/src/functions/pr-trigger.ts b/packages/mergebot/src/functions/pr-trigger.ts index a5dae1372f..44a52cde72 100644 --- a/packages/mergebot/src/functions/pr-trigger.ts +++ b/packages/mergebot/src/functions/pr-trigger.ts @@ -12,10 +12,11 @@ import { httpLog, shouldRunRequest } from "../util/verify"; import type { CheckSuiteEvent, IssueCommentEvent, - ProjectCardEvent, + ProjectsV2ItemEvent, PullRequestEvent, PullRequestReviewEvent, } from "@octokit/webhooks-types"; +import { runQueryToGetPRForCardId } from "../queries/card-id-to-pr-query"; app.http("PR-Trigger", { methods: ["GET", "POST"], authLevel: "anonymous", handler: httpTrigger }); const eventNames = [ @@ -23,7 +24,7 @@ const eventNames = [ "issue_comment.created", "issue_comment.deleted", "issue_comment.edited", - "project_card.moved", + "projects_v2_item.edited", "pull_request.closed", "pull_request.edited", "pull_request.opened", @@ -36,7 +37,7 @@ const eventNames = [ type PrEvent = | { name: "check_suite"; payload: CheckSuiteEvent } | { name: "issue_comment"; payload: IssueCommentEvent } - | { name: "project_card"; payload: ProjectCardEvent } + | { name: "projects_v2_item"; payload: ProjectsV2ItemEvent } | { name: "pull_request"; payload: PullRequestEvent } | { name: "pull_request_review"; payload: PullRequestReviewEvent }; @@ -120,15 +121,11 @@ const prFromEvent = async (event: PrEvent) => { return prFromCheckSuiteEvent(event.payload); case "issue_comment": return event.payload.issue; - // "Parse" project_card.content_url according to repository.pulls_url - case "project_card": { - const url = event.payload.project_card.content_url; - return url - ? { number: +url.replace(/^.*\//, "") } - : new IgnoredBecause( - `Couldn't find PR number since content_url is missing: ${JSON.stringify(event.payload.project_card)}`, - ); - } + case "projects_v2_item": + const pr = await runQueryToGetPRForCardId(event.payload.projects_v2_item.node_id); + return pr + ? { number: pr.number } + : new IgnoredBecause(`Could not find PR for card_id: ${event.payload.projects_v2_item.node_id}`); case "pull_request": return event.payload.pull_request; case "pull_request_review": diff --git a/packages/mergebot/src/run.ts b/packages/mergebot/src/run.ts index 414c64485d..9b7f61fabe 100755 --- a/packages/mergebot/src/run.ts +++ b/packages/mergebot/src/run.ts @@ -126,12 +126,8 @@ const start = async function () { // console.log("Cleaning up cards"); const { columns, id: projectId } = await getProjectBoardCards(); - const deleteObject = async (id: string, shoulda?: string) => { - if (shoulda) { - // don't automatically delete these, eg, PRs that were created - // during the scan would end up here. - return console.log(` Should delete "${id}" (${shoulda})`); - } + const deleteObject = async (id: string) => { + console.log(` Deleting card ${id}`); const mutation = createMutation("deleteProjectV2Item", { projectId, itemId: id }); await client.mutate(mutation); }; @@ -150,15 +146,18 @@ const start = async function () { // Handle other columns for (const [name, cards] of columns) { if (name === "Recently Merged") continue; - const ids = cards.map((c) => c.id).filter((c) => !cardIDs.includes(c)); + const ids = cards.map((c) => c.id).filter((id) => !cardIDs.includes(id)); if (ids.length === 0) continue; console.log(`Cleaning up closed PRs in "${name}"`); for (const id of ids) { const info = await runQueryToGetPRForCardId(id); - await deleteObject( - id, - info === undefined ? "???" : info.state === "CLOSED" || info.state === "MERGED" ? undefined : "#" + info.number, - ); + if (!info) { + // don't automatically delete these, eg, PRs that were created + // during the scan would end up here. + console.log(` Should delete "${id}" (PR #???)`); + } else if (info.state !== "OPEN") { + await deleteObject(id); + } } } if (failures.length) {