@@ -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,22 @@ 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 ) {
293
+ logger . info (
294
+ "Not performing diff-informed analysis " +
295
+ "because we are not analyzing a pull request." ,
296
+ ) ;
274
297
return undefined ;
275
298
}
276
299
277
- const baseRef = pull_request . base . ref as string ;
278
- const headLabel = pull_request . head . label as string ;
279
-
280
300
return await withGroupAsync (
281
301
"Generating diff range extension pack" ,
282
302
async ( ) => {
283
- const diffRanges = await getPullRequestEditedDiffRanges (
284
- baseRef ,
285
- headLabel ,
286
- logger ,
303
+ logger . info (
304
+ `Calculating diff ranges for ${ branches . base } ...${ branches . head } ` ,
287
305
) ;
306
+ const diffRanges = await getPullRequestEditedDiffRanges ( branches , logger ) ;
288
307
const packDir = writeDiffRangeDataExtensionPack ( logger , diffRanges ) ;
289
308
if ( packDir === undefined ) {
290
309
logger . warning (
@@ -304,21 +323,18 @@ export async function setupDiffInformedQueryRun(
304
323
/**
305
324
* Return the file line ranges that were added or modified in the pull request.
306
325
*
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.
326
+ * @param branches The base and head branches of the pull request.
310
327
* @param logger
311
328
* @returns An array of tuples, where each tuple contains the absolute path of a
312
329
* file, the start line and the end line (both 1-based and inclusive) of an
313
330
* added or modified range in that file. Returns `undefined` if the action was
314
331
* not triggered by a pull request or if there was an error.
315
332
*/
316
333
async function getPullRequestEditedDiffRanges (
317
- baseRef : string ,
318
- headLabel : string ,
334
+ branches : PullRequestBranches ,
319
335
logger : Logger ,
320
336
) : Promise < DiffThunkRange [ ] | undefined > {
321
- const fileDiffs = await getFileDiffsWithBasehead ( baseRef , headLabel , logger ) ;
337
+ const fileDiffs = await getFileDiffsWithBasehead ( branches , logger ) ;
322
338
if ( fileDiffs === undefined ) {
323
339
return undefined ;
324
340
}
@@ -357,14 +373,13 @@ interface FileDiff {
357
373
}
358
374
359
375
async function getFileDiffsWithBasehead (
360
- baseRef : string ,
361
- headLabel : string ,
376
+ branches : PullRequestBranches ,
362
377
logger : Logger ,
363
378
) : Promise < FileDiff [ ] | undefined > {
364
379
const ownerRepo = util . getRequiredEnvParam ( "GITHUB_REPOSITORY" ) . split ( "/" ) ;
365
380
const owner = ownerRepo [ 0 ] ;
366
381
const repo = ownerRepo [ 1 ] ;
367
- const basehead = `${ baseRef } ...${ headLabel } ` ;
382
+ const basehead = `${ branches . base } ...${ branches . head } ` ;
368
383
try {
369
384
const response = await getApiClient ( ) . rest . repos . compareCommitsWithBasehead (
370
385
{
0 commit comments