diff --git a/lib/entry-points.js b/lib/entry-points.js index 40188eb2d..48a157aa6 100644 --- a/lib/entry-points.js +++ b/lib/entry-points.js @@ -151429,12 +151429,13 @@ function parseUserConfig(logger, pathInput, contents, validateConfig) { } // src/config/remote-file.ts +var DEFAULT_CONFIG_FILE_REF = "main"; function parseRemoteFileAddress(configFile) { const format = new RegExp( - "(?[^/]+)/(?[^/]+)/(?[^@]+)@(?.*)" + "(?[^/]+)/(?[^/]+)/(?[^@]+)(@(?.*))?" ); const pieces = format.exec(configFile); - if (pieces?.groups === void 0 || pieces.length < 5) { + if (!pieces?.groups?.owner || !pieces?.groups?.repo || !pieces?.groups?.path) { throw new ConfigurationError( getConfigFileRepoFormatInvalidMessage(configFile) ); @@ -151443,7 +151444,7 @@ function parseRemoteFileAddress(configFile) { owner: pieces.groups.owner, repo: pieces.groups.repo, path: pieces.groups.path, - ref: pieces.groups.ref + ref: pieces.groups.ref || DEFAULT_CONFIG_FILE_REF }; } diff --git a/src/config/remote-file.test.ts b/src/config/remote-file.test.ts index fe8589d94..d45c2ec6d 100644 --- a/src/config/remote-file.test.ts +++ b/src/config/remote-file.test.ts @@ -2,7 +2,11 @@ import test from "ava"; import { ConfigurationError } from "../util"; -import { parseRemoteFileAddress, RemoteFileAddress } from "./remote-file"; +import { + DEFAULT_CONFIG_FILE_REF, + parseRemoteFileAddress, + RemoteFileAddress, +} from "./remote-file"; test("expandConfigFileInput accepts full remote addresses", async (t) => { t.deepEqual(parseRemoteFileAddress("owner/repo/path@ref"), { @@ -23,6 +27,22 @@ test("expandConfigFileInput accepts full remote addresses", async (t) => { ); }); +test("expandConfigFileInput accepts remote address without a ref", async (t) => { + t.deepEqual(parseRemoteFileAddress("owner/repo/path"), { + owner: "owner", + repo: "repo", + path: "path", + ref: DEFAULT_CONFIG_FILE_REF, + } satisfies RemoteFileAddress); + + t.deepEqual(parseRemoteFileAddress("owner/repo/path@"), { + owner: "owner", + repo: "repo", + path: "path", + ref: DEFAULT_CONFIG_FILE_REF, + } satisfies RemoteFileAddress); +}); + test("expandConfigFileInput rejects invalid values", async (t) => { t.throws(() => parseRemoteFileAddress(" "), { instanceOf: ConfigurationError, diff --git a/src/config/remote-file.ts b/src/config/remote-file.ts index 66328586f..9ab89ad4a 100644 --- a/src/config/remote-file.ts +++ b/src/config/remote-file.ts @@ -13,6 +13,9 @@ export interface RemoteFileAddress { ref: string; } +/** The default ref to use in configuration file shorthands. */ +export const DEFAULT_CONFIG_FILE_REF = "main"; + /** * Attempts to parse `configFile` into an array of `RemoteFileAddress` components. * @@ -23,12 +26,16 @@ export interface RemoteFileAddress { export function parseRemoteFileAddress(configFile: string): RemoteFileAddress { // retrieve the various parts of the config location, and ensure they're present const format = new RegExp( - "(?[^/]+)/(?[^/]+)/(?[^@]+)@(?.*)", + "(?[^/]+)/(?[^/]+)/(?[^@]+)(@(?.*))?", ); const pieces = format.exec(configFile); - // 5 = 4 groups + the whole expression - if (pieces?.groups === undefined || pieces.length < 5) { + // Check that the regular expression matched and that we have at least the required components. + if ( + !pieces?.groups?.owner || + !pieces?.groups?.repo || + !pieces?.groups?.path + ) { throw new ConfigurationError( errorMessages.getConfigFileRepoFormatInvalidMessage(configFile), ); @@ -38,6 +45,6 @@ export function parseRemoteFileAddress(configFile: string): RemoteFileAddress { owner: pieces.groups.owner, repo: pieces.groups.repo, path: pieces.groups.path, - ref: pieces.groups.ref, + ref: pieces.groups.ref || DEFAULT_CONFIG_FILE_REF, }; }