|
| 1 | +import { Probot } from "probot"; |
| 2 | +import { fetchCrcrAllowlist } from "../crcrAllowlist"; |
| 3 | +import { isPyTorchbotSupportedOrg } from "./utils"; |
| 4 | + |
| 5 | +const FAILURE_CONCLUSIONS: ReadonlySet<string> = new Set([ |
| 6 | + "failure", |
| 7 | + "cancelled", |
| 8 | + "timed_out", |
| 9 | + "action_required", |
| 10 | +]); |
| 11 | + |
| 12 | +const MARKER = "<!-- crcr-oncall -->"; |
| 13 | + |
| 14 | +/** |
| 15 | + * Parse the downstream ``owner/repo`` out of a CRCR check run name. |
| 16 | + * |
| 17 | + * Check runs are named ``crcr/<owner>/<repo>/<workflow_name>`` (see the |
| 18 | + * Python gh_helper.check_run_name), so the downstream repo is the first two |
| 19 | + * path segments after the ``crcr/`` prefix. |
| 20 | + */ |
| 21 | +function downstreamRepoFromCheckRunName(name: string): string | null { |
| 22 | + const prefix = "crcr/"; |
| 23 | + if (!name.startsWith(prefix)) { |
| 24 | + return null; |
| 25 | + } |
| 26 | + const parts = name.slice(prefix.length).split("/"); |
| 27 | + if (parts.length < 3 || !parts[0] || !parts[1]) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + return `${parts[0]}/${parts[1]}`; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Search existing PR comments for one with the CRCR on-call marker. |
| 35 | + * Returns its ID (or 0 if none found). |
| 36 | + */ |
| 37 | +async function findExistingComment( |
| 38 | + octokit: any, |
| 39 | + owner: string, |
| 40 | + repo: string, |
| 41 | + prNumber: number |
| 42 | +): Promise<number> { |
| 43 | + const commentsRes = await octokit.rest.issues.listComments({ |
| 44 | + owner, |
| 45 | + repo, |
| 46 | + issue_number: prNumber, |
| 47 | + }); |
| 48 | + for (const comment of commentsRes.data) { |
| 49 | + if (comment.body?.includes(MARKER)) { |
| 50 | + return comment.id; |
| 51 | + } |
| 52 | + } |
| 53 | + return 0; |
| 54 | +} |
| 55 | + |
| 56 | +export default function crcrOncallBot(app: Probot): void { |
| 57 | + app.on("check_run.completed", async (ctx) => { |
| 58 | + const owner = ctx.payload.repository.owner.login; |
| 59 | + if (!isPyTorchbotSupportedOrg(owner)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const repo = ctx.payload.repository.name; |
| 64 | + const checkRun = ctx.payload.check_run; |
| 65 | + |
| 66 | + // Only act on CRCR-created check runs |
| 67 | + const downstreamRepo = downstreamRepoFromCheckRunName(checkRun.name); |
| 68 | + if (!downstreamRepo) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // Only comment on failures |
| 73 | + const conclusion = checkRun.conclusion ?? ""; |
| 74 | + if (!FAILURE_CONCLUSIONS.has(conclusion)) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Get the PR this check run belongs to |
| 79 | + const prs = checkRun.pull_requests ?? []; |
| 80 | + if (prs.length === 0) { |
| 81 | + ctx.log( |
| 82 | + `crcrOncall: no PR associated with check run ${checkRun.name}, skipping` |
| 83 | + ); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const headSha = checkRun.head_sha; |
| 88 | + const checkRunUrl = checkRun.html_url ?? ""; |
| 89 | + |
| 90 | + // Load oncalls from the allowlist |
| 91 | + let oncalls: string[]; |
| 92 | + try { |
| 93 | + const allowlist = await fetchCrcrAllowlist(ctx.octokit); |
| 94 | + oncalls = allowlist.getOncallsForRepo(downstreamRepo); |
| 95 | + } catch (err) { |
| 96 | + ctx.log({ err }, "crcrOncall: failed to load allowlist, skipping"); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + if (oncalls.length === 0) { |
| 101 | + ctx.log( |
| 102 | + `crcrOncall: no oncalls configured for ${downstreamRepo}, skipping` |
| 103 | + ); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + const mentions = oncalls.map((o) => `@${o}`).join(" "); |
| 108 | + |
| 109 | + // Post a comment on each associated PR (typically just one) |
| 110 | + for (const pr of prs) { |
| 111 | + try { |
| 112 | + const prNumber = pr.number; |
| 113 | + |
| 114 | + // Dedup by marker: only comment once per PR. |
| 115 | + const existingId = await findExistingComment( |
| 116 | + ctx.octokit, |
| 117 | + owner, |
| 118 | + repo, |
| 119 | + prNumber |
| 120 | + ); |
| 121 | + if (existingId !== 0) { |
| 122 | + ctx.log( |
| 123 | + `crcrOncall: comment already exists on PR #${prNumber}, skipping` |
| 124 | + ); |
| 125 | + continue; |
| 126 | + } |
| 127 | + |
| 128 | + const commentBody = `${MARKER} |
| 129 | +## :x: CRCR downstream CI failure |
| 130 | +
|
| 131 | +The downstream CI workflow in **${downstreamRepo}** has failed on commit \`${headSha.slice(0, 7)}\`. |
| 132 | +
|
| 133 | +${mentions} please investigate. |
| 134 | +
|
| 135 | +### Details |
| 136 | +
|
| 137 | +| Field | Value | |
| 138 | +|---|---| |
| 139 | +| **Check Run** | [${checkRun.name}](${checkRunUrl}) | |
| 140 | +| **Commit** | \`${headSha}\` | |
| 141 | +| **Conclusion** | \`${conclusion}\` | |
| 142 | +`; |
| 143 | + |
| 144 | + await ctx.octokit.issues.createComment({ |
| 145 | + owner, |
| 146 | + repo, |
| 147 | + issue_number: prNumber, |
| 148 | + body: commentBody, |
| 149 | + }); |
| 150 | + |
| 151 | + ctx.log( |
| 152 | + `crcrOncall: commented on PR #${prNumber} for ${downstreamRepo} (${conclusion})` |
| 153 | + ); |
| 154 | + } catch (err) { |
| 155 | + ctx.log({ err }, `crcrOncall: failed to comment on PR for ${downstreamRepo}`); |
| 156 | + } |
| 157 | + } |
| 158 | + }); |
| 159 | +} |
0 commit comments