@expo/code-review-cli 0.2.1 → 0.2.2
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/build/core/render.js +60 -7
- package/build/reporters/github.js +40 -6
- package/package.json +1 -1
package/build/core/render.js
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { fingerprintFinding, SEVERITIES, SEVERITY_RANK } from './schema.js';
|
|
3
|
+
/**
|
|
4
|
+
* Build the file → right-side-line-numbers index from changed files' patch text,
|
|
5
|
+
* by walking each unified-diff hunk (`@@ -a,b +c,d @@`) and collecting the new-tree
|
|
6
|
+
* line number of every added (`+`) and context (` `) line. Deleted (`-`) lines have
|
|
7
|
+
* no right-side line and are skipped.
|
|
8
|
+
*/
|
|
9
|
+
export function buildDiffLineIndex(files) {
|
|
10
|
+
const index = new Map();
|
|
11
|
+
const hunkRe = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/;
|
|
12
|
+
for (const file of files) {
|
|
13
|
+
const lines = new Set();
|
|
14
|
+
let right = 0;
|
|
15
|
+
let inHunk = false;
|
|
16
|
+
for (const raw of file.patch.split('\n')) {
|
|
17
|
+
const hunk = hunkRe.exec(raw);
|
|
18
|
+
if (hunk) {
|
|
19
|
+
right = parseInt(hunk[1], 10);
|
|
20
|
+
inHunk = true;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (!inHunk || raw.startsWith('+++') || raw.startsWith('---') || raw.startsWith('\\')) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const marker = raw[0];
|
|
27
|
+
if (marker === '+' || marker === ' ') {
|
|
28
|
+
lines.add(right);
|
|
29
|
+
right++;
|
|
30
|
+
}
|
|
31
|
+
// '-' is left-side only (no new-tree line); anything else is ignored.
|
|
32
|
+
}
|
|
33
|
+
if (lines.size > 0) {
|
|
34
|
+
index.set(file.path, lines);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return index;
|
|
38
|
+
}
|
|
3
39
|
const DECISION_LABEL = {
|
|
4
40
|
approve: 'Approve',
|
|
5
41
|
approve_with_comments: 'Approve with comments',
|
|
@@ -30,19 +66,36 @@ function locationText(finding) {
|
|
|
30
66
|
return finding.line != null ? `${finding.file}:${finding.line}` : finding.file;
|
|
31
67
|
}
|
|
32
68
|
/**
|
|
33
|
-
* Render a finding's location as inline code, linked to the
|
|
34
|
-
* PR's "Files changed" tab
|
|
35
|
-
* diff
|
|
69
|
+
* Render a finding's location as inline code, linked to the code it points at:
|
|
70
|
+
* - in the diff (file+line shown in a hunk) → the PR's "Files changed" tab at that
|
|
71
|
+
* line (`#diff-<sha256(path)>R<n>`), so the reader lands in the review diff;
|
|
72
|
+
* - not in the diff (unchanged code the PR references, e.g. a caller/helper) → the
|
|
73
|
+
* source blob on the PR base at that line (`/blob/<baseSha>/<path>#L<n>`);
|
|
74
|
+
* - if neither is possible (no link context / no base SHA) → plain inline code.
|
|
75
|
+
* Never emits a dead diff anchor for a line that isn't in the diff.
|
|
36
76
|
*/
|
|
37
77
|
function location(finding, link) {
|
|
38
78
|
const text = locationText(finding);
|
|
39
79
|
if (!link) {
|
|
40
80
|
return `\`${text}\``;
|
|
41
81
|
}
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
82
|
+
const fileLines = link.diffLines?.get(finding.file);
|
|
83
|
+
// In the diff when the file is present and (if the finding names a line) that line
|
|
84
|
+
// is one of the diff's right-side lines. A file-level finding (no line) counts as
|
|
85
|
+
// in-diff as long as the file appears in the diff.
|
|
86
|
+
const inDiff = fileLines != null && (finding.line == null || fileLines.has(finding.line));
|
|
87
|
+
if (inDiff) {
|
|
88
|
+
const fileHash = createHash('sha256').update(finding.file).digest('hex');
|
|
89
|
+
const anchor = finding.line != null ? `diff-${fileHash}R${finding.line}` : `diff-${fileHash}`;
|
|
90
|
+
const url = `https://github.com/${link.repo}/pull/${link.prNumber}/files#${anchor}`;
|
|
91
|
+
return `[\`${text}\`](${url})`;
|
|
92
|
+
}
|
|
93
|
+
if (link.baseSha) {
|
|
94
|
+
const lineAnchor = finding.line != null ? `#L${finding.line}` : '';
|
|
95
|
+
const url = `https://github.com/${link.repo}/blob/${link.baseSha}/${finding.file}${lineAnchor}`;
|
|
96
|
+
return `[\`${text}\`](${url})`;
|
|
97
|
+
}
|
|
98
|
+
return `\`${text}\``;
|
|
46
99
|
}
|
|
47
100
|
/**
|
|
48
101
|
* GitHub comment body. The marker + embedded state enable in-place updates and
|
|
@@ -2,7 +2,8 @@ import { writeFile, mkdtemp, rm } from 'node:fs/promises';
|
|
|
2
2
|
import { tmpdir } from 'node:os';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { run } from '../core/exec.js';
|
|
5
|
-
import {
|
|
5
|
+
import { parseUnifiedDiff } from '../core/diff.js';
|
|
6
|
+
import { buildDiffLineIndex, commentMarker, parseReviewState, renderMarkdown } from '../core/render.js';
|
|
6
7
|
import { fingerprintFinding } from '../core/schema.js';
|
|
7
8
|
const MAINTAINER_ASSOCIATIONS = new Set(['OWNER', 'MEMBER', 'COLLABORATOR']);
|
|
8
9
|
/**
|
|
@@ -33,11 +34,43 @@ export class GitHubReporter {
|
|
|
33
34
|
const dismissed = existing
|
|
34
35
|
? (parseReviewState(existing.body, this.options.commentTag)?.dismissed ?? [])
|
|
35
36
|
: [];
|
|
36
|
-
|
|
37
|
+
const link = await this.linkContextAsync();
|
|
38
|
+
await this.upsertComment(renderMarkdown(review, this.options.commentTag, dismissed, link));
|
|
37
39
|
}
|
|
38
|
-
/**
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
/**
|
|
41
|
+
* PR context for turning finding locations into links: the set of lines actually
|
|
42
|
+
* in the diff (for in-diff findings → diff-anchor links) and the base commit SHA
|
|
43
|
+
* (for out-of-diff findings → source-blob links on the base). Both fetches fail
|
|
44
|
+
* soft — a missing piece just degrades to a plain-text location, never a dead link.
|
|
45
|
+
*/
|
|
46
|
+
async linkContextAsync() {
|
|
47
|
+
const link = { repo: this.options.repo, prNumber: this.options.prNumber };
|
|
48
|
+
const prArgs = [String(this.options.prNumber), '--repo', this.options.repo];
|
|
49
|
+
const cwd = this.options.cwd;
|
|
50
|
+
await Promise.all([
|
|
51
|
+
(async () => {
|
|
52
|
+
try {
|
|
53
|
+
const { stdout } = await run('gh', ['pr', 'diff', ...prArgs], { cwd });
|
|
54
|
+
link.diffLines = buildDiffLineIndex(parseUnifiedDiff(stdout));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// leave diffLines unset → in-diff findings degrade to plain text
|
|
58
|
+
}
|
|
59
|
+
})(),
|
|
60
|
+
(async () => {
|
|
61
|
+
try {
|
|
62
|
+
const { stdout } = await run('gh', ['pr', 'view', ...prArgs, '--json', 'baseRefOid'], { cwd });
|
|
63
|
+
const oid = JSON.parse(stdout).baseRefOid;
|
|
64
|
+
if (oid) {
|
|
65
|
+
link.baseSha = oid;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// leave baseSha unset → out-of-diff findings degrade to plain text
|
|
70
|
+
}
|
|
71
|
+
})(),
|
|
72
|
+
]);
|
|
73
|
+
return link;
|
|
41
74
|
}
|
|
42
75
|
/**
|
|
43
76
|
* Add or remove per-PR finding dismissals in the reviewer's comment and re-render
|
|
@@ -61,7 +94,8 @@ export class GitHubReporter {
|
|
|
61
94
|
dismissed.push({ fp, by, reason });
|
|
62
95
|
}
|
|
63
96
|
}
|
|
64
|
-
|
|
97
|
+
const link = await this.linkContextAsync();
|
|
98
|
+
await this.patchComment(existing.id, renderMarkdown(state.review, this.options.commentTag, dismissed, link));
|
|
65
99
|
return { dismissedCount: dismissed.length, matched, unmatched };
|
|
66
100
|
}
|
|
67
101
|
/** Newest reviewer-tagged comment (id + body), or null if none posted yet. */
|