@@ -3,6 +3,7 @@ import * as path from "path";
33
44import * as core from "@actions/core" ;
55import * as toolrunner from "@actions/exec/lib/toolrunner" ;
6+ import * as github from "@actions/github" ;
67import * as io from "@actions/io" ;
78import { JSONSchemaForNPMPackageJsonFiles } from "@schemastore/package" ;
89
@@ -363,3 +364,48 @@ export const restoreInputs = function () {
363364 }
364365 }
365366} ;
367+
368+ export interface PullRequestBranches {
369+ base : string ;
370+ head : string ;
371+ }
372+
373+ /**
374+ * Returns the base and head branches of the pull request being analyzed.
375+ *
376+ * @returns the base and head branches of the pull request, or undefined if
377+ * we are not analyzing a pull request.
378+ */
379+ export function getPullRequestBranches ( ) : PullRequestBranches | undefined {
380+ const pullRequest = github . context . payload . pull_request ;
381+ if ( pullRequest ) {
382+ return {
383+ base : pullRequest . base . ref ,
384+ // We use the head label instead of the head ref here, because the head
385+ // ref lacks owner information and by itself does not uniquely identify
386+ // the head branch (which may be in a forked repository).
387+ head : pullRequest . head . label ,
388+ } ;
389+ }
390+
391+ // PR analysis under Default Setup does not have the pull_request context,
392+ // but it should set CODE_SCANNING_REF and CODE_SCANNING_BASE_BRANCH.
393+ const codeScanningRef = process . env . CODE_SCANNING_REF ;
394+ const codeScanningBaseBranch = process . env . CODE_SCANNING_BASE_BRANCH ;
395+ if ( codeScanningRef && codeScanningBaseBranch ) {
396+ return {
397+ base : codeScanningBaseBranch ,
398+ // PR analysis under Default Setup analyzes the PR head commit instead of
399+ // the merge commit, so we can use the provided ref directly.
400+ head : codeScanningRef ,
401+ } ;
402+ }
403+ return undefined ;
404+ }
405+
406+ /**
407+ * Returns whether we are analyzing a pull request.
408+ */
409+ export function isAnalyzingPullRequest ( ) : boolean {
410+ return getPullRequestBranches ( ) !== undefined ;
411+ }
0 commit comments