pr-evidence 0.2.0 → 0.3.0
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 +2 -2
- package/dist/cli.js +0 -0
- package/dist/evidenceComment.js +58 -35
- package/package.json +1 -1
- package/dist/gifBuilder.js +0 -106
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Your coding agent tests a branch in a real browser and posts each criterion it checked, with the screenshots that prove it, on its pull request.
|
|
4
4
|
|
|
5
|
-
[](https://github.com/debrine/pr-evidence-demo/pull/1)
|
|
6
6
|
|
|
7
7
|
This comment was posted from a real run on [a demo pull request](https://github.com/debrine/pr-evidence-demo/pull/1) that fixes a double-clicked Save adding a note twice.
|
|
8
8
|
|
|
@@ -15,7 +15,7 @@ This comment was posted from a real run on [a demo pull request](https://github.
|
|
|
15
15
|
|
|
16
16
|
<!-- TRANSCRIPT: replace with 10-15 lines trimmed from the showcase run -->
|
|
17
17
|
|
|
18
|
-
Each criterion gets its own section, with its note and
|
|
18
|
+
Each criterion gets its own numbered section, with its note and screenshot thumbnails. Passed criteria are collapsed. If a criterion fails, the comment says "Failed", marks that criterion "❌ Fail" and shows its section open. The tool only reports. It never changes the code under test.
|
|
19
19
|
|
|
20
20
|
## Quickstart
|
|
21
21
|
|
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/evidenceComment.js
CHANGED
|
@@ -1,28 +1,49 @@
|
|
|
1
1
|
export const commentMarker = "<!-- pr-evidence -->";
|
|
2
|
-
|
|
2
|
+
const npmPackageUrl = "https://www.npmjs.com/package/pr-evidence";
|
|
3
|
+
/**
|
|
4
|
+
* Every screenshot shows at this height, so an open criterion fits on about one screen. GitHub turns
|
|
5
|
+
* a height with no width into a maximum height that keeps the aspect ratio, and links each image to
|
|
6
|
+
* its full size.
|
|
7
|
+
*/
|
|
8
|
+
const thumbnailHeight = 240;
|
|
9
|
+
/** Stops untrusted text being read as HTML. `&` goes first so the other entities stay intact. */
|
|
10
|
+
function escapeHtml(text) {
|
|
11
|
+
return text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Makes untrusted text safe for one line of Markdown, such as a heading or a `<summary>`. It uses
|
|
15
|
+
* no backslash escapes, because GitHub shows those literally inside `<summary>`.
|
|
16
|
+
*/
|
|
3
17
|
function escapeInline(text) {
|
|
4
|
-
return text
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
.replace(/\s*\n\s*/g, " ");
|
|
18
|
+
return escapeHtml(text).replace(/\s*\n\s*/g, " ");
|
|
19
|
+
}
|
|
20
|
+
/** Makes untrusted text safe inside a double-quoted HTML attribute. */
|
|
21
|
+
function escapeAttribute(text) {
|
|
22
|
+
return escapeHtml(text).replaceAll('"', """);
|
|
10
23
|
}
|
|
11
24
|
/** Encodes a branch name as one URL path segment that is also safe in a Markdown link destination. */
|
|
12
25
|
function encodeBranchForLink(branch) {
|
|
13
26
|
return encodeURIComponent(branch).replaceAll("(", "%28").replaceAll(")", "%29");
|
|
14
27
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
28
|
+
function thumbnail(fileName, url) {
|
|
29
|
+
const source = escapeAttribute(url);
|
|
30
|
+
return `<img src="${source}" height="${thumbnailHeight}" alt="${escapeAttribute(fileName)}">`;
|
|
31
|
+
}
|
|
32
|
+
/** One line of thumbnails, so they sit side by side and wrap to the comment's width. */
|
|
33
|
+
function thumbnailRow(fileNames, screenshotUrls) {
|
|
34
|
+
return fileNames.map((fileName) => thumbnail(fileName, screenshotUrls.get(fileName))).join(" ");
|
|
17
35
|
}
|
|
18
36
|
function renderSummary(input) {
|
|
19
37
|
const { repository, record, outcome } = input;
|
|
20
38
|
const repositoryUrl = `https://${repository.host}/${repository.owner}/${repository.name}`;
|
|
39
|
+
const passedCount = outcome.criteria.filter((criterion) => criterion.result === "pass").length;
|
|
40
|
+
const failedCount = outcome.criteria.length - passedCount;
|
|
21
41
|
const lines = [
|
|
22
42
|
`## PR evidence: ${outcome.result === "pass" ? "Passed" : "Failed"}`,
|
|
23
43
|
"",
|
|
24
44
|
`- **Tested commit:** [${record.sha}](${repositoryUrl}/commit/${record.sha})`,
|
|
25
45
|
`- **Ticket:** ${outcome.ticket === null ? "No ticket" : escapeInline(outcome.ticket)}`,
|
|
46
|
+
`- **Criteria:** ${passedCount} passed, ${failedCount} failed`,
|
|
26
47
|
];
|
|
27
48
|
if (record.branch !== null) {
|
|
28
49
|
lines.push(`- **Later changes:** [compare with the branch](${repositoryUrl}/compare/${record.sha}...${encodeBranchForLink(record.branch)})`);
|
|
@@ -35,34 +56,25 @@ function renderSummary(input) {
|
|
|
35
56
|
}
|
|
36
57
|
return lines;
|
|
37
58
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return [
|
|
43
|
-
"",
|
|
44
|
-
"| Result | Criterion |",
|
|
45
|
-
"| --- | --- |",
|
|
46
|
-
...outcome.criteria.map((criterion) => `| ${resultLabel(criterion)} | ${escapeInline(criterion.description)} |`),
|
|
47
|
-
];
|
|
59
|
+
/** The number, icon and word together, so the result reads anywhere the emoji does not render. */
|
|
60
|
+
function criterionTitle(criterion, criterionNumber) {
|
|
61
|
+
const result = criterion.result === "pass" ? "✅ Pass" : "❌ Fail";
|
|
62
|
+
return `${criterionNumber}. ${result}: ${escapeInline(criterion.description)}`;
|
|
48
63
|
}
|
|
49
64
|
function renderProof(criterion, screenshotUrls) {
|
|
50
65
|
const note = criterion.note ? ["", escapeInline(criterion.note)] : [];
|
|
51
|
-
const
|
|
52
|
-
?
|
|
53
|
-
: criterion.screenshots
|
|
54
|
-
|
|
55
|
-
image(fileName, screenshotUrls.get(fileName)),
|
|
56
|
-
]);
|
|
57
|
-
return [...note, ...images];
|
|
66
|
+
const screenshots = criterion.screenshots.length === 0
|
|
67
|
+
? "No screenshots"
|
|
68
|
+
: thumbnailRow(criterion.screenshots, screenshotUrls);
|
|
69
|
+
return [...note, "", screenshots];
|
|
58
70
|
}
|
|
59
71
|
/** Failures are expanded so the proof to act on is visible; passes stay one click away. */
|
|
60
|
-
function renderCriterionSection(criterion, screenshotUrls) {
|
|
61
|
-
const title =
|
|
72
|
+
function renderCriterionSection(criterion, criterionNumber, screenshotUrls) {
|
|
73
|
+
const title = criterionTitle(criterion, criterionNumber);
|
|
62
74
|
const proof = renderProof(criterion, screenshotUrls);
|
|
63
75
|
return criterion.result === "fail"
|
|
64
|
-
? [
|
|
65
|
-
: ["
|
|
76
|
+
? [`### ${title}`, ...proof]
|
|
77
|
+
: ["<details>", `<summary>${title}</summary>`, ...proof, "", "</details>"];
|
|
66
78
|
}
|
|
67
79
|
/** Screenshots no criterion cites, such as setup steps, kept out of the way. */
|
|
68
80
|
function renderOtherScreenshots({ outcome, screenshotUrls }) {
|
|
@@ -72,26 +84,37 @@ function renderOtherScreenshots({ outcome, screenshotUrls }) {
|
|
|
72
84
|
return [];
|
|
73
85
|
}
|
|
74
86
|
return [
|
|
75
|
-
"",
|
|
76
87
|
"<details>",
|
|
77
88
|
`<summary>Other screenshots (${others.length})</summary>`,
|
|
78
89
|
...others.flatMap((fileName) => [
|
|
79
90
|
"",
|
|
80
91
|
`**${escapeInline(fileName)}**`,
|
|
81
92
|
"",
|
|
82
|
-
|
|
93
|
+
thumbnail(fileName, screenshotUrls.get(fileName)),
|
|
83
94
|
]),
|
|
84
95
|
"",
|
|
85
96
|
"</details>",
|
|
86
97
|
];
|
|
87
98
|
}
|
|
99
|
+
/** A small plug so reviewers who like the comment can find the tool. */
|
|
100
|
+
function renderFooter() {
|
|
101
|
+
return [`<sub>Want to try it yourself? Check out [pr-evidence on npm](${npmPackageUrl}).</sub>`];
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Puts a rule between sections, so many criteria never blur together, and leaves out empty ones.
|
|
105
|
+
* The blank line before each rule stops it turning the line above into a heading.
|
|
106
|
+
*/
|
|
107
|
+
function joinWithRules(sections) {
|
|
108
|
+
return sections
|
|
109
|
+
.filter((section) => section.length > 0)
|
|
110
|
+
.flatMap((section, index) => [...(index === 0 ? [] : ["", "---"]), "", ...section]);
|
|
111
|
+
}
|
|
88
112
|
export function renderEvidenceComment(input) {
|
|
113
|
+
const criterionSections = input.outcome.criteria.map((criterion, index) => renderCriterionSection(criterion, index + 1, input.screenshotUrls));
|
|
89
114
|
return [
|
|
90
115
|
commentMarker,
|
|
91
116
|
...renderSummary(input),
|
|
92
|
-
...
|
|
93
|
-
...input.outcome.criteria.flatMap((criterion) => renderCriterionSection(criterion, input.screenshotUrls)),
|
|
94
|
-
...renderOtherScreenshots(input),
|
|
117
|
+
...joinWithRules([...criterionSections, renderOtherScreenshots(input), renderFooter()]),
|
|
95
118
|
"",
|
|
96
119
|
].join("\n");
|
|
97
120
|
}
|
package/package.json
CHANGED
package/dist/gifBuilder.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
|
-
import gifenc from "gifenc";
|
|
3
|
-
import { PNG } from "pngjs";
|
|
4
|
-
import { CliError } from "./errors.js";
|
|
5
|
-
import { imageLimitBytes } from "./uploader.js";
|
|
6
|
-
const stepDelayMilliseconds = 1500;
|
|
7
|
-
const maximumGifDimension = 65535;
|
|
8
|
-
const maximumColors = 256;
|
|
9
|
-
const candidateWidths = [1280, 960, 720, 480];
|
|
10
|
-
// About 50x faster than the default on busy images, and fine for UI screenshots.
|
|
11
|
-
const colorFormat = "rgb444";
|
|
12
|
-
function decodeScreenshot(path) {
|
|
13
|
-
try {
|
|
14
|
-
const image = PNG.sync.read(readFileSync(path));
|
|
15
|
-
return { width: image.width, height: image.height, rgba: image.data };
|
|
16
|
-
}
|
|
17
|
-
catch (cause) {
|
|
18
|
-
throw new CliError("screenshot_invalid", `${path} is not a readable PNG: ${String(cause)}`);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
/** Averages the source pixels behind one canvas pixel, which keeps small text legible. */
|
|
22
|
-
function averageSourceBlock(source, startX, endX, startY, endY, target, targetOffset) {
|
|
23
|
-
let red = 0;
|
|
24
|
-
let green = 0;
|
|
25
|
-
let blue = 0;
|
|
26
|
-
for (let y = startY; y < endY; y += 1) {
|
|
27
|
-
for (let x = startX; x < endX; x += 1) {
|
|
28
|
-
const offset = (y * source.width + x) * 4;
|
|
29
|
-
red += source.rgba[offset];
|
|
30
|
-
green += source.rgba[offset + 1];
|
|
31
|
-
blue += source.rgba[offset + 2];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
const pixelCount = (endX - startX) * (endY - startY);
|
|
35
|
-
target[targetOffset] = red / pixelCount;
|
|
36
|
-
target[targetOffset + 1] = green / pixelCount;
|
|
37
|
-
target[targetOffset + 2] = blue / pixelCount;
|
|
38
|
-
target[targetOffset + 3] = 255;
|
|
39
|
-
}
|
|
40
|
-
/** Fits the image inside the canvas, centred on white, keeping its proportions. */
|
|
41
|
-
function fitToCanvas(source, canvasWidth, canvasHeight) {
|
|
42
|
-
const scale = Math.min(canvasWidth / source.width, canvasHeight / source.height);
|
|
43
|
-
const fittedWidth = Math.max(1, Math.round(source.width * scale));
|
|
44
|
-
const fittedHeight = Math.max(1, Math.round(source.height * scale));
|
|
45
|
-
const leftMargin = Math.floor((canvasWidth - fittedWidth) / 2);
|
|
46
|
-
const topMargin = Math.floor((canvasHeight - fittedHeight) / 2);
|
|
47
|
-
const rgba = new Uint8Array(canvasWidth * canvasHeight * 4).fill(255);
|
|
48
|
-
for (let y = 0; y < fittedHeight; y += 1) {
|
|
49
|
-
const startY = Math.floor((y * source.height) / fittedHeight);
|
|
50
|
-
const endY = Math.max(startY + 1, Math.floor(((y + 1) * source.height) / fittedHeight));
|
|
51
|
-
for (let x = 0; x < fittedWidth; x += 1) {
|
|
52
|
-
const startX = Math.floor((x * source.width) / fittedWidth);
|
|
53
|
-
const endX = Math.max(startX + 1, Math.floor(((x + 1) * source.width) / fittedWidth));
|
|
54
|
-
const targetOffset = ((y + topMargin) * canvasWidth + x + leftMargin) * 4;
|
|
55
|
-
averageSourceBlock(source, startX, endX, startY, endY, rgba, targetOffset);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return { width: canvasWidth, height: canvasHeight, rgba };
|
|
59
|
-
}
|
|
60
|
-
function encodeGif(frames, width, height) {
|
|
61
|
-
const scale = Math.min(1, maximumGifDimension / width, maximumGifDimension / height);
|
|
62
|
-
const canvasWidth = Math.max(1, Math.round(width * scale));
|
|
63
|
-
const canvasHeight = Math.max(1, Math.round(height * scale));
|
|
64
|
-
const encoder = gifenc.GIFEncoder();
|
|
65
|
-
for (const frame of frames) {
|
|
66
|
-
const { rgba } = fitToCanvas(frame, canvasWidth, canvasHeight);
|
|
67
|
-
const palette = gifenc.quantize(rgba, maximumColors, { format: colorFormat });
|
|
68
|
-
encoder.writeFrame(gifenc.applyPalette(rgba, palette, colorFormat), canvasWidth, canvasHeight, {
|
|
69
|
-
palette,
|
|
70
|
-
delay: stepDelayMilliseconds,
|
|
71
|
-
repeat: 0,
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
encoder.finish();
|
|
75
|
-
return encoder.bytes();
|
|
76
|
-
}
|
|
77
|
-
/** Every second frame, always keeping the last so the final state is shown. */
|
|
78
|
-
function dropEverySecondFrame(frames) {
|
|
79
|
-
const kept = frames.filter((_frame, index) => index % 2 === 0);
|
|
80
|
-
const lastFrame = frames.at(-1);
|
|
81
|
-
return kept.at(-1) === lastFrame ? kept : [...kept, lastFrame];
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Writes the GIF, shrinking it until it fits GitHub's image limit: first by
|
|
85
|
-
* width, then by dropping steps.
|
|
86
|
-
*/
|
|
87
|
-
export function buildGif(screenshotPaths, outputPath) {
|
|
88
|
-
let frames = screenshotPaths.map(decodeScreenshot);
|
|
89
|
-
const fullWidth = Math.max(...frames.map((frame) => frame.width));
|
|
90
|
-
const fullHeight = Math.max(...frames.map((frame) => frame.height));
|
|
91
|
-
const widths = [...new Set(candidateWidths.map((width) => Math.min(width, fullWidth)))];
|
|
92
|
-
for (;;) {
|
|
93
|
-
for (const width of widths) {
|
|
94
|
-
const height = Math.max(1, Math.round((fullHeight * width) / fullWidth));
|
|
95
|
-
const bytes = encodeGif(frames, width, height);
|
|
96
|
-
if (bytes.length <= imageLimitBytes) {
|
|
97
|
-
writeFileSync(outputPath, bytes);
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (frames.length <= 2) {
|
|
102
|
-
throw new CliError("gif_too_large", "The GIF could not be kept under GitHub's image size limit.");
|
|
103
|
-
}
|
|
104
|
-
frames = dropEverySecondFrame(frames);
|
|
105
|
-
}
|
|
106
|
-
}
|