|
| 1 | +import type { Logger } from "pino"; |
| 2 | + |
| 3 | +function normalizeConfiguredTeam(team: string): string { |
| 4 | + const trimmed = team.trim().toLowerCase(); |
| 5 | + if (trimmed.length === 0) return ""; |
| 6 | + const lastSlash = trimmed.lastIndexOf("/"); |
| 7 | + return lastSlash >= 0 ? trimmed.slice(lastSlash + 1) : trimmed; |
| 8 | +} |
| 9 | + |
| 10 | +export function buildRereviewTeamCandidates(team: string): string[] { |
| 11 | + const primary = normalizeConfiguredTeam(team); |
| 12 | + if (primary.length === 0) return []; |
| 13 | + |
| 14 | + const candidates = new Set<string>([primary]); |
| 15 | + if (primary === "ai-review") candidates.add("aireview"); |
| 16 | + if (primary === "aireview") candidates.add("ai-review"); |
| 17 | + return Array.from(candidates); |
| 18 | +} |
| 19 | + |
| 20 | +function getStatusCode(err: unknown): number | undefined { |
| 21 | + if (typeof err !== "object" || err === null) return undefined; |
| 22 | + const value = (err as { status?: unknown }).status; |
| 23 | + return typeof value === "number" ? value : undefined; |
| 24 | +} |
| 25 | + |
| 26 | +export async function requestRereviewTeamBestEffort(options: { |
| 27 | + octokit: { |
| 28 | + rest: { |
| 29 | + pulls: { |
| 30 | + listRequestedReviewers: (params: { |
| 31 | + owner: string; |
| 32 | + repo: string; |
| 33 | + pull_number: number; |
| 34 | + }) => Promise<{ data: { teams?: Array<{ slug?: string | null; name?: string | null }> } }>; |
| 35 | + requestReviewers: (params: { |
| 36 | + owner: string; |
| 37 | + repo: string; |
| 38 | + pull_number: number; |
| 39 | + team_reviewers: string[]; |
| 40 | + }) => Promise<unknown>; |
| 41 | + }; |
| 42 | + }; |
| 43 | + }; |
| 44 | + owner: string; |
| 45 | + repo: string; |
| 46 | + prNumber: number; |
| 47 | + configuredTeam: string; |
| 48 | + logger: Logger; |
| 49 | +}): Promise<{ requestedTeam?: string; alreadyRequested: boolean }> { |
| 50 | + const { octokit, owner, repo, prNumber, configuredTeam, logger } = options; |
| 51 | + const candidates = buildRereviewTeamCandidates(configuredTeam); |
| 52 | + if (candidates.length === 0) return { alreadyRequested: false }; |
| 53 | + |
| 54 | + try { |
| 55 | + const requested = await octokit.rest.pulls.listRequestedReviewers({ |
| 56 | + owner, |
| 57 | + repo, |
| 58 | + pull_number: prNumber, |
| 59 | + }); |
| 60 | + |
| 61 | + const already = (requested.data.teams ?? []).some((team) => { |
| 62 | + const slug = (team.slug ?? "").trim().toLowerCase(); |
| 63 | + const name = (team.name ?? "").trim().toLowerCase(); |
| 64 | + return candidates.includes(slug) || candidates.includes(name); |
| 65 | + }); |
| 66 | + if (already) return { alreadyRequested: true }; |
| 67 | + } catch (err) { |
| 68 | + logger.warn( |
| 69 | + { err, owner, repo, prNumber, configuredTeam }, |
| 70 | + "Failed to list requested reviewers; attempting rereview request anyway", |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + for (const candidate of candidates) { |
| 75 | + try { |
| 76 | + await octokit.rest.pulls.requestReviewers({ |
| 77 | + owner, |
| 78 | + repo, |
| 79 | + pull_number: prNumber, |
| 80 | + team_reviewers: [candidate], |
| 81 | + }); |
| 82 | + return { requestedTeam: candidate, alreadyRequested: false }; |
| 83 | + } catch (err) { |
| 84 | + const status = getStatusCode(err); |
| 85 | + const canFallback = status === 422 && candidate !== candidates[candidates.length - 1]; |
| 86 | + logger.warn( |
| 87 | + { |
| 88 | + err, |
| 89 | + owner, |
| 90 | + repo, |
| 91 | + prNumber, |
| 92 | + configuredTeam, |
| 93 | + candidate, |
| 94 | + status, |
| 95 | + fallbackRemaining: canFallback, |
| 96 | + }, |
| 97 | + "Failed to request rereview team candidate", |
| 98 | + ); |
| 99 | + if (!canFallback) break; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return { alreadyRequested: false }; |
| 104 | +} |
0 commit comments