Skip to content

Commit 66df0bc

Browse files
committed
Add and use parseUserConfig
- Throws a `ConfigurationError` if parsing the YAML fails - Add a couple of tests for it
1 parent 70205d3 commit 66df0bc

File tree

5 files changed

+96
-6
lines changed

5 files changed

+96
-6
lines changed

lib/init-action.js

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

src/config-utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
calculateAugmentation,
2020
ExcludeQueryFilter,
2121
generateCodeScanningConfig,
22+
parseUserConfig,
2223
UserConfig,
2324
} from "./config/db-config";
2425
import { shouldPerformDiffInformedAnalysis } from "./diff-informed-analysis-utils";
@@ -905,7 +906,7 @@ function getLocalConfig(configFile: string): UserConfig {
905906
);
906907
}
907908

908-
return yaml.load(fs.readFileSync(configFile, "utf8")) as UserConfig;
909+
return parseUserConfig(configFile, fs.readFileSync(configFile, "utf-8"));
909910
}
910911

911912
async function getRemoteConfig(
@@ -946,9 +947,10 @@ async function getRemoteConfig(
946947
);
947948
}
948949

949-
return yaml.load(
950+
return parseUserConfig(
951+
configFile,
950952
Buffer.from(fileContents, "base64").toString("binary"),
951-
) as UserConfig;
953+
);
952954
}
953955

954956
/**

src/config/db-config.test.ts

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

33
import { RepositoryProperties } from "../feature-flags/properties";
44
import { KnownLanguage, Language } from "../languages";
5-
import { prettyPrintPack } from "../util";
5+
import { ConfigurationError, prettyPrintPack } from "../util";
66

77
import * as dbConfig from "./db-config";
88

@@ -391,3 +391,42 @@ test(
391391
{},
392392
/"a-pack-without-a-scope" is not a valid pack/,
393393
);
394+
395+
test("parseUserConfig - successfully parses valid YAML", (t) => {
396+
const result = dbConfig.parseUserConfig(
397+
"test",
398+
`
399+
paths-ignore:
400+
- "some/path"
401+
queries:
402+
- uses: foo
403+
`,
404+
);
405+
t.truthy(result);
406+
if (t.truthy(result["paths-ignore"])) {
407+
t.is(result["paths-ignore"].length, 1);
408+
t.is(result["paths-ignore"][0], "some/path");
409+
}
410+
if (t.truthy(result["queries"])) {
411+
t.is(result["queries"].length, 1);
412+
t.deepEqual(result["queries"][0], { uses: "foo" });
413+
}
414+
});
415+
416+
test("parseUserConfig - throws a ConfigurationError if the file is not valid YAML", (t) => {
417+
t.throws(
418+
() =>
419+
dbConfig.parseUserConfig(
420+
"test",
421+
`
422+
paths-ignore:
423+
- "some/path"
424+
queries:
425+
- foo
426+
`,
427+
),
428+
{
429+
instanceOf: ConfigurationError,
430+
},
431+
);
432+
});

src/config/db-config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "path";
22

3+
import * as yaml from "js-yaml";
34
import * as semver from "semver";
45

56
import * as errorMessages from "../error-messages";
@@ -474,3 +475,27 @@ export function generateCodeScanningConfig(
474475

475476
return augmentedConfig;
476477
}
478+
479+
/**
480+
* Attempts to parse `contents` into a `UserConfig` value.
481+
*
482+
* @param pathInput The path to the file where `contents` was obtained from, for use in error messages.
483+
* @param contents The string contents of a YAML file to try and parse as a `UserConfig`.
484+
* @returns The `UserConfig` corresponding to `contents`, if parsing was successful.
485+
* @throws A `ConfigurationError` if parsing failed.
486+
*/
487+
export function parseUserConfig(
488+
pathInput: string,
489+
contents: string,
490+
): UserConfig {
491+
try {
492+
return yaml.load(contents) as UserConfig;
493+
} catch (error) {
494+
if (error instanceof yaml.YAMLException) {
495+
throw new ConfigurationError(
496+
errorMessages.getConfigFileParseErrorMessage(pathInput, error.message),
497+
);
498+
}
499+
throw error;
500+
}
501+
}

src/error-messages.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export function getConfigFileDoesNotExistErrorMessage(
1414
return `The configuration file "${configFile}" does not exist`;
1515
}
1616

17+
export function getConfigFileParseErrorMessage(
18+
configFile: string,
19+
message: string,
20+
): string {
21+
return `Cannot parse "${configFile}": ${message}`;
22+
}
23+
1724
export function getConfigFileRepoFormatInvalidMessage(
1825
configFile: string,
1926
): string {

0 commit comments

Comments
 (0)