1+ import { debugFn } from '../../utils/debug.mts'
2+ import { spawn } from '../../utils/spawn.mts'
13import { fetchOrgFullScanList } from '../scan/fetch-list-scans.mts'
24
35import type { CResult } from '../../types.mts'
@@ -10,11 +12,178 @@ export type SelectBaselineScanConfig = {
1012 repo : string
1113}
1214
15+ const ANCESTOR_WALK_MAX_DEPTH = 100
16+ const SCAN_LOOKUP_PAGE_SIZE = 100
17+ const SCAN_LOOKUP_MAX_PAGES = 100
18+
19+ function isUsableScan (
20+ scan : FullScanItem ,
21+ ) : scan is FullScanItem & { commit_hash : string ; id : string } {
22+ return Boolean (
23+ typeof scan . id === 'string' &&
24+ scan . id &&
25+ ! ( scan as FullScanItem & { tmp ?: boolean } ) . tmp &&
26+ typeof scan . commit_hash === 'string' &&
27+ scan . commit_hash ,
28+ )
29+ }
30+
31+ function getNextScanPage (
32+ nextPage : number | null | undefined ,
33+ currentPage : number ,
34+ resultCount : number ,
35+ ) : CResult < number | undefined > {
36+ if (
37+ resultCount < SCAN_LOOKUP_PAGE_SIZE ||
38+ nextPage === undefined ||
39+ nextPage === null ||
40+ nextPage === 0
41+ ) {
42+ return { ok : true , data : undefined }
43+ }
44+ if ( ! Number . isSafeInteger ( nextPage ) || nextPage <= currentPage ) {
45+ return {
46+ ok : false ,
47+ message : 'Cannot select PR scan baseline' ,
48+ cause : `Full-scan listing did not advance to a new page (nextPage=${ String ( nextPage ) } ).` ,
49+ }
50+ }
51+ return { ok : true , data : nextPage }
52+ }
53+
1354export function findBaselineScan (
1455 scans : FullScanItem [ ] ,
1556 commitHash : string ,
1657) : FullScanItem | undefined {
17- return scans . find ( scan => scan . commit_hash === commitHash )
58+ return scans . find (
59+ scan => scan . commit_hash === commitHash && isUsableScan ( scan ) ,
60+ )
61+ }
62+
63+ async function firstParentCommits (
64+ commitHash : string ,
65+ cwd = process . cwd ( ) ,
66+ ) : Promise < string [ ] > {
67+ try {
68+ const resolved = await spawn (
69+ 'git' ,
70+ [ 'rev-parse' , '--verify' , '--end-of-options' , `${ commitHash } ^{commit}` ] ,
71+ { cwd } ,
72+ )
73+ const expectedCommit = resolved . stdout . trim ( )
74+ if ( ! expectedCommit ) {
75+ return [ ]
76+ }
77+ const result = await spawn (
78+ 'git' ,
79+ [
80+ 'rev-list' ,
81+ '--first-parent' ,
82+ `--max-count=${ ANCESTOR_WALK_MAX_DEPTH } ` ,
83+ '--end-of-options' ,
84+ expectedCommit ,
85+ ] ,
86+ { cwd } ,
87+ )
88+ const commits = result . stdout
89+ . split ( / \r ? \n / u)
90+ . map ( commit => commit . trim ( ) )
91+ . filter ( Boolean )
92+ return commits [ 0 ] === expectedCommit ? commits : [ ]
93+ } catch ( error ) {
94+ debugFn ( 'debug' , `Unable to walk first-parent history from ${ commitHash } ` , error )
95+ return [ ]
96+ }
97+ }
98+
99+ async function findNearestScannedAncestor ( {
100+ commitHash,
101+ orgSlug,
102+ repo,
103+ } : Pick < SelectBaselineScanConfig , 'commitHash' | 'orgSlug' | 'repo' > ) : Promise <
104+ CResult <
105+ | { scan : FullScanItem ; commitHash : string ; commitsBack : number }
106+ | undefined
107+ >
108+ > {
109+ const history = await firstParentCommits ( commitHash )
110+ const ancestors = history . slice ( 1 )
111+ if ( ! ancestors . length ) {
112+ return { ok : true , data : undefined }
113+ }
114+
115+ const ancestorDistances = new Map (
116+ ancestors . map ( ( commit , index ) => [ commit , index + 1 ] ) ,
117+ )
118+ const scansByCommit = new Map < string , FullScanItem > ( )
119+ let page = 1
120+
121+ for ( let pageCount = 0 ; pageCount < SCAN_LOOKUP_MAX_PAGES ; pageCount += 1 ) {
122+ // eslint-disable-next-line no-await-in-loop -- Pagination must consume one page at a time.
123+ const scansCResult = await fetchOrgFullScanList ( {
124+ branch : '' ,
125+ direction : 'desc' ,
126+ from_time : '' ,
127+ orgSlug,
128+ page,
129+ perPage : SCAN_LOOKUP_PAGE_SIZE ,
130+ repo,
131+ sort : 'created_at' ,
132+ } )
133+ if ( ! scansCResult . ok ) {
134+ return scansCResult
135+ }
136+
137+ const results = scansCResult . data . results
138+ for ( const scan of results ) {
139+ if ( ! isUsableScan ( scan ) || ! ancestorDistances . has ( scan . commit_hash ) ) {
140+ continue
141+ }
142+ // Results are newest first, so retain the newest persisted scan per commit.
143+ if ( ! scansByCommit . has ( scan . commit_hash ) ) {
144+ scansByCommit . set ( scan . commit_hash , scan )
145+ }
146+ }
147+
148+ if ( scansByCommit . has ( ancestors [ 0 ] ! ) ) {
149+ break
150+ }
151+
152+ const nextPageCResult = getNextScanPage ( (
153+ scansCResult . data as typeof scansCResult . data & {
154+ nextPage ?: number | null
155+ }
156+ ) . nextPage , page , results . length )
157+ if ( ! nextPageCResult . ok ) {
158+ return nextPageCResult
159+ }
160+ if ( nextPageCResult . data === undefined ) {
161+ break
162+ }
163+ page = nextPageCResult . data
164+ if ( pageCount === SCAN_LOOKUP_MAX_PAGES - 1 ) {
165+ return {
166+ ok : false ,
167+ message : 'Cannot select PR scan baseline' ,
168+ cause : `Full-scan listing exceeded ${ SCAN_LOOKUP_MAX_PAGES } pages while searching for a scanned ancestor.` ,
169+ }
170+ }
171+ }
172+
173+ for ( const ancestor of ancestors ) {
174+ const scan = scansByCommit . get ( ancestor )
175+ if ( scan ) {
176+ return {
177+ ok : true ,
178+ data : {
179+ scan,
180+ commitHash : ancestor ,
181+ commitsBack : ancestorDistances . get ( ancestor ) ! ,
182+ } ,
183+ }
184+ }
185+ }
186+ return { ok : true , data : undefined }
18187}
19188
20189export async function selectBaselineScan ( {
@@ -31,27 +200,72 @@ export async function selectBaselineScan({
31200 }
32201 }
33202
34- const scansCResult = await fetchOrgFullScanList ( {
35- branch,
203+ let scan : FullScanItem | undefined
204+ let page = 1
205+ for ( let pageCount = 0 ; pageCount < SCAN_LOOKUP_MAX_PAGES ; pageCount += 1 ) {
206+ // eslint-disable-next-line no-await-in-loop -- Pagination must consume one page at a time.
207+ const scansCResult = await fetchOrgFullScanList ( {
208+ branch,
209+ commitHash,
210+ direction : 'desc' ,
211+ from_time : '' ,
212+ orgSlug,
213+ page,
214+ perPage : SCAN_LOOKUP_PAGE_SIZE ,
215+ repo,
216+ sort : 'created_at' ,
217+ } )
218+ if ( ! scansCResult . ok ) {
219+ return scansCResult
220+ }
221+ scan = findBaselineScan ( scansCResult . data . results , commitHash )
222+ if ( scan ) {
223+ break
224+ }
225+ const results = scansCResult . data . results
226+ const nextPageCResult = getNextScanPage ( (
227+ scansCResult . data as typeof scansCResult . data & {
228+ nextPage ?: number | null
229+ }
230+ ) . nextPage , page , results . length )
231+ if ( ! nextPageCResult . ok ) {
232+ return nextPageCResult
233+ }
234+ if ( nextPageCResult . data === undefined ) {
235+ break
236+ }
237+ page = nextPageCResult . data
238+ if ( pageCount === SCAN_LOOKUP_MAX_PAGES - 1 ) {
239+ return {
240+ ok : false ,
241+ message : 'Cannot select PR scan baseline' ,
242+ cause : `Full-scan listing exceeded ${ SCAN_LOOKUP_MAX_PAGES } pages while searching for the exact commit.` ,
243+ }
244+ }
245+ }
246+ if ( scan ) {
247+ return { ok : true , data : scan }
248+ }
249+
250+ const ancestorCResult = await findNearestScannedAncestor ( {
36251 commitHash,
37- direction : 'desc' ,
38- from_time : '' ,
39252 orgSlug,
40- page : 1 ,
41- perPage : 1 ,
42253 repo,
43- sort : 'created_at' ,
44254 } )
45- if ( ! scansCResult . ok ) {
46- return scansCResult
255+ if ( ! ancestorCResult . ok ) {
256+ return ancestorCResult
257+ }
258+ const ancestor = ancestorCResult . data
259+ if ( ancestor ) {
260+ console . error (
261+ `No scan exists for PR base commit ${ commitHash } ; using its scanned first-parent ancestor ${ ancestor . commitHash } as the baseline (${ ancestor . commitsBack } commit(s) earlier). The diff is wider than the PR base.` ,
262+ )
263+ return { ok : true , data : ancestor . scan }
47264 }
48265
49- const scan = findBaselineScan ( scansCResult . data . results , commitHash )
50- return scan
51- ? { ok : true , data : scan }
52- : {
53- ok : false ,
54- message : 'No matching PR scan baseline found' ,
55- cause : `No scan exists for commit ${ commitHash } on ${ repo } /${ branch } . Run a scan for the base commit first.` ,
56- }
266+ return {
267+ ok : false ,
268+ message : 'No matching PR scan baseline found' ,
269+ cause : `No scan exists for commit ${ commitHash } or a scanned first-parent ancestor within ${ ANCESTOR_WALK_MAX_DEPTH } commits on ${ repo } /${ branch } . Run a scan for the base commit first.` ,
270+ }
57271}
0 commit comments