diff --git a/lib/entry-points.js b/lib/entry-points.js index e59d461fa..c52cc88f5 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -175748,14 +175748,14 @@ function logPerLanguageBundleFallback({ logger: logger2 }, language, location) { // src/setup/repository.ts var CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action"; -function getCodeQLActionRepository(logger2) { - if (isRunningLocalAction()) { - logger2.info( +function getCodeQLActionRepository(action) { + if (isRunningLocalAction(action.env)) { + action.logger.info( "The CodeQL Action is checked out locally. Using the default CodeQL Action repository." ); return CODEQL_DEFAULT_ACTION_REPOSITORY; } - return getRequiredEnvParam("GITHUB_ACTION_REPOSITORY"); + return action.env.getRequired("GITHUB_ACTION_REPOSITORY" /* GITHUB_ACTION_REPOSITORY */); } // src/tar.ts @@ -176155,7 +176155,10 @@ var CODEQL_BUNDLE_VERSION_ALIAS = ["linked", "latest"]; var CODEQL_NIGHTLY_TOOLS_INPUTS = ["nightly", "nightly-latest"]; var CODEQL_TOOLCACHE_INPUT = "toolcache"; async function getCodeQLBundleDownloadURL(tagName, apiDetails, codeQLBundleName, logger2) { - const codeQLActionRepository = getCodeQLActionRepository(logger2); + const codeQLActionRepository = getCodeQLActionRepository({ + logger: logger2, + env: getEnv() + }); const potentialDownloadSources = [ // This GitHub instance, and this Action. [apiDetails.url, codeQLActionRepository], diff --git a/src/actions-util.ts b/src/actions-util.ts index 62ab42440..6db7146d1 100644 --- a/src/actions-util.ts +++ b/src/actions-util.ts @@ -101,7 +101,7 @@ export function getWorkflowEventName(env: Env = getEnv()) { * Returns whether the current workflow is executing a local copy of the Action, e.g. we're running * a workflow on the codeql-action repo itself. */ -export function isRunningLocalAction(env: Env = getEnv()): boolean { +export function isRunningLocalAction(env: ReadOnlyEnv = getEnv()): boolean { const relativeScriptPath = getRelativeScriptPath(env); return ( relativeScriptPath.startsWith("..") || path.isAbsolute(relativeScriptPath) @@ -113,7 +113,7 @@ export function isRunningLocalAction(env: Env = getEnv()): boolean { * * This can be used to get the Action's name or tell if we're running a local Action. */ -function getRelativeScriptPath(env: Env): string { +function getRelativeScriptPath(env: ReadOnlyEnv): string { const runnerTemp = env.getRequired(ActionsEnvVars.RUNNER_TEMP); const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions"); return path.relative(actionsDirectory, __filename); diff --git a/src/setup-codeql.ts b/src/setup-codeql.ts index 760f6e4aa..54117c95d 100644 --- a/src/setup-codeql.ts +++ b/src/setup-codeql.ts @@ -82,7 +82,10 @@ async function getCodeQLBundleDownloadURL( codeQLBundleName: string, logger: Logger, ): Promise { - const codeQLActionRepository = getCodeQLActionRepository(logger); + const codeQLActionRepository = getCodeQLActionRepository({ + logger, + env: getEnv(), + }); const potentialDownloadSources = [ // This GitHub instance, and this Action. [apiDetails.url, codeQLActionRepository], diff --git a/src/setup/repository.test.ts b/src/setup/repository.test.ts index c3dccbf9c..56eb74786 100644 --- a/src/setup/repository.test.ts +++ b/src/setup/repository.test.ts @@ -4,28 +4,31 @@ import test from "ava"; import * as sinon from "sinon"; import * as actionsUtil from "../actions-util"; -import { getRunnerLogger } from "../logging"; -import { setupTests } from "../testing-utils"; +import { ActionsEnvVars } from "../environment"; +import { callee, setupTests } from "../testing-utils"; import { initializeEnvironment } from "../util"; import { getCodeQLActionRepository } from "./repository"; setupTests(test); -test.serial("getCodeQLActionRepository", (t) => { - const logger = getRunnerLogger(true); - +test.serial("getCodeQLActionRepository", async (t) => { initializeEnvironment("1.2.3"); + const target = callee(getCodeQLActionRepository) + .withArgs() + .withEnv((env) => { + env.set(ActionsEnvVars.RUNNER_TEMP, path.dirname(__dirname)); + }); + // isRunningLocalAction() === true - delete process.env["GITHUB_ACTION_REPOSITORY"]; - process.env["RUNNER_TEMP"] = path.dirname(__dirname); - const repoLocalRunner = getCodeQLActionRepository(logger); - t.deepEqual(repoLocalRunner, "github/codeql-action"); + await target.passes(t.deepEqual, "github/codeql-action"); // isRunningLocalAction() === false sinon.stub(actionsUtil, "isRunningLocalAction").returns(false); - process.env["GITHUB_ACTION_REPOSITORY"] = "xxx/yyy"; - const repoEnv = getCodeQLActionRepository(logger); - t.deepEqual(repoEnv, "xxx/yyy"); + await target + .withEnv((env) => { + env.set(ActionsEnvVars.GITHUB_ACTION_REPOSITORY, "xxx/yyy"); + }) + .passes(t.deepEqual, "xxx/yyy"); }); diff --git a/src/setup/repository.ts b/src/setup/repository.ts index 908cc39ee..c58470630 100644 --- a/src/setup/repository.ts +++ b/src/setup/repository.ts @@ -1,19 +1,30 @@ +import { ActionState } from "../action-common"; import { isRunningLocalAction } from "../actions-util"; -import { Logger } from "../logging"; -import * as util from "../util"; +import { ActionsEnvVars } from "../environment"; +/** The NWO of the standard CodeQL Action repository. */ export const CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action"; -export function getCodeQLActionRepository(logger: Logger): string { - if (isRunningLocalAction()) { +/** + * Retrieves the NWO of the repository that the CodeQL Action is loaded from. + * Typically, this will be `github/codeql-action`, but it may be something else for + * forks of the Action or in environments other than GitHub.com. + * + * If this function detects that we are running the Action locally (e.g. in tests), + * then we default to `CODEQL_DEFAULT_ACTION_REPOSITORY`. + */ +export function getCodeQLActionRepository( + action: ActionState<["ReadOnlyEnv", "Logger"]>, +): string { + if (isRunningLocalAction(action.env)) { // This handles the case where the Action does not come from an Action repository, // e.g. our integration tests which use the Action code from the current checkout. // In these cases, the GITHUB_ACTION_REPOSITORY environment variable is not set. - logger.info( + action.logger.info( "The CodeQL Action is checked out locally. Using the default CodeQL Action repository.", ); return CODEQL_DEFAULT_ACTION_REPOSITORY; } - return util.getRequiredEnvParam("GITHUB_ACTION_REPOSITORY"); + return action.env.getRequired(ActionsEnvVars.GITHUB_ACTION_REPOSITORY); }