Refactor parseRemoteFileAddress out of getRemoteConfig

This commit is contained in:
Michael B. Gale
2026-06-23 16:44:31 +01:00
parent 07b6b1eac5
commit 9ef8be7176
3 changed files with 54 additions and 30 deletions

View File

@@ -9,6 +9,7 @@ import { Logger } from "../logging";
import { ConfigurationError } from "../util";
import { parseUserConfig, UserConfig } from "./db-config";
import { parseRemoteFileAddress } from "./remote-file";
/**
* Gets the value that is configured for the configuration file, if any.
@@ -44,25 +45,15 @@ export async function getRemoteConfig(
apiDetails: api.GitHubApiCombinedDetails,
validateConfig: boolean,
): Promise<UserConfig> {
// retrieve the various parts of the config location, and ensure they're present
const format = new RegExp(
"(?<owner>[^/]+)/(?<repo>[^/]+)/(?<path>[^@]+)@(?<ref>.*)",
);
const pieces = format.exec(configFile);
// 5 = 4 groups + the whole expression
if (pieces?.groups === undefined || pieces.length < 5) {
throw new ConfigurationError(
errorMessages.getConfigFileRepoFormatInvalidMessage(configFile),
);
}
const groups = parseRemoteFileAddress(configFile);
const response = await api
.getApiClientWithExternalAuth(apiDetails)
.rest.repos.getContent({
owner: pieces.groups.owner,
repo: pieces.groups.repo,
path: pieces.groups.path,
ref: pieces.groups.ref,
owner: groups.owner,
repo: groups.repo,
path: groups.path,
ref: groups.ref,
});
let fileContents: string;

View File

@@ -1,3 +1,6 @@
import * as errorMessages from "../error-messages";
import { ConfigurationError } from "../util";
/** Represents remote file addresses. */
export interface RemoteFileAddress {
/** The owner of the repository. */
@@ -9,3 +12,27 @@ export interface RemoteFileAddress {
/** The ref of the repository. */
ref: string;
}
/**
* Attempts to parse `configFile` into an array of `RemoteFileAddress` components.
*
* @param configFile The string to try and parse.
* @returns The successful result of executing the regex.
* @throws `ConfigurationError` if the format of `configFile` is not valid.
*/
export function parseRemoteFileAddress(configFile: string) {
// retrieve the various parts of the config location, and ensure they're present
const format = new RegExp(
"(?<owner>[^/]+)/(?<repo>[^/]+)/(?<path>[^@]+)@(?<ref>.*)",
);
const pieces = format.exec(configFile);
// 5 = 4 groups + the whole expression
if (pieces?.groups === undefined || pieces.length < 5) {
throw new ConfigurationError(
errorMessages.getConfigFileRepoFormatInvalidMessage(configFile),
);
}
return pieces.groups;
}