Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 163 additions & 4 deletions .github/scripts/cli-minimum-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';

export const ISSUE_TITLE = 'CLI fails on its minimum Node';
Expand Down Expand Up @@ -95,9 +96,141 @@ async function findOpenIssue(github, context) {
* A failed run opens the issue, or comments on the open one; a passing run
* closes an open issue this job opened. Returns what it did.
*/
export async function reportIssue({github, context, ok, version, runUrl}) {
/**
* Each leg's verdict from the files the CLI test legs upload
* (`{"shard": <n>, "verdict": "pass" | "fail" | "none"}`): "none" for a leg
* with no file, which the job limit cancelled before it could write one.
* @param {string} dir
* @param {number} count legs
* @returns {Array<'pass' | 'fail' | 'none'>}
*/
export function readLegVerdicts(dir, count) {
const verdicts = Array.from({length: count}, () => 'none');
let names = [];
try {
names = fs.readdirSync(dir);
} catch {
// No verdict was uploaded at all.
}
for (const name of names) {
try {
const {shard, verdict} = JSON.parse(
fs.readFileSync(path.join(dir, name), 'utf8'),
);
if (
Number.isInteger(shard) &&
shard >= 1 &&
shard <= count &&
['pass', 'fail', 'none'].includes(verdict)
)
verdicts[shard - 1] = verdict;
} catch {
// An unreadable file leaves that leg without a verdict.
}
}
return verdicts;
}

/** How far past its minimum-Node record a file may run before it is named. */
export const SLOW_RATIO = 1.5;
/** Seconds past its record a file must also run before it is named. */
export const SLOW_MARGIN_S = 10;

/**
* The files that ran past their minimum-Node record: more than SLOW_RATIO
* times it and more than SLOW_MARGIN_S over it, slowest past it first. A
* file the record lacks is not judged.
* @param {Record<string, number>} times seconds per test file, this run
* @param {Record<string, number>} recorded seconds per test file, the record
* @returns {{file: string, recorded: number, measured: number}[]}
*/
export function slowFiles(times, recorded) {
return Object.entries(times)
.filter(([file, seconds]) => {
const before = recorded[file];
return (
typeof before === 'number' &&
seconds > before * SLOW_RATIO &&
seconds - before > SLOW_MARGIN_S
);
})
.map(([file, measured]) => ({file, recorded: recorded[file], measured}))
.sort((a, b) => b.measured - b.recorded - (a.measured - a.recorded));
}
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved

/**
* The record with this run's times in it: each measured file takes its new
* time, and every other file keeps its own.
* @param {{seconds?: Record<string, number>}} record
* @param {Record<string, number>} times
*/
export function refreshRecord(record, times) {
const seconds = {...record.seconds, ...times};
return {
...record,
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
seconds: Object.fromEntries(
Object.entries(seconds).sort(([a], [b]) => a.localeCompare(b)),
),
};
}

/**
* Every leg's per-file times, as one map, from the files the legs upload:
* each artifact downloads into its own folder under `dir`.
* @param {string} dir
* @returns {Record<string, number>}
*/
export function readLegTimes(dir) {
const times = {};
let files = [];
try {
files = fs
.readdirSync(dir, {recursive: true, withFileTypes: true})
.filter(entry => entry.isFile() && entry.name.endsWith('.json'))
.map(entry => path.join(entry.parentPath ?? entry.path, entry.name));
} catch {
// No leg uploaded its times.
}
for (const file of files) {
try {
Object.assign(times, JSON.parse(fs.readFileSync(file, 'utf8')));
} catch {
// An unreadable file adds no times.
}
}
return times;
}

/** A job's result as a verdict: only success passes and only failure fails. */
const jobVerdict = result =>
result === 'success' ? 'pass' : result === 'failure' ? 'fail' : 'none';

/**
* The run's verdict: "fail" when any part failed a check, else "none" when
* any part judged nothing (stopped, cancelled or skipped), else "pass".
* @param {{version: string, legs: Array<'pass' | 'fail' | 'none'>, pack: string, smoke: string}} parts
* @returns {'pass' | 'fail' | 'none'}
*/
export function runVerdict({version, legs, pack, smoke}) {
if (version !== 'success') return 'none';
const all = [...legs, jobVerdict(pack), jobVerdict(smoke)];
if (all.includes('fail')) return 'fail';
return all.includes('none') ? 'none' : 'pass';
}

export async function reportIssue({github, context, verdict, version, runUrl}) {
const issue = await findOpenIssue(github, context);
if (ok) {
// No verdict neither closes the issue nor says the CLI failed.
if (verdict === 'none') {
if (!issue) return 'none';
await github.rest.issues.createComment({
...context.repo,
issue_number: issue.number,
body: `No verdict on Node ${version}: part of the run stopped or was cancelled at its time limit before it judged anything ([run](${runUrl})). This issue stays as it is.`,
});
return 'no verdict noted';
}
if (verdict === 'pass') {
if (!issue) return 'none';
await github.rest.issues.createComment({
...context.repo,
Expand Down Expand Up @@ -133,10 +266,36 @@ if (
process.argv[1] &&
fs.realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)
) {
const [, , command, flag, file] = process.argv;
const [, , command, flag, file, recordFlag, recordFile] = process.argv;
if (command === 'slow' && flag === '--times' && recordFlag === '--record') {
// One leg's files past their minimum-Node record: a warning each, and a
// line in the step summary. Never a failure.
let times = {};
try {
times = JSON.parse(fs.readFileSync(file, 'utf8'));
} catch {
console.log(`No per-file times at ${file}.`);
process.exit(0);
}
const recorded =
JSON.parse(fs.readFileSync(recordFile, 'utf8')).seconds ?? {};
const slow = slowFiles(times, recorded);
for (const {file: test, recorded: before, measured} of slow)
console.log(
`::warning file=${test},title=Past its minimum-Node time::${test} ran ${measured} s; ${recordFile} records ${before} s.`,
);
if (slow.length && process.env.GITHUB_STEP_SUMMARY)
fs.appendFileSync(
process.env.GITHUB_STEP_SUMMARY,
`### Files past their minimum-Node time\n\n${slow.map(({file: test, recorded: before, measured}) => `- \`${test}\`: ${measured} s (recorded ${before} s)`).join('\n')}\n`,
);
if (!slow.length)
console.log('Every file ran within its minimum-Node time.');
process.exit(0);
}
if (command !== 'minimum' || flag !== '--package' || !file) {
console.error(
'usage: cli-minimum-node.mjs minimum --package <package.json>',
'usage: cli-minimum-node.mjs minimum --package <package.json>\n cli-minimum-node.mjs slow --times <file> --record <file>',
);
process.exit(2);
}
Expand Down
Loading