Skip to content

Commit 9e15a7f

Browse files
committed
Introduce PullRequestBranches
1 parent 914b568 commit 9e15a7f

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/analyze.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,22 @@ async function finalizeDatabaseCreation(
243243
};
244244
}
245245

246+
interface PullRequestBranches {
247+
baseRef: string;
248+
headLabel: string;
249+
}
250+
251+
function getPullRequestBranches(): PullRequestBranches | undefined {
252+
const pullRequest = github.context.payload.pull_request;
253+
if (pullRequest) {
254+
return {
255+
baseRef: pullRequest.base.ref,
256+
headLabel: pullRequest.head.label,
257+
};
258+
}
259+
return undefined;
260+
}
261+
246262
/**
247263
* Set up the diff-informed analysis feature.
248264
*
@@ -258,22 +274,15 @@ export async function setupDiffInformedQueryRun(
258274
return undefined;
259275
}
260276

261-
const pull_request = github.context.payload.pull_request;
262-
if (!pull_request) {
277+
const branches = getPullRequestBranches();
278+
if (!branches) {
263279
return undefined;
264280
}
265281

266-
const baseRef = pull_request.base.ref as string;
267-
const headLabel = pull_request.head.label as string;
268-
269282
return await withGroupAsync(
270283
"Generating diff range extension pack",
271284
async () => {
272-
const diffRanges = await getPullRequestEditedDiffRanges(
273-
baseRef,
274-
headLabel,
275-
logger,
276-
);
285+
const diffRanges = await getPullRequestEditedDiffRanges(branches, logger);
277286
const packDir = writeDiffRangeDataExtensionPack(logger, diffRanges);
278287
if (packDir === undefined) {
279288
logger.warning(
@@ -293,21 +302,18 @@ export async function setupDiffInformedQueryRun(
293302
/**
294303
* Return the file line ranges that were added or modified in the pull request.
295304
*
296-
* @param baseRef The base branch name, used for calculating the diff range.
297-
* @param headLabel The label that uniquely identifies the head branch across
298-
* repositories, used for calculating the diff range.
305+
* @param branches The base and head branches of the pull request.
299306
* @param logger
300307
* @returns An array of tuples, where each tuple contains the absolute path of a
301308
* file, the start line and the end line (both 1-based and inclusive) of an
302309
* added or modified range in that file. Returns `undefined` if the action was
303310
* not triggered by a pull request or if there was an error.
304311
*/
305312
async function getPullRequestEditedDiffRanges(
306-
baseRef: string,
307-
headLabel: string,
313+
branches: PullRequestBranches,
308314
logger: Logger,
309315
): Promise<DiffThunkRange[] | undefined> {
310-
const fileDiffs = await getFileDiffsWithBasehead(baseRef, headLabel, logger);
316+
const fileDiffs = await getFileDiffsWithBasehead(branches, logger);
311317
if (fileDiffs === undefined) {
312318
return undefined;
313319
}
@@ -346,14 +352,13 @@ interface FileDiff {
346352
}
347353

348354
async function getFileDiffsWithBasehead(
349-
baseRef: string,
350-
headLabel: string,
355+
branches: PullRequestBranches,
351356
logger: Logger,
352357
): Promise<FileDiff[] | undefined> {
353358
const ownerRepo = util.getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
354359
const owner = ownerRepo[0];
355360
const repo = ownerRepo[1];
356-
const basehead = `${baseRef}...${headLabel}`;
361+
const basehead = `${branches.baseRef}...${branches.headLabel}`;
357362
try {
358363
const response = await getApiClient().rest.repos.compareCommitsWithBasehead(
359364
{

0 commit comments

Comments
 (0)