Skip to content

Commit 9ebca4c

Browse files
committed
Move codeql out of InitConfigInputs
1 parent 046ce56 commit 9ebca4c

File tree

4 files changed

+75
-62
lines changed

4 files changed

+75
-62
lines changed

src/config-utils.test.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,6 @@ function createTestInitConfigInputs(
6565
debugDatabaseName: "",
6666
repository: { owner: "github", repo: "example" },
6767
tempDir: "",
68-
codeql: createStubCodeQL({
69-
async betterResolveLanguages() {
70-
return {
71-
extractors: {
72-
html: [{ extractor_root: "" }],
73-
javascript: [{ extractor_root: "" }],
74-
},
75-
};
76-
},
77-
}),
7868
workspacePath: "",
7969
sourceRoot: "",
8070
githubVersion,
@@ -99,6 +89,20 @@ function createConfigFile(inputFileContents: string, tmpDir: string): string {
9989
return configFilePath;
10090
}
10191

92+
// Returns a default CodeQL stub for tests
93+
function createDefaultTestCodeQL() {
94+
return createStubCodeQL({
95+
async betterResolveLanguages() {
96+
return {
97+
extractors: {
98+
html: [{ extractor_root: "" }],
99+
javascript: [{ extractor_root: "" }],
100+
},
101+
};
102+
},
103+
});
104+
}
105+
102106
type GetContentsResponse = { content?: string } | object[];
103107

104108
function mockGetContents(
@@ -153,19 +157,19 @@ test("load empty config", async (t) => {
153157
languagesInput: languages,
154158
repository: { owner: "github", repo: "example" },
155159
tempDir,
156-
codeql,
157160
logger,
158161
}),
162+
codeql,
159163
);
160164

161165
const expectedConfig = await configUtils.initActionState(
162166
createTestInitConfigInputs({
163167
languagesInput: languages,
164168
tempDir,
165-
codeql,
166169
logger,
167170
}),
168171
{},
172+
codeql,
169173
);
170174

171175
t.deepEqual(config, expectedConfig);
@@ -193,9 +197,9 @@ test("load code quality config", async (t) => {
193197
languagesInput: languages,
194198
repository: { owner: "github", repo: "example" },
195199
tempDir,
196-
codeql,
197200
logger,
198201
}),
202+
codeql,
199203
);
200204

201205
// And the config we expect it to result in
@@ -277,10 +281,10 @@ test("initActionState doesn't throw if there are queries configured in the repos
277281
languagesInput: languages,
278282
repository: { owner: "github", repo: "example" },
279283
tempDir,
280-
codeql,
281284
repositoryProperties,
282285
logger,
283286
}),
287+
codeql,
284288
);
285289

286290
t.deepEqual(config, expectedConfig);
@@ -313,10 +317,10 @@ test("loading a saved config produces the same config", async (t) => {
313317
createTestInitConfigInputs({
314318
languagesInput: "javascript,python",
315319
tempDir,
316-
codeql,
317320
workspacePath: tempDir,
318321
logger,
319322
}),
323+
codeql,
320324
);
321325
await configUtils.saveConfig(config1, logger);
322326

@@ -364,10 +368,10 @@ test("loading config with version mismatch throws", async (t) => {
364368
createTestInitConfigInputs({
365369
languagesInput: "javascript,python",
366370
tempDir,
367-
codeql,
368371
workspacePath: tempDir,
369372
logger,
370373
}),
374+
codeql,
371375
);
372376
// initConfig does not save the config, so we do it here.
373377
await configUtils.saveConfig(config, logger);
@@ -394,6 +398,7 @@ test("load input outside of workspace", async (t) => {
394398
tempDir,
395399
workspacePath: tempDir,
396400
}),
401+
createDefaultTestCodeQL(),
397402
);
398403
throw new Error("initConfig did not throw error");
399404
} catch (err) {
@@ -421,6 +426,7 @@ test("load non-local input with invalid repo syntax", async (t) => {
421426
tempDir,
422427
workspacePath: tempDir,
423428
}),
429+
createDefaultTestCodeQL(),
424430
);
425431
throw new Error("initConfig did not throw error");
426432
} catch (err) {
@@ -450,6 +456,7 @@ test("load non-existent input", async (t) => {
450456
tempDir,
451457
workspacePath: tempDir,
452458
}),
459+
createDefaultTestCodeQL(),
453460
);
454461
throw new Error("initConfig did not throw error");
455462
} catch (err) {
@@ -534,9 +541,9 @@ test("load non-empty input", async (t) => {
534541
debugArtifactName: "my-artifact",
535542
debugDatabaseName: "my-db",
536543
tempDir,
537-
codeql,
538544
workspacePath: tempDir,
539545
}),
546+
codeql,
540547
);
541548

542549
// Should exactly equal the object we constructed earlier
@@ -588,9 +595,9 @@ test("Using config input and file together, config input should be used.", async
588595
configFile: configFilePath,
589596
configInput,
590597
tempDir,
591-
codeql,
592598
workspacePath: tempDir,
593599
}),
600+
codeql,
594601
);
595602

596603
t.deepEqual(config.originalUserInput, yaml.load(configInput));
@@ -637,9 +644,9 @@ test("API client used when reading remote config", async (t) => {
637644
languagesInput,
638645
configFile,
639646
tempDir,
640-
codeql,
641647
workspacePath: tempDir,
642648
}),
649+
codeql,
643650
);
644651
t.assert(spyGetContents.called);
645652
});
@@ -658,6 +665,7 @@ test("Remote config handles the case where a directory is provided", async (t) =
658665
tempDir,
659666
workspacePath: tempDir,
660667
}),
668+
createDefaultTestCodeQL(),
661669
);
662670
throw new Error("initConfig did not throw error");
663671
} catch (err) {
@@ -686,6 +694,7 @@ test("Invalid format of remote config handled correctly", async (t) => {
686694
tempDir,
687695
workspacePath: tempDir,
688696
}),
697+
createDefaultTestCodeQL(),
689698
);
690699
throw new Error("initConfig did not throw error");
691700
} catch (err) {
@@ -712,9 +721,9 @@ test("No detected languages", async (t) => {
712721
await configUtils.initConfig(
713722
createTestInitConfigInputs({
714723
tempDir,
715-
codeql,
716724
workspacePath: tempDir,
717725
}),
726+
codeql,
718727
);
719728
throw new Error("initConfig did not throw error");
720729
} catch (err) {
@@ -737,6 +746,7 @@ test("Unknown languages", async (t) => {
737746
tempDir,
738747
workspacePath: tempDir,
739748
}),
749+
createDefaultTestCodeQL(),
740750
);
741751
throw new Error("initConfig did not throw error");
742752
} catch (err) {

src/config-utils.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ export interface InitConfigInputs {
420420
debugDatabaseName: string;
421421
repository: RepositoryNwo;
422422
tempDir: string;
423-
codeql: CodeQL;
424423
workspacePath: string;
425424
sourceRoot: string;
426425
githubVersion: GitHubVersion;
@@ -450,14 +449,14 @@ export async function initActionState(
450449
debugDatabaseName,
451450
repository,
452451
tempDir,
453-
codeql,
454452
sourceRoot,
455453
githubVersion,
456454
features,
457455
repositoryProperties,
458456
logger,
459457
}: InitConfigInputs,
460458
userConfig: UserConfig,
459+
codeql: CodeQL,
461460
): Promise<Config> {
462461
const analysisKinds = await parseAnalysisKinds(analysisKindsInput);
463462

@@ -834,7 +833,10 @@ function hasQueryCustomisation(userConfig: UserConfig): boolean {
834833
* This will parse the config from the user input if present, or generate
835834
* a default config. The parsed config is then stored to a known location.
836835
*/
837-
export async function initConfig(inputs: InitConfigInputs): Promise<Config> {
836+
export async function initConfig(
837+
inputs: InitConfigInputs,
838+
codeql: CodeQL,
839+
): Promise<Config> {
838840
const { logger, tempDir } = inputs;
839841

840842
// if configInput is set, it takes precedence over configFile
@@ -862,7 +864,7 @@ export async function initConfig(inputs: InitConfigInputs): Promise<Config> {
862864
);
863865
}
864866

865-
const config = await initActionState(inputs, userConfig);
867+
const config = await initActionState(inputs, userConfig, codeql);
866868

867869
// If Code Quality analysis is the only enabled analysis kind, then we will initialise
868870
// the database for Code Quality. That entails disabling the default queries and only
@@ -889,7 +891,7 @@ export async function initConfig(inputs: InitConfigInputs): Promise<Config> {
889891
// rest of the config has been populated.
890892
const { overlayDatabaseMode, useOverlayDatabaseCaching } =
891893
await getOverlayDatabaseMode(
892-
inputs.codeql,
894+
codeql,
893895
inputs.repository,
894896
inputs.features,
895897
config.languages,
@@ -908,11 +910,7 @@ export async function initConfig(inputs: InitConfigInputs): Promise<Config> {
908910

909911
if (
910912
overlayDatabaseMode === OverlayDatabaseMode.Overlay ||
911-
(await shouldPerformDiffInformedAnalysis(
912-
inputs.codeql,
913-
inputs.features,
914-
logger,
915-
))
913+
(await shouldPerformDiffInformedAnalysis(codeql, inputs.features, logger))
916914
) {
917915
config.extraQueryExclusions.push({
918916
exclude: { tags: "exclude-from-incremental" },

src/init-action.ts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -297,38 +297,42 @@ async function run() {
297297
);
298298
}
299299

300-
config = await initConfig({
301-
analysisKindsInput: getRequiredInput("analysis-kinds"),
302-
languagesInput: getOptionalInput("languages"),
303-
queriesInput: getOptionalInput("queries"),
304-
qualityQueriesInput,
305-
packsInput: getOptionalInput("packs"),
306-
buildModeInput: getOptionalInput("build-mode"),
307-
configFile,
308-
dbLocation: getOptionalInput("db-location"),
309-
configInput: getOptionalInput("config"),
310-
trapCachingEnabled: getTrapCachingEnabled(),
311-
dependencyCachingEnabled: getDependencyCachingEnabled(),
312-
// Debug mode is enabled if:
313-
// - The `init` Action is passed `debug: true`.
314-
// - Actions step debugging is enabled (e.g. by [enabling debug logging for a rerun](https://docs.github.com/en/actions/managing-workflow-runs/re-running-workflows-and-jobs#re-running-all-the-jobs-in-a-workflow),
315-
// or by setting the `ACTIONS_STEP_DEBUG` secret to `true`).
316-
debugMode: getOptionalInput("debug") === "true" || core.isDebug(),
317-
debugArtifactName:
318-
getOptionalInput("debug-artifact-name") || DEFAULT_DEBUG_ARTIFACT_NAME,
319-
debugDatabaseName:
320-
getOptionalInput("debug-database-name") || DEFAULT_DEBUG_DATABASE_NAME,
321-
repository: repositoryNwo,
322-
tempDir: getTemporaryDirectory(),
300+
config = await initConfig(
301+
{
302+
analysisKindsInput: getRequiredInput("analysis-kinds"),
303+
languagesInput: getOptionalInput("languages"),
304+
queriesInput: getOptionalInput("queries"),
305+
qualityQueriesInput,
306+
packsInput: getOptionalInput("packs"),
307+
buildModeInput: getOptionalInput("build-mode"),
308+
configFile,
309+
dbLocation: getOptionalInput("db-location"),
310+
configInput: getOptionalInput("config"),
311+
trapCachingEnabled: getTrapCachingEnabled(),
312+
dependencyCachingEnabled: getDependencyCachingEnabled(),
313+
// Debug mode is enabled if:
314+
// - The `init` Action is passed `debug: true`.
315+
// - Actions step debugging is enabled (e.g. by [enabling debug logging for a rerun](https://docs.github.com/en/actions/managing-workflow-runs/re-running-workflows-and-jobs#re-running-all-the-jobs-in-a-workflow),
316+
// or by setting the `ACTIONS_STEP_DEBUG` secret to `true`).
317+
debugMode: getOptionalInput("debug") === "true" || core.isDebug(),
318+
debugArtifactName:
319+
getOptionalInput("debug-artifact-name") ||
320+
DEFAULT_DEBUG_ARTIFACT_NAME,
321+
debugDatabaseName:
322+
getOptionalInput("debug-database-name") ||
323+
DEFAULT_DEBUG_DATABASE_NAME,
324+
repository: repositoryNwo,
325+
tempDir: getTemporaryDirectory(),
326+
workspacePath: getRequiredEnvParam("GITHUB_WORKSPACE"),
327+
sourceRoot,
328+
githubVersion: gitHubVersion,
329+
apiDetails,
330+
features,
331+
repositoryProperties,
332+
logger,
333+
},
323334
codeql,
324-
workspacePath: getRequiredEnvParam("GITHUB_WORKSPACE"),
325-
sourceRoot,
326-
githubVersion: gitHubVersion,
327-
apiDetails,
328-
features,
329-
repositoryProperties,
330-
logger,
331-
});
335+
);
332336

333337
await checkInstallPython311(config.languages, codeql);
334338
} catch (unwrappedError) {

src/init.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ export async function initCodeQL(
6060

6161
export async function initConfig(
6262
inputs: configUtils.InitConfigInputs,
63+
codeql: CodeQL,
6364
): Promise<configUtils.Config> {
6465
return await withGroupAsync("Load language configuration", async () => {
65-
return await configUtils.initConfig(inputs);
66+
return await configUtils.initConfig(inputs, codeql);
6667
});
6768
}
6869

0 commit comments

Comments
 (0)