Skip to content

Commit 3835fde

Browse files
max-schaeferPeter Bengtssonpeterbe
authored
Add autofix coverage information for CodeQL queries (#49781)
Co-authored-by: Peter Bengtsson <[email protected]> Co-authored-by: Peter Bengtsson <[email protected]>
1 parent fe9ccc1 commit 3835fde

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Install CocoFix
2+
3+
description: Installs the private package `@github/cocofix` from the GitHub Package Registry.
4+
5+
inputs:
6+
token:
7+
description: PAT
8+
required: true
9+
10+
runs:
11+
using: 'composite'
12+
steps:
13+
- name: Install CoCoFix
14+
shell: bash
15+
env:
16+
TOKEN: ${{ inputs.token }}
17+
run: |
18+
npm install --no-save \
19+
'--@github:registry=https://npm.pkg.github.com' \
20+
'--//npm.pkg.github.com/:_authToken=${TOKEN}' \
21+
@github/cocofix

.github/workflows/generate-code-scanning-query-lists.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ jobs:
6060
sleep 3
6161
codeql --version
6262
63+
- uses: ./.github/actions/install-cocofix
64+
with:
65+
token: ${{ secrets.DOCS_BOT_PAT_WORKFLOW }}
66+
6367
- name: Build code scanning query list
6468
run: |
6569
for lang in "cpp" "csharp" "go" "java" "javascript" "python" "ruby" "swift"; do

.github/workflows/lint-code.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ jobs:
4545
- name: Run Prettier
4646
run: npm run prettier-check
4747

48+
- uses: ./.github/actions/install-cocofix
49+
with:
50+
token: ${{ secrets.DOCS_BOT_PAT_WORKFLOW }}
51+
4852
- name: Run TypeScript
4953
run: npm run tsc

src/code-scanning/scripts/generate-code-scanning-query-list.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
*
3030
* /Users/peterbe/.local/share/gh/extensions/gh-codeql/dist/nightly/codeql-bundle-20231204/codeql
3131
*
32+
* Finally, you need to install `@github/cocofix`. This is a private package,
33+
* so you first need to get the `DOCS_BOT_PAT_WORKFLOW` PAT from the vault and
34+
* store it in the environment variable `DOCS_BOT_PAT_WORKFLOW`.
35+
* Then run the following command from the root of this repo:
36+
*
37+
* ```sh
38+
* npm i --no-save '--@github:registry=https://npm.pkg.github.com' '--//npm.pkg.github.com/:_authToken=${DOCS_BOT_PAT_WORKFLOW}' @github/cocofix
39+
* ```
40+
*
3241
* If you've git cloned github/codeql in /tmp/ now you can execute this script.
3342
* For example, to generate the Markdown
3443
* for Python:
@@ -44,6 +53,10 @@ import { execFileSync } from 'child_process'
4453

4554
import chalk from 'chalk'
4655
import { program } from 'commander'
56+
// We don't want to introduce a global dependency on @github/cocofix, so we install it by hand
57+
// as described above and suppress the import warning.
58+
import { getSupportedQueries } from '@github/cocofix/dist/querySuites' // eslint-disable-line import/no-unresolved
59+
import { type Language } from '@github/cocofix/dist/codeql' // eslint-disable-line import/no-unresolved
4760

4861
program
4962
.description('Generate a reusable Markdown for for a code scanning query language')
@@ -74,6 +87,7 @@ type Query = {
7487
url: string
7588
packs: string[]
7689
cwes: string[]
90+
autofixSupport: 'none' | 'default'
7791
}
7892

7993
const opts = program.opts()
@@ -105,6 +119,12 @@ async function main(options: Options, language: string) {
105119
[id: string]: Query
106120
} = {}
107121

122+
const autofixSupportedQueryIds = await getSupportedQueries(
123+
'default',
124+
language as Language,
125+
'CodeQL',
126+
)
127+
108128
for (const pack of options.packs) {
109129
const languagePack = `${language}-${pack}.qls`
110130
if (options.verbose) console.log(chalk.dim(`Searching for queries in ${languagePack}`))
@@ -123,12 +143,13 @@ async function main(options: Options, language: string) {
123143
if (id && name) {
124144
const cwes = getCWEs(tags || '')
125145
const url = getDocsLink(language, id)
146+
const autofixSupport = autofixSupportedQueryIds.includes(id) ? 'default' : 'none'
126147
127148
// Only include queries that have CWEs, since the other queries deal with code scanning
128149
// metadata and metrics (e.g. counting lines of code or number of files) and have no docs link
129150
if (cwes.length) {
130151
if (!(id in queries)) {
131-
queries[id] = { url, name, packs: [], cwes }
152+
queries[id] = { url, name, packs: [], cwes, autofixSupport }
132153
}
133154
queries[id].packs.push(pack)
134155
} else {
@@ -150,7 +171,7 @@ function printQueries(options: Options, queries: Query[]) {
150171
const markdown = []
151172
markdown.push('{% rowheaders %}')
152173
markdown.push('') // blank line
153-
const header = ['Query name', 'Related CWEs', 'Default', 'Extended']
174+
const header = ['Query name', 'Related CWEs', 'Default', 'Extended', 'Autofix']
154175
markdown.push(`| ${header.join(' | ')} |`)
155176
markdown.push(`| ${header.map(() => '---').join(' | ')} |`)
156177
@@ -161,14 +182,18 @@ function printQueries(options: Options, queries: Query[]) {
161182
const markdownLink = `[${query.name}](${query.url})`
162183
let defaultIcon = notIncludedOcticon
163184
let extendedIcon = notIncludedOcticon
185+
let autofixIcon = notIncludedOcticon
164186
if (query.packs.includes('code-scanning')) {
165187
defaultIcon = includedOcticon
166188
}
167189
if (query.packs.includes('security-extended')) {
168190
extendedIcon = includedOcticon
169191
}
192+
if (query.autofixSupport === 'default') {
193+
autofixIcon = includedOcticon
194+
}
170195
markdown.push(
171-
`| ${markdownLink} | ${query.cwes.join(', ')} | ${defaultIcon} | ${extendedIcon} |`,
196+
`| ${markdownLink} | ${query.cwes.join(', ')} | ${defaultIcon} | ${extendedIcon} | ${autofixIcon} |`,
172197
)
173198
}
174199
markdown.push('') // blank line

0 commit comments

Comments
 (0)