Skip to content

Commit 1cfbd1f

Browse files
committed
Add isAnalyzingPullRequest()
1 parent de47bf1 commit 1cfbd1f

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

src/actions-util.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path";
33

44
import * as core from "@actions/core";
55
import * as toolrunner from "@actions/exec/lib/toolrunner";
6+
import * as github from "@actions/github";
67
import * as io from "@actions/io";
78
import { 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+
}

src/analyze.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import * as io from "@actions/io";
66
import del from "del";
77
import * as yaml from "js-yaml";
88

9-
import * as actionsUtil from "./actions-util";
9+
import {
10+
getRequiredInput,
11+
getTemporaryDirectory,
12+
PullRequestBranches,
13+
} from "./actions-util";
1014
import { getApiClient } from "./api-client";
1115
import { setupCppAutobuild } from "./autobuild";
1216
import { CodeQL, getCodeQL } from "./codeql";
@@ -15,7 +19,6 @@ import { getJavaTempDependencyDir } from "./dependency-caching";
1519
import { addDiagnostic, makeDiagnostic } from "./diagnostics";
1620
import {
1721
DiffThunkRange,
18-
PullRequestBranches,
1922
writeDiffRangesJsonFile,
2023
} from "./diff-informed-analysis-utils";
2124
import { EnvVar } from "./environment";
@@ -392,7 +395,7 @@ function getDiffRanges(
392395
// uses forward slashes as the path separator, so on Windows we need to
393396
// replace any backslashes with forward slashes.
394397
const filename = path
395-
.join(actionsUtil.getRequiredInput("checkout_path"), fileDiff.filename)
398+
.join(getRequiredInput("checkout_path"), fileDiff.filename)
396399
.replaceAll(path.sep, "/");
397400

398401
if (fileDiff.patch === undefined) {
@@ -498,10 +501,7 @@ function writeDiffRangeDataExtensionPack(
498501
ranges = [{ path: "", startLine: 0, endLine: 0 }];
499502
}
500503

501-
const diffRangeDir = path.join(
502-
actionsUtil.getTemporaryDirectory(),
503-
"pr-diff-range",
504-
);
504+
const diffRangeDir = path.join(getTemporaryDirectory(), "pr-diff-range");
505505

506506
// We expect the Actions temporary directory to already exist, so are mainly
507507
// using `recursive: true` to avoid errors if the directory already exists,

src/diff-informed-analysis-utils.ts

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4-
import * as github from "@actions/github";
5-
64
import * as actionsUtil from "./actions-util";
5+
import type { PullRequestBranches } from "./actions-util";
76
import type { CodeQL } from "./codeql";
87
import { Feature, FeatureEnablement } from "./feature-flags";
98
import { Logger } from "./logging";
109

11-
export interface PullRequestBranches {
12-
base: string;
13-
head: string;
14-
}
15-
16-
function getPullRequestBranches(): PullRequestBranches | undefined {
17-
const pullRequest = github.context.payload.pull_request;
18-
if (pullRequest) {
19-
return {
20-
base: pullRequest.base.ref,
21-
// We use the head label instead of the head ref here, because the head
22-
// ref lacks owner information and by itself does not uniquely identify
23-
// the head branch (which may be in a forked repository).
24-
head: pullRequest.head.label,
25-
};
26-
}
27-
28-
// PR analysis under Default Setup does not have the pull_request context,
29-
// but it should set CODE_SCANNING_REF and CODE_SCANNING_BASE_BRANCH.
30-
const codeScanningRef = process.env.CODE_SCANNING_REF;
31-
const codeScanningBaseBranch = process.env.CODE_SCANNING_BASE_BRANCH;
32-
if (codeScanningRef && codeScanningBaseBranch) {
33-
return {
34-
base: codeScanningBaseBranch,
35-
// PR analysis under Default Setup analyzes the PR head commit instead of
36-
// the merge commit, so we can use the provided ref directly.
37-
head: codeScanningRef,
38-
};
39-
}
40-
return undefined;
41-
}
42-
4310
/**
4411
* Check if the action should perform diff-informed analysis.
4512
*/
@@ -70,7 +37,7 @@ export async function getDiffInformedAnalysisBranches(
7037
return undefined;
7138
}
7239

73-
const branches = getPullRequestBranches();
40+
const branches = actionsUtil.getPullRequestBranches();
7441
if (!branches) {
7542
logger.info(
7643
"Not performing diff-informed analysis " +

0 commit comments

Comments
 (0)