@@ -254,6 +254,25 @@ async function finalizeDatabaseCreation(
254
254
} ;
255
255
}
256
256
257
+ interface PullRequestBranches {
258
+ base : string ;
259
+ head : string ;
260
+ }
261
+
262
+ function getPullRequestBranches ( ) : PullRequestBranches | undefined {
263
+ const pullRequest = github . context . payload . pull_request ;
264
+ if ( pullRequest ) {
265
+ return {
266
+ base : pullRequest . base . ref ,
267
+ // We use the head label instead of the head ref here, because the head
268
+ // ref lacks owner information and by itself does not uniquely identify
269
+ // the head branch (which may be in a forked repository).
270
+ head : pullRequest . head . label ,
271
+ } ;
272
+ }
273
+ return undefined ;
274
+ }
275
+
257
276
/**
258
277
* Set up the diff-informed analysis feature.
259
278
*
@@ -269,22 +288,15 @@ export async function setupDiffInformedQueryRun(
269
288
return undefined ;
270
289
}
271
290
272
- const pull_request = github . context . payload . pull_request ;
273
- if ( ! pull_request ) {
291
+ const branches = getPullRequestBranches ( ) ;
292
+ if ( ! branches ) {
274
293
return undefined ;
275
294
}
276
295
277
- const baseRef = pull_request . base . ref as string ;
278
- const headLabel = pull_request . head . label as string ;
279
-
280
296
return await withGroupAsync (
281
297
"Generating diff range extension pack" ,
282
298
async ( ) => {
283
- const diffRanges = await getPullRequestEditedDiffRanges (
284
- baseRef ,
285
- headLabel ,
286
- logger ,
287
- ) ;
299
+ const diffRanges = await getPullRequestEditedDiffRanges ( branches , logger ) ;
288
300
const packDir = writeDiffRangeDataExtensionPack ( logger , diffRanges ) ;
289
301
if ( packDir === undefined ) {
290
302
logger . warning (
@@ -304,21 +316,18 @@ export async function setupDiffInformedQueryRun(
304
316
/**
305
317
* Return the file line ranges that were added or modified in the pull request.
306
318
*
307
- * @param baseRef The base branch name, used for calculating the diff range.
308
- * @param headLabel The label that uniquely identifies the head branch across
309
- * repositories, used for calculating the diff range.
319
+ * @param branches The base and head branches of the pull request.
310
320
* @param logger
311
321
* @returns An array of tuples, where each tuple contains the absolute path of a
312
322
* file, the start line and the end line (both 1-based and inclusive) of an
313
323
* added or modified range in that file. Returns `undefined` if the action was
314
324
* not triggered by a pull request or if there was an error.
315
325
*/
316
326
async function getPullRequestEditedDiffRanges (
317
- baseRef : string ,
318
- headLabel : string ,
327
+ branches : PullRequestBranches ,
319
328
logger : Logger ,
320
329
) : Promise < DiffThunkRange [ ] | undefined > {
321
- const fileDiffs = await getFileDiffsWithBasehead ( baseRef , headLabel , logger ) ;
330
+ const fileDiffs = await getFileDiffsWithBasehead ( branches , logger ) ;
322
331
if ( fileDiffs === undefined ) {
323
332
return undefined ;
324
333
}
@@ -357,14 +366,13 @@ interface FileDiff {
357
366
}
358
367
359
368
async function getFileDiffsWithBasehead (
360
- baseRef : string ,
361
- headLabel : string ,
369
+ branches : PullRequestBranches ,
362
370
logger : Logger ,
363
371
) : Promise < FileDiff [ ] | undefined > {
364
372
const ownerRepo = util . getRequiredEnvParam ( "GITHUB_REPOSITORY" ) . split ( "/" ) ;
365
373
const owner = ownerRepo [ 0 ] ;
366
374
const repo = ownerRepo [ 1 ] ;
367
- const basehead = `${ baseRef } ...${ headLabel } ` ;
375
+ const basehead = `${ branches . base } ...${ branches . head } ` ;
368
376
try {
369
377
const response = await getApiClient ( ) . rest . repos . compareCommitsWithBasehead (
370
378
{
0 commit comments