Skip to content

Commit e722121

Browse files
authored
Merge pull request github#7618 from github/aeisenberg/getting-started-docs
Docs: Simplify getting started docs
2 parents 5d0f7ef + f712177 commit e722121

File tree

3 files changed

+127
-68
lines changed

3 files changed

+127
-68
lines changed

docs/codeql/codeql-cli/analyzing-databases-with-the-codeql-cli.rst

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Running ``codeql database analyze``
2626

2727
When you run ``database analyze``, it:
2828

29+
#. Optionally downloads any referenced CodeQL packages that are not available locally.
2930
#. Executes one or more query files, by running them over a CodeQL database.
3031
#. Interprets the results, based on certain query metadata, so that alerts can be
3132
displayed in the correct location in the source code.
@@ -50,23 +51,30 @@ You must specify:
5051

5152
You can also specify:
5253

53-
- ``<queries>``: the queries to run over your database. You can
54-
list one or more individual query files, specify a directory that will be
55-
searched recursively for query files, or name a query suite that defines a
56-
particular set of queries. If omitted, the default query suite for the language
57-
of the database being analyzed will be usedFor more information, see the
58-
:ref:`examples <database-analyze-examples>` below.
54+
- ``...<query-specifications>``: a list of queries to run over your database. This
55+
is a list of arguments. Where each argument can be:
56+
57+
- a path to a query file
58+
- a path to a directory containing query files
59+
- a path to a query suite file
60+
- the name of a CodeQL query pack
61+
If omitted, the default query suite for the language
62+
of the database being analyzed will be used. For more information, see the
63+
:ref:`examples <database-analyze-examples>` below.
5964

6065
- ``--sarif-category``: an identifying category for the results. Used when
6166
you want to upload more than one set of results for a commit.
6267
For example, when you use ``github upload-results`` to send results for more than one
6368
language to the GitHub code scanning API. For more information about this use case,
6469
see `Configuring CodeQL CLI in your CI system <https://docs.github.com/en/code-security/secure-coding/using-codeql-code-scanning-with-your-existing-ci-system/configuring-codeql-cli-in-your-ci-system>`__ in the GitHub documentation.
6570

66-
- ``--sarif-add-query-help``: (supported in version 2.7.1 onwards) adds any custom query help written
67-
in markdown to SARIF files (v2.1.0 or later) generated by the analysis. Query help stored in ``.qhelp`` files must be
68-
converted to ``.md`` before running the analysis. For further information,
69-
see ":ref:`Including query help for custom CodeQL queries in SARIF files <including-query-help-for-custom-codeql-queries-in-sarif-files>`."
71+
- ``--sarif-add-query-help``: (supported in version 2.7.1 onwards) adds any custom query help written
72+
in markdown to SARIF files (v2.1.0 or later) generated by the analysis. Query help stored in ``.qhelp`` files must be
73+
converted to ``.md`` before running the analysis. For further information,
74+
see ":ref:`Including query help for custom CodeQL queries in SARIF files <including-query-help-for-custom-codeql-queries-in-sarif-files>`."
75+
76+
- ``--download``: a boolean flag that will allow the CLI to download any referenced CodeQL packages that are not available locally.
77+
If this flag is missing and a referenced CodeQL package is not available locally, the command will fail.
7078

7179
- .. include:: ../reusables/threads-query-execution.rst
7280

@@ -119,17 +127,16 @@ Running a CodeQL pack
119127

120128
.. include:: ../reusables/beta-note-package-management.rst
121129

122-
To run an existing CodeQL query pack from the GitHub Container registry, you need to download it first::
123-
124-
codeql pack download microsoft/[email protected]
130+
To run an existing CodeQL query pack from the GitHub Container registry, you can specify one or more
131+
pack names and use the ``--download`` flag::
125132

126-
Afterwards, you can run the pack on a specific database::
133+
codeql database analyze <database> microsoft/[email protected] github/security-queries --format=sarifv2.1.0 --output=query-results.sarif --download
127134

128-
codeql database analyze <database> microsoft/[email protected] <scope>/<other-pack> --format=sarifv2.1.0 --output=query-results.sarif
129-
130-
The ``analyze`` command above runs the default suite from ``microsoft/coding-standards v1.0.0`` and the latest version of ``scope/other-pack`` on the specified database.
135+
The ``analyze`` command above runs the default suite from ``microsoft/coding-standards v1.0.0`` and the latest version of ``github/secutiry-queries`` on the specified database.
131136
For further information about default suites, see ":ref:`Publishing and using CodeQL packs <publishing-and-using-codeql-packs>`".
132137

138+
For more information about CodeQL packs, see :doc:`About CodeQL Packs <about-codeql-packs>`.
139+
133140
Running query suites
134141
~~~~~~~~~~~~~~~~~~~~
135142

@@ -176,7 +183,7 @@ Integrating a CodeQL pack into a code scanning workflow in GitHub
176183

177184
.. include:: ../reusables/beta-note-package-management.rst
178185

179-
You can use CodeQL query packs in your Code Scanning setup. This allows you to select query packs published by various sources and use them to analyze your code.
186+
You can use CodeQL query packs in your code scanning setup. This allows you to select query packs published by various sources and use them to analyze your code.
180187
For more information, see "`Using CodeQL query packs in the CodeQL action <https://docs.github.com/en/code-security/secure-coding/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-codeql-query-packs/>`_" or "`Downloading and using CodeQL query packs in your CI system <https://docs.github.com/en/code-security/secure-coding/using-codeql-code-scanning-with-your-existing-ci-system/configuring-codeql-cli-in-your-ci-system#downloading-and-using-codeql-query-packs>`_."
181188

182189

@@ -206,28 +213,28 @@ A SARIF results file is generated. Specifying ``--format=sarif-latest`` ensures
206213
that the results are formatted according to the most recent SARIF specification
207214
supported by CodeQL.
208215

209-
.. _including-query-help-for-custom-codeql-queries-in-sarif-files:
216+
.. _including-query-help-for-custom-codeql-queries-in-sarif-files:
210217

211-
Including query help for custom CodeQL queries in SARIF files
218+
Including query help for custom CodeQL queries in SARIF files
212219
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
213220

214221
If you use the CodeQL CLI to to run code scanning analyses on third party CI/CD systems,
215222
you can include the query help for your custom queries in SARIF files generated during an analysis.
216-
After uploading the SARIF file to GitHub, the query help is shown in the code scanning UI for any
217-
alerts generated by the custom queries.
223+
After uploading the SARIF file to GitHub, the query help is shown in the code scanning UI for any
224+
alerts generated by the custom queries.
218225

219-
From CodeQL CLI 2.7.1 onwards, you can include markdown-rendered query help in SARIF files
226+
From CodeQL CLI 2.7.1 onwards, you can include markdown-rendered query help in SARIF files
220227
by providing the ``--sarif-add-query-help`` option when running
221-
``codeql database analyze``.
228+
``codeql database analyze``.
222229
For more information, see `Configuring CodeQL CLI in your CI system <https://docs.github.com/en/code-security/code-scanning/using-codeql-code-scanning-with-your-existing-ci-system/configuring-codeql-cli-in-your-ci-system#analyzing-a-codeql-database>`__
223230
in the GitHub documentation.
224231

225232
You can write query help for custom queries directly in a markdown file and save it alongside the
226-
corresponding query. Alternatively, for consistency with the standard CodeQL queries,
227-
you can write query help in the ``.qhelp`` format. Query help written in ``.qhelp``
233+
corresponding query. Alternatively, for consistency with the standard CodeQL queries,
234+
you can write query help in the ``.qhelp`` format. Query help written in ``.qhelp``
228235
files can't be included in SARIF files, and they can't be processed by code
229-
scanning so must be converted to markdown before running
230-
the analysis. For more information, see ":ref:`Query help files <query-help-files>`"
236+
scanning so must be converted to markdown before running
237+
the analysis. For more information, see ":ref:`Query help files <query-help-files>`"
231238
and ":doc:`Testing query help files <testing-query-help-files>`."
232239

233240
Results

docs/codeql/codeql-cli/getting-started-with-the-codeql-cli.rst

Lines changed: 87 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ to display in GitHub as code scanning alerts, see
3030
`Installing CodeQL CLI in your CI system <https://docs.github.com/en/code-security/secure-coding/using-codeql-code-scanning-with-your-existing-ci-system/installing-codeql-cli-in-your-ci-system>`__
3131
in the GitHub documentation.
3232

33+
.. _download-cli:
34+
3335
1. Download the CodeQL CLI zip package
3436
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3537

@@ -84,6 +86,90 @@ Alternatively, you can download ``codeql.zip``, which contains the CLI for all s
8486
iv. Ensure the check-box labeled **Open "safe" files after downloading**.
8587
is unchecked.
8688

89+
2. Extract the zip archive
90+
~~~~~~~~~~~~~~~~~~~~~~~~~~
91+
92+
For Linux, Windows, and macOS users (version 10.14 "Mojave", and earlier)
93+
simply extract the zip archive.
94+
95+
.. container:: toggle
96+
97+
.. container:: name
98+
99+
**Information for macOS "Catalina" (or newer) users**
100+
101+
.. pull-quote:: macOS "Catalina"
102+
103+
macOS "Catalina", "Big Sur", or "Monterey" users should run the following
104+
commands in the Terminal, where ``${extraction-root}`` is the path to the
105+
directory where you will extract the CodeQL CLI zip archive:
106+
107+
i. ``mv ~/Downloads/codeql*.zip ${extraction-root}``
108+
ii. ``cd ${extraction-root}``
109+
iii. ``/usr/bin/xattr -c codeql*.zip``
110+
iv. ``unzip codeql*.zip``
111+
112+
.. _launch-codeql-cli:
113+
114+
3. Launch ``codeql``
115+
~~~~~~~~~~~~~~~~~~~~
116+
117+
Once extracted, you can run CodeQL processes by running the ``codeql``
118+
executable in a couple of ways:
119+
120+
- By executing ``<extraction-root>/codeql/codeql``, where
121+
``<extraction-root>`` is the folder where you extracted the CodeQL CLI
122+
package.
123+
- By adding ``<extraction-root>/codeql`` to your ``PATH``, so that you
124+
can run the executable as just ``codeql``.
125+
126+
At this point, you can execute CodeQL commands. For a full list of the CodeQL
127+
CLI commands, see the "`CodeQL CLI manual <../manual>`__."
128+
129+
.. pull-quote:: Note
130+
131+
If you add ``codeql`` to your ``PATH``, it can be accessed by CodeQL
132+
for Visual Studio Code to compile and run queries.
133+
For more information about configuring VS Code to access the CodeQL CLI, see
134+
":ref:`Setting up CodeQL in Visual Studio Code <setting-up-codeql-in-visual-studio-code>`."
135+
136+
137+
4. Verify your CodeQL CLI setup
138+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139+
140+
CodeQL CLI has subcommands you can execute to verify that you are correctly set
141+
up to create and analyze databases:
142+
143+
- Run ``codeql resolve languages`` to show which languages are
144+
available for database creation. This will list the languages supported by
145+
default in your CodeQL CLI package.
146+
- (Optional) You can download some ":ref:`CodeQL packs <about-codeql-packs>`" containing pre-compiled queries you would like to run.
147+
To do this, run ``codeql pack download <pack-name> [...pack-name]``, where ``pack-name`` is the name of
148+
the pack you want to download. The core query packs are a good place to start. They are:
149+
150+
- ``codeql/cpp-queries``
151+
- ``codeql/csharp-queries``
152+
- ``codeql/java-queries``
153+
- ``codeql/javascript-queries``
154+
- ``codeql/python-queries``
155+
- ``codeql/ruby-queries``
156+
157+
Alternatively, you can download query packs during the analysis by using the ``--download`` flag of the ``codeql database analyze``
158+
command.
159+
160+
161+
Checking out the CodeQL source code directly
162+
--------------------------------------------
163+
164+
Some users prefer working with CodeQL query sources directly in order to work on or contribute to the Open Source shared queries. In
165+
order to do this, the following steps are recommended. Note that the following instructions are a slightly more complicated alternative
166+
to working with CodeQL packages as explained above.
167+
168+
1. Download the CodeQL CLI zip
169+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
170+
171+
Follow :ref:`step 1 from the previous section<download-cli>`.
172+
87173
2. Create a new CodeQL directory
88174
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89175

@@ -164,45 +250,11 @@ For example, if the path to your copy of the CodeQL repository is
164250
``$HOME/codeql-home/codeql-repo``, then extract the CLI into
165251
``$HOME/codeql-home/``.
166252

167-
.. container:: toggle
168-
169-
.. container:: name
170-
171-
**Information for macOS "Catalina" (or newer) users**
172-
173-
.. pull-quote:: macOS "Catalina"
174-
175-
macOS "Catalina", "Big Sur", or "Monterey" users should run the following
176-
commands in the Terminal, where ``${install_loc}`` is the path to the
177-
directory you created in step 2:
178-
179-
i. ``mv ~/Downloads/codeql*.zip ${install_loc}``
180-
ii. ``cd ${install_loc}``
181-
iii. ``/usr/bin/xattr -c codeql*.zip``
182-
iv. ``unzip codeql*.zip``
183253

184254
5. Launch ``codeql``
185255
~~~~~~~~~~~~~~~~~~~~
186256

187-
Once extracted, you can run CodeQL processes by running the ``codeql``
188-
executable in a couple of ways:
189-
190-
- By executing ``<extraction-root>/codeql/codeql``, where
191-
``<extraction-root>`` is the folder where you extracted the CodeQL CLI
192-
package.
193-
- By adding ``<extraction-root>/codeql`` to your ``PATH``, so that you
194-
can run the executable as just ``codeql``.
195-
196-
At this point, you can execute CodeQL commands. For a full list of the CodeQL
197-
CLI commands, see the "`CodeQL CLI manual <../manual>`__."
198-
199-
.. pull-quote:: Note
200-
201-
If you add ``codeql`` to your ``PATH``, it can be accessed by CodeQL
202-
for Visual Studio Code to compile and run queries.
203-
For more information about configuring VS Code to access the CodeQL CLI, see
204-
":ref:`Setting up CodeQL in Visual Studio Code <setting-up-codeql-in-visual-studio-code>`."
205-
257+
See :ref:`step 3 from the previous section<launch-codeql-cli>`.
206258

207259
6. Verify your CodeQL CLI setup
208260
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -227,7 +279,6 @@ up to create and analyze databases:
227279
- Legacy packs that ensure custom queries and libraries created using older products are
228280
compatible with your version of CodeQL.
229281

230-
231282
.. _using-two-versions-of-the-codeql-cli:
232283

233284
Using two versions of the CodeQL CLI

docs/codeql/codeql-cli/upgrading-codeql-databases.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ Upgrading CodeQL databases
44
==========================
55

66
As the CodeQL CLI tools and queries evolve, you may find that some of your
7-
CodeQL databases become out of date. You must upgrade out-of-date databases
7+
CodeQL databases become out of date. You must upgrade out-of-date databases
88
before you can analyze them.
99

1010
Databases become out of date when:
1111

1212
- For databases created using the CodeQL CLI, the version of CLI tools used to
13-
create them is older than your copy of the CodeQL queries.
13+
create them is older than your copy of the CodeQL queries.
1414
- For databases downloaded from LGTM.com, the CodeQL tools used by LGTM.com to create
1515
that revision of the code are older than your copy of the CodeQL queries.
1616

17-
The ``main`` branch of the CodeQL queries is updated more often than both the
17+
If you have a local checkout of the ``github/codeql`` repository, please note that
18+
the ``main`` branch of the CodeQL queries is updated more often than both the
1819
CLI and LGTM.com, so databases are most likely to become out of date if you use
1920
the queries on this branch. For more information about the different versions of
20-
the CodeQL queries, see ":ref:`Getting started with the CodeQL CLI <local-copy-codeql-queries>`."
21+
the CodeQL queries, see ":ref:`Getting started with the CodeQL CLI <local-copy-codeql-queries>`."
2122

2223
Out-of-date databases must be upgraded before they can be analyzed. This topic
2324
shows you how to upgrade a CodeQL database using the ``database upgrade``

0 commit comments

Comments
 (0)