Skip to content

Commit

Permalink
chore: fix generate-bot-super-report tool
Browse files Browse the repository at this point in the history
  • Loading branch information
gre committed Nov 7, 2023
1 parent de2a012 commit 1721bcd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
27 changes: 23 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 29 additions & 25 deletions tools/actions/generate-bot-super-report/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Parse } from "unzipper";
import { parser } from "stream-json";
import { streamValues } from "stream-json/streamers/StreamValues";
import { delay, retry, promiseAllBatched } from "./promise";
import fetch from "node-fetch";
import groupBy from "lodash/groupBy";
import { Readable } from "stream";

type AppCandidate = {
path: string;
Expand Down Expand Up @@ -41,48 +41,45 @@ export type MinimalSerializedReport = {
export type Artifact = {
created_at: string;
archive_download_url: string;
name: string;
workflow_run: {
head_branch: string;
};
};

function handleErrors(response) {
if (!response.ok) {
throw Error(response.url + ": " + response.status + " " + response.statusText);
}
return response;
}

async function downloadArchive(
githubToken: string,
url: string,
): Promise<MinimalSerializedReport | null> {
console.log("retrieving " + url);
const blob = await retry(() =>

const blob: Blob | null = await retry(() =>
fetch(url, {
headers: {
Authorization: `Bearer ${githubToken}`,
"Content-Type": "application/json",
},
}),
).then(r => {
if (r.ok) {
return r.blob();
}
if (r.status === 410) {
return null;
}
return handleErrors(r);
});
)
.then(r => {
// no more content
if (r.status === 410) {
return null;
}
})
.then(r => (r ? handleErrors(r) : null))
.then(r => (r ? r.blob() : null));
if (!blob) return null;
return new Promise((resolve, reject) => {
blob
.stream()
Readable.fromWeb(blob.stream() as any)
.pipe(Parse())
.on("entry", entry => {
const fileName = entry.path;
if (fileName === "report.json") {
entry
.pipe(parser())
.pipe(streamValues())
.on("data", e => resolve(e.value))
.on("data", (e: { value: any }) => resolve(e.value))
.on("error", reject);
} else {
entry.autodrain();
Expand All @@ -92,17 +89,24 @@ async function downloadArchive(
}

// true if a is before b
function isDateBefore(a, b) {
function isDateBefore(a: string | Date, b: string | Date) {
return new Date(a) < new Date(b);
}

function successRateDisplay(a, b) {
function successRateDisplay(a: number, b: number) {
if (!b) return "N/A";
if (!a) return "❌ 0%";
const r = a / b;
return (r === 1 ? "✅ " : r < 0.8 ? "⚠️ " : "") + Math.round(100 * r) + "%";
}

function handleErrors(response: Response) {
if (!response.ok) {
throw Error(response.url + ": " + response.status + " " + response.statusText);
}
return response;
}

export async function loadReports({
branch,
days,
Expand Down Expand Up @@ -137,7 +141,7 @@ export async function loadReports({
}),
)
.then(r => (r.ok ? r.json() : r.status === 502 ? {} : handleErrors(r)))
.then(r => {
.then((r: { artifacts?: Artifact[] }) => {
if (r.artifacts) {
return r.artifacts;
}
Expand Down Expand Up @@ -450,7 +454,7 @@ function groupSimilarError(str: string): string {
return str;
}

function safeErrorDisplay(txt) {
function safeErrorDisplay(txt: string) {
return txt.slice(0, 200).replace(/[\s`]/g, " ");
}

Expand Down
6 changes: 4 additions & 2 deletions tools/actions/generate-bot-super-report/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
"devDependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^5.1.1",
"@types/node": "20",
"@types/unzipper": "0.10.8",
"lodash": "4.17.21",
"node-fetch": "^3.3.2",
"@types/stream-json": "1.7",
"@types/lodash": "4",
"lodash": "4",
"stream-json": "1.8.0",
"tsup": "^7.0.0",
"unzipper": "0.10.14"
Expand Down
4 changes: 4 additions & 0 deletions tools/actions/generate-bot-super-report/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../../tsconfig.base",
"exclude": ["build"]
}

0 comments on commit 1721bcd

Please sign in to comment.