From 1994ea768eb3060a022857b2e7d2fea058610022 Mon Sep 17 00:00:00 2001 From: Chuan-kai Lin Date: Thu, 27 Mar 2025 10:17:31 -0700 Subject: [PATCH] Move shouldPerformDiffInformedAnalysis() --- src/analyze.ts | 61 +--------------------------- src/diff-informed-analysis-utils.ts | 62 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 59 deletions(-) diff --git a/src/analyze.ts b/src/analyze.ts index db5c737aa..b11219aa2 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -2,7 +2,6 @@ import * as fs from "fs"; import * as path from "path"; import { performance } from "perf_hooks"; -import * as github from "@actions/github"; import * as io from "@actions/io"; import del from "del"; import * as yaml from "js-yaml"; @@ -16,6 +15,8 @@ import { getJavaTempDependencyDir } from "./dependency-caching"; import { addDiagnostic, makeDiagnostic } from "./diagnostics"; import { DiffThunkRange, + PullRequestBranches, + shouldPerformDiffInformedAnalysis, writeDiffRangesJsonFile, } from "./diff-informed-analysis-utils"; import { EnvVar } from "./environment"; @@ -255,64 +256,6 @@ async function finalizeDatabaseCreation( }; } -interface PullRequestBranches { - base: string; - head: string; -} - -function getPullRequestBranches(): PullRequestBranches | undefined { - const pullRequest = github.context.payload.pull_request; - if (pullRequest) { - return { - base: pullRequest.base.ref, - // We use the head label instead of the head ref here, because the head - // ref lacks owner information and by itself does not uniquely identify - // the head branch (which may be in a forked repository). - head: pullRequest.head.label, - }; - } - - // PR analysis under Default Setup does not have the pull_request context, - // but it should set CODE_SCANNING_REF and CODE_SCANNING_BASE_BRANCH. - const codeScanningRef = process.env.CODE_SCANNING_REF; - const codeScanningBaseBranch = process.env.CODE_SCANNING_BASE_BRANCH; - if (codeScanningRef && codeScanningBaseBranch) { - return { - base: codeScanningBaseBranch, - // PR analysis under Default Setup analyzes the PR head commit instead of - // the merge commit, so we can use the provided ref directly. - head: codeScanningRef, - }; - } - return undefined; -} - -/** - * Check if the action should perform diff-informed analysis. - * - * @returns If the action should perform diff-informed analysis, return - * the base and head branches that should be used to compute the diff ranges. - * Otherwise return `undefined`. - */ -async function shouldPerformDiffInformedAnalysis( - codeql: CodeQL, - features: FeatureEnablement, - logger: Logger, -): Promise { - if (!(await features.getValue(Feature.DiffInformedQueries, codeql))) { - return undefined; - } - - const branches = getPullRequestBranches(); - if (!branches) { - logger.info( - "Not performing diff-informed analysis " + - "because we are not analyzing a pull request.", - ); - } - return branches; -} - /** * Set up the diff-informed analysis feature. * diff --git a/src/diff-informed-analysis-utils.ts b/src/diff-informed-analysis-utils.ts index 22754fb24..611be3622 100644 --- a/src/diff-informed-analysis-utils.ts +++ b/src/diff-informed-analysis-utils.ts @@ -1,9 +1,71 @@ import * as fs from "fs"; import * as path from "path"; +import * as github from "@actions/github"; + import * as actionsUtil from "./actions-util"; +import type { CodeQL } from "./codeql"; +import { Feature, FeatureEnablement } from "./feature-flags"; import { Logger } from "./logging"; +export interface PullRequestBranches { + base: string; + head: string; +} + +function getPullRequestBranches(): PullRequestBranches | undefined { + const pullRequest = github.context.payload.pull_request; + if (pullRequest) { + return { + base: pullRequest.base.ref, + // We use the head label instead of the head ref here, because the head + // ref lacks owner information and by itself does not uniquely identify + // the head branch (which may be in a forked repository). + head: pullRequest.head.label, + }; + } + + // PR analysis under Default Setup does not have the pull_request context, + // but it should set CODE_SCANNING_REF and CODE_SCANNING_BASE_BRANCH. + const codeScanningRef = process.env.CODE_SCANNING_REF; + const codeScanningBaseBranch = process.env.CODE_SCANNING_BASE_BRANCH; + if (codeScanningRef && codeScanningBaseBranch) { + return { + base: codeScanningBaseBranch, + // PR analysis under Default Setup analyzes the PR head commit instead of + // the merge commit, so we can use the provided ref directly. + head: codeScanningRef, + }; + } + return undefined; +} + +/** + * Check if the action should perform diff-informed analysis. + * + * @returns If the action should perform diff-informed analysis, return + * the base and head branches that should be used to compute the diff ranges. + * Otherwise return `undefined`. + */ +export async function shouldPerformDiffInformedAnalysis( + codeql: CodeQL, + features: FeatureEnablement, + logger: Logger, +): Promise { + if (!(await features.getValue(Feature.DiffInformedQueries, codeql))) { + return undefined; + } + + const branches = getPullRequestBranches(); + if (!branches) { + logger.info( + "Not performing diff-informed analysis " + + "because we are not analyzing a pull request.", + ); + } + return branches; +} + export interface DiffThunkRange { path: string; startLine: number;