Skip to content

Commit 3102a44

Browse files
authored
Merge pull request #10389 from github/aeisenberg/suites-docs
Tweak the query suites documentation
2 parents dbd5195 + 3c1f67d commit 3102a44

File tree

1 file changed

+79
-12
lines changed

1 file changed

+79
-12
lines changed

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

Lines changed: 79 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``, and ``problem.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,37 @@ 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+
Examples of filtering which queries are run
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 specific queries that the user does not want to run. In general, we
141+
recommend filtering on the query ``id``, which is a unique and stable identifier for
142+
each query. The following three query suite definitions are semantically identical and
143+
filter by the query ``id``:
144+
145+
This filter matches all the queries in the default suite of ``codeql/cpp-queries``, except for the two queries with the excluded identifiers::
146+
147+
- qlpack: codeql/cpp-queries
148+
- exclude:
149+
id:
150+
- cpp/cleartext-transmission
151+
- cpp/cleartext-storage-file
152+
153+
In this example, a separate ``exclude`` instruction is used for each query::
154+
155+
- qlpack: codeql/cpp-queries
156+
- exclude:
157+
id: cpp/cleartext-transmission
158+
- exclude:
159+
id: cpp/cleartext-storage-file
160+
161+
In this example, a regular expression excludes the same two queries. It would also exclude any future queries added to the suite with identifiers that begin: ``cpp/cleartext-``::
162+
163+
- qlpack: codeql/cpp-queries
164+
- exclude:
165+
id:
166+
- /^cpp\/cleartext-.*/
136167

137168
To define a suite that selects all queries in the default suite of the
138169
``codeql/cpp-queries`` CodeQL pack, and then refines them to only include
@@ -150,6 +181,15 @@ and ``@precision high`` from the ``my-custom-queries`` directory, use::
150181
kind: problem
151182
precision: very-high
152183

184+
Note that the following query suite definition behaves differently from the definition above. This definition selects queries that are ``@kind problem`` *or*
185+
are ``@precision very-high``::
186+
187+
- queries: my-custom-queries
188+
- include:
189+
kind: problem
190+
- include:
191+
precision: very-high
192+
153193
To create a suite that selects all queries with ``@kind problem`` from the
154194
``my-custom-queries`` directory except those with ``@problem.severity
155195
recommendation``, use::
@@ -172,6 +212,15 @@ use::
172212
- high
173213
- very-high
174214

215+
.. pull-quote::
216+
217+
Tip
218+
219+
You can use the ``codeql resolve queries /path/to/suite.qls`` command to see
220+
which queries are selected by a query suite definition. For more information,
221+
see the `resolve queries <../../codeql-cli/manual/resolve-queries>`__
222+
reference documentation.
223+
175224
Reusing existing query suite definitions
176225
-----------------------------------------
177226

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

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-
~~~~~~~
260+
Reusability Examples
261+
~~~~~~~~~~~~~~~~~~~~
219262

220263
To use the same conditions in multiple query suite definitions, create a
221264
separate ``.yml`` file containing your instructions. For example, save the
@@ -252,6 +295,30 @@ instruction::
252295
from: my-org/my-custom-instructions
253296
version: ^1.2.3 # optional
254297

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

0 commit comments

Comments
 (0)