From 677254076760bf7b2b138022f3476e0937adf95c Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Wed, 15 Jun 2022 16:23:23 -0700 Subject: [PATCH] Extract query-filters test into a composite action Removes duplicated yaml. Also add some better typings. --- .github/query-filter-test/action.yml | 52 ++++++++++++++++++++++ .github/workflows/query-filters.yml | 66 +++++----------------------- src/analyze.ts | 10 +++-- src/config-utils.ts | 16 +++++++ 4 files changed, 86 insertions(+), 58 deletions(-) create mode 100644 .github/query-filter-test/action.yml diff --git a/.github/query-filter-test/action.yml b/.github/query-filter-test/action.yml new file mode 100644 index 0000000000..2b06a2d55e --- /dev/null +++ b/.github/query-filter-test/action.yml @@ -0,0 +1,52 @@ +name: Query Filter Test +description: Runs a test of query filters using the check sarif action +inputs: + sarif-file: + required: true + description: The sarif file to check + + queries-run: + required: true + description: | + Comma separated list of query ids that should be included in this SARIF file. + + queries-not-run: + required: true + description: | + Comma separated list of query ids that should NOT be included in this SARIF file. + + config-file: + required: true + description: | + The location of the codeql configuration file to use. + + tools: + required: true + description: | + The url of codeql to use. + +runs: + using: composite + steps: + - uses: ./../action/init + with: + languages: javascript + config-file: ./.github/codeql/codeql-config-query-filters1.yml + tools: ${{ inputs.tools }} + db-location: ${{ runner.temp }}/test1 + - uses: ./../action/analyze + with: + output: ${{ runner.temp }}/results + upload-database: false + upload: false + env: + TEST_MODE: "true" + - name: Check Sarif + uses: ./../action/.github/check-sarif + with: + sarif-file: ${{ inputs.sarif-file }} + queries-run: ${{ inputs.queries-run}} + queries-not-run: ${{ inputs.queries-not-run}} + - name: Cleanup after test + shell: bash + run: rm -rf "$RUNNER_TEMP/results" diff --git a/.github/workflows/query-filters.yml b/.github/workflows/query-filters.yml index 848557be46..0a30759b7c 100644 --- a/.github/workflows/query-filters.yml +++ b/.github/workflows/query-filters.yml @@ -27,71 +27,29 @@ jobs: with: version: latest - # Test 1 - - uses: ./../action/init - with: - languages: javascript - config-file: ./.github/codeql/codeql-config-query-filters1.yml - tools: ${{ steps.prepare-test.outputs.tools-url }} - db-location: ${{ runner.temp }}/test1 - - uses: ./../action/analyze - with: - output: ${{ runner.temp }}/results - upload-database: false - upload: false - env: - TEST_MODE: true - - name: Check Sarif - uses: ./../action/.github/check-sarif + - name: Check Sarif for default queries with Single include, Single exclude + uses: ./../action/.github/query-filter-test with: sarif-file: ${{ runner.temp }}/results/javascript.sarif queries-run: js/zipslip queries-not-run: js/path-injection - - name: Cleanup after test - run: rm -rf "$RUNNER_TEMP/results" - - # Test 2 - - uses: ./../action/init - with: - languages: javascript - config-file: ./.github/codeql/codeql-config-query-filters2.yml + config-file: ./.github/codeql/codeql-config-query-filters1.yml tools: ${{ steps.prepare-test.outputs.tools-url }} - db-location: ${{ runner.temp }}/test2 - - uses: ./../action/analyze - with: - output: ${{ runner.temp }}/results - upload-database: false - upload: false - env: - TEST_MODE: true - - name: Check Sarif - uses: ./../action/.github/check-sarif + + - name: Check Sarif for query packs with Single include, Single exclude + uses: ./../action/.github/query-filter-test with: sarif-file: ${{ runner.temp }}/results/javascript.sarif queries-run: js/zipslip,javascript/example/empty-or-one-block queries-not-run: js/path-injection - - name: Cleanup after test - run: rm -rf "$RUNNER_TEMP/results" - - # Test 3 - - uses: ./../action/init - with: - languages: javascript - config-file: ./.github/codeql/codeql-config-query-filters3.yml + config-file: ./.github/codeql/codeql-config-query-filters2.yml tools: ${{ steps.prepare-test.outputs.tools-url }} - db-location: ${{ runner.temp }}/test3 - - uses: ./../action/analyze - with: - output: ${{ runner.temp }}/results - upload-database: false - upload: false - env: - TEST_MODE: true - - name: Check Sarif - uses: ./../action/.github/check-sarif + + - name: Check Sarif for query packs and local queries with Single include, Single exclude + uses: ./../action/.github/query-filter-test with: sarif-file: ${{ runner.temp }}/results/javascript.sarif queries-run: js/zipslip,javascript/example/empty-or-one-block,inrepo-javascript-querypack/show-ifs queries-not-run: js/path-injection,complex-python-querypack/show-ifs,complex-python-querypack/foo/bar/show-ifs - - name: Cleanup after test - run: rm -rf "$RUNNER_TEMP/results" + config-file: ./.github/codeql/codeql-config-query-filters3.yml + tools: ${{ steps.prepare-test.outputs.tools-url }} diff --git a/src/analyze.ts b/src/analyze.ts index 8f1b23a38a..0edb367119 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -402,9 +402,9 @@ export async function runQueries( } // combine the list of packs into a query suite in order to run them all simultaneously. - const querySuite = packs - .map(convertPackToQuerySuiteEntry) - .concat(queryFilters as any[]); + const querySuite = ( + packs.map(convertPackToQuerySuiteEntry) as configUtils.QuerySuiteEntry[] + ).concat(queryFilters); const querySuitePath = `${databasePath}-queries-${type}.qls`; fs.writeFileSync(querySuitePath, yaml.dump(querySuite)); @@ -424,7 +424,9 @@ export async function runQueries( } } -export function convertPackToQuerySuiteEntry(packStr: string) { +export function convertPackToQuerySuiteEntry( + packStr: string +): configUtils.QuerySuitePackEntry { const pack = configUtils.parsePacksSpecification(packStr); return { qlpack: !pack.path ? pack.name : undefined, diff --git a/src/config-utils.ts b/src/config-utils.ts index db883618c0..8027a6e8fb 100644 --- a/src/config-utils.ts +++ b/src/config-utils.ts @@ -65,6 +65,22 @@ interface IncludeQueryFilter { include: Record; } +export type QuerySuitePackEntry = { + version?: string; +} & ( + | { + qlpack: string; + } + | { + from?: string; + query?: string; + queries?: string; + apply?: string; + } +); + +export type QuerySuiteEntry = QuerySuitePackEntry | QueryFilter; + /** * Lists of query files for each language. * Will only contain .ql files and not other kinds of files,