From 60a2a7d623c0d21af7158d2d6e63042cd702f577 Mon Sep 17 00:00:00 2001 From: Chuan-kai Lin Date: Mon, 21 Apr 2025 12:29:54 -0700 Subject: [PATCH] Add isAnalyzingPullRequest() --- src/actions-util.ts | 46 +++++++++++++++++++++++++++++ src/analyze.ts | 14 ++++----- src/diff-informed-analysis-utils.ts | 37 ++--------------------- 3 files changed, 55 insertions(+), 42 deletions(-) diff --git a/src/actions-util.ts b/src/actions-util.ts index cd4e163f3..38a503ed9 100644 --- a/src/actions-util.ts +++ b/src/actions-util.ts @@ -3,6 +3,7 @@ import * as path from "path"; import * as core from "@actions/core"; import * as toolrunner from "@actions/exec/lib/toolrunner"; +import * as github from "@actions/github"; import * as io from "@actions/io"; import { JSONSchemaForNPMPackageJsonFiles } from "@schemastore/package"; @@ -363,3 +364,48 @@ export const restoreInputs = function () { } } }; + +export interface PullRequestBranches { + base: string; + head: string; +} + +/** + * Returns the base and head branches of the pull request being analyzed. + * + * @returns the base and head branches of the pull request, or undefined if + * we are not analyzing a pull request. + */ +export 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; +} + +/** + * Returns whether we are analyzing a pull request. + */ +export function isAnalyzingPullRequest(): boolean { + return getPullRequestBranches() !== undefined; +} diff --git a/src/analyze.ts b/src/analyze.ts index e2894c0d4..0d0d6a9cc 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -6,7 +6,11 @@ import * as io from "@actions/io"; import del from "del"; import * as yaml from "js-yaml"; -import * as actionsUtil from "./actions-util"; +import { + getRequiredInput, + getTemporaryDirectory, + PullRequestBranches, +} from "./actions-util"; import { getApiClient } from "./api-client"; import { setupCppAutobuild } from "./autobuild"; import { CodeQL, getCodeQL } from "./codeql"; @@ -15,7 +19,6 @@ import { getJavaTempDependencyDir } from "./dependency-caching"; import { addDiagnostic, makeDiagnostic } from "./diagnostics"; import { DiffThunkRange, - PullRequestBranches, writeDiffRangesJsonFile, } from "./diff-informed-analysis-utils"; import { EnvVar } from "./environment"; @@ -392,7 +395,7 @@ function getDiffRanges( // uses forward slashes as the path separator, so on Windows we need to // replace any backslashes with forward slashes. const filename = path - .join(actionsUtil.getRequiredInput("checkout_path"), fileDiff.filename) + .join(getRequiredInput("checkout_path"), fileDiff.filename) .replaceAll(path.sep, "/"); if (fileDiff.patch === undefined) { @@ -498,10 +501,7 @@ function writeDiffRangeDataExtensionPack( ranges = [{ path: "", startLine: 0, endLine: 0 }]; } - const diffRangeDir = path.join( - actionsUtil.getTemporaryDirectory(), - "pr-diff-range", - ); + const diffRangeDir = path.join(getTemporaryDirectory(), "pr-diff-range"); // We expect the Actions temporary directory to already exist, so are mainly // using `recursive: true` to avoid errors if the directory already exists, diff --git a/src/diff-informed-analysis-utils.ts b/src/diff-informed-analysis-utils.ts index 2975d8c49..0433ae0bb 100644 --- a/src/diff-informed-analysis-utils.ts +++ b/src/diff-informed-analysis-utils.ts @@ -1,45 +1,12 @@ import * as fs from "fs"; import * as path from "path"; -import * as github from "@actions/github"; - import * as actionsUtil from "./actions-util"; +import type { PullRequestBranches } 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. */ @@ -70,7 +37,7 @@ export async function getDiffInformedAnalysisBranches( return undefined; } - const branches = getPullRequestBranches(); + const branches = actionsUtil.getPullRequestBranches(); if (!branches) { logger.info( "Not performing diff-informed analysis " +