Use ActionState for getCodeQLActionRepository

This commit is contained in:
Michael B. Gale
2026-10-06 14:00:12 +01:00
parent 424f4853ad
commit 604755773b
5 changed files with 46 additions and 26 deletions

13
lib/entry-points.js generated
View File

@@ -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],

View File

@@ -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);

View File

@@ -82,7 +82,10 @@ async function getCodeQLBundleDownloadURL(
codeQLBundleName: string,
logger: Logger,
): Promise<string> {
const codeQLActionRepository = getCodeQLActionRepository(logger);
const codeQLActionRepository = getCodeQLActionRepository({
logger,
env: getEnv(),
});
const potentialDownloadSources = [
// This GitHub instance, and this Action.
[apiDetails.url, codeQLActionRepository],

View File

@@ -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");
});

View File

@@ -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);
}