Skip to content

Commit 598d008

Browse files
committed
Make ref optional in parseRemoteFileAddress
1 parent 85c8a8c commit 598d008

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

lib/entry-points.js

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/remote-file.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import test from "ava";
22

33
import { ConfigurationError } from "../util";
44

5-
import { parseRemoteFileAddress, RemoteFileAddress } from "./remote-file";
5+
import {
6+
DEFAULT_CONFIG_FILE_REF,
7+
parseRemoteFileAddress,
8+
RemoteFileAddress,
9+
} from "./remote-file";
610

711
test("expandConfigFileInput accepts full remote addresses", async (t) => {
812
t.deepEqual(parseRemoteFileAddress("owner/repo/path@ref"), {
@@ -23,6 +27,22 @@ test("expandConfigFileInput accepts full remote addresses", async (t) => {
2327
);
2428
});
2529

30+
test("expandConfigFileInput accepts remote address without a ref", async (t) => {
31+
t.deepEqual(parseRemoteFileAddress("owner/repo/path"), {
32+
owner: "owner",
33+
repo: "repo",
34+
path: "path",
35+
ref: DEFAULT_CONFIG_FILE_REF,
36+
} satisfies RemoteFileAddress);
37+
38+
t.deepEqual(parseRemoteFileAddress("owner/repo/path@"), {
39+
owner: "owner",
40+
repo: "repo",
41+
path: "path",
42+
ref: DEFAULT_CONFIG_FILE_REF,
43+
} satisfies RemoteFileAddress);
44+
});
45+
2646
test("expandConfigFileInput rejects invalid values", async (t) => {
2747
t.throws(() => parseRemoteFileAddress(" "), {
2848
instanceOf: ConfigurationError,

src/config/remote-file.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ export interface RemoteFileAddress {
1313
ref: string;
1414
}
1515

16+
/** The default ref to use in configuration file shorthands. */
17+
export const DEFAULT_CONFIG_FILE_REF = "main";
18+
1619
/**
1720
* Attempts to parse `configFile` into an array of `RemoteFileAddress` components.
1821
*
@@ -23,12 +26,16 @@ export interface RemoteFileAddress {
2326
export function parseRemoteFileAddress(configFile: string): RemoteFileAddress {
2427
// retrieve the various parts of the config location, and ensure they're present
2528
const format = new RegExp(
26-
"(?<owner>[^/]+)/(?<repo>[^/]+)/(?<path>[^@]+)@(?<ref>.*)",
29+
"(?<owner>[^/]+)/(?<repo>[^/]+)/(?<path>[^@]+)(@(?<ref>.*))?",
2730
);
2831
const pieces = format.exec(configFile);
2932

30-
// 5 = 4 groups + the whole expression
31-
if (pieces?.groups === undefined || pieces.length < 5) {
33+
// Check that the regular expression matched and that we have at least the required components.
34+
if (
35+
!pieces?.groups?.owner ||
36+
!pieces?.groups?.repo ||
37+
!pieces?.groups?.path
38+
) {
3239
throw new ConfigurationError(
3340
errorMessages.getConfigFileRepoFormatInvalidMessage(configFile),
3441
);
@@ -38,6 +45,6 @@ export function parseRemoteFileAddress(configFile: string): RemoteFileAddress {
3845
owner: pieces.groups.owner,
3946
repo: pieces.groups.repo,
4047
path: pieces.groups.path,
41-
ref: pieces.groups.ref,
48+
ref: pieces.groups.ref || DEFAULT_CONFIG_FILE_REF,
4249
};
4350
}

0 commit comments

Comments
 (0)