@@ -8,7 +8,11 @@ import del from "del";
88import * as yaml from "js-yaml" ;
99
1010import * as actionsUtil from "./actions-util" ;
11- import { getApiClient } from "./api-client" ;
11+ import {
12+ getApiClient ,
13+ getPullRequestBranchesFromApi ,
14+ PullRequestBranches ,
15+ } from "./api-client" ;
1216import { setupCppAutobuild } from "./autobuild" ;
1317import { CodeQL , getCodeQL } from "./codeql" ;
1418import * 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 ( / ^ r e f s \/ p u l l \/ ( \d + ) \/ h e a d $ / ) ;
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 }
0 commit comments