pr-evidence 0.1.1 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -116
- package/dist/appRunner.js +14 -72
- package/dist/cli.js +19 -3
- package/dist/commands/browser.js +62 -0
- package/dist/commands/finish.js +48 -31
- package/dist/commands/publish.js +4 -2
- package/dist/commands/setup.js +16 -4
- package/dist/commands/start.js +22 -30
- package/dist/commands/stop.js +2 -2
- package/dist/evidenceComment.js +45 -19
- package/dist/evidenceStore.js +3 -6
- package/dist/helperInstall.js +52 -0
- package/dist/prerequisites.js +5 -5
- package/dist/processControl.js +179 -0
- package/dist/updateCheck.js +77 -0
- package/dist/uploader.js +14 -5
- package/package.json +5 -5
- package/skill/SKILL.md +30 -27
- package/dist/gifBuilder.js +0 -106
package/dist/commands/start.js
CHANGED
|
@@ -4,31 +4,10 @@ import { CliError } from "../errors.js";
|
|
|
4
4
|
import { appLogPathOf, assertRunAvailable, browserProfileDirectoryOf, createRun, pruneExpiredRuns, readRunRecord, screenshotDirectoryOf, writeRunRecord, } from "../evidenceStore.js";
|
|
5
5
|
import { describeUnsupportedHost, GitHubClient, readUploadToken } from "../github.js";
|
|
6
6
|
import { helperVersion } from "../packageInfo.js";
|
|
7
|
-
import {
|
|
7
|
+
import { checkGitHubCli } from "../prerequisites.js";
|
|
8
8
|
import { readRepoState } from "../repoState.js";
|
|
9
|
+
import { describeSetupCommand, findNewerHelperVersion } from "../updateCheck.js";
|
|
9
10
|
import { stopLatestRunApp } from "./stop.js";
|
|
10
|
-
function shellQuote(value) {
|
|
11
|
-
return `'${value.replaceAll("'", `'\\''`)}'`;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Pins the browser to a dedicated profile. The unset variables would otherwise
|
|
15
|
-
* attach it to the developer's own Chrome and their real sessions.
|
|
16
|
-
*/
|
|
17
|
-
function describeBrowser(repositoryKey) {
|
|
18
|
-
const profileDirectory = browserProfileDirectoryOf(repositoryKey);
|
|
19
|
-
const sessionName = `pr-evidence-${repositoryKey.replace(/[^A-Za-z0-9_-]/g, "-")}`.slice(0, 64);
|
|
20
|
-
return {
|
|
21
|
-
profileDirectory,
|
|
22
|
-
commandPrefix: [
|
|
23
|
-
"env",
|
|
24
|
-
"-u CHROME_DEVTOOLS_AXI_AUTO_CONNECT",
|
|
25
|
-
"-u CHROME_DEVTOOLS_AXI_BROWSER_URL",
|
|
26
|
-
"-u CHROME_DEVTOOLS_AXI_MCP_SERVER_URL",
|
|
27
|
-
`CHROME_DEVTOOLS_AXI_USER_DATA_DIR=${shellQuote(profileDirectory)}`,
|
|
28
|
-
`CHROME_DEVTOOLS_AXI_SESSION=${shellQuote(sessionName)}`,
|
|
29
|
-
].join(" "),
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
11
|
/** Why this commit may not be what the open PR shows, or null when nothing suggests that. */
|
|
33
12
|
async function describePullRequestHeadProblem(repository, branch, sha) {
|
|
34
13
|
if (branch === null) {
|
|
@@ -45,7 +24,7 @@ async function describePullRequestHeadProblem(repository, branch, sha) {
|
|
|
45
24
|
return `Could not compare this commit with the open PR. ${error.message}`;
|
|
46
25
|
}
|
|
47
26
|
}
|
|
48
|
-
async function collectWarnings(repoState, skillVersion) {
|
|
27
|
+
async function collectWarnings(repoState, skillVersion, newerHelperVersionLookup) {
|
|
49
28
|
const warnings = [];
|
|
50
29
|
const { repository, branch, sha } = repoState;
|
|
51
30
|
const hostProblem = repository && describeUnsupportedHost(repository.host);
|
|
@@ -66,7 +45,11 @@ async function collectWarnings(repoState, skillVersion) {
|
|
|
66
45
|
}
|
|
67
46
|
}
|
|
68
47
|
if (skillVersion !== undefined && skillVersion !== helperVersion) {
|
|
69
|
-
warnings.push(`The skill is version ${skillVersion} but the helper is ${helperVersion}. Run:
|
|
48
|
+
warnings.push(`The skill is version ${skillVersion} but the helper is ${helperVersion}. Run: ${describeSetupCommand(helperVersion)}`);
|
|
49
|
+
}
|
|
50
|
+
const newerVersion = await newerHelperVersionLookup;
|
|
51
|
+
if (newerVersion !== null) {
|
|
52
|
+
warnings.push(`pr-evidence ${newerVersion} is available, and this is ${helperVersion}. Run: ${describeSetupCommand(newerVersion)}`);
|
|
70
53
|
}
|
|
71
54
|
return warnings;
|
|
72
55
|
}
|
|
@@ -100,10 +83,19 @@ export async function start(options) {
|
|
|
100
83
|
if (options.url === undefined && config === null) {
|
|
101
84
|
throw new CliError("config_missing", `No ${configFileName} in this repo. Add one with start and readyUrl, or pass --url.`);
|
|
102
85
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
86
|
+
// Looked up while the app starts, so that a slow registry adds as little as possible to the run.
|
|
87
|
+
const lookupCancellation = new AbortController();
|
|
88
|
+
const newerHelperVersionLookup = findNewerHelperVersion(lookupCancellation.signal);
|
|
89
|
+
try {
|
|
90
|
+
return await startRun(options, config, repoState, newerHelperVersionLookup);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
// An open request would keep the process alive until the lookup times out.
|
|
94
|
+
lookupCancellation.abort();
|
|
95
|
+
throw error;
|
|
106
96
|
}
|
|
97
|
+
}
|
|
98
|
+
async function startRun(options, config, repoState, newerHelperVersionLookup) {
|
|
107
99
|
assertRunAvailable(repoState);
|
|
108
100
|
await stopLatestRunApp(repoState);
|
|
109
101
|
pruneExpiredRuns();
|
|
@@ -119,7 +111,7 @@ export async function start(options) {
|
|
|
119
111
|
screenshotDirectory: screenshotDirectoryOf(runDirectory),
|
|
120
112
|
login: config?.login ?? null,
|
|
121
113
|
ticketFolder: config?.ticketFolder ?? null,
|
|
122
|
-
browser:
|
|
123
|
-
warnings: await collectWarnings(repoState, options.skillVersion),
|
|
114
|
+
browser: { profileDirectory: browserProfileDirectoryOf(repoState.repositoryKey) },
|
|
115
|
+
warnings: await collectWarnings(repoState, options.skillVersion, newerHelperVersionLookup),
|
|
124
116
|
};
|
|
125
117
|
}
|
package/dist/commands/stop.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { stopProcess } from "../processControl.js";
|
|
2
2
|
import { findLatestRun, writeRunRecord } from "../evidenceStore.js";
|
|
3
3
|
/** Stops the app recorded on this checkout's latest run. Returns whether one was stopped. */
|
|
4
4
|
export async function stopLatestRunApp(repoState) {
|
|
@@ -6,7 +6,7 @@ export async function stopLatestRunApp(repoState) {
|
|
|
6
6
|
if (run === null || run.record.app === undefined) {
|
|
7
7
|
return false;
|
|
8
8
|
}
|
|
9
|
-
const stopped = await
|
|
9
|
+
const stopped = await stopProcess(run.record.app);
|
|
10
10
|
const { app: _stoppedApp, ...remainingRecord } = run.record;
|
|
11
11
|
writeRunRecord(run.directory, remainingRecord);
|
|
12
12
|
return stopped;
|
package/dist/evidenceComment.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const commentMarker = "<!-- pr-evidence -->";
|
|
2
|
+
const npmPackageUrl = "https://www.npmjs.com/package/pr-evidence";
|
|
2
3
|
/** Makes untrusted text safe for one line of Markdown, including inside a table cell. */
|
|
3
4
|
function escapeInline(text) {
|
|
4
5
|
return text
|
|
@@ -35,31 +36,47 @@ function renderSummary(input) {
|
|
|
35
36
|
}
|
|
36
37
|
return lines;
|
|
37
38
|
}
|
|
38
|
-
function
|
|
39
|
+
function resultLabel(criterion) {
|
|
40
|
+
return criterion.result === "pass" ? "Pass" : "Fail";
|
|
41
|
+
}
|
|
42
|
+
function renderTable(outcome) {
|
|
39
43
|
return [
|
|
40
44
|
"",
|
|
41
|
-
"| Result | Criterion |
|
|
42
|
-
"| --- | --- |
|
|
43
|
-
...outcome.criteria.map((criterion) => `| ${
|
|
45
|
+
"| Result | Criterion |",
|
|
46
|
+
"| --- | --- |",
|
|
47
|
+
...outcome.criteria.map((criterion) => `| ${resultLabel(criterion)} | ${escapeInline(criterion.description)} |`),
|
|
44
48
|
];
|
|
45
49
|
}
|
|
46
|
-
function
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
50
|
+
function renderProof(criterion, screenshotUrls) {
|
|
51
|
+
const note = criterion.note ? ["", escapeInline(criterion.note)] : [];
|
|
52
|
+
const images = criterion.screenshots.length === 0
|
|
53
|
+
? ["", "No screenshots"]
|
|
54
|
+
: criterion.screenshots.flatMap((fileName) => [
|
|
55
|
+
"",
|
|
56
|
+
image(fileName, screenshotUrls.get(fileName)),
|
|
57
|
+
]);
|
|
58
|
+
return [...note, ...images];
|
|
53
59
|
}
|
|
54
|
-
|
|
55
|
-
|
|
60
|
+
/** Failures are expanded so the proof to act on is visible; passes stay one click away. */
|
|
61
|
+
function renderCriterionSection(criterion, screenshotUrls) {
|
|
62
|
+
const title = `${resultLabel(criterion)}: ${escapeInline(criterion.description)}`;
|
|
63
|
+
const proof = renderProof(criterion, screenshotUrls);
|
|
64
|
+
return criterion.result === "fail"
|
|
65
|
+
? ["", `### ${title}`, ...proof]
|
|
66
|
+
: ["", "<details>", `<summary>${title}</summary>`, ...proof, "", "</details>"];
|
|
67
|
+
}
|
|
68
|
+
/** Screenshots no criterion cites, such as setup steps, kept out of the way. */
|
|
69
|
+
function renderOtherScreenshots({ outcome, screenshotUrls }) {
|
|
70
|
+
const cited = new Set(outcome.criteria.flatMap((criterion) => criterion.screenshots));
|
|
71
|
+
const others = outcome.screenshots.filter((fileName) => !cited.has(fileName));
|
|
72
|
+
if (others.length === 0) {
|
|
56
73
|
return [];
|
|
57
74
|
}
|
|
58
75
|
return [
|
|
59
76
|
"",
|
|
60
77
|
"<details>",
|
|
61
|
-
`<summary>
|
|
62
|
-
...
|
|
78
|
+
`<summary>Other screenshots (${others.length})</summary>`,
|
|
79
|
+
...others.flatMap((fileName) => [
|
|
63
80
|
"",
|
|
64
81
|
`**${escapeInline(fileName)}**`,
|
|
65
82
|
"",
|
|
@@ -69,14 +86,23 @@ function renderScreenshots({ outcome, screenshotUrls }) {
|
|
|
69
86
|
"</details>",
|
|
70
87
|
];
|
|
71
88
|
}
|
|
89
|
+
/** A small plug so reviewers who like the comment can find the tool. */
|
|
90
|
+
function renderFooter() {
|
|
91
|
+
return [
|
|
92
|
+
"",
|
|
93
|
+
"---",
|
|
94
|
+
"",
|
|
95
|
+
`<sub>Want to try it yourself? Check out [pr-evidence on npm](${npmPackageUrl}).</sub>`,
|
|
96
|
+
];
|
|
97
|
+
}
|
|
72
98
|
export function renderEvidenceComment(input) {
|
|
73
99
|
return [
|
|
74
100
|
commentMarker,
|
|
75
101
|
...renderSummary(input),
|
|
76
|
-
...
|
|
77
|
-
...
|
|
78
|
-
...(input
|
|
79
|
-
...
|
|
102
|
+
...renderTable(input.outcome),
|
|
103
|
+
...input.outcome.criteria.flatMap((criterion) => renderCriterionSection(criterion, input.screenshotUrls)),
|
|
104
|
+
...renderOtherScreenshots(input),
|
|
105
|
+
...renderFooter(),
|
|
80
106
|
"",
|
|
81
107
|
].join("\n");
|
|
82
108
|
}
|
package/dist/evidenceStore.js
CHANGED
|
@@ -2,11 +2,11 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
-
import {
|
|
5
|
+
import { isProcessRunning } from "./processControl.js";
|
|
6
6
|
import { CliError } from "./errors.js";
|
|
7
7
|
const runFileName = "run.json";
|
|
8
8
|
const retentionMilliseconds = 14 * 24 * 60 * 60 * 1000;
|
|
9
|
-
function evidenceRoot() {
|
|
9
|
+
export function evidenceRoot() {
|
|
10
10
|
return join(homedir(), ".pr-evidence");
|
|
11
11
|
}
|
|
12
12
|
/** One pointer per checkout, so runs in separate worktrees of a repo stay apart. */
|
|
@@ -46,9 +46,6 @@ export function createRun(repoState, url) {
|
|
|
46
46
|
writeFileSync(latestRunPointerPath(record.repositoryKey, record.rootPath), record.sha);
|
|
47
47
|
return runDirectory;
|
|
48
48
|
}
|
|
49
|
-
export function gifPathOf(runDirectory) {
|
|
50
|
-
return join(runDirectory, "evidence.gif");
|
|
51
|
-
}
|
|
52
49
|
export function listScreenshots(runDirectory) {
|
|
53
50
|
return readdirSync(screenshotDirectoryOf(runDirectory), { withFileTypes: true })
|
|
54
51
|
.filter((entry) => entry.isFile() && entry.name.toLowerCase().endsWith(".png"))
|
|
@@ -78,7 +75,7 @@ export function readRunRecord(runDirectory) {
|
|
|
78
75
|
function hasRunningApp(runDirectory) {
|
|
79
76
|
try {
|
|
80
77
|
const app = readRunRecord(runDirectory)?.app;
|
|
81
|
-
return app !== undefined &&
|
|
78
|
+
return app !== undefined && isProcessRunning(app);
|
|
82
79
|
}
|
|
83
80
|
catch {
|
|
84
81
|
return false;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { CliError } from "./errors.js";
|
|
5
|
+
import { evidenceRoot } from "./evidenceStore.js";
|
|
6
|
+
import { helperVersion } from "./packageInfo.js";
|
|
7
|
+
/** Written last, because an interrupted `npm install` can leave the CLI without its dependencies. */
|
|
8
|
+
const installCompleteMarker = ".install-complete";
|
|
9
|
+
function appDirectory() {
|
|
10
|
+
return join(evidenceRoot(), "app");
|
|
11
|
+
}
|
|
12
|
+
function runNpmInstall(versionDirectory, packageSource) {
|
|
13
|
+
mkdirSync(versionDirectory, { recursive: true });
|
|
14
|
+
// Without a manifest here, npm walks up and installs into any project above, such as the home directory.
|
|
15
|
+
writeFileSync(join(versionDirectory, "package.json"), '{ "private": true }\n');
|
|
16
|
+
try {
|
|
17
|
+
// Through a shell, because on Windows npm is a .cmd file, which Node cannot run without one.
|
|
18
|
+
// --prefer-offline uses what npx has just cached without asking the registry again. Without it, an
|
|
19
|
+
// unreachable registry makes npm crash part way through the dependency tree, even with all of it cached.
|
|
20
|
+
execSync(`npm install "${packageSource}" --save-exact --prefer-offline --no-audit --no-fund --no-update-notifier --loglevel=error`, { cwd: versionDirectory, stdio: ["ignore", "pipe", "pipe"], encoding: "utf8", windowsHide: true });
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
throw new CliError("install_failed", `Could not install ${packageSource} with npm. Check your registry and proxy settings: ` +
|
|
24
|
+
"`npm config get registry` shows the registry in use, and `npm config set proxy <url>` sets a proxy.", { npmOutput: String(error.stderr ?? error).trim() });
|
|
25
|
+
}
|
|
26
|
+
writeFileSync(join(versionDirectory, installCompleteMarker), "");
|
|
27
|
+
}
|
|
28
|
+
/** Installs this exact version, if it is not there yet, and returns the path of its CLI. */
|
|
29
|
+
export function installHelper() {
|
|
30
|
+
const versionDirectory = join(appDirectory(), helperVersion);
|
|
31
|
+
const packageSourceOverride = process.env.PR_EVIDENCE_INSTALL_SOURCE;
|
|
32
|
+
if (packageSourceOverride !== undefined) {
|
|
33
|
+
// An unreleased build's version says nothing about its contents, and npm would keep the old copy.
|
|
34
|
+
rmSync(versionDirectory, { recursive: true, force: true });
|
|
35
|
+
}
|
|
36
|
+
if (!existsSync(join(versionDirectory, installCompleteMarker))) {
|
|
37
|
+
runNpmInstall(versionDirectory, packageSourceOverride ?? `pr-evidence@${helperVersion}`);
|
|
38
|
+
}
|
|
39
|
+
return join(versionDirectory, "node_modules", "pr-evidence", "dist", "cli.js");
|
|
40
|
+
}
|
|
41
|
+
/** Best effort: on Windows a running browser bridge can hold an old version's files open. */
|
|
42
|
+
export function removeOldHelperVersions() {
|
|
43
|
+
const oldVersions = readdirSync(appDirectory()).filter((version) => version !== helperVersion);
|
|
44
|
+
for (const version of oldVersions) {
|
|
45
|
+
try {
|
|
46
|
+
rmSync(join(appDirectory(), version), { recursive: true, force: true });
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
process.stderr.write(`Could not remove old helper version ${version}. The next setup tries again.\n`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
package/dist/prerequisites.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { findWindowsShell, windowsShellPrerequisite } from "./processControl.js";
|
|
2
3
|
function succeeds(command, args) {
|
|
3
4
|
try {
|
|
4
5
|
execFileSync(command, args, { stdio: "ignore" });
|
|
@@ -16,11 +17,10 @@ export function checkGitHubCli() {
|
|
|
16
17
|
fix: "Install the GitHub CLI from https://cli.github.com and run: gh auth login",
|
|
17
18
|
};
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
: { name: "chrome-devtools-axi", fix: "Run: npm install -g chrome-devtools-axi" };
|
|
20
|
+
/** Only Windows, where the start command needs Git for Windows' sh.exe. POSIX always has `sh`. */
|
|
21
|
+
function checkWindowsShell() {
|
|
22
|
+
return process.platform !== "win32" || findWindowsShell() !== null ? null : windowsShellPrerequisite;
|
|
23
23
|
}
|
|
24
24
|
export function findMissingPrerequisites() {
|
|
25
|
-
return [checkGitHubCli(),
|
|
25
|
+
return [checkGitHubCli(), checkWindowsShell()].filter((missing) => missing !== null);
|
|
26
26
|
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
2
|
+
import { once } from "node:events";
|
|
3
|
+
import { closeSync, existsSync, openSync } from "node:fs";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
6
|
+
import { CliError } from "./errors.js";
|
|
7
|
+
/** The shell Windows needs to run the start command, and how to get it. */
|
|
8
|
+
export const windowsShellPrerequisite = {
|
|
9
|
+
name: "sh.exe",
|
|
10
|
+
fix: "Install Git for Windows from https://git-scm.com/download/win. The start command runs with its sh.exe, found from `git --exec-path`.",
|
|
11
|
+
};
|
|
12
|
+
const posix = {
|
|
13
|
+
spawnShell: (command, { cwd, logDescriptor }) => spawn("sh", ["-c", command], {
|
|
14
|
+
cwd,
|
|
15
|
+
detached: true,
|
|
16
|
+
stdio: ["ignore", logDescriptor, logDescriptor],
|
|
17
|
+
}),
|
|
18
|
+
// The process id is also the process group id, because the command is launched as a group leader.
|
|
19
|
+
isAlive: (processGroupId) => isSignallable(-processGroupId),
|
|
20
|
+
readStartTime: (processId) => readCommandOutput("ps", ["-o", "lstart=", "-p", String(processId)]),
|
|
21
|
+
trustsUnreadableStartTime: true,
|
|
22
|
+
requestStop: (processGroupId) => process.kill(-processGroupId, "SIGTERM"),
|
|
23
|
+
forceStop: (processGroupId) => process.kill(-processGroupId, "SIGKILL"),
|
|
24
|
+
};
|
|
25
|
+
const windows = {
|
|
26
|
+
spawnShell: (command, { cwd, logDescriptor }) => {
|
|
27
|
+
const shell = findWindowsShell();
|
|
28
|
+
if (shell === null) {
|
|
29
|
+
throw new CliError("missing_prerequisite", `${windowsShellPrerequisite.name} is required. ${windowsShellPrerequisite.fix}`);
|
|
30
|
+
}
|
|
31
|
+
return spawn(shell, ["-c", command], {
|
|
32
|
+
cwd,
|
|
33
|
+
// Otherwise Node ends the app when this CLI exits.
|
|
34
|
+
detached: true,
|
|
35
|
+
windowsHide: true,
|
|
36
|
+
stdio: ["ignore", logDescriptor, logDescriptor],
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
isAlive: isSignallable,
|
|
40
|
+
readStartTime: (processId) => readCommandOutput(windowsSystemProgram("WindowsPowerShell", "v1.0", "powershell.exe"), [
|
|
41
|
+
"-NoProfile",
|
|
42
|
+
"-NonInteractive",
|
|
43
|
+
"-Command",
|
|
44
|
+
// Imported by path, because finding it through PSModulePath can take over 20 seconds when that is unset.
|
|
45
|
+
"Import-Module \"$PSHOME\\Modules\\Microsoft.PowerShell.Management\"; " +
|
|
46
|
+
`(Get-Process -Id ${processId} -ErrorAction Stop).StartTime.ToUniversalTime().ToString('o')`,
|
|
47
|
+
]),
|
|
48
|
+
trustsUnreadableStartTime: false,
|
|
49
|
+
requestStop: (processId) => runTaskkill(processId, []),
|
|
50
|
+
forceStop: (processId) => runTaskkill(processId, ["/F"]),
|
|
51
|
+
};
|
|
52
|
+
const platform = process.platform === "win32" ? windows : posix;
|
|
53
|
+
const stopGraceMilliseconds = 5_000;
|
|
54
|
+
const stopPollMilliseconds = 50;
|
|
55
|
+
/**
|
|
56
|
+
* Runs `command` through the shell in `cwd`, with stdout and stderr appended
|
|
57
|
+
* to `logPath`. Rejects with an `app_exited` CliError if the command could not
|
|
58
|
+
* be launched at all, or `missing_prerequisite` when there is no shell to run it.
|
|
59
|
+
*/
|
|
60
|
+
export async function launchProcess(command, options) {
|
|
61
|
+
// Not append mode: Git for Windows' sh.exe drops what it writes to an append-only handle.
|
|
62
|
+
// The run directory is new for each launch, so there is nothing earlier to keep.
|
|
63
|
+
const logDescriptor = openSync(options.logPath, "w");
|
|
64
|
+
let child;
|
|
65
|
+
try {
|
|
66
|
+
child = platform.spawnShell(command, { cwd: options.cwd, logDescriptor });
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
closeSync(logDescriptor);
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
await once(child, "spawn");
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
throw new CliError("app_exited", `Could not launch the start command: ${command}. ${String(error)}`);
|
|
76
|
+
}
|
|
77
|
+
const processId = child.pid;
|
|
78
|
+
return {
|
|
79
|
+
identity: { processId, processStartTime: platform.readStartTime(processId) },
|
|
80
|
+
exitStatus: () => child.exitCode === null && child.signalCode === null
|
|
81
|
+
? null
|
|
82
|
+
: { exitCode: child.exitCode, signal: child.signalCode },
|
|
83
|
+
release: () => child.unref(),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Whether the process tree is still running and may still be the one that was launched. */
|
|
87
|
+
export function isProcessRunning(identity) {
|
|
88
|
+
const ownership = checkOwnership(identity);
|
|
89
|
+
// A process that cannot be confirmed may be the app, so it is kept rather than forgotten.
|
|
90
|
+
return ownership === "launched" || ownership === "unconfirmed";
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Stops the process tree, and only if it is still the one that was launched.
|
|
94
|
+
* Returns whether a stop was sent. Throws an `app_not_confirmed` CliError,
|
|
95
|
+
* without stopping anything, when a live process cannot be confirmed as the app.
|
|
96
|
+
*/
|
|
97
|
+
export async function stopProcess(identity) {
|
|
98
|
+
const ownership = checkOwnership(identity);
|
|
99
|
+
if (ownership === "unconfirmed") {
|
|
100
|
+
throw new CliError("app_not_confirmed", `Process ${identity.processId} is running, but could not be confirmed as the app this tool started, so it was not stopped. If it is the app, stop it yourself.`);
|
|
101
|
+
}
|
|
102
|
+
if (ownership !== "launched") {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
platform.requestStop(identity.processId);
|
|
106
|
+
const deadline = Date.now() + stopGraceMilliseconds;
|
|
107
|
+
while (Date.now() < deadline) {
|
|
108
|
+
if (!platform.isAlive(identity.processId)) {
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
await sleep(stopPollMilliseconds);
|
|
112
|
+
}
|
|
113
|
+
platform.forceStop(identity.processId);
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
/** Whether the process is ours. `gone` and `other` both mean nothing of ours is left to stop. */
|
|
117
|
+
function checkOwnership(identity) {
|
|
118
|
+
if (!platform.isAlive(identity.processId)) {
|
|
119
|
+
return "gone";
|
|
120
|
+
}
|
|
121
|
+
const currentStartTime = platform.readStartTime(identity.processId);
|
|
122
|
+
const startTimeUnreadable = currentStartTime === null || identity.processStartTime === null;
|
|
123
|
+
if (startTimeUnreadable && !platform.trustsUnreadableStartTime) {
|
|
124
|
+
return "unconfirmed";
|
|
125
|
+
}
|
|
126
|
+
return currentStartTime === null || currentStartTime === identity.processStartTime ? "launched" : "other";
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The `sh.exe` of the Git for Windows install that `git` belongs to, or null.
|
|
130
|
+
* Never looked up on PATH, where `bash.exe` in the Windows directory is the WSL launcher.
|
|
131
|
+
*/
|
|
132
|
+
export function findWindowsShell() {
|
|
133
|
+
// Such as C:/Program Files/Git/mingw64/libexec/git-core, three levels below the install.
|
|
134
|
+
const gitExecPath = readCommandOutput("git", ["--exec-path"]);
|
|
135
|
+
if (gitExecPath === null) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
const shell = join(resolve(gitExecPath, "..", "..", ".."), "usr", "bin", "sh.exe");
|
|
139
|
+
return existsSync(shell) ? shell : null;
|
|
140
|
+
}
|
|
141
|
+
/** Signal 0 only checks that the process, or with a negative id the group, exists. */
|
|
142
|
+
function isSignallable(processId) {
|
|
143
|
+
try {
|
|
144
|
+
process.kill(processId, 0);
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
return error.code === "EPERM";
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Trimmed stdout of a successful run, or null when it failed or printed nothing. */
|
|
152
|
+
function readCommandOutput(command, args) {
|
|
153
|
+
try {
|
|
154
|
+
const output = execFileSync(command, args, {
|
|
155
|
+
encoding: "utf8",
|
|
156
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
157
|
+
windowsHide: true,
|
|
158
|
+
}).trim();
|
|
159
|
+
return output === "" ? null : output;
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** A program in the Windows system directory, by full path so that PATH cannot replace it. */
|
|
166
|
+
function windowsSystemProgram(...pathInSystem32) {
|
|
167
|
+
return join(process.env.SystemRoot ?? "C:\\Windows", "System32", ...pathInSystem32);
|
|
168
|
+
}
|
|
169
|
+
function runTaskkill(processId, extraArgs) {
|
|
170
|
+
try {
|
|
171
|
+
execFileSync(windowsSystemProgram("taskkill.exe"), ["/PID", String(processId), "/T", ...extraArgs], {
|
|
172
|
+
stdio: "ignore",
|
|
173
|
+
windowsHide: true,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
// Ignored, because the polling and the forced stop that follow decide the outcome.
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { helperVersion } from "./packageInfo.js";
|
|
2
|
+
/** Long enough for a slow registry, short enough that a stalled proxy does not hold up a run. */
|
|
3
|
+
const lookupTimeoutMilliseconds = 2_000;
|
|
4
|
+
/** PR_EVIDENCE_REGISTRY_URL lets tests point the update check at a local fake. */
|
|
5
|
+
function registryUrl() {
|
|
6
|
+
return process.env.PR_EVIDENCE_REGISTRY_URL ?? "https://registry.npmjs.org";
|
|
7
|
+
}
|
|
8
|
+
/** The command that installs a version of the helper and its skill, in the form this platform runs. */
|
|
9
|
+
export function describeSetupCommand(version) {
|
|
10
|
+
const npxCommand = process.platform === "win32" ? "npx.cmd" : "npx";
|
|
11
|
+
return `${npxCommand} pr-evidence@${version} setup`;
|
|
12
|
+
}
|
|
13
|
+
function parseVersion(version) {
|
|
14
|
+
const match = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(version);
|
|
15
|
+
if (match === null)
|
|
16
|
+
return null;
|
|
17
|
+
return {
|
|
18
|
+
release: match.slice(1, 4).map(Number),
|
|
19
|
+
prerelease: match[4]?.split(".") ?? [],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function compareIdentifiers(left, right) {
|
|
23
|
+
const leftIsNumber = /^\d+$/.test(left);
|
|
24
|
+
const rightIsNumber = /^\d+$/.test(right);
|
|
25
|
+
if (leftIsNumber && rightIsNumber)
|
|
26
|
+
return Number(left) - Number(right);
|
|
27
|
+
// Semantic versioning ranks numeric identifiers below alphanumeric ones.
|
|
28
|
+
if (leftIsNumber !== rightIsNumber)
|
|
29
|
+
return leftIsNumber ? -1 : 1;
|
|
30
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
31
|
+
}
|
|
32
|
+
/** Semantic version precedence: below zero when left is older, above zero when it is newer. */
|
|
33
|
+
function compareVersions(left, right) {
|
|
34
|
+
for (let index = 0; index < 3; index++) {
|
|
35
|
+
const difference = left.release[index] - right.release[index];
|
|
36
|
+
if (difference !== 0)
|
|
37
|
+
return difference;
|
|
38
|
+
}
|
|
39
|
+
// A prerelease comes before the release it leads up to.
|
|
40
|
+
if (left.prerelease.length === 0 || right.prerelease.length === 0) {
|
|
41
|
+
return right.prerelease.length - left.prerelease.length;
|
|
42
|
+
}
|
|
43
|
+
for (let index = 0; index < Math.max(left.prerelease.length, right.prerelease.length); index++) {
|
|
44
|
+
if (left.prerelease[index] === undefined)
|
|
45
|
+
return -1;
|
|
46
|
+
if (right.prerelease[index] === undefined)
|
|
47
|
+
return 1;
|
|
48
|
+
const difference = compareIdentifiers(left.prerelease[index], right.prerelease[index]);
|
|
49
|
+
if (difference !== 0)
|
|
50
|
+
return difference;
|
|
51
|
+
}
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
function isNewerThanThisHelper(publishedVersion) {
|
|
55
|
+
if (typeof publishedVersion !== "string")
|
|
56
|
+
return false;
|
|
57
|
+
const published = parseVersion(publishedVersion);
|
|
58
|
+
const installed = parseVersion(helperVersion);
|
|
59
|
+
return published !== null && installed !== null && compareVersions(published, installed) > 0;
|
|
60
|
+
}
|
|
61
|
+
/** The latest published version when it is newer than this one, otherwise null, including when the registry did not say. */
|
|
62
|
+
export async function findNewerHelperVersion(cancellation) {
|
|
63
|
+
try {
|
|
64
|
+
const response = await fetch(`${registryUrl()}/pr-evidence`, {
|
|
65
|
+
signal: AbortSignal.any([cancellation, AbortSignal.timeout(lookupTimeoutMilliseconds)]),
|
|
66
|
+
});
|
|
67
|
+
if (!response.ok)
|
|
68
|
+
return null;
|
|
69
|
+
const packageDocument = (await response.json());
|
|
70
|
+
const latestVersion = packageDocument["dist-tags"]?.latest;
|
|
71
|
+
return isNewerThanThisHelper(latestVersion) ? latestVersion : null;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// Being offline or behind a proxy must never get in the way of a run.
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
package/dist/uploader.js
CHANGED
|
@@ -1,12 +1,21 @@
|
|
|
1
1
|
import { readFileSync, statSync } from "node:fs";
|
|
2
2
|
import { basename, extname } from "node:path";
|
|
3
3
|
import { CliError } from "./errors.js";
|
|
4
|
-
|
|
4
|
+
const imageLimitBytes = 10 * 1024 * 1024;
|
|
5
|
+
/** Why GitHub would refuse an image of this size, or null if it accepts it. */
|
|
6
|
+
export function imageSizeProblem(size) {
|
|
7
|
+
if (size === 0) {
|
|
8
|
+
return "it is empty";
|
|
9
|
+
}
|
|
10
|
+
if (size > imageLimitBytes) {
|
|
11
|
+
return `it is ${size} bytes, over GitHub's ${imageLimitBytes}-byte image limit`;
|
|
12
|
+
}
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
5
15
|
const uploadTimeoutMilliseconds = 60_000;
|
|
6
16
|
const attachmentPath = /^\/user-attachments\/assets\/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
7
17
|
const contentTypes = {
|
|
8
18
|
".png": "image/png",
|
|
9
|
-
".gif": "image/gif",
|
|
10
19
|
};
|
|
11
20
|
function failed(filePath, problem) {
|
|
12
21
|
return new CliError("upload_failed", `Could not upload ${basename(filePath)}: ${problem}`);
|
|
@@ -26,9 +35,9 @@ export class GitHubAttachmentUploader {
|
|
|
26
35
|
if (contentType === undefined) {
|
|
27
36
|
throw failed(filePath, "unsupported file type.");
|
|
28
37
|
}
|
|
29
|
-
const
|
|
30
|
-
if (
|
|
31
|
-
throw failed(filePath,
|
|
38
|
+
const sizeProblem = imageSizeProblem(statSync(filePath).size);
|
|
39
|
+
if (sizeProblem !== null) {
|
|
40
|
+
throw failed(filePath, `${sizeProblem}.`);
|
|
32
41
|
}
|
|
33
42
|
const query = new URLSearchParams({
|
|
34
43
|
name: basename(filePath),
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pr-evidence",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Test a branch in a real browser and post screenshots
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Test a branch in a real browser and post screenshots of each criterion as evidence on its PR.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -27,17 +27,17 @@
|
|
|
27
27
|
"typecheck": "tsc --noEmit",
|
|
28
28
|
"test": "vitest run",
|
|
29
29
|
"test:smoke": "vitest run --config vitest.smoke.config.ts",
|
|
30
|
+
"test:smoke:browser": "vitest run --config vitest.browserSmoke.config.ts",
|
|
30
31
|
"prepack": "npm run build"
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
|
-
"
|
|
34
|
-
"pngjs": "^7.0.0",
|
|
34
|
+
"chrome-devtools-axi": "0.1.46",
|
|
35
35
|
"yaml": "^2.9.1"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/node": "^22.20.5",
|
|
39
39
|
"@types/pngjs": "^6.0.5",
|
|
40
|
-
"
|
|
40
|
+
"pngjs": "^7.0.0",
|
|
41
41
|
"typescript": "^7.0.2",
|
|
42
42
|
"vitest": "^5.1.0"
|
|
43
43
|
}
|