From 739fc3a5308acc60ba24edaa59684296d2eae38c Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Mon, 12 Oct 2026 15:27:32 +0100 Subject: [PATCH] Add and use `RepositoryNwoStr` type --- lib/entry-points.js | 13 ++++++++++++- src/repository.ts | 8 ++++++++ src/setup/repository.ts | 19 +++++++++++++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/entry-points.js b/lib/entry-points.js index e4a956372..5e723fded 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -170638,6 +170638,9 @@ function getCurrentInstance(env) { } // src/repository.ts +function isRepositoryNwoStr(val) { + return val.includes("/"); +} function getRepositoryNwo() { return getRepositoryNwoFromEnv("GITHUB_REPOSITORY"); } @@ -175719,7 +175722,15 @@ function getCodeQLActionRepository(action) { ); return CODEQL_DEFAULT_ACTION_REPOSITORY; } - return action.env.getRequired("GITHUB_ACTION_REPOSITORY" /* GITHUB_ACTION_REPOSITORY */); + const actionRepository = action.env.getRequired( + "GITHUB_ACTION_REPOSITORY" /* GITHUB_ACTION_REPOSITORY */ + ); + if (isRepositoryNwoStr(actionRepository)) { + return actionRepository; + } + throw new Error( + `The value of '${"GITHUB_ACTION_REPOSITORY" /* GITHUB_ACTION_REPOSITORY */}' is '${actionRepository}', which does not match the expected 'owner/repo' format.` + ); } var DEFAULT_DOWNLOAD_SOURCE = [ GITHUB_DOTCOM_URL, diff --git a/src/repository.ts b/src/repository.ts index f1e7f369b..652521fe3 100644 --- a/src/repository.ts +++ b/src/repository.ts @@ -6,6 +6,14 @@ export interface RepositoryNwo { repo: string; } +/** An approximation of what a repository NWO string looks like. */ +export type RepositoryNwoStr = `${string}/${string}`; + +/** A basic check that `val` looks like a `RepositoryNwoStr`. */ +export function isRepositoryNwoStr(val: string): val is RepositoryNwoStr { + return val.includes("/"); +} + /** * Get the repository name with owner from the environment variable * `GITHUB_REPOSITORY`. diff --git a/src/setup/repository.ts b/src/setup/repository.ts index e2c370f88..2f68abd30 100644 --- a/src/setup/repository.ts +++ b/src/setup/repository.ts @@ -3,6 +3,7 @@ import { isRunningLocalAction } from "../actions-util"; import * as api from "../api-client"; import { ActionsEnvVars } from "../environment"; import { GITHUB_DOTCOM_URL } from "../github"; +import { isRepositoryNwoStr, type RepositoryNwoStr } from "../repository"; /** The NWO of the standard CodeQL Action repository. */ export const CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action"; @@ -17,7 +18,7 @@ export const CODEQL_DEFAULT_ACTION_REPOSITORY = "github/codeql-action"; */ export function getCodeQLActionRepository( action: ActionState<["ReadOnlyEnv", "Logger"]>, -): string { +): RepositoryNwoStr { 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. @@ -28,11 +29,21 @@ export function getCodeQLActionRepository( return CODEQL_DEFAULT_ACTION_REPOSITORY; } - return action.env.getRequired(ActionsEnvVars.GITHUB_ACTION_REPOSITORY); + const actionRepository = action.env.getRequired( + ActionsEnvVars.GITHUB_ACTION_REPOSITORY, + ); + + if (isRepositoryNwoStr(actionRepository)) { + return actionRepository; + } + + throw new Error( + `The value of '${ActionsEnvVars.GITHUB_ACTION_REPOSITORY}' is '${actionRepository}', which does not match the expected 'owner/repo' format.`, + ); } /**A download source is a pair of strings. */ -export type DownloadSource = [string, string]; +export type DownloadSource = [string, RepositoryNwoStr]; /** GitHub.com, and the canonical Action. */ export const DEFAULT_DOWNLOAD_SOURCE: DownloadSource = [ @@ -77,7 +88,7 @@ export async function getCodeQLAssetDownloadURL( ): Promise { const codeQLActionRepository = getCodeQLActionRepository(action); - const potentialDownloadSources: Array<[string, string]> = [ + const potentialDownloadSources: Array<[string, RepositoryNwoStr]> = [ // This GitHub instance, and this Action. [apiDetails.url, codeQLActionRepository], // This GitHub instance, and the canonical Action.