Skip to content

Commit 22daf8f

Browse files
authored
fix(pr): paginate changed file metadata (#106217)
1 parent a8ae27f commit 22daf8f

3 files changed

Lines changed: 157 additions & 4 deletions

File tree

‎scripts/pr-lib/review.sh‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,10 @@ review_init() {
496496
local pr="$1"
497497
enter_worktree "$pr" true
498498

499-
local json
499+
local json pr_url
500500
json=$(pr_meta_json "$pr")
501501
write_pr_meta_files "$json"
502+
pr_url=$(printf '%s\n' "$json" | jq -r .url)
502503

503504
git fetch origin "pull/$pr/head:pr-$pr" --force
504505
local mb
@@ -512,9 +513,9 @@ review_init() {
512513
> .local/review-context.env
513514
set_review_mode main
514515

515-
printf '%s\n' "$json" | jq '{number,title,url,state,isDraft,author:.author.login,base:.baseRefName,head:.headRefName,headSha:.headRefOid,headRepo:.headRepository.nameWithOwner,additions,deletions,files:(.files|length)}'
516+
printf '%s\n' "$json" | jq '{number,title,url,state,isDraft,author:.author.login,base:.baseRefName,head:.headRefName,headSha:.headRefOid,headRepo:.headRepository.nameWithOwner,additions,deletions,files:.changedFiles}'
516517
echo "worktree=$PWD"
517-
echo "pr_url=${PR_URL:-}"
518+
echo "pr_url=$pr_url"
518519
echo "merge_base=$mb"
519520
echo "branch=$(git branch --show-current)"
520521
echo "wrote=.local/pr-meta.json .local/pr-meta.env .local/review-context.env .local/review-mode.env"

‎scripts/pr-lib/worktree.sh‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,43 @@ enter_worktree() {
6262

6363
pr_meta_json() {
6464
local pr="$1"
65-
gh pr view "$pr" --json number,title,state,isDraft,author,baseRefName,headRefName,headRefOid,headRepository,headRepositoryOwner,url,body,labels,assignees,reviewRequests,files,additions,deletions,statusCheckRollup
65+
local metadata files expected_file_count actual_file_count head_before head_after
66+
metadata=$(gh pr view "$pr" --json number,title,state,isDraft,author,baseRefName,headRefName,headRefOid,headRepository,headRepositoryOwner,url,body,labels,assignees,reviewRequests,changedFiles,additions,deletions,statusCheckRollup)
67+
head_before=$(printf '%s\n' "$metadata" | jq -r .headRefOid)
68+
69+
# Raw `gh pr view --json files` stops at 100 entries. Paginate REST so
70+
# review guards see every path, then fail closed on head or API drift.
71+
files=$(
72+
gh api --paginate "repos/{owner}/{repo}/pulls/$pr/files?per_page=100" |
73+
jq -cs '
74+
add
75+
| map({
76+
path: .filename,
77+
additions: .additions,
78+
deletions: .deletions,
79+
changeType: (
80+
if .status == "removed" then "DELETED"
81+
else (.status | ascii_upcase)
82+
end
83+
)
84+
})
85+
'
86+
)
87+
88+
head_after=$(gh pr view "$pr" --json headRefOid | jq -r .headRefOid)
89+
if [ "$head_after" != "$head_before" ]; then
90+
echo "PR head changed while collecting file metadata for #$pr (started at $head_before, ended at $head_after). Retry review initialization." >&2
91+
return 1
92+
fi
93+
94+
expected_file_count=$(printf '%s\n' "$metadata" | jq -r .changedFiles)
95+
actual_file_count=$(printf '%s\n' "$files" | jq 'length')
96+
if [ "$actual_file_count" -ne "$expected_file_count" ]; then
97+
echo "Incomplete PR file metadata for #$pr: expected $expected_file_count changed files, received $actual_file_count from paginated REST." >&2
98+
return 1
99+
fi
100+
101+
printf '%s\n%s\n' "$metadata" "$files" | jq -cs '.[0] + {files: .[1]}'
66102
}
67103

68104
write_pr_meta_files() {

‎test/scripts/pr-metadata.test.ts‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { spawnSync } from "node:child_process";
2+
import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { join } from "node:path";
5+
import { afterEach, describe, expect, it } from "vitest";
6+
7+
const tempDirs: string[] = [];
8+
9+
function createFakeGh(): string {
10+
const dir = mkdtempSync(join(tmpdir(), "openclaw-pr-metadata-"));
11+
const gh = join(dir, "gh");
12+
tempDirs.push(dir);
13+
writeFileSync(
14+
gh,
15+
`#!/usr/bin/env bash
16+
set -euo pipefail
17+
18+
if [ "$1" = "pr" ] && [ "$2" = "view" ]; then
19+
if [[ "$*" == *changedFiles* ]]; then
20+
printf '{"number":42,"url":"https://example.test/pr/42","headRefOid":"head-a","changedFiles":%s}\n' "\${FAKE_CHANGED_FILES:-101}"
21+
else
22+
printf '{"headRefOid":"%s"}\n' "\${FAKE_HEAD_AFTER:-head-a}"
23+
fi
24+
exit 0
25+
fi
26+
27+
if [ "$1" = "api" ] && [ "$2" = "--paginate" ]; then
28+
[ "$3" = 'repos/{owner}/{repo}/pulls/42/files?per_page=100' ] || { echo "unexpected endpoint: $3" >&2; exit 4; }
29+
jq -nc '[range(0; 100) | {filename: ("src/file-" + (tostring) + ".ts"), status: "modified", additions: 1, deletions: 0}]'
30+
jq -nc '[{filename: "src/file-100.ts", status: "removed", additions: 0, deletions: 1}]'
31+
exit 0
32+
fi
33+
34+
echo "unexpected gh command: $*" >&2
35+
exit 2
36+
`,
37+
);
38+
chmodSync(gh, 0o755);
39+
return dir;
40+
}
41+
42+
function readPrMetadata(
43+
fakeGhDir: string,
44+
options: { changedFiles?: string; headAfter?: string } = {},
45+
) {
46+
return spawnSync(
47+
"bash",
48+
["-c", "set -euo pipefail; source scripts/pr-lib/worktree.sh; pr_meta_json 42"],
49+
{
50+
cwd: process.cwd(),
51+
env: {
52+
...process.env,
53+
FAKE_CHANGED_FILES: options.changedFiles ?? "101",
54+
FAKE_HEAD_AFTER: options.headAfter ?? "head-a",
55+
PATH: `${fakeGhDir}:${process.env.PATH}`,
56+
},
57+
encoding: "utf8",
58+
},
59+
);
60+
}
61+
62+
afterEach(() => {
63+
for (const dir of tempDirs.splice(0)) {
64+
rmSync(dir, { recursive: true, force: true });
65+
}
66+
});
67+
68+
describe("PR metadata", () => {
69+
it("paginates all changed files and preserves the GraphQL file shape", () => {
70+
const result = readPrMetadata(createFakeGh());
71+
72+
expect(result.status).toBe(0);
73+
expect(result.stderr).toBe("");
74+
const metadata = JSON.parse(result.stdout) as {
75+
changedFiles: number;
76+
files: Array<{
77+
path: string;
78+
additions: number;
79+
deletions: number;
80+
changeType: string;
81+
}>;
82+
};
83+
expect(metadata.changedFiles).toBe(101);
84+
expect(metadata.files).toHaveLength(101);
85+
expect(metadata.files[0]).toEqual({
86+
path: "src/file-0.ts",
87+
additions: 1,
88+
deletions: 0,
89+
changeType: "MODIFIED",
90+
});
91+
expect(metadata.files[100]).toEqual({
92+
path: "src/file-100.ts",
93+
additions: 0,
94+
deletions: 1,
95+
changeType: "DELETED",
96+
});
97+
});
98+
99+
it("rejects incomplete paginated file metadata", () => {
100+
const result = readPrMetadata(createFakeGh(), { changedFiles: "102" });
101+
102+
expect(result.status).toBe(1);
103+
expect(result.stderr).toContain(
104+
"Incomplete PR file metadata for #42: expected 102 changed files, received 101 from paginated REST.",
105+
);
106+
});
107+
108+
it("rejects files collected while the PR head changes", () => {
109+
const result = readPrMetadata(createFakeGh(), { headAfter: "head-b" });
110+
111+
expect(result.status).toBe(1);
112+
expect(result.stderr).toContain(
113+
"PR head changed while collecting file metadata for #42 (started at head-a, ended at head-b). Retry review initialization.",
114+
);
115+
});
116+
});

0 commit comments

Comments
 (0)