|
| 1 | +import { Stainless } from "@stainless-api/sdk"; |
| 2 | +import { logger } from "./logger"; |
| 3 | +import type { Outcomes } from "./outcomes"; |
| 4 | + |
| 5 | +export async function isOnlyStatsChanged({ |
| 6 | + stainless, |
| 7 | + outcomes, |
| 8 | + baseOutcomes, |
| 9 | + headBuildId, |
| 10 | +}: { |
| 11 | + stainless: Stainless; |
| 12 | + outcomes: Outcomes; |
| 13 | + baseOutcomes: Outcomes; |
| 14 | + headBuildId: string; |
| 15 | +}): Promise<boolean> { |
| 16 | + for (const lang of Object.keys(baseOutcomes)) { |
| 17 | + if (!(lang in outcomes)) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + for (const [lang, head] of Object.entries(outcomes)) { |
| 23 | + if (!(lang in baseOutcomes)) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + const base = baseOutcomes[lang]!; |
| 27 | + |
| 28 | + const headConclusion = head.commit?.conclusion; |
| 29 | + if (headConclusion === "noop") { |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + if (!base.commit?.completed?.commit || !head.commit?.completed?.commit) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + const baseSha = base.commit.completed.commit.sha; |
| 38 | + const headSha = head.commit.completed.commit.sha; |
| 39 | + const { owner, name } = head.commit.completed.commit.repo; |
| 40 | + |
| 41 | + let token: string; |
| 42 | + try { |
| 43 | + const output = await stainless.builds.targetOutputs.retrieve({ |
| 44 | + build_id: headBuildId, |
| 45 | + target: lang as Stainless.Target, |
| 46 | + type: "source", |
| 47 | + output: "git", |
| 48 | + }); |
| 49 | + if (output.output !== "git") { |
| 50 | + logger.debug( |
| 51 | + `targetOutputs for ${lang} returned non-git output, skipping stats check`, |
| 52 | + ); |
| 53 | + return false; |
| 54 | + } |
| 55 | + token = output.token; |
| 56 | + } catch (e) { |
| 57 | + logger.debug( |
| 58 | + `Could not get git access for ${lang}, skipping stats check`, |
| 59 | + e, |
| 60 | + ); |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + const response = await fetch( |
| 66 | + `https://api.github.com/repos/${owner}/${name}/compare/${baseSha}...${headSha}`, |
| 67 | + { |
| 68 | + headers: { |
| 69 | + Authorization: `token ${token}`, |
| 70 | + Accept: "application/vnd.github.v3+json", |
| 71 | + }, |
| 72 | + }, |
| 73 | + ); |
| 74 | + |
| 75 | + if (!response.ok) { |
| 76 | + logger.debug( |
| 77 | + `GitHub compare API returned ${response.status} for ${lang}, skipping stats check`, |
| 78 | + ); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + const data = (await response.json()) as { |
| 83 | + status: string; |
| 84 | + files?: Array<{ filename: string }>; |
| 85 | + }; |
| 86 | + |
| 87 | + const files = data.files ?? []; |
| 88 | + if (!files.every((f) => f.filename === ".stats.yml")) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + } catch (e) { |
| 92 | + logger.debug( |
| 93 | + `Error comparing commits for ${lang}, skipping stats check`, |
| 94 | + e, |
| 95 | + ); |
| 96 | + return false; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | +} |
0 commit comments