Skip to content

Commit b4b96a0

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

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-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
@@ -20,6 +20,7 @@ import {
2020
calculateAugmentation,
2121
ExcludeQueryFilter,
2222
generateCodeScanningConfig,
23+
parseUserConfig,
2324
UserConfig,
2425
} from "./config/db-config";
2526
import { shouldPerformDiffInformedAnalysis } from "./diff-informed-analysis-utils";
@@ -920,7 +921,7 @@ function getLocalConfig(configFile: string): UserConfig {
920921
);
921922
}
922923

923-
return yaml.load(fs.readFileSync(configFile, "utf8")) as UserConfig;
924+
return parseUserConfig(configFile, fs.readFileSync(configFile, "utf-8"));
924925
}
925926

926927
async function getRemoteConfig(
@@ -961,9 +962,10 @@ async function getRemoteConfig(
961962
);
962963
}
963964

964-
return yaml.load(
965+
return parseUserConfig(
966+
configFile,
965967
Buffer.from(fileContents, "base64").toString("binary"),
966-
) as UserConfig;
968+
);
967969
}
968970

969971
/**

src/config/db-config.test.ts

Lines changed: 41 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,43 @@ 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+
- foo
403+
404+
`,
405+
);
406+
t.truthy(result);
407+
if (t.truthy(result["paths-ignore"])) {
408+
t.is(result["paths-ignore"].length, 1);
409+
t.is(result["paths-ignore"][0], "some/path");
410+
}
411+
if (t.truthy(result["queries"])) {
412+
t.is(result["queries"].length, 1);
413+
t.is(result["queries"][0], "foo");
414+
}
415+
});
416+
417+
test("parseUserConfig - throws a ConfigurationError if the file is not valid YAML", (t) => {
418+
t.throws(
419+
() =>
420+
dbConfig.parseUserConfig(
421+
"test",
422+
`
423+
paths-ignore:
424+
- "some/path"
425+
queries:
426+
- foo
427+
`,
428+
),
429+
{
430+
instanceOf: ConfigurationError,
431+
},
432+
);
433+
});

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";
@@ -490,3 +491,27 @@ export function generateCodeScanningConfig(
490491

491492
return augmentedConfig;
492493
}
494+
495+
/**
496+
* Attempts to parse `contents` into a `UserConfig` value.
497+
*
498+
* @param pathInput The path to the file where `contents` was obtained from, for use in error messages.
499+
* @param contents The string contents of a YAML file to try and parse as a `UserConfig`.
500+
* @returns The `UserConfig` corresponding to `contents`, if parsing was successful.
501+
* @throws A `ConfigurationError` if parsing failed.
502+
*/
503+
export function parseUserConfig(
504+
pathInput: string,
505+
contents: string,
506+
): UserConfig {
507+
try {
508+
return yaml.load(contents) as UserConfig;
509+
} catch (error) {
510+
if (error instanceof yaml.YAMLException) {
511+
throw new ConfigurationError(
512+
errorMessages.getConfigFileParseErrorMessage(pathInput, error.message),
513+
);
514+
}
515+
throw error;
516+
}
517+
}

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)