Add and use parseUserConfig

- Throws a `ConfigurationError` if parsing the YAML fails
- Add a couple of tests for it
This commit is contained in:
Michael B. Gale
2025-10-12 12:52:53 +01:00
parent 70205d3d12
commit 66df0bc515
5 changed files with 96 additions and 6 deletions
+19 -2
View File
@@ -85319,6 +85319,7 @@ function renamed(from, to) {
var load = loader.load;
var loadAll = loader.loadAll;
var dump = dumper.dump;
var YAMLException = exception;
var safeLoad = renamed("safeLoad", "load");
var safeLoadAll = renamed("safeLoadAll", "loadAll");
var safeDump = renamed("safeDump", "dump");
@@ -86310,6 +86311,9 @@ function getConfigFileOutsideWorkspaceErrorMessage(configFile) {
function getConfigFileDoesNotExistErrorMessage(configFile) {
return `The configuration file "${configFile}" does not exist`;
}
function getConfigFileParseErrorMessage(configFile, message) {
return `Cannot parse "${configFile}": ${message}`;
}
function getConfigFileRepoFormatInvalidMessage(configFile) {
let error2 = `The configuration file "${configFile}" is not a supported remote file reference.`;
error2 += " Expected format <owner>/<repository>/<file-path>@<ref>";
@@ -86601,6 +86605,18 @@ function generateCodeScanningConfig(logger, originalUserInput, augmentationPrope
}
return augmentedConfig;
}
function parseUserConfig(pathInput, contents) {
try {
return load(contents);
} catch (error2) {
if (error2 instanceof YAMLException) {
throw new ConfigurationError(
getConfigFileParseErrorMessage(pathInput, error2.message)
);
}
throw error2;
}
}
// src/feature-flags.ts
var fs7 = __toESM(require("fs"));
@@ -88033,7 +88049,7 @@ function getLocalConfig(configFile) {
getConfigFileDoesNotExistErrorMessage(configFile)
);
}
return load(fs9.readFileSync(configFile, "utf8"));
return parseUserConfig(configFile, fs9.readFileSync(configFile, "utf-8"));
}
async function getRemoteConfig(configFile, apiDetails) {
const format = new RegExp(
@@ -88063,7 +88079,8 @@ async function getRemoteConfig(configFile, apiDetails) {
getConfigFileFormatInvalidMessage(configFile)
);
}
return load(
return parseUserConfig(
configFile,
Buffer.from(fileContents, "base64").toString("binary")
);
}