Skip to content

Commit 6772540

Browse files
committed
Extract query-filters test into a composite action
Removes duplicated yaml. Also add some better typings.
1 parent 428caf0 commit 6772540

File tree

4 files changed

+86
-58
lines changed

4 files changed

+86
-58
lines changed

.github/query-filter-test/action.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Query Filter Test
2+
description: Runs a test of query filters using the check sarif action
3+
inputs:
4+
sarif-file:
5+
required: true
6+
description: The sarif file to check
7+
8+
queries-run:
9+
required: true
10+
description: |
11+
Comma separated list of query ids that should be included in this SARIF file.
12+
13+
queries-not-run:
14+
required: true
15+
description: |
16+
Comma separated list of query ids that should NOT be included in this SARIF file.
17+
18+
config-file:
19+
required: true
20+
description: |
21+
The location of the codeql configuration file to use.
22+
23+
tools:
24+
required: true
25+
description: |
26+
The url of codeql to use.
27+
28+
runs:
29+
using: composite
30+
steps:
31+
- uses: ./../action/init
32+
with:
33+
languages: javascript
34+
config-file: ./.github/codeql/codeql-config-query-filters1.yml
35+
tools: ${{ inputs.tools }}
36+
db-location: ${{ runner.temp }}/test1
37+
- uses: ./../action/analyze
38+
with:
39+
output: ${{ runner.temp }}/results
40+
upload-database: false
41+
upload: false
42+
env:
43+
TEST_MODE: "true"
44+
- name: Check Sarif
45+
uses: ./../action/.github/check-sarif
46+
with:
47+
sarif-file: ${{ inputs.sarif-file }}
48+
queries-run: ${{ inputs.queries-run}}
49+
queries-not-run: ${{ inputs.queries-not-run}}
50+
- name: Cleanup after test
51+
shell: bash
52+
run: rm -rf "$RUNNER_TEMP/results"

.github/workflows/query-filters.yml

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,71 +27,29 @@ jobs:
2727
with:
2828
version: latest
2929

30-
# Test 1
31-
- uses: ./../action/init
32-
with:
33-
languages: javascript
34-
config-file: ./.github/codeql/codeql-config-query-filters1.yml
35-
tools: ${{ steps.prepare-test.outputs.tools-url }}
36-
db-location: ${{ runner.temp }}/test1
37-
- uses: ./../action/analyze
38-
with:
39-
output: ${{ runner.temp }}/results
40-
upload-database: false
41-
upload: false
42-
env:
43-
TEST_MODE: true
44-
- name: Check Sarif
45-
uses: ./../action/.github/check-sarif
30+
- name: Check Sarif for default queries with Single include, Single exclude
31+
uses: ./../action/.github/query-filter-test
4632
with:
4733
sarif-file: ${{ runner.temp }}/results/javascript.sarif
4834
queries-run: js/zipslip
4935
queries-not-run: js/path-injection
50-
- name: Cleanup after test
51-
run: rm -rf "$RUNNER_TEMP/results"
52-
53-
# Test 2
54-
- uses: ./../action/init
55-
with:
56-
languages: javascript
57-
config-file: ./.github/codeql/codeql-config-query-filters2.yml
36+
config-file: ./.github/codeql/codeql-config-query-filters1.yml
5837
tools: ${{ steps.prepare-test.outputs.tools-url }}
59-
db-location: ${{ runner.temp }}/test2
60-
- uses: ./../action/analyze
61-
with:
62-
output: ${{ runner.temp }}/results
63-
upload-database: false
64-
upload: false
65-
env:
66-
TEST_MODE: true
67-
- name: Check Sarif
68-
uses: ./../action/.github/check-sarif
38+
39+
- name: Check Sarif for query packs with Single include, Single exclude
40+
uses: ./../action/.github/query-filter-test
6941
with:
7042
sarif-file: ${{ runner.temp }}/results/javascript.sarif
7143
queries-run: js/zipslip,javascript/example/empty-or-one-block
7244
queries-not-run: js/path-injection
73-
- name: Cleanup after test
74-
run: rm -rf "$RUNNER_TEMP/results"
75-
76-
# Test 3
77-
- uses: ./../action/init
78-
with:
79-
languages: javascript
80-
config-file: ./.github/codeql/codeql-config-query-filters3.yml
45+
config-file: ./.github/codeql/codeql-config-query-filters2.yml
8146
tools: ${{ steps.prepare-test.outputs.tools-url }}
82-
db-location: ${{ runner.temp }}/test3
83-
- uses: ./../action/analyze
84-
with:
85-
output: ${{ runner.temp }}/results
86-
upload-database: false
87-
upload: false
88-
env:
89-
TEST_MODE: true
90-
- name: Check Sarif
91-
uses: ./../action/.github/check-sarif
47+
48+
- name: Check Sarif for query packs and local queries with Single include, Single exclude
49+
uses: ./../action/.github/query-filter-test
9250
with:
9351
sarif-file: ${{ runner.temp }}/results/javascript.sarif
9452
queries-run: js/zipslip,javascript/example/empty-or-one-block,inrepo-javascript-querypack/show-ifs
9553
queries-not-run: js/path-injection,complex-python-querypack/show-ifs,complex-python-querypack/foo/bar/show-ifs
96-
- name: Cleanup after test
97-
run: rm -rf "$RUNNER_TEMP/results"
54+
config-file: ./.github/codeql/codeql-config-query-filters3.yml
55+
tools: ${{ steps.prepare-test.outputs.tools-url }}

src/analyze.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,9 @@ export async function runQueries(
402402
}
403403

404404
// combine the list of packs into a query suite in order to run them all simultaneously.
405-
const querySuite = packs
406-
.map(convertPackToQuerySuiteEntry)
407-
.concat(queryFilters as any[]);
405+
const querySuite = (
406+
packs.map(convertPackToQuerySuiteEntry) as configUtils.QuerySuiteEntry[]
407+
).concat(queryFilters);
408408

409409
const querySuitePath = `${databasePath}-queries-${type}.qls`;
410410
fs.writeFileSync(querySuitePath, yaml.dump(querySuite));
@@ -424,7 +424,9 @@ export async function runQueries(
424424
}
425425
}
426426

427-
export function convertPackToQuerySuiteEntry(packStr: string) {
427+
export function convertPackToQuerySuiteEntry(
428+
packStr: string
429+
): configUtils.QuerySuitePackEntry {
428430
const pack = configUtils.parsePacksSpecification(packStr);
429431
return {
430432
qlpack: !pack.path ? pack.name : undefined,

src/config-utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ interface IncludeQueryFilter {
6565
include: Record<string, string[] | string>;
6666
}
6767

68+
export type QuerySuitePackEntry = {
69+
version?: string;
70+
} & (
71+
| {
72+
qlpack: string;
73+
}
74+
| {
75+
from?: string;
76+
query?: string;
77+
queries?: string;
78+
apply?: string;
79+
}
80+
);
81+
82+
export type QuerySuiteEntry = QuerySuitePackEntry | QueryFilter;
83+
6884
/**
6985
* Lists of query files for each language.
7086
* Will only contain .ql files and not other kinds of files,

0 commit comments

Comments
 (0)