Skip to content

Commit 939878c

Browse files
committed
perf(pr): cache complete small file lists
1 parent 107bc4f commit 939878c

2 files changed

Lines changed: 118 additions & 19 deletions

File tree

‎scripts/pr-lib/worktree.sh‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,62 @@ enter_worktree() {
9191
pr_meta_json() {
9292
local pr="$1"
9393
local metadata files expected_file_count actual_file_count head_before head_after
94-
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)
94+
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,files)
9595
head_before=$(printf '%s\n' "$metadata" | jq -r .headRefOid)
96+
expected_file_count=$(printf '%s\n' "$metadata" | jq -r .changedFiles)
9697

97-
# Raw `gh pr view --json files` stops at 100 entries. Paginate REST so
98-
# review guards see every path, then fail closed on head or API drift.
99-
if ! files=$(
100-
set -o pipefail
101-
gh api --paginate "repos/{owner}/{repo}/pulls/$pr/files?per_page=100" |
102-
jq -cs '
103-
add
104-
| map({
105-
path: .filename,
98+
# `gh pr view --json files` is cacheable but stops at 100 entries. Use it
99+
# when complete; only large or incomplete responses spend uncached REST quota.
100+
files='[]'
101+
if [ "$expected_file_count" -le 100 ]; then
102+
files=$(printf '%s\n' "$metadata" | jq -c '
103+
.files
104+
| if type == "array"
105+
and all(.[];
106+
(.path | type == "string")
107+
and (.additions | type == "number")
108+
and (.deletions | type == "number")
109+
and (.changeType | type == "string" and length > 0)
110+
)
111+
then map({
112+
path: .path,
106113
additions: .additions,
107114
deletions: .deletions,
108115
changeType: (
109-
if .status == "removed" then "DELETED"
110-
else (.status | ascii_upcase)
116+
if (.changeType | ascii_downcase) == "removed"
117+
or (.changeType | ascii_downcase) == "deleted"
118+
then "DELETED"
119+
else (.changeType | ascii_upcase)
111120
end
112121
)
113122
})
114-
'
115-
); then
116-
echo "Failed to collect paginated PR file metadata for #$pr." >&2
117-
return 1
123+
else []
124+
end
125+
' 2>/dev/null || printf '[]')
126+
fi
127+
128+
actual_file_count=$(printf '%s\n' "$files" | jq -r 'length')
129+
if [ "$actual_file_count" -ne "$expected_file_count" ]; then
130+
if ! files=$(
131+
set -o pipefail
132+
gh api --paginate "repos/{owner}/{repo}/pulls/$pr/files?per_page=100" |
133+
jq -cs '
134+
add
135+
| map({
136+
path: .filename,
137+
additions: .additions,
138+
deletions: .deletions,
139+
changeType: (
140+
if .status == "removed" then "DELETED"
141+
else (.status | ascii_upcase)
142+
end
143+
)
144+
})
145+
'
146+
); then
147+
echo "Failed to collect paginated PR file metadata for #$pr." >&2
148+
return 1
149+
fi
118150
fi
119151

120152
head_after=$(gh pr view "$pr" --json headRefOid | jq -r .headRefOid)
@@ -123,7 +155,6 @@ pr_meta_json() {
123155
return 1
124156
fi
125157

126-
expected_file_count=$(printf '%s\n' "$metadata" | jq -r .changedFiles)
127158
if ! actual_file_count=$(
128159
printf '%s\n' "$files" |
129160
jq -er 'if type == "array" then length else error("expected an array") end'

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

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,25 @@ set -euo pipefail
1717
1818
if [ "$1" = "pr" ] && [ "$2" = "view" ]; then
1919
if [[ "$*" == *changedFiles* ]]; then
20-
printf '{"number":42,"url":"https://example.test/pr/42","headRefOid":"head-a","changedFiles":%s}\n' "\${FAKE_CHANGED_FILES:-101}"
20+
jq -nc --argjson changedFiles "\${FAKE_CHANGED_FILES:-101}" --argjson fileCount "\${FAKE_GRAPHQL_FILE_COUNT:-100}" --argjson includeChangeType "\${FAKE_GRAPHQL_CHANGE_TYPE:-true}" '
21+
{
22+
number: 42,
23+
url: "https://example.test/pr/42",
24+
headRefOid: "head-a",
25+
changedFiles: $changedFiles,
26+
files: [
27+
range(0; $fileCount)
28+
| ({
29+
path: ("src/graphql-file-" + (tostring) + ".ts"),
30+
additions: 1,
31+
deletions: 0,
32+
originalPath: ""
33+
} + if $includeChangeType then {
34+
changeType: (if . == ($fileCount - 1) then "removed" else "modified" end)
35+
} else {} end)
36+
]
37+
}
38+
'
2139
else
2240
printf '{"headRefOid":"%s"}\n' "\${FAKE_HEAD_AFTER:-head-a}"
2341
fi
@@ -26,6 +44,10 @@ fi
2644
2745
if [ "$1" = "api" ] && [ "$2" = "--paginate" ]; then
2846
[ "$3" = 'repos/{owner}/{repo}/pulls/42/files?per_page=100' ] || { echo "unexpected endpoint: $3" >&2; exit 4; }
47+
if [ "\${FAKE_REST_FILE_COUNT:-101}" = "2" ]; then
48+
jq -nc '[range(0; 2) | {filename: ("src/file-" + (tostring) + ".ts"), status: (if . == 1 then "removed" else "modified" end), additions: 1, deletions: 0}]'
49+
exit 0
50+
fi
2951
jq -nc '[range(0; 100) | {filename: ("src/file-" + (tostring) + ".ts"), status: "modified", additions: 1, deletions: 0}]'
3052
if [ "\${FAKE_FILES_API_FAILURE:-0}" = "1" ]; then
3153
echo "files API failed" >&2
@@ -45,7 +67,14 @@ exit 2
4567

4668
function readPrMetadata(
4769
fakeGhDir: string,
48-
options: { changedFiles?: string; filesApiFailure?: boolean; headAfter?: string } = {},
70+
options: {
71+
changedFiles?: string;
72+
filesApiFailure?: boolean;
73+
graphqlChangeType?: boolean;
74+
graphqlFileCount?: string;
75+
headAfter?: string;
76+
restFileCount?: string;
77+
} = {},
4978
) {
5079
return spawnSync(
5180
"bash",
@@ -56,7 +85,10 @@ function readPrMetadata(
5685
...process.env,
5786
FAKE_CHANGED_FILES: options.changedFiles ?? "101",
5887
FAKE_FILES_API_FAILURE: options.filesApiFailure ? "1" : "0",
88+
FAKE_GRAPHQL_CHANGE_TYPE: options.graphqlChangeType === false ? "false" : "true",
89+
FAKE_GRAPHQL_FILE_COUNT: options.graphqlFileCount ?? "100",
5990
FAKE_HEAD_AFTER: options.headAfter ?? "head-a",
91+
FAKE_REST_FILE_COUNT: options.restFileCount ?? "101",
6092
PATH: `${fakeGhDir}:${process.env.PATH}`,
6193
},
6294
encoding: "utf8",
@@ -71,6 +103,42 @@ afterEach(() => {
71103
});
72104

73105
describe("PR metadata", () => {
106+
it("uses cacheable GraphQL file metadata when the complete list fits", () => {
107+
const result = readPrMetadata(createFakeGh(), {
108+
changedFiles: "2",
109+
filesApiFailure: true,
110+
graphqlFileCount: "2",
111+
});
112+
113+
expect(result.status).toBe(0);
114+
expect(result.stderr).toBe("");
115+
const metadata = JSON.parse(result.stdout) as {
116+
files: Array<{ changeType: string; path: string }>;
117+
};
118+
expect(metadata.files).toEqual([
119+
{ path: "src/graphql-file-0.ts", additions: 1, deletions: 0, changeType: "MODIFIED" },
120+
{ path: "src/graphql-file-1.ts", additions: 1, deletions: 0, changeType: "DELETED" },
121+
]);
122+
});
123+
124+
it("falls back to REST when cacheable file metadata lacks change types", () => {
125+
const result = readPrMetadata(createFakeGh(), {
126+
changedFiles: "2",
127+
graphqlChangeType: false,
128+
graphqlFileCount: "2",
129+
restFileCount: "2",
130+
});
131+
132+
expect(result.status).toBe(0);
133+
const metadata = JSON.parse(result.stdout) as {
134+
files: Array<{ changeType: string; path: string }>;
135+
};
136+
expect(metadata.files).toEqual([
137+
{ path: "src/file-0.ts", additions: 1, deletions: 0, changeType: "MODIFIED" },
138+
{ path: "src/file-1.ts", additions: 1, deletions: 0, changeType: "DELETED" },
139+
]);
140+
});
141+
74142
it("paginates all changed files and preserves the GraphQL file shape", () => {
75143
const result = readPrMetadata(createFakeGh());
76144

0 commit comments

Comments
 (0)