-
Notifications
You must be signed in to change notification settings - Fork 138
[CRCR] Implement on-call bot and allowlist functionality for downstream CI failures #8183
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
Open
KarhouTam
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
KarhouTam:crcr-mergebot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import { Probot } from "probot"; | ||
| import { fetchCrcrAllowlist } from "../crcrAllowlist"; | ||
| import { isPyTorchbotSupportedOrg } from "./utils"; | ||
|
|
||
| export const FAILURE_CONCLUSIONS: ReadonlySet<string> = new Set([ | ||
| "failure", | ||
| "cancelled", | ||
| "timed_out", | ||
| ]); | ||
|
|
||
| function markerForRepo(downstreamRepo: string): string { | ||
| return `<!-- crcr-oncall:${downstreamRepo} -->`; | ||
| } | ||
|
|
||
| /** | ||
| * Parse the downstream ``owner/repo`` out of a CRCR check run name. | ||
| * | ||
| * Check runs are named ``crcr/<owner>/<repo>/<workflow_name>/<job_name>`` | ||
| * (see the Python gh_helper.check_run_name), so the downstream repo is the | ||
| * first two path segments after the ``crcr/`` prefix. | ||
| */ | ||
| export function downstreamRepoFromCheckRunName(name: string): string | null { | ||
| const prefix = "crcr/"; | ||
| if (!name.startsWith(prefix)) { | ||
| return null; | ||
| } | ||
| const parts = name.slice(prefix.length).split("/"); | ||
| if (parts.length < 4 || !parts[0] || !parts[1]) { | ||
| return null; | ||
| } | ||
| return `${parts[0]}/${parts[1]}`; | ||
| } | ||
|
|
||
| /** | ||
| * Search existing PR comments for one with the CRCR on-call marker. | ||
| * Returns its ID (or 0 if none found). Uses pagination so the marker | ||
| * is found even when a PR has more than 30 comments. | ||
| */ | ||
| async function findExistingComment( | ||
| octokit: any, | ||
| owner: string, | ||
| repo: string, | ||
| prNumber: number, | ||
| downstreamRepo: string | ||
| ): Promise<number> { | ||
| const marker = markerForRepo(downstreamRepo); | ||
| const comments = await octokit.paginate(octokit.rest.issues.listComments, { | ||
| owner, | ||
| repo, | ||
| issue_number: prNumber, | ||
| }); | ||
| for (const comment of comments) { | ||
| if (comment.body?.includes(marker)) { | ||
| return comment.id; | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| export default function crcrOncallBot(app: Probot): void { | ||
| app.on("check_run.completed", async (ctx) => { | ||
| const owner = ctx.payload.repository.owner.login; | ||
| if (!isPyTorchbotSupportedOrg(owner)) { | ||
| return; | ||
| } | ||
|
|
||
| const repo = ctx.payload.repository.name; | ||
| const checkRun = ctx.payload.check_run; | ||
|
|
||
| // Only act on CRCR-created check runs | ||
| const downstreamRepo = downstreamRepoFromCheckRunName(checkRun.name); | ||
| if (!downstreamRepo) { | ||
| return; | ||
| } | ||
|
|
||
| // Only comment on failures | ||
| const conclusion = checkRun.conclusion ?? ""; | ||
| if (!FAILURE_CONCLUSIONS.has(conclusion)) { | ||
| return; | ||
| } | ||
|
|
||
| // Get the PR this check run belongs to | ||
| const prs = checkRun.pull_requests ?? []; | ||
| if (prs.length === 0) { | ||
| ctx.log( | ||
| `crcrOncall: no PR associated with check run ${checkRun.name}, skipping` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const headSha = checkRun.head_sha; | ||
| const checkRunUrl = checkRun.html_url ?? ""; | ||
|
|
||
| // Load oncalls from the allowlist | ||
| let oncalls: string[]; | ||
| try { | ||
| const allowlist = await fetchCrcrAllowlist(ctx.octokit); | ||
| oncalls = allowlist.getOncallsForRepo(downstreamRepo); | ||
| } catch (err) { | ||
| ctx.log({ err }, "crcrOncall: failed to load allowlist, skipping"); | ||
| return; | ||
| } | ||
|
|
||
| if (oncalls.length === 0) { | ||
| ctx.log( | ||
| `crcrOncall: no oncalls configured for ${downstreamRepo}, skipping` | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const mentions = oncalls.map((o) => `@${o}`).join(" "); | ||
|
|
||
| // Post a comment on each associated PR (typically just one) | ||
| for (const pr of prs) { | ||
| try { | ||
| const prNumber = pr.number; | ||
|
|
||
| // Dedup by marker: only comment once per PR. | ||
| // NOTE: There is a theoretical TOCTOU race here — two concurrent | ||
| // check_run.completed events for the same PR could both pass the | ||
| // marker check and post duplicate comments. In practice, checks | ||
| // arrive sequentially, so this is unlikely to cause issues. | ||
| const existingId = await findExistingComment( | ||
| ctx.octokit, | ||
| owner, | ||
| repo, | ||
| prNumber, | ||
| downstreamRepo | ||
| ); | ||
| if (existingId !== 0) { | ||
| ctx.log( | ||
| `crcrOncall: comment already exists on PR #${prNumber}, skipping` | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| const commentBody = `${markerForRepo(downstreamRepo)} | ||
| ## :x: CRCR downstream CI failure | ||
|
|
||
| The downstream CI workflow in **${downstreamRepo}** has failed on commit \`${headSha.slice( | ||
| 0, | ||
| 7 | ||
| )}\`. | ||
|
|
||
| ${mentions} please investigate. | ||
|
|
||
| ### Details | ||
|
|
||
| | Field | Value | | ||
| |---|---| | ||
| | **Check Run** | [${checkRun.name}](${checkRunUrl}) | | ||
| | **Commit** | \`${headSha}\` | | ||
| | **Conclusion** | \`${conclusion}\` | | ||
| `; | ||
|
|
||
| await ctx.octokit.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number: prNumber, | ||
| body: commentBody, | ||
| }); | ||
|
|
||
| ctx.log( | ||
| `crcrOncall: commented on PR #${prNumber} for ${downstreamRepo} (${conclusion})` | ||
| ); | ||
| } catch (err) { | ||
| ctx.log( | ||
| { err }, | ||
| `crcrOncall: failed to comment on PR for ${downstreamRepo}` | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This marker is global per PR — if backend A fails first and gets a comment, then backend B fails later, the bot finds the existing marker and skips entirely. This means backend B's oncalls are never notified.
Consider using a per-repo marker (e.g.,
<!-- crcr-oncall:intel/torch-xpu-ops -->) so each downstream repo gets its own dedup scope.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.