Skip to content

Commit a9202b5

Browse files
committed
Tweak the query suites documentation
- Add examples to filter on `@id` - Add examples that include regexes - Add examples that include both lists and single elements - Add some `import` examples - Remove mention of `eval` since it is not a user-facing instruction
1 parent ed66388 commit a9202b5

File tree

1 file changed

+76
-12
lines changed

1 file changed

+76
-12
lines changed

docs/codeql/codeql-cli/creating-codeql-query-suites.rst

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,10 @@ typically a query metadata property. The value can be:
118118

119119
To match a constraint, a metadata value must match one of the strings or
120120
regular expressions. When there is more than one metadata key, each key must be matched.
121-
For more information about query metadata properties, see ":ref:`Metadata for CodeQL queries
122-
<metadata-for-codeql-queries>`."
121+
The standard metadata keys available to match on are: ``description``, ``id``, ``kind``,
122+
``name``, ``tags``, ``precision``, ``problem.severity``, and ``security-severity``.
123+
For more information about query metadata properties, see
124+
":ref:`Metadata for CodeQL queries <metadata-for-codeql-queries>`."
123125

124126
In addition to metadata tags, the keys in the constraint block can also be:
125127

@@ -131,8 +133,36 @@ In addition to metadata tags, the keys in the constraint block can also be:
131133
- ``tags contain all``---each of the given match strings must match one of the
132134
components of the ``@tags`` metadata property.
133135

134-
Examples
135-
~~~~~~~~
136+
Filtering Examples
137+
~~~~~~~~~~~~~~~~~~
138+
139+
A common use case is to create a query suite that runs all queries in a CodeQL pack,
140+
except for a few that are known to be problematic. The following three query suite
141+
definitions are semantically identical:
142+
143+
Matches all queries in ``codeql/cpp-queries``, except for the two queries with either given ``id``::
144+
145+
- qlpack: codeql/cpp-queries
146+
- exclude:
147+
id:
148+
- cpp/cleartext-transmission
149+
- cpp/cleartext-storage-file
150+
151+
As above, but splits the matching into two ``exclude`` instructions::
152+
153+
- qlpack: codeql/cpp-queries
154+
- exclude:
155+
id: cpp/cleartext-transmission
156+
- exclude:
157+
id: cpp/cleartext-storage-file
158+
159+
As above, but uses a regular expression to match the ``id`` (assuming
160+
that the regular expression matches exactly the previous queries)::
161+
162+
- qlpack: codeql/cpp-queries
163+
- exclude:
164+
id:
165+
- /^cpp\/cleartext-.*/
136166

137167
To define a suite that selects all queries in the default suite of the
138168
``codeql/cpp-queries`` CodeQL pack, and then refines them to only include
@@ -150,6 +180,15 @@ and ``@precision high`` from the ``my-custom-queries`` directory, use::
150180
kind: problem
151181
precision: very-high
152182

183+
Note that the following query suite definition is not equivalent. This definition will select
184+
queries that are ``@kind problem`` *or* are ``@precision very-high``::
185+
186+
- queries: my-custom-queries
187+
- include:
188+
kind: problem
189+
- include:
190+
precision: very-high
191+
153192
To create a suite that selects all queries with ``@kind problem`` from the
154193
``my-custom-queries`` directory except those with ``@problem.severity
155194
recommendation``, use::
@@ -172,6 +211,13 @@ use::
172211
- high
173212
- very-high
174213

214+
.. pull-quote::
215+
216+
Tip
217+
218+
You can use the ``codeql resolve queries /path/to/suite.qls`` command to see
219+
which queries are selected by a query suite definition.
220+
175221
Reusing existing query suite definitions
176222
-----------------------------------------
177223

@@ -208,14 +254,8 @@ Existing query suite definitions can be reused by specifying:
208254
conditions, saved in a ``.yml`` file, to multiple query definitions. For more
209255
information, see the `example <#example>`__ below.
210256

211-
- An ``eval`` instruction---performs the same function as an ``import``
212-
instruction, but takes a full suite definition as the argument, rather than the
213-
path to a ``.qls`` file on disk.
214-
215-
To see what queries are included in a query suite, you can run the ``codeql resolve queries my-suite.qls`` command.
216-
217-
Example
218-
~~~~~~~
257+
Reusability Examples
258+
~~~~~~~~~~~~~~~~~~~~
219259

220260
To use the same conditions in multiple query suite definitions, create a
221261
separate ``.yml`` file containing your instructions. For example, save the
@@ -252,6 +292,30 @@ instruction::
252292
from: my-org/my-custom-instructions
253293
version: ^1.2.3 # optional
254294

295+
A common use case an ``import`` instruction is to apply a further filter to queries from another
296+
query suite. For example, this suite will further filter the ``cpp-security-and-quality`` suite
297+
and exclude ``low`` and ``medium`` precision queries::
298+
299+
- import: codeql-suites/cpp-security-and-quality.qls
300+
from: codeql/cpp-queries
301+
- exclude:
302+
precision:
303+
- low
304+
- medium
305+
306+
If you want to ``include`` queries imported from another suite, the syntax is a little different::
307+
308+
- import: codeql-suites/cpp-security-and-quality.qls
309+
from: codeql/cpp-queries
310+
- exclude: {}
311+
- include:
312+
precision:
313+
- very-high
314+
- high
315+
316+
Notice the empty ``exclude`` instruction. This is required to ensure that the subsequent ``include``
317+
instruction is able to filter queries from the imported suite.
318+
255319
Naming a query suite
256320
--------------------
257321

0 commit comments

Comments
 (0)