Skip to content

Commit 91c96ad

Browse files
authored
Merge pull request github#4550 from github/query-help-tests
[docs] Add new process to generate query help for help site
2 parents 19d334c + 65a048b commit 91c96ad

File tree

13 files changed

+469
-2
lines changed

13 files changed

+469
-2
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Generate CodeQL query help documentation using Sphinx
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- 'rc/**'
8+
- 'lgtm.com'
9+
pull_request:
10+
paths:
11+
- 'docs/codeql/query-help/**'
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Clone github/codeql
18+
uses: actions/checkout@v2
19+
with:
20+
path: codeql
21+
- name: Clone github/codeql-go
22+
uses: actions/checkout@v2
23+
with:
24+
repository: 'github/codeql-go'
25+
path: codeql-go
26+
- name: Set up Python 3.8
27+
uses: actions/setup-python@v2
28+
with:
29+
python-version: 3.8
30+
- name: Download CodeQL CLI
31+
uses: dsaltares/fetch-gh-release-asset@aa37ae5c44d3c9820bc12fe675e8670ecd93bd1c
32+
with:
33+
repo: "github/codeql-cli-binaries"
34+
version: "latest"
35+
file: "codeql-linux64.zip"
36+
token: ${{ secrets.GITHUB_TOKEN }}
37+
- name: Unzip CodeQL CLI
38+
run: unzip -d codeql-cli codeql-linux64.zip
39+
- name: Set up query help docs folder
40+
run: |
41+
cp -r codeql/docs/codeql/** .
42+
- name: Query help to markdown
43+
run: |
44+
PATH="$PATH:codeql-cli/codeql" python codeql/docs/codeql/query-help-markdown.py
45+
- name: Run Sphinx for query help
46+
uses: ammaraskar/sphinx-action@master
47+
with:
48+
docs-folder: "query-help/"
49+
pre-build-command: "python -m pip install --upgrade recommonmark"
50+
build-command: "sphinx-build -b dirhtml . _build"
51+
- name: Upload HTML artifacts
52+
uses: actions/upload-artifact@v2
53+
with:
54+
name: query-help-html
55+
path: query-help/_build
56+

docs/codeql/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,5 @@ def setup(sphinx):
109109
# so a file named "default.css" will overwrite the builtin "default.css".
110110
html_static_path = ['_static']
111111

112-
exclude_patterns = ['vale*', '_static', '_templates', 'codeql', 'learn-ql', 'reusables', 'images', 'support', 'ql-training', '_build', '*.py*', 'README.rst']
113-
##############################################################################
112+
exclude_patterns = ['vale*', '_static', '_templates', 'reusables', 'images', 'support', 'ql-training', 'query-help','_build', '*.py*', 'README.rst']
113+
##############################################################################

docs/codeql/query-help-markdown.py

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
import re
2+
import subprocess
3+
import json
4+
import csv
5+
import sys
6+
import os
7+
8+
"""
9+
This script collects CodeQL queries that are part of code scanning query packs,
10+
renders the accompanying query help as markdown, inserts some useful metadata
11+
into the help, and adds a link to the query in the CodeQL repo.
12+
13+
This script requires that 'git' and 'codeql' commands
14+
are on the PATH. It'll try to automatically set the CodeQL search path correctly,
15+
as long as you run the script from one of the following locations:
16+
- anywhere from within a clone of the CodeQL Git repo
17+
- from the parent directory of a clone of the CodeQL Git repo (assuming 'codeql'
18+
and 'codeql-go' directories both exist)
19+
"""
20+
21+
# Define which languages and query packs to consider
22+
languages = [ "cpp", "csharp", "go", "java", "javascript", "python"]
23+
24+
# Query suites to generate help for - lgtm suite should cover the queries that users are interested in
25+
packs = ["lgtm"]
26+
27+
def prefix_repo_nwo(filename):
28+
"""
29+
Replaces an absolute path prefix with a GitHub repository name with owner (NWO).
30+
This function relies on `git` being available.
31+
For example:
32+
/home/alice/git/ql/java/ql/src/MyQuery.ql
33+
becomes:
34+
github/codeql/java/ql/src/MyQuery.ql
35+
36+
If we can't detect a known NWO (e.g. github/codeql, github/codeql-go), the
37+
path will be truncated to the root of the git repo:
38+
ql/java/ql/src/MyQuery.ql
39+
40+
If the filename is not part of a Git repo, the return value is the
41+
same as the input value: the whole path.
42+
"""
43+
dirname = os.path.dirname(filename)
44+
45+
try:
46+
git_toplevel_dir_subp = subprocess_run(
47+
["git", "-C", dirname, "rev-parse", "--show-toplevel"])
48+
except:
49+
# Not a Git repo
50+
return filename
51+
52+
git_toplevel_dir = git_toplevel_dir_subp.stdout.strip()
53+
54+
# Detect 'github/codeql' and 'github/codeql-go' repositories by checking the remote (it's a bit
55+
# of a hack but will work in most cases, as long as the remotes have 'codeql' and 'codeql-go'
56+
# in the URL
57+
git_remotes = subprocess_run(
58+
["git", "-C", dirname, "remote", "-v"]).stdout.strip()
59+
60+
if "codeql-go" in git_remotes:
61+
prefix = "github/codeql-go"
62+
elif "codeql" in git_remotes:
63+
prefix = "github/codeql"
64+
else:
65+
prefix = os.path.basename(git_toplevel_dir)
66+
67+
return os.path.join(prefix, filename[len(git_toplevel_dir)+1:])
68+
69+
70+
def single_spaces(input):
71+
"""
72+
Workaround for https://github.com/github/codeql-coreql-team/issues/470 which causes
73+
some metadata strings to contain newlines and spaces without a good reason.
74+
"""
75+
return " ".join(input.split())
76+
77+
78+
def get_query_metadata(key, metadata, queryfile):
79+
"""Returns query metadata or prints a warning to stderr if a particular piece of metadata is not available."""
80+
if key in metadata:
81+
return single_spaces(metadata[key])
82+
query_id = metadata['id'] if 'id' in metadata else 'unknown'
83+
print("Warning: no '%s' metadata for query with ID '%s' (%s)" %
84+
(key, query_id, queryfile), file=sys.stderr)
85+
return ""
86+
87+
88+
def subprocess_run(cmd):
89+
"""Runs a command through subprocess.run, with a few tweaks. Raises an Exception if exit code != 0."""
90+
return subprocess.run(cmd, capture_output=True, text=True, env=os.environ.copy(), check=True)
91+
92+
93+
try: # Check for `git` on path
94+
subprocess_run(["git", "--version"])
95+
except Exception as e:
96+
print("Error: couldn't invoke 'git'. Is it on the path? Aborting.", file=sys.stderr)
97+
raise e
98+
99+
try: # Check for `codeql` on path
100+
subprocess_run(["codeql", "--version"])
101+
except Exception as e:
102+
print("Error: couldn't invoke CodeQL CLI 'codeql'. Is it on the path? Aborting.", file=sys.stderr)
103+
raise e
104+
105+
# Define CodeQL search path so it'll find the CodeQL repositories:
106+
# - anywhere in the current Git clone (including current working directory)
107+
# - the 'codeql' subdirectory of the cwd
108+
#
109+
# (and assumes the codeql-go repo is in a similar location)
110+
111+
codeql_search_path = "./codeql:./codeql-go" # will be extended further down
112+
# Extend CodeQL search path by detecting root of the current Git repo (if any). This means that you
113+
# can run this script from any location within the CodeQL git repository.
114+
try:
115+
git_toplevel_dir = subprocess_run(["git", "rev-parse", "--show-toplevel"])
116+
117+
# Current working directory is in a Git repo. Add it to the search path, just in case it's the CodeQL repo
118+
#git_toplevel_dir = git_toplevel_dir.stdout.strip()
119+
codeql_search_path += ":" + git_toplevel_dir + ":" + git_toplevel_dir + "/../codeql-go"
120+
codeql_search_path = git_toplevel_dir = git_toplevel_dir.stdout.strip()
121+
except:
122+
# git rev-parse --show-toplevel exited with non-zero exit code. We're not in a Git repo
123+
pass
124+
125+
# Iterate over all languages and packs, and resolve which queries are part of those packs
126+
for lang in languages:
127+
128+
code_scanning_queries = subprocess_run(
129+
["codeql", "resolve", "queries", "--search-path", codeql_search_path, "%s-code-scanning.qls" % (lang)]).stdout.strip()
130+
security_extended_queries = subprocess_run(
131+
["codeql", "resolve", "queries", "--search-path", codeql_search_path, "%s-security-extended.qls" % (lang)]).stdout.strip()
132+
security_and_quality_queries = subprocess_run(
133+
["codeql", "resolve", "queries", "--search-path", codeql_search_path, "%s-security-and-quality.qls" % (lang)]).stdout.strip()
134+
# Define empty dictionary to store @name:filename pairs to generate alphabetically sorted Sphinx toctree
135+
index_file_dictionary = {}
136+
for pack in packs:
137+
# Get absolute paths to queries in this pack by using 'codeql resolve queries'
138+
try:
139+
140+
queries_subp = subprocess_run(
141+
["codeql", "resolve", "queries", "--search-path", codeql_search_path, "%s-%s.qls" % (lang, pack)])
142+
except Exception as e:
143+
# Resolving queries might go wrong if the github/codeql and github/codeql-go repositories are not
144+
# on the search path.
145+
print(
146+
"Warning: couldn't find query pack '%s' for language '%s'. Do you have the right repositories in the right places (search path: '%s')?" % (
147+
pack, lang, codeql_search_path),
148+
file=sys.stderr
149+
)
150+
continue
151+
152+
# Define empty dictionary to store @name:filename pairs to generate alphabetically sorted Sphinx toctree later
153+
index_file_dictionary = {}
154+
155+
# Investigate metadata for every query by using 'codeql resolve metadata'
156+
for queryfile in queries_subp.stdout.strip().split("\n"):
157+
query_metadata_json = subprocess_run(
158+
["codeql", "resolve", "metadata", queryfile]).stdout.strip()
159+
meta = json.loads(query_metadata_json)
160+
161+
# Turn an absolute path to a query file into an nwo-prefixed path (e.g. github/codeql/java/ql/src/....)
162+
queryfile_nwo = prefix_repo_nwo(queryfile)
163+
164+
# Generate the query help for each query
165+
try:
166+
query_help = subprocess_run(
167+
["codeql", "generate", "query-help", "--format=markdown", "--warnings=error", queryfile]).stdout.strip()
168+
except:
169+
# Print a message if generate query help fails
170+
print("Failed to generate query help for '%s'" % (queryfile_nwo))
171+
continue
172+
173+
# Pull out relevant query metadata properties that we want to display in the query help
174+
query_name_meta = get_query_metadata('name', meta, queryfile)
175+
query_description = get_query_metadata(
176+
'description', meta, queryfile)
177+
query_id = "ID: " + \
178+
get_query_metadata('id', meta, queryfile) + "\n"
179+
query_kind = "Kind: " + \
180+
get_query_metadata('kind', meta, queryfile) + "\n"
181+
query_severity = "Severity: " + \
182+
get_query_metadata('problem.severity', meta, queryfile) + "\n"
183+
query_precision = "Precision: " + \
184+
get_query_metadata('precision', meta, queryfile) + "\n"
185+
query_tags = "Tags:\n - " + \
186+
get_query_metadata('tags', meta, queryfile).replace(" ", "\n - ") + "\n"
187+
188+
# Build a link to the query source file for display in the query help
189+
if "go" in prefix_repo_nwo(queryfile):
190+
transform_link = prefix_repo_nwo(queryfile).replace(
191+
"codeql-go", "codeql-go/tree/main").replace(" ", "%20").replace("\\", "/")
192+
else:
193+
transform_link = prefix_repo_nwo(queryfile).replace(
194+
"codeql", "codeql/tree/main").replace(" ", "%20").replace("\\", "/")
195+
query_link = "[Click to see the query in the CodeQL repository](https://github.com/" + \
196+
transform_link + ")\n"
197+
198+
if queryfile in code_scanning_queries:
199+
cs_suites = ' - ' + lang +'-code-scanning.qls\n'
200+
else:
201+
cs_suites = ""
202+
if queryfile in security_extended_queries:
203+
se_suites = ' - ' + lang + '-security-extended.qls\n'
204+
else:
205+
se_suites = ""
206+
if queryfile in security_and_quality_queries:
207+
sq_suites = ' - ' +lang + '-security-and-quality.qls\n'
208+
else:
209+
sq_Suites = ""
210+
211+
if queryfile in code_scanning_queries or queryfile in security_extended_queries or queryfile in security_and_quality_queries:
212+
suites_list = "Query suites:\n" + cs_suites + se_suites + sq_suites
213+
else:
214+
suites_list = ""
215+
216+
# Join metadata into a literal block and add query link below
217+
meta_string = "\n"*2 + "```\n" + query_id + query_kind + query_severity + \
218+
query_precision + query_tags + suites_list + "```\n\n" + query_link + "\n"
219+
220+
# Insert metadata block into query help directly under title
221+
full_help = query_help.replace("\n", meta_string, 1)
222+
223+
# Use id property to make name for markdown file, replacing any "/" characters with "-"
224+
query_name = query_id[4:-1].replace("/", "-")
225+
226+
# Populate index_file_dictionary with @name extracted from metadata and corresponding query filename
227+
index_file_dictionary[query_name_meta] = lang + "/" + query_name
228+
229+
# Make paths for output of the form: query-help-markdown/<lang>/<queryfile>.md
230+
docs_dir = 'query-help'
231+
md_dir_path = os.path.join(docs_dir, lang)
232+
md_file_path = os.path.join(md_dir_path, query_name + ".md")
233+
234+
# Make directories for output paths they don't already exist
235+
if not os.path.isdir(md_dir_path):
236+
os.makedirs(md_dir_path)
237+
238+
# Generate query help at chosen path if output file doesn't already exist
239+
if not os.path.exists(md_file_path):
240+
file = open(md_file_path, "x")
241+
file.write(full_help)
242+
file.close()
243+
244+
# Sort index_file_dictionary alphabetically by @name key, and create column of filename values
245+
sorted_index = dict(sorted(index_file_dictionary.items()))
246+
sorted_index = ("\n" + " ").join(sorted_index.values())
247+
248+
# Add directives to make sorted_index a valid toctree for sphinx source files
249+
toc_directive = ".. toctree::\n :titlesonly:\n\n "
250+
toc_include = toc_directive + sorted_index
251+
252+
# Write toctree to rst
253+
toc_file = os.path.join(docs_dir, "toc-" + lang + ".rst")
254+
file = open(toc_file, "x")
255+
file.write(toc_include)
256+
file.close()

docs/codeql/query-help/conf.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# CodeQL query help configuration file
4+
#
5+
# This file is execfile()d with the current directory set to its
6+
# containing dir.
7+
#
8+
# Note that not all possible configuration values are present in this
9+
# autogenerated file.
10+
#
11+
# All configuration values have a default; values that are commented out
12+
# serve to show the default.
13+
14+
# For details of all possible config values,
15+
# see https://www.sphinx-doc.org/en/master/usage/configuration.html
16+
17+
# -- Project-specific configuration -----------------------------------
18+
19+
# The master toctree document.
20+
master_doc = 'index'
21+
22+
# General information about the project.
23+
project = u'CodeQL query help'
24+
25+
# Add md parser to process query help markdown files
26+
extensions =['recommonmark']
27+
28+
source_suffix = {
29+
'.rst': 'restructuredtext',
30+
'.md': 'markdown',
31+
}
32+
33+
# -- Project-specific options for HTML output ----------------------------------------------
34+
35+
# Theme options are theme-specific and customize the look and feel of a theme
36+
# further. For a list of options available for each theme, see the
37+
# documentation.
38+
html_theme_options = {'font_size': '16px',
39+
'body_text': '#333',
40+
'link': '#2F1695',
41+
'link_hover': '#2F1695',
42+
'show_powered_by': False,
43+
'nosidebar':True,
44+
'head_font_family': '-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"',
45+
}
46+
47+
highlight_language = "none"
48+
49+
# Add any paths that contain templates here, relative to this directory.
50+
templates_path = ['../_templates']
51+
52+
# Add any paths that contain custom static files (such as style sheets) here,
53+
# relative to this directory. They are copied after the builtin static files,
54+
# so a file named "default.css" will overwrite the builtin "default.css".
55+
html_static_path = ['../_static']
56+
57+
# List of patterns, relative to source directory, that match files and
58+
# directories to ignore when looking for source files.
59+
60+
exclude_patterns = ['toc-*', 'readme.md'] # ignore toc-<lang>.rst files as they are 'included' in index pages

docs/codeql/query-help/cpp.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CodeQL query help for C and C++
2+
===============================
3+
4+
.. include:: ../reusables/query-help-overview.rst
5+
6+
For shorter queries that you can use as building blocks when writing your own queries, see the `example queries in the CodeQL repository <https://github.com/github/codeql/tree/main/cpp/ql/examples>`__.
7+
8+
.. include:: toc-cpp.rst
9+

0 commit comments

Comments
 (0)