Skip to content

Commit 1994ea7

Browse files
committed
Move shouldPerformDiffInformedAnalysis()
1 parent 534bc63 commit 1994ea7

File tree

2 files changed

+64
-59
lines changed

2 files changed

+64
-59
lines changed

src/analyze.ts

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as fs from "fs";
22
import * as path from "path";
33
import { performance } from "perf_hooks";
44

5-
import * as github from "@actions/github";
65
import * as io from "@actions/io";
76
import del from "del";
87
import * as yaml from "js-yaml";
@@ -16,6 +15,8 @@ import { getJavaTempDependencyDir } from "./dependency-caching";
1615
import { addDiagnostic, makeDiagnostic } from "./diagnostics";
1716
import {
1817
DiffThunkRange,
18+
PullRequestBranches,
19+
shouldPerformDiffInformedAnalysis,
1920
writeDiffRangesJsonFile,
2021
} from "./diff-informed-analysis-utils";
2122
import { EnvVar } from "./environment";
@@ -255,64 +256,6 @@ async function finalizeDatabaseCreation(
255256
};
256257
}
257258

258-
interface PullRequestBranches {
259-
base: string;
260-
head: string;
261-
}
262-
263-
function getPullRequestBranches(): PullRequestBranches | undefined {
264-
const pullRequest = github.context.payload.pull_request;
265-
if (pullRequest) {
266-
return {
267-
base: pullRequest.base.ref,
268-
// We use the head label instead of the head ref here, because the head
269-
// ref lacks owner information and by itself does not uniquely identify
270-
// the head branch (which may be in a forked repository).
271-
head: pullRequest.head.label,
272-
};
273-
}
274-
275-
// PR analysis under Default Setup does not have the pull_request context,
276-
// but it should set CODE_SCANNING_REF and CODE_SCANNING_BASE_BRANCH.
277-
const codeScanningRef = process.env.CODE_SCANNING_REF;
278-
const codeScanningBaseBranch = process.env.CODE_SCANNING_BASE_BRANCH;
279-
if (codeScanningRef && codeScanningBaseBranch) {
280-
return {
281-
base: codeScanningBaseBranch,
282-
// PR analysis under Default Setup analyzes the PR head commit instead of
283-
// the merge commit, so we can use the provided ref directly.
284-
head: codeScanningRef,
285-
};
286-
}
287-
return undefined;
288-
}
289-
290-
/**
291-
* Check if the action should perform diff-informed analysis.
292-
*
293-
* @returns If the action should perform diff-informed analysis, return
294-
* the base and head branches that should be used to compute the diff ranges.
295-
* Otherwise return `undefined`.
296-
*/
297-
async function shouldPerformDiffInformedAnalysis(
298-
codeql: CodeQL,
299-
features: FeatureEnablement,
300-
logger: Logger,
301-
): Promise<PullRequestBranches | undefined> {
302-
if (!(await features.getValue(Feature.DiffInformedQueries, codeql))) {
303-
return undefined;
304-
}
305-
306-
const branches = getPullRequestBranches();
307-
if (!branches) {
308-
logger.info(
309-
"Not performing diff-informed analysis " +
310-
"because we are not analyzing a pull request.",
311-
);
312-
}
313-
return branches;
314-
}
315-
316259
/**
317260
* Set up the diff-informed analysis feature.
318261
*

src/diff-informed-analysis-utils.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,71 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4+
import * as github from "@actions/github";
5+
46
import * as actionsUtil from "./actions-util";
7+
import type { CodeQL } from "./codeql";
8+
import { Feature, FeatureEnablement } from "./feature-flags";
59
import { Logger } from "./logging";
610

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+
43+
/**
44+
* Check if the action should perform diff-informed analysis.
45+
*
46+
* @returns If the action should perform diff-informed analysis, return
47+
* the base and head branches that should be used to compute the diff ranges.
48+
* Otherwise return `undefined`.
49+
*/
50+
export async function shouldPerformDiffInformedAnalysis(
51+
codeql: CodeQL,
52+
features: FeatureEnablement,
53+
logger: Logger,
54+
): Promise<PullRequestBranches | undefined> {
55+
if (!(await features.getValue(Feature.DiffInformedQueries, codeql))) {
56+
return undefined;
57+
}
58+
59+
const branches = getPullRequestBranches();
60+
if (!branches) {
61+
logger.info(
62+
"Not performing diff-informed analysis " +
63+
"because we are not analyzing a pull request.",
64+
);
65+
}
66+
return branches;
67+
}
68+
769
export interface DiffThunkRange {
870
path: string;
971
startLine: number;

0 commit comments

Comments
 (0)