From f88459c0a3ae3430060d6464a121d192dbc2cf1a Mon Sep 17 00:00:00 2001 From: Chuan-kai Lin Date: Wed, 26 Mar 2025 10:18:40 -0700 Subject: [PATCH] Use getRepositoryNwo() --- src/analyze-action.ts | 8 +++----- src/analyze.ts | 9 ++++----- src/api-client.ts | 18 ++++++------------ src/autobuild.ts | 8 +++----- src/init-action-post-helper.ts | 6 ++---- src/init-action-post.ts | 13 +++---------- src/init-action.ts | 6 ++---- src/status-report.ts | 8 ++++---- src/upload-lib.ts | 8 ++------ src/upload-sarif-action.ts | 9 +++------ 10 files changed, 32 insertions(+), 61 deletions(-) diff --git a/src/analyze-action.ts b/src/analyze-action.ts index efcc8a014..3b50a6e4c 100644 --- a/src/analyze-action.ts +++ b/src/analyze-action.ts @@ -26,7 +26,7 @@ import { EnvVar } from "./environment"; import { Features } from "./feature-flags"; import { Language } from "./languages"; import { getActionsLogger, Logger } from "./logging"; -import { parseRepositoryNwo } from "./repository"; +import { getRepositoryNwo } from "./repository"; import * as statusReport from "./status-report"; import { ActionName, @@ -251,9 +251,7 @@ async function run() { logger, ); - const repositoryNwo = parseRepositoryNwo( - util.getRequiredEnvParam("GITHUB_REPOSITORY"), - ); + const repositoryNwo = getRepositoryNwo(); const gitHubVersion = await getGitHubVersion(); @@ -359,7 +357,7 @@ async function run() { actionsUtil.getRequiredInput("wait-for-processing") === "true" ) { await uploadLib.waitForProcessing( - parseRepositoryNwo(util.getRequiredEnvParam("GITHUB_REPOSITORY")), + getRepositoryNwo(), uploadResult.sarifID, getActionsLogger(), ); diff --git a/src/analyze.ts b/src/analyze.ts index 4747f23dd..592c9ab90 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -22,6 +22,7 @@ import { EnvVar } from "./environment"; import { FeatureEnablement, Feature } from "./feature-flags"; import { isScannedLanguage, Language } from "./languages"; import { Logger, withGroupAsync } from "./logging"; +import { getRepositoryNwo } from "./repository"; import { DatabaseCreationTimings, EventReport } from "./status-report"; import { ToolsFeature } from "./tools-features"; import { endTracingForCluster } from "./tracer-config"; @@ -389,15 +390,13 @@ async function getFileDiffsWithBasehead( branches: PullRequestBranches, logger: Logger, ): Promise { - const ownerRepo = util.getRequiredEnvParam("GITHUB_REPOSITORY").split("/"); - const owner = ownerRepo[0]; - const repo = ownerRepo[1]; + const repositoryNwo = getRepositoryNwo(); const basehead = `${branches.base}...${branches.head}`; try { const response = await getApiClient().rest.repos.compareCommitsWithBasehead( { - owner, - repo, + owner: repositoryNwo.owner, + repo: repositoryNwo.repo, basehead, per_page: 1, }, diff --git a/src/api-client.ts b/src/api-client.ts index 697a4cbe6..d84aa6f5b 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -4,7 +4,7 @@ import * as retry from "@octokit/plugin-retry"; import consoleLogLevel from "console-log-level"; import { getActionVersion, getRequiredInput } from "./actions-util"; -import { parseRepositoryNwo } from "./repository"; +import { getRepositoryNwo } from "./repository"; import { ConfigurationError, getRequiredEnvParam, @@ -123,17 +123,15 @@ export async function getGitHubVersion(): Promise { * Get the path of the currently executing workflow relative to the repository root. */ export async function getWorkflowRelativePath(): Promise { - const repo_nwo = getRequiredEnvParam("GITHUB_REPOSITORY").split("/"); - const owner = repo_nwo[0]; - const repo = repo_nwo[1]; + const repo_nwo = getRepositoryNwo(); const run_id = Number(getRequiredEnvParam("GITHUB_RUN_ID")); const apiClient = getApiClient(); const runsResponse = await apiClient.request( "GET /repos/:owner/:repo/actions/runs/:run_id?exclude_pull_requests=true", { - owner, - repo, + owner: repo_nwo.owner, + repo: repo_nwo.repo, run_id, }, ); @@ -218,9 +216,7 @@ export async function listActionsCaches( key: string, ref: string, ): Promise { - const repositoryNwo = parseRepositoryNwo( - getRequiredEnvParam("GITHUB_REPOSITORY"), - ); + const repositoryNwo = getRepositoryNwo(); return await getApiClient().paginate( "GET /repos/{owner}/{repo}/actions/caches", @@ -235,9 +231,7 @@ export async function listActionsCaches( /** Delete an Actions cache item by its ID. */ export async function deleteActionsCache(id: number) { - const repositoryNwo = parseRepositoryNwo( - getRequiredEnvParam("GITHUB_REPOSITORY"), - ); + const repositoryNwo = getRepositoryNwo(); await getApiClient().rest.actions.deleteActionsCacheById({ owner: repositoryNwo.owner, diff --git a/src/autobuild.ts b/src/autobuild.ts index 30dfc46ae..de044a306 100644 --- a/src/autobuild.ts +++ b/src/autobuild.ts @@ -9,9 +9,9 @@ import { EnvVar } from "./environment"; import { Feature, featureConfig, Features } from "./feature-flags"; import { isTracedLanguage, Language } from "./languages"; import { Logger } from "./logging"; -import { parseRepositoryNwo } from "./repository"; +import { getRepositoryNwo } from "./repository"; import { ToolsFeature } from "./tools-features"; -import { BuildMode, getRequiredEnvParam } from "./util"; +import { BuildMode } from "./util"; export async function determineAutobuildLanguages( codeql: CodeQL, @@ -117,9 +117,7 @@ export async function setupCppAutobuild(codeql: CodeQL, logger: Logger) { const envVar = featureConfig[Feature.CppDependencyInstallation].envVar; const featureName = "C++ automatic installation of dependencies"; const gitHubVersion = await getGitHubVersion(); - const repositoryNwo = parseRepositoryNwo( - getRequiredEnvParam("GITHUB_REPOSITORY"), - ); + const repositoryNwo = getRepositoryNwo(); const features = new Features( gitHubVersion, repositoryNwo, diff --git a/src/init-action-post-helper.ts b/src/init-action-post-helper.ts index 78aa40f19..07df95ea3 100644 --- a/src/init-action-post-helper.ts +++ b/src/init-action-post-helper.ts @@ -10,7 +10,7 @@ import { Config } from "./config-utils"; import { EnvVar } from "./environment"; import { Feature, FeatureEnablement } from "./feature-flags"; import { Logger } from "./logging"; -import { RepositoryNwo, parseRepositoryNwo } from "./repository"; +import { RepositoryNwo, getRepositoryNwo } from "./repository"; import { JobStatus } from "./status-report"; import * as uploadLib from "./upload-lib"; import { @@ -255,9 +255,7 @@ async function removeUploadedSarif( const client = getApiClient(); try { - const repositoryNwo = parseRepositoryNwo( - getRequiredEnvParam("GITHUB_REPOSITORY"), - ); + const repositoryNwo = getRepositoryNwo(); // Wait to make sure the analysis is ready for download before requesting it. await delay(5000); diff --git a/src/init-action-post.ts b/src/init-action-post.ts index 29d53cbf0..531134c3d 100644 --- a/src/init-action-post.ts +++ b/src/init-action-post.ts @@ -17,7 +17,7 @@ import * as debugArtifacts from "./debug-artifacts"; import { Features } from "./feature-flags"; import * as initActionPostHelper from "./init-action-post-helper"; import { getActionsLogger } from "./logging"; -import { parseRepositoryNwo } from "./repository"; +import { getRepositoryNwo } from "./repository"; import { StatusReportBase, sendStatusReport, @@ -26,12 +26,7 @@ import { ActionName, getJobStatusDisplayName, } from "./status-report"; -import { - checkDiskUsage, - checkGitHubVersionInRange, - getRequiredEnvParam, - wrapError, -} from "./util"; +import { checkDiskUsage, checkGitHubVersionInRange, wrapError } from "./util"; interface InitPostStatusReport extends StatusReportBase, @@ -52,9 +47,7 @@ async function runWrapper() { const gitHubVersion = await getGitHubVersion(); checkGitHubVersionInRange(gitHubVersion, logger); - const repositoryNwo = parseRepositoryNwo( - getRequiredEnvParam("GITHUB_REPOSITORY"), - ); + const repositoryNwo = getRepositoryNwo(); const features = new Features( gitHubVersion, repositoryNwo, diff --git a/src/init-action.ts b/src/init-action.ts index e534ab9fb..b399d9dc5 100644 --- a/src/init-action.ts +++ b/src/init-action.ts @@ -44,7 +44,7 @@ import { import { Language } from "./languages"; import { getActionsLogger, Logger } from "./logging"; import { OverlayDatabaseMode } from "./overlay-database-utils"; -import { parseRepositoryNwo } from "./repository"; +import { getRepositoryNwo } from "./repository"; import { ToolsSource } from "./setup-codeql"; import { ActionName, @@ -281,9 +281,7 @@ async function run() { checkGitHubVersionInRange(gitHubVersion, logger); checkActionVersion(getActionVersion(), gitHubVersion); - const repositoryNwo = parseRepositoryNwo( - getRequiredEnvParam("GITHUB_REPOSITORY"), - ); + const repositoryNwo = getRepositoryNwo(); const features = new Features( gitHubVersion, diff --git a/src/status-report.ts b/src/status-report.ts index 1cb37830a..832367c61 100644 --- a/src/status-report.ts +++ b/src/status-report.ts @@ -17,6 +17,7 @@ import { DocUrl } from "./doc-url"; import { EnvVar } from "./environment"; import { getRef } from "./git-utils"; import { Logger } from "./logging"; +import { getRepositoryNwo } from "./repository"; import { ConfigurationError, isHTTPError, @@ -393,16 +394,15 @@ export async function sendStatusReport( return; } - const nwo = getRequiredEnvParam("GITHUB_REPOSITORY"); - const [owner, repo] = nwo.split("/"); + const nwo = getRepositoryNwo(); const client = getApiClient(); try { await client.request( "PUT /repos/:owner/:repo/code-scanning/analysis/status", { - owner, - repo, + owner: nwo.owner, + repo: nwo.repo, data: statusReportJSON, }, ); diff --git a/src/upload-lib.ts b/src/upload-lib.ts index bbba97451..d1f65930f 100644 --- a/src/upload-lib.ts +++ b/src/upload-lib.ts @@ -21,7 +21,7 @@ import * as fingerprints from "./fingerprints"; import * as gitUtils from "./git-utils"; import { initCodeQL } from "./init"; import { Logger } from "./logging"; -import { parseRepositoryNwo, RepositoryNwo } from "./repository"; +import { getRepositoryNwo, RepositoryNwo } from "./repository"; import { ToolsFeature } from "./tools-features"; import * as util from "./util"; import { @@ -624,11 +624,7 @@ export async function uploadFiles( logger.debug(`Number of results in upload: ${numResultInSarif}`); // Make the upload - const sarifID = await uploadPayload( - payload, - parseRepositoryNwo(util.getRequiredEnvParam("GITHUB_REPOSITORY")), - logger, - ); + const sarifID = await uploadPayload(payload, getRepositoryNwo(), logger); logger.endGroup(); diff --git a/src/upload-sarif-action.ts b/src/upload-sarif-action.ts index 027d07615..9922ebdc2 100644 --- a/src/upload-sarif-action.ts +++ b/src/upload-sarif-action.ts @@ -5,7 +5,7 @@ import { getActionVersion, getTemporaryDirectory } from "./actions-util"; import { getGitHubVersion } from "./api-client"; import { Features } from "./feature-flags"; import { Logger, getActionsLogger } from "./logging"; -import { parseRepositoryNwo } from "./repository"; +import { getRepositoryNwo } from "./repository"; import { createStatusReportBase, sendStatusReport, @@ -20,7 +20,6 @@ import { checkActionVersion, checkDiskUsage, getErrorMessage, - getRequiredEnvParam, initializeEnvironment, isInTestMode, wrapError, @@ -63,9 +62,7 @@ async function run() { // Make inputs accessible in the `post` step. actionsUtil.persistInputs(); - const repositoryNwo = parseRepositoryNwo( - getRequiredEnvParam("GITHUB_REPOSITORY"), - ); + const repositoryNwo = getRepositoryNwo(); const features = new Features( gitHubVersion, repositoryNwo, @@ -100,7 +97,7 @@ async function run() { core.debug("In test mode. Waiting for processing is disabled."); } else if (actionsUtil.getRequiredInput("wait-for-processing") === "true") { await upload_lib.waitForProcessing( - parseRepositoryNwo(getRequiredEnvParam("GITHUB_REPOSITORY")), + getRepositoryNwo(), uploadResult.sarifID, logger, );