Skip to content

Commit 8d21d04

Browse files
committed
Introduce PullRequestBranches
1 parent 6adda79 commit 8d21d04

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/analyze.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,25 @@ async function finalizeDatabaseCreation(
254254
};
255255
}
256256

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+
257276
/**
258277
* Set up the diff-informed analysis feature.
259278
*
@@ -269,22 +288,15 @@ export async function setupDiffInformedQueryRun(
269288
return undefined;
270289
}
271290

272-
const pull_request = github.context.payload.pull_request;
273-
if (!pull_request) {
291+
const branches = getPullRequestBranches();
292+
if (!branches) {
274293
return undefined;
275294
}
276295

277-
const baseRef = pull_request.base.ref as string;
278-
const headLabel = pull_request.head.label as string;
279-
280296
return await withGroupAsync(
281297
"Generating diff range extension pack",
282298
async () => {
283-
const diffRanges = await getPullRequestEditedDiffRanges(
284-
baseRef,
285-
headLabel,
286-
logger,
287-
);
299+
const diffRanges = await getPullRequestEditedDiffRanges(branches, logger);
288300
const packDir = writeDiffRangeDataExtensionPack(logger, diffRanges);
289301
if (packDir === undefined) {
290302
logger.warning(
@@ -304,21 +316,18 @@ export async function setupDiffInformedQueryRun(
304316
/**
305317
* Return the file line ranges that were added or modified in the pull request.
306318
*
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.
310320
* @param logger
311321
* @returns An array of tuples, where each tuple contains the absolute path of a
312322
* file, the start line and the end line (both 1-based and inclusive) of an
313323
* added or modified range in that file. Returns `undefined` if the action was
314324
* not triggered by a pull request or if there was an error.
315325
*/
316326
async function getPullRequestEditedDiffRanges(
317-
baseRef: string,
318-
headLabel: string,
327+
branches: PullRequestBranches,
319328
logger: Logger,
320329
): Promise<DiffThunkRange[] | undefined> {
321-
const fileDiffs = await getFileDiffsWithBasehead(baseRef, headLabel, logger);
330+
const fileDiffs = await getFileDiffsWithBasehead(branches, logger);
322331
if (fileDiffs === undefined) {
323332
return undefined;
324333
}
@@ -357,14 +366,13 @@ interface FileDiff {
357366
}
358367

359368
async function getFileDiffsWithBasehead(
360-
baseRef: string,
361-
headLabel: string,
369+
branches: PullRequestBranches,
362370
logger: Logger,
363371
): Promise<FileDiff[] | undefined> {
364372
const ownerRepo = util.getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
365373
const owner = ownerRepo[0];
366374
const repo = ownerRepo[1];
367-
const basehead = `${baseRef}...${headLabel}`;
375+
const basehead = `${branches.base}...${branches.head}`;
368376
try {
369377
const response = await getApiClient().rest.repos.compareCommitsWithBasehead(
370378
{

0 commit comments

Comments
 (0)