@@ -17,7 +17,25 @@ set -euo pipefail
1717
1818if [ "$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
2644
2745if [ "$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
4668function 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
73105describe ( "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