Skip to content

Commit 3e5446c

Browse files
committed
Introduce PullRequestBranches
1 parent 6adda79 commit 3e5446c

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

src/analyze.ts

+33-18
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,22 @@ 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) {
293+
logger.info(
294+
"Not performing diff-informed analysis " +
295+
"because we are not analyzing a pull request.",
296+
);
274297
return undefined;
275298
}
276299

277-
const baseRef = pull_request.base.ref as string;
278-
const headLabel = pull_request.head.label as string;
279-
280300
return await withGroupAsync(
281301
"Generating diff range extension pack",
282302
async () => {
283-
const diffRanges = await getPullRequestEditedDiffRanges(
284-
baseRef,
285-
headLabel,
286-
logger,
303+
logger.info(
304+
`Calculating diff ranges for ${branches.base}...${branches.head}`,
287305
);
306+
const diffRanges = await getPullRequestEditedDiffRanges(branches, logger);
288307
const packDir = writeDiffRangeDataExtensionPack(logger, diffRanges);
289308
if (packDir === undefined) {
290309
logger.warning(
@@ -304,21 +323,18 @@ export async function setupDiffInformedQueryRun(
304323
/**
305324
* Return the file line ranges that were added or modified in the pull request.
306325
*
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.
310327
* @param logger
311328
* @returns An array of tuples, where each tuple contains the absolute path of a
312329
* file, the start line and the end line (both 1-based and inclusive) of an
313330
* added or modified range in that file. Returns `undefined` if the action was
314331
* not triggered by a pull request or if there was an error.
315332
*/
316333
async function getPullRequestEditedDiffRanges(
317-
baseRef: string,
318-
headLabel: string,
334+
branches: PullRequestBranches,
319335
logger: Logger,
320336
): Promise<DiffThunkRange[] | undefined> {
321-
const fileDiffs = await getFileDiffsWithBasehead(baseRef, headLabel, logger);
337+
const fileDiffs = await getFileDiffsWithBasehead(branches, logger);
322338
if (fileDiffs === undefined) {
323339
return undefined;
324340
}
@@ -357,14 +373,13 @@ interface FileDiff {
357373
}
358374

359375
async function getFileDiffsWithBasehead(
360-
baseRef: string,
361-
headLabel: string,
376+
branches: PullRequestBranches,
362377
logger: Logger,
363378
): Promise<FileDiff[] | undefined> {
364379
const ownerRepo = util.getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
365380
const owner = ownerRepo[0];
366381
const repo = ownerRepo[1];
367-
const basehead = `${baseRef}...${headLabel}`;
382+
const basehead = `${branches.base}...${branches.head}`;
368383
try {
369384
const response = await getApiClient().rest.repos.compareCommitsWithBasehead(
370385
{

0 commit comments

Comments
 (0)