diff --git a/src/codeql.ts b/src/codeql.ts index 9dea038d8..540a2b849 100644 --- a/src/codeql.ts +++ b/src/codeql.ts @@ -1274,6 +1274,15 @@ async function generateCodeScanningConfig( if (Array.isArray(augmentedConfig.packs) && !augmentedConfig.packs.length) { delete augmentedConfig.packs; } + + augmentedConfig["query-filters"] = [ + ...(config.augmentationProperties.defaultQueryFilters || []), + ...(augmentedConfig["query-filters"] || []), + ]; + if (augmentedConfig["query-filters"]?.length === 0) { + delete augmentedConfig["query-filters"]; + } + logger.info( `Writing augmented user configuration file to ${codeScanningConfigFile}`, ); diff --git a/src/config-utils.test.ts b/src/config-utils.test.ts index cc8c6e928..642c833c2 100644 --- a/src/config-utils.test.ts +++ b/src/config-utils.test.ts @@ -809,11 +809,12 @@ const calculateAugmentationMacro = test.macro({ languages: Language[], expectedAugmentationProperties: configUtils.AugmentationProperties, ) => { - const actualAugmentationProperties = configUtils.calculateAugmentation( - rawPacksInput, - rawQueriesInput, - languages, - ); + const actualAugmentationProperties = + await configUtils.calculateAugmentation( + rawPacksInput, + rawQueriesInput, + languages, + ); t.deepEqual(actualAugmentationProperties, expectedAugmentationProperties); }, title: (_, title) => `Calculate Augmentation: ${title}`, @@ -830,6 +831,7 @@ test( queriesInput: undefined, packsInputCombines: false, packsInput: undefined, + defaultQueryFilters: [], } as configUtils.AugmentationProperties, ); @@ -844,6 +846,7 @@ test( queriesInput: [{ uses: "a" }, { uses: "b" }, { uses: "c" }, { uses: "d" }], packsInputCombines: false, packsInput: undefined, + defaultQueryFilters: [], } as configUtils.AugmentationProperties, ); @@ -858,6 +861,7 @@ test( queriesInput: [{ uses: "a" }, { uses: "b" }, { uses: "c" }, { uses: "d" }], packsInputCombines: false, packsInput: undefined, + defaultQueryFilters: [], } as configUtils.AugmentationProperties, ); @@ -872,6 +876,7 @@ test( queriesInput: undefined, packsInputCombines: false, packsInput: ["codeql/a", "codeql/b", "codeql/c", "codeql/d"], + defaultQueryFilters: [], } as configUtils.AugmentationProperties, ); @@ -886,6 +891,7 @@ test( queriesInput: undefined, packsInputCombines: true, packsInput: ["codeql/a", "codeql/b", "codeql/c", "codeql/d"], + defaultQueryFilters: [], } as configUtils.AugmentationProperties, ); @@ -898,7 +904,7 @@ const calculateAugmentationErrorMacro = test.macro({ languages: Language[], expectedError: RegExp | string, ) => { - t.throws( + await t.throwsAsync( () => configUtils.calculateAugmentation( rawPacksInput, diff --git a/src/config-utils.ts b/src/config-utils.ts index 36e55ad35..008a027c0 100644 --- a/src/config-utils.ts +++ b/src/config-utils.ts @@ -173,10 +173,16 @@ export interface AugmentationProperties { * Whether or not the packs input combines with the packs in the config. */ packsInputCombines: boolean; + /** * The packs input from the `with` block of the action declaration */ packsInput?: string[]; + + /** + * Default query filters to apply to the queries in the config. + */ + defaultQueryFilters?: QueryFilter[]; } /** @@ -188,6 +194,7 @@ export const defaultAugmentationProperties: AugmentationProperties = { packsInputCombines: false, packsInput: undefined, queriesInput: undefined, + defaultQueryFilters: [], }; export type Packs = Partial>; @@ -461,7 +468,7 @@ export async function getDefaultConfig({ logger, ); - const augmentationProperties = calculateAugmentation( + const augmentationProperties = await calculateAugmentation( packsInput, queriesInput, languages, @@ -567,7 +574,7 @@ async function loadConfig({ logger, ); - const augmentationProperties = calculateAugmentation( + const augmentationProperties = await calculateAugmentation( packsInput, queriesInput, languages, @@ -617,11 +624,11 @@ async function loadConfig({ * not have exactly one language. */ // exported for testing. -export function calculateAugmentation( +export async function calculateAugmentation( rawPacksInput: string | undefined, rawQueriesInput: string | undefined, languages: Language[], -): AugmentationProperties { +): Promise { const packsInputCombines = shouldCombine(rawPacksInput); const packsInput = parsePacksFromInput( rawPacksInput, @@ -634,11 +641,14 @@ export function calculateAugmentation( queriesInputCombines, ); + const defaultQueryFilters: QueryFilter[] = []; + return { packsInputCombines, packsInput: packsInput?.[languages[0]], queriesInput, queriesInputCombines, + defaultQueryFilters, }; }