Use ActionState for getCodeQLActionRepository

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

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