@expo/code-review-cli 0.2.1 → 0.2.3
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 +5 -3
- package/build/core/render.js +60 -7
- package/build/core/review.js +23 -0
- package/build/reporters/github.js +40 -6
- package/build/sources/github-pr.js +46 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -84,9 +84,11 @@ Options (most to least common):
|
|
|
84
84
|
| `--no-fail` | Always exit 0 (otherwise a `request_changes` decision exits non-zero). |
|
|
85
85
|
| `-h`, `--help` | Show help. |
|
|
86
86
|
|
|
87
|
-
`--pr` uses the PR's diff (authoritative)
|
|
88
|
-
|
|
89
|
-
`
|
|
87
|
+
`--pr` uses the PR's diff (authoritative) and checks the PR head out into a
|
|
88
|
+
throwaway worktree so the agents' surrounding-source reads and the verifier see the
|
|
89
|
+
PR's versions of files — no manual `gh pr checkout` needed, and your working tree is
|
|
90
|
+
left untouched. (If that materialization can't run — e.g. not a git checkout — it
|
|
91
|
+
falls back to reading the current working directory.)
|
|
90
92
|
|
|
91
93
|
In CI it runs automatically from the scaffolded workflows — by label or a `/review`
|
|
92
94
|
comment (see **CI usage**). From Claude Code (or another agent), add a slash command
|
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
|
package/build/core/review.js
CHANGED
|
@@ -67,7 +67,28 @@ export async function runReview(source, options) {
|
|
|
67
67
|
});
|
|
68
68
|
return output;
|
|
69
69
|
}
|
|
70
|
+
// Prepare auth BEFORE the chdir below: it doesn't depend on the working directory,
|
|
71
|
+
// and doing it first means nothing that can throw sits between the chdir and the
|
|
72
|
+
// guarded blocks — so a prepareAuth failure can't leak the worktree or leave cwd
|
|
73
|
+
// pointing at it.
|
|
70
74
|
const auth = await prepareAuth(config);
|
|
75
|
+
// Read the PR-head tree (not the current checkout) when the source can materialize
|
|
76
|
+
// it, so the agents' surrounding-source reads and the verifier's re-reads see the
|
|
77
|
+
// versions that match the diff. Config is already fully loaded in memory, so the
|
|
78
|
+
// chdir doesn't affect it; run-log/patch paths are absolute; gh/git calls already
|
|
79
|
+
// ran above. Fails soft to the current directory.
|
|
80
|
+
const originalCwd = process.cwd();
|
|
81
|
+
const readRoot = (await source.prepareReadRootAsync?.()) ?? null;
|
|
82
|
+
const restoreCwd = async () => {
|
|
83
|
+
if (readRoot) {
|
|
84
|
+
process.chdir(originalCwd);
|
|
85
|
+
await readRoot.cleanup();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
if (readRoot) {
|
|
89
|
+
progress('Reviewing the PR-head tree (so reads match the PR, not the checkout).');
|
|
90
|
+
process.chdir(readRoot.dir);
|
|
91
|
+
}
|
|
71
92
|
progress('Starting OpenCode server…');
|
|
72
93
|
let handle = null;
|
|
73
94
|
try {
|
|
@@ -75,6 +96,7 @@ export async function runReview(source, options) {
|
|
|
75
96
|
}
|
|
76
97
|
catch (error) {
|
|
77
98
|
await auth.cleanup();
|
|
99
|
+
await restoreCwd();
|
|
78
100
|
throw new Error(`Failed to start the OpenCode server. Ensure the \`opencode\` CLI is installed and ` +
|
|
79
101
|
`model credentials are configured.\n${errorMessage(error)}`);
|
|
80
102
|
}
|
|
@@ -382,6 +404,7 @@ export async function runReview(source, options) {
|
|
|
382
404
|
finally {
|
|
383
405
|
handle?.close();
|
|
384
406
|
await auth.cleanup();
|
|
407
|
+
await restoreCwd();
|
|
385
408
|
}
|
|
386
409
|
}
|
|
387
410
|
/**
|
|
@@ -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. */
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
1
4
|
import { run } from '../core/exec.js';
|
|
2
5
|
import { parseUnifiedDiff } from '../core/diff.js';
|
|
3
6
|
/**
|
|
@@ -33,4 +36,47 @@ export class GitHubPRSource {
|
|
|
33
36
|
const { stdout } = await run('gh', ['pr', 'diff', String(this.options.prNumber), ...this.repoArgs()], { cwd: this.options.cwd });
|
|
34
37
|
return parseUnifiedDiff(stdout);
|
|
35
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Check the PR HEAD out into a throwaway git worktree so the agents and verifier
|
|
41
|
+
* read the PR's versions of files (not whatever branch happens to be checked out).
|
|
42
|
+
* Fetches the head from the repo's own URL — `refs/pull/<n>/head`, which the base
|
|
43
|
+
* repo hosts even for fork PRs — so it's always the correct PR, independent of the
|
|
44
|
+
* local `origin`. Fails SOFT: any problem (not a git repo, fetch/worktree error)
|
|
45
|
+
* returns null, and the review falls back to reading the current checkout.
|
|
46
|
+
*/
|
|
47
|
+
async prepareReadRootAsync() {
|
|
48
|
+
const cwd = this.options.cwd;
|
|
49
|
+
if (!this.options.repo) {
|
|
50
|
+
// Without an explicit owner/repo we can't build the fetch URL safely.
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const url = `https://github.com/${this.options.repo}.git`;
|
|
54
|
+
const ref = `refs/pull/${this.options.prNumber}/head`;
|
|
55
|
+
let parent;
|
|
56
|
+
try {
|
|
57
|
+
await run('git', ['fetch', '--no-tags', '--depth=1', url, ref], { cwd });
|
|
58
|
+
parent = await mkdtemp(path.join(tmpdir(), 'ecr-prhead-'));
|
|
59
|
+
const dir = path.join(parent, 'head'); // must not pre-exist for `worktree add`
|
|
60
|
+
await run('git', ['worktree', 'add', '--detach', dir, 'FETCH_HEAD'], { cwd });
|
|
61
|
+
const removeParent = parent;
|
|
62
|
+
return {
|
|
63
|
+
dir,
|
|
64
|
+
cleanup: async () => {
|
|
65
|
+
try {
|
|
66
|
+
await run('git', ['worktree', 'remove', '--force', dir], { cwd });
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// best effort — fall through to removing the temp dir
|
|
70
|
+
}
|
|
71
|
+
await rm(removeParent, { recursive: true, force: true });
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
if (parent) {
|
|
77
|
+
await rm(parent, { recursive: true, force: true }).catch(() => { });
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
36
82
|
}
|