Skip to content

Commit 113b18e

Browse files
henrymercerCopilot
andcommitted
Parse the config input once
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent e869836 commit 113b18e

8 files changed

Lines changed: 175 additions & 90 deletions

‎lib/entry-points.js‎

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

‎src/config-utils.test.ts‎

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,9 @@ const simpleConfigFileContents = `
473473
queries:
474474
- uses: ./foo_file`;
475475

476+
/** The configuration in `simpleConfigFileContents`, as parsed from the `config` input. */
477+
const simpleConfigInput = yaml.load(simpleConfigFileContents) as UserConfig;
478+
476479
/** A less minimal configuration file. */
477480
const otherConfigFileContents = `
478481
name: my config
@@ -591,7 +594,7 @@ test.serial(
591594
createTestInitConfigInputs({
592595
languagesInput,
593596
configFile: configFilePath,
594-
configInput,
597+
configInput: yaml.load(configInput) as UserConfig,
595598
tempDir,
596599
codeql,
597600
workspacePath: tempDir,
@@ -2343,6 +2346,40 @@ test("applyIncrementalAnalysisSettings: adds exclusions for diff-informed-only r
23432346
]);
23442347
});
23452348

2349+
test("parseConfigInput - returns undefined when the input isn't set", async (t) => {
2350+
await callee(configUtils.parseConfigInput)
2351+
.withArgs(undefined)
2352+
.passes(t.is, undefined);
2353+
});
2354+
2355+
test("parseConfigInput - parses the input as YAML", async (t) => {
2356+
await callee(configUtils.parseConfigInput)
2357+
.withArgs(simpleConfigFileContents)
2358+
.passes(t.deepEqual, {
2359+
name: "my config",
2360+
queries: [{ uses: "./foo_file" }],
2361+
});
2362+
});
2363+
2364+
test("parseConfigInput - throws a ConfigurationError naming the input if it isn't valid YAML", async (t) => {
2365+
await callee(configUtils.parseConfigInput)
2366+
.withArgs("queries: [")
2367+
.throws(t, {
2368+
instanceOf: ConfigurationError,
2369+
message: /^Cannot parse "`config` input"/,
2370+
});
2371+
});
2372+
2373+
test("parseConfigInput - throws a ConfigurationError naming the input if validation fails", async (t) => {
2374+
await callee(configUtils.parseConfigInput)
2375+
.withFeatures([Feature.ValidateDbConfig])
2376+
.withArgs("queries: 1")
2377+
.throws(t, {
2378+
instanceOf: ConfigurationError,
2379+
message: /^The configuration file "`config` input" is invalid/,
2380+
});
2381+
});
2382+
23462383
test("determineUserConfig - empty config when neither input is specified", async (t) => {
23472384
await withTmpDir(async (tmpDir) => {
23482385
const target = callee(configUtils.determineUserConfig)
@@ -2413,7 +2450,7 @@ test("determineUserConfig - loads config input", async (t) => {
24132450
const expectedConfigPath = configUtils.userConfigFromActionPath(tmpDir);
24142451

24152452
const inputs = createTestInitConfigInputs({
2416-
configInput: simpleConfigFileContents,
2453+
configInput: simpleConfigInput,
24172454
configFile: undefined,
24182455
workspacePath: tmpDir,
24192456
});
@@ -2423,17 +2460,15 @@ test("determineUserConfig - loads config input", async (t) => {
24232460

24242461
await target
24252462
// The input source and path of the generated config file should have been logged.
2426-
.logs(
2427-
t,
2428-
"Using config from action input:",
2429-
`Using configuration file: ${expectedConfigPath}`,
2430-
)
2431-
// The message about no configuration input and
2432-
// the warning about both inputs should not have been logged.
2463+
.logs(t, `Using config from action input: ${expectedConfigPath}`)
2464+
// The message about no configuration input and the warning about both inputs should not have
2465+
// been logged. The generated config file isn't loaded, since the `config` input has already
2466+
// been parsed.
24332467
.notLogs(
24342468
t,
24352469
"No configuration file was provided",
24362470
"Both a config file and config input were provided. Ignoring config file.",
2471+
`Using configuration file: ${expectedConfigPath}`,
24372472
)
24382473
// The loaded configuration should match `simpleConfigFileContents`.
24392474
.passes(t.deepEqual, {
@@ -2452,7 +2487,7 @@ test("determineUserConfig - ignores config file input when both specified", asyn
24522487
const expectedConfigPath = configUtils.userConfigFromActionPath(tmpDir);
24532488

24542489
const inputs = createTestInitConfigInputs({
2455-
configInput: simpleConfigFileContents,
2490+
configInput: simpleConfigInput,
24562491
configFile: configFilePath,
24572492
workspacePath: tmpDir,
24582493
});
@@ -2466,10 +2501,14 @@ test("determineUserConfig - ignores config file input when both specified", asyn
24662501
.logs(
24672502
t,
24682503
`Using config from action input: ${expectedConfigPath}`,
2469-
`Using configuration file: ${expectedConfigPath}`,
24702504
"Both a config file and config input were provided. Ignoring config file.",
24712505
)
2472-
.notLogs(t, "No configuration file was provided")
2506+
// The generated config file isn't loaded, since the `config` input has already been parsed.
2507+
.notLogs(
2508+
t,
2509+
"No configuration file was provided",
2510+
`Using configuration file: ${expectedConfigPath}`,
2511+
)
24732512
// The loaded configuration should match `simpleConfigFileContents`.
24742513
.passes(t.deepEqual, {
24752514
name: "my config",
@@ -2481,12 +2520,12 @@ test("determineUserConfig - ignores config file input when both specified", asyn
24812520
});
24822521
});
24832522

2484-
/** A `config` input that we might get from Default Setup. */
2485-
const defaultSetupConfigInput = `
2523+
/** The configuration from a `config` input that we might get from Default Setup. */
2524+
const defaultSetupConfigInput = yaml.load(`
24862525
threat-models: [local, remote]
24872526
default-setup:
24882527
org:
2489-
model-packs: [foo, bar]`;
2528+
model-packs: [foo, bar]`) as UserConfig;
24902529

24912530
test("determineUserConfig - merges configs if FF is enabled in Default Setup", async (t) => {
24922531
await withTmpDir(async (tmpDir) => {
@@ -2555,7 +2594,7 @@ test("determineUserConfig - ignores config file input in Default Setup if FF is
25552594
.withArgs(
25562595
tmpDir,
25572596
createTestInitConfigInputs({
2558-
configInput: simpleConfigFileContents,
2597+
configInput: simpleConfigInput,
25592598
configFile: configFilePath,
25602599
workspacePath: tmpDir,
25612600
}),
@@ -2565,10 +2604,14 @@ test("determineUserConfig - ignores config file input in Default Setup if FF is
25652604
.logs(
25662605
t,
25672606
`Using config from action input: ${expectedConfigPath}`,
2568-
`Using configuration file: ${expectedConfigPath}`,
25692607
"Both a config file and config input were provided. Ignoring config file.",
25702608
)
2571-
.notLogs(t, "No configuration file was provided")
2609+
// The generated config file isn't loaded, since the `config` input has already been parsed.
2610+
.notLogs(
2611+
t,
2612+
"No configuration file was provided",
2613+
`Using configuration file: ${expectedConfigPath}`,
2614+
)
25722615
.passes(t.deepEqual, {
25732616
name: "my config",
25742617
queries: [{ uses: "./foo_file" }],
@@ -2587,7 +2630,7 @@ test("determineUserConfig - ignores config file input outside Default Setup if F
25872630
.withArgs(
25882631
tmpDir,
25892632
createTestInitConfigInputs({
2590-
configInput: simpleConfigFileContents,
2633+
configInput: simpleConfigInput,
25912634
configFile: configFilePath,
25922635
workspacePath: tmpDir,
25932636
}),
@@ -2597,10 +2640,14 @@ test("determineUserConfig - ignores config file input outside Default Setup if F
25972640
.logs(
25982641
t,
25992642
`Using config from action input: ${expectedConfigPath}`,
2600-
`Using configuration file: ${expectedConfigPath}`,
26012643
"Both a config file and config input were provided. Ignoring config file.",
26022644
)
2603-
.notLogs(t, "No configuration file was provided")
2645+
// The generated config file isn't loaded, since the `config` input has already been parsed.
2646+
.notLogs(
2647+
t,
2648+
"No configuration file was provided",
2649+
`Using configuration file: ${expectedConfigPath}`,
2650+
)
26042651
.passes(t.deepEqual, {
26052652
name: "my config",
26062653
queries: [{ uses: "./foo_file" }],

‎src/config-utils.ts‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,8 @@ export interface InitConfigInputs {
337337
packsInput: string | undefined;
338338
configFile: string | undefined;
339339
dbLocation: string | undefined;
340-
configInput: string | undefined;
340+
/** The configuration from the `config` input. */
341+
configInput: UserConfig | undefined;
341342
buildModeInput: string | undefined;
342343
ramInput: string | undefined;
343344
dependencyCachingEnabled: string | undefined;
@@ -1043,6 +1044,29 @@ export async function applyIncrementalAnalysisSettings(
10431044
}
10441045
}
10451046

1047+
/**
1048+
* Parses the `config` input, which contains a configuration in YAML.
1049+
*
1050+
* @returns The configuration, or `undefined` if the input isn't set. Unless configuration validation
1051+
* is enabled, the configuration might not be a mapping.
1052+
* @throws A `ConfigurationError` if the input isn't valid YAML or, when configuration validation is
1053+
* enabled, isn't a valid configuration.
1054+
*/
1055+
export async function parseConfigInput(
1056+
{ logger, features }: ActionState<["Logger", "FeatureFlags"]>,
1057+
configInput: string | undefined,
1058+
): Promise<UserConfig | undefined> {
1059+
if (configInput === undefined) {
1060+
return undefined;
1061+
}
1062+
return parseUserConfig(
1063+
logger,
1064+
"`config` input",
1065+
configInput,
1066+
await features.getValue(Feature.ValidateDbConfig),
1067+
);
1068+
}
1069+
10461070
/**
10471071
* Determines where to load the `UserConfig` for the CLI from and loads it.
10481072
*
@@ -1057,17 +1081,13 @@ export async function determineUserConfig(
10571081
tempDir: string,
10581082
inputs: InitConfigInputs,
10591083
): Promise<UserConfig> {
1060-
const validateConfig = await action.features.getValue(
1061-
Feature.ValidateDbConfig,
1062-
);
1063-
10641084
// We have the following cases:
10651085
// 1. A `config` or `config-file` input is provided, but not both: use the provided one.
10661086
// 2. Both are provided and we are in an advanced workflow: ignore the `config-file` input.
10671087
// 3. Both are provided and we are in Default Setup: the `config` input uses a limited
10681088
// set of options, which are supported by `mergeDefaultSetupAndUserConfigs`,
10691089
// and we merge the two configs.
1070-
if (inputs.configInput) {
1090+
if (inputs.configInput !== undefined) {
10711091
const computedConfigPath = userConfigFromActionPath(tempDir);
10721092

10731093
// Get a function which enables us to determine whether the FF that allows us to
@@ -1084,12 +1104,6 @@ export async function determineUserConfig(
10841104
) {
10851105
// If the FF is enabled and we are in Default Setup, combine the supported
10861106
// configuration file properties and write the result to disk.
1087-
const fromConfigInput = parseUserConfig(
1088-
action.logger,
1089-
"`config` input",
1090-
inputs.configInput,
1091-
validateConfig,
1092-
);
10931107
const fromConfigFile = await loadUserConfig(
10941108
action,
10951109
inputs.configFile,
@@ -1102,7 +1116,7 @@ export async function determineUserConfig(
11021116
// the CLI or other CodeQL Action steps.
11031117
const mergedConfig = mergeDefaultSetupAndUserConfigs(
11041118
action.logger,
1105-
fromConfigInput,
1119+
inputs.configInput,
11061120
fromConfigFile,
11071121
);
11081122
fs.writeFileSync(computedConfigPath, yaml.dump(mergedConfig));
@@ -1122,12 +1136,13 @@ export async function determineUserConfig(
11221136
);
11231137
}
11241138

1125-
// Write the `config` input straight to disk.
1126-
fs.writeFileSync(computedConfigPath, inputs.configInput);
1139+
// Write the `config` input to disk.
1140+
fs.writeFileSync(computedConfigPath, yaml.dump(inputs.configInput));
11271141
inputs.configFile = computedConfigPath;
11281142
action.logger.debug(
11291143
`Using config from action input: ${inputs.configFile}`,
11301144
);
1145+
return inputs.configInput;
11311146
}
11321147
}
11331148

0 commit comments

Comments
 (0)