Skip to content

Commit fb77176

Browse files
committed
Extract generateCodeScanningConfig()
1 parent d799ff5 commit fb77176

File tree

3 files changed

+73
-62
lines changed

3 files changed

+73
-62
lines changed

src/codeql.ts

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "./actions-util";
1414
import * as api from "./api-client";
1515
import { CliError, wrapCliConfigurationError } from "./cli-errors";
16-
import { type Config } from "./config-utils";
16+
import { generateCodeScanningConfig, type Config } from "./config-utils";
1717
import { DocUrl } from "./doc-url";
1818
import { EnvVar } from "./environment";
1919
import {
@@ -35,7 +35,7 @@ import { ToolsDownloadStatusReport } from "./tools-download";
3535
import { ToolsFeature, isSupportedToolsFeature } from "./tools-features";
3636
import { shouldEnableIndirectTracing } from "./tracer-config";
3737
import * as util from "./util";
38-
import { BuildMode, cloneObject, getErrorMessage } from "./util";
38+
import { BuildMode, getErrorMessage } from "./util";
3939

4040
type Options = Array<string | number | boolean>;
4141

@@ -574,7 +574,7 @@ export async function getCodeQLForCmd(
574574
extraArgs.push(`--trace-process-name=${processName}`);
575575
}
576576

577-
const codeScanningConfigFile = await generateCodeScanningConfig(
577+
const codeScanningConfigFile = await writeCodeScanningConfigFile(
578578
config,
579579
logger,
580580
);
@@ -1217,66 +1217,15 @@ async function runCli(
12171217
* @param config The configuration to use.
12181218
* @returns the path to the generated user configuration file.
12191219
*/
1220-
async function generateCodeScanningConfig(
1220+
async function writeCodeScanningConfigFile(
12211221
config: Config,
12221222
logger: Logger,
12231223
): Promise<string> {
12241224
const codeScanningConfigFile = getGeneratedCodeScanningConfigPath(config);
1225-
1226-
// make a copy so we can modify it
1227-
const augmentedConfig = cloneObject(config.originalUserInput);
1228-
1229-
// Inject the queries from the input
1230-
if (config.augmentationProperties.queriesInput) {
1231-
if (config.augmentationProperties.queriesInputCombines) {
1232-
augmentedConfig.queries = (augmentedConfig.queries || []).concat(
1233-
config.augmentationProperties.queriesInput,
1234-
);
1235-
} else {
1236-
augmentedConfig.queries = config.augmentationProperties.queriesInput;
1237-
}
1238-
}
1239-
if (augmentedConfig.queries?.length === 0) {
1240-
delete augmentedConfig.queries;
1241-
}
1242-
1243-
// Inject the packs from the input
1244-
if (config.augmentationProperties.packsInput) {
1245-
if (config.augmentationProperties.packsInputCombines) {
1246-
// At this point, we already know that this is a single-language analysis
1247-
if (Array.isArray(augmentedConfig.packs)) {
1248-
augmentedConfig.packs = (augmentedConfig.packs || []).concat(
1249-
config.augmentationProperties.packsInput,
1250-
);
1251-
} else if (!augmentedConfig.packs) {
1252-
augmentedConfig.packs = config.augmentationProperties.packsInput;
1253-
} else {
1254-
// At this point, we know there is only one language.
1255-
// If there were more than one language, an error would already have been thrown.
1256-
const language = Object.keys(augmentedConfig.packs)[0];
1257-
augmentedConfig.packs[language] = augmentedConfig.packs[
1258-
language
1259-
].concat(config.augmentationProperties.packsInput);
1260-
}
1261-
} else {
1262-
augmentedConfig.packs = config.augmentationProperties.packsInput;
1263-
}
1264-
}
1265-
if (Array.isArray(augmentedConfig.packs) && !augmentedConfig.packs.length) {
1266-
delete augmentedConfig.packs;
1267-
}
1268-
1269-
augmentedConfig["query-filters"] = [
1270-
// Ordering matters. If the first filter is an inclusion, it implicitly
1271-
// excludes all queries that are not included. If it is an exclusion,
1272-
// it implicitly includes all queries that are not excluded. So user
1273-
// filters (if any) should always be first to preserve intent.
1274-
...(augmentedConfig["query-filters"] || []),
1275-
...(config.augmentationProperties.extraQueryExclusions || []),
1276-
];
1277-
if (augmentedConfig["query-filters"]?.length === 0) {
1278-
delete augmentedConfig["query-filters"];
1279-
}
1225+
const augmentedConfig = generateCodeScanningConfig(
1226+
config.originalUserInput,
1227+
config.augmentationProperties,
1228+
);
12801229

12811230
logger.info(
12821231
`Writing augmented user configuration file to ${codeScanningConfigFile}`,

src/config-utils.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as semver from "semver";
88
import { isAnalyzingPullRequest } from "./actions-util";
99
import * as api from "./api-client";
1010
import { CachingKind, getCachingKind } from "./caching-utils";
11-
import { CodeQL } from "./codeql";
11+
import { type CodeQL } from "./codeql";
1212
import { shouldPerformDiffInformedAnalysis } from "./diff-informed-analysis-utils";
1313
import { Feature, FeatureEnablement } from "./feature-flags";
1414
import { getGitRoot, isAnalyzingDefaultBranch } from "./git-utils";
@@ -26,6 +26,7 @@ import {
2626
ConfigurationError,
2727
BuildMode,
2828
codeQlVersionAtLeast,
29+
cloneObject,
2930
} from "./util";
3031

3132
// Property names from the user-supplied config file.
@@ -1313,3 +1314,64 @@ export async function parseBuildModeInput(
13131314
}
13141315
return input as BuildMode;
13151316
}
1317+
1318+
export function generateCodeScanningConfig(
1319+
originalUserInput: UserConfig,
1320+
augmentationProperties: AugmentationProperties,
1321+
): UserConfig {
1322+
// make a copy so we can modify it
1323+
const augmentedConfig = cloneObject(originalUserInput);
1324+
1325+
// Inject the queries from the input
1326+
if (augmentationProperties.queriesInput) {
1327+
if (augmentationProperties.queriesInputCombines) {
1328+
augmentedConfig.queries = (augmentedConfig.queries || []).concat(
1329+
augmentationProperties.queriesInput,
1330+
);
1331+
} else {
1332+
augmentedConfig.queries = augmentationProperties.queriesInput;
1333+
}
1334+
}
1335+
if (augmentedConfig.queries?.length === 0) {
1336+
delete augmentedConfig.queries;
1337+
}
1338+
1339+
// Inject the packs from the input
1340+
if (augmentationProperties.packsInput) {
1341+
if (augmentationProperties.packsInputCombines) {
1342+
// At this point, we already know that this is a single-language analysis
1343+
if (Array.isArray(augmentedConfig.packs)) {
1344+
augmentedConfig.packs = (augmentedConfig.packs || []).concat(
1345+
augmentationProperties.packsInput,
1346+
);
1347+
} else if (!augmentedConfig.packs) {
1348+
augmentedConfig.packs = augmentationProperties.packsInput;
1349+
} else {
1350+
// At this point, we know there is only one language.
1351+
// If there were more than one language, an error would already have been thrown.
1352+
const language = Object.keys(augmentedConfig.packs)[0];
1353+
augmentedConfig.packs[language] = augmentedConfig.packs[
1354+
language
1355+
].concat(augmentationProperties.packsInput);
1356+
}
1357+
} else {
1358+
augmentedConfig.packs = augmentationProperties.packsInput;
1359+
}
1360+
}
1361+
if (Array.isArray(augmentedConfig.packs) && !augmentedConfig.packs.length) {
1362+
delete augmentedConfig.packs;
1363+
}
1364+
1365+
augmentedConfig["query-filters"] = [
1366+
// Ordering matters. If the first filter is an inclusion, it implicitly
1367+
// excludes all queries that are not included. If it is an exclusion,
1368+
// it implicitly includes all queries that are not excluded. So user
1369+
// filters (if any) should always be first to preserve intent.
1370+
...(augmentedConfig["query-filters"] || []),
1371+
...augmentationProperties.extraQueryExclusions,
1372+
];
1373+
if (augmentedConfig["query-filters"]?.length === 0) {
1374+
delete augmentedConfig["query-filters"];
1375+
}
1376+
return augmentedConfig;
1377+
}

src/trap-caching.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import * as actionsCache from "@actions/cache";
55

66
import * as actionsUtil from "./actions-util";
77
import * as apiClient from "./api-client";
8-
import { CodeQL } from "./codeql";
9-
import type { Config } from "./config-utils";
8+
import { type CodeQL } from "./codeql";
9+
import { type Config } from "./config-utils";
1010
import { DocUrl } from "./doc-url";
1111
import { Feature, FeatureEnablement } from "./feature-flags";
1212
import * as gitUtils from "./git-utils";

0 commit comments

Comments
 (0)