Skip to content

Commit 1bda7b0

Browse files
committed
setupDiffInformedQueryRun(): support Default Setup
1 parent 9e15a7f commit 1bda7b0

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/analyze.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import del from "del";
88
import * as yaml from "js-yaml";
99

1010
import * as actionsUtil from "./actions-util";
11-
import { getApiClient } from "./api-client";
11+
import {
12+
getApiClient,
13+
getPullRequestBranchesFromApi,
14+
PullRequestBranches,
15+
} from "./api-client";
1216
import { setupCppAutobuild } from "./autobuild";
1317
import { CodeQL, getCodeQL } from "./codeql";
1418
import * as configUtils from "./config-utils";
@@ -243,22 +247,35 @@ async function finalizeDatabaseCreation(
243247
};
244248
}
245249

246-
interface PullRequestBranches {
247-
baseRef: string;
248-
headLabel: string;
249-
}
250-
251-
function getPullRequestBranches(): PullRequestBranches | undefined {
250+
async function getPullRequestBranches(
251+
logger: Logger,
252+
): Promise<PullRequestBranches | undefined> {
252253
const pullRequest = github.context.payload.pull_request;
253254
if (pullRequest) {
254255
return {
255256
baseRef: pullRequest.base.ref,
256257
headLabel: pullRequest.head.label,
257258
};
258259
}
260+
const pullRequestNumber = getDefaultSetupPullRequestNumber();
261+
if (pullRequestNumber) {
262+
try {
263+
return await getPullRequestBranchesFromApi(pullRequestNumber);
264+
} catch (e) {
265+
logger.warning(
266+
`Cannot identify branches for PR #${pullRequestNumber}: ${e}`,
267+
);
268+
}
269+
}
259270
return undefined;
260271
}
261272

273+
function getDefaultSetupPullRequestNumber(): number | undefined {
274+
const ref = process.env.CODE_SCANNING_REF;
275+
const match = ref?.match(/^refs\/pull\/(\d+)\/head$/);
276+
return match ? parseInt(match[1], 10) : undefined;
277+
}
278+
262279
/**
263280
* Set up the diff-informed analysis feature.
264281
*
@@ -274,7 +291,7 @@ export async function setupDiffInformedQueryRun(
274291
return undefined;
275292
}
276293

277-
const branches = getPullRequestBranches();
294+
const branches = await getPullRequestBranches(logger);
278295
if (!branches) {
279296
return undefined;
280297
}

src/api-client.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,31 @@ export async function deleteActionsCache(id: number) {
246246
});
247247
}
248248

249+
export interface PullRequestBranches {
250+
baseRef: string;
251+
headLabel: string;
252+
}
253+
254+
/** Get the base and head branches for a pull request. */
255+
export async function getPullRequestBranchesFromApi(
256+
pullRequestNumber: number,
257+
): Promise<PullRequestBranches | undefined> {
258+
const repositoryNwo = parseRepositoryNwo(
259+
getRequiredEnvParam("GITHUB_REPOSITORY"),
260+
);
261+
262+
const pullRequest = await getApiClient().rest.pulls.get({
263+
owner: repositoryNwo.owner,
264+
repo: repositoryNwo.repo,
265+
pull_number: pullRequestNumber,
266+
});
267+
268+
return {
269+
baseRef: pullRequest.data.base.ref,
270+
headLabel: pullRequest.data.head.label,
271+
};
272+
}
273+
249274
export function wrapApiConfigurationError(e: unknown) {
250275
if (isHTTPError(e)) {
251276
if (

0 commit comments

Comments
 (0)