Skip to content

Commit a9f936f

Browse files
felickzCopilot
andauthored
Add query suite counts to releases (#256)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent f63244b commit a9f936f

4 files changed

Lines changed: 170 additions & 5 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
# Usage: build-query-suite-table.sh [upstream-codeql-checkout]
3+
set -euo pipefail
4+
5+
REPO="${GITHUB_REPOSITORY:-GitHubSecurityLab/CodeQL-Community-Packs}"
6+
REPO_SHA="${GITHUB_SHA:-$(git rev-parse HEAD)}"
7+
RESOLVE_OPTIONS=(--format=json --additional-packs=.)
8+
if [ -n "${1:-}" ]; then
9+
RESOLVE_OPTIONS+=(--search-path="$1")
10+
fi
11+
12+
lang_label() {
13+
case "$1" in
14+
actions) echo "Actions" ;;
15+
cpp) echo "C++" ;;
16+
csharp) echo "C#" ;;
17+
go) echo "Go" ;;
18+
java) echo "Java" ;;
19+
javascript) echo "JavaScript" ;;
20+
python) echo "Python" ;;
21+
ruby) echo "Ruby" ;;
22+
rust) echo "Rust" ;;
23+
swift) echo "Swift" ;;
24+
*) echo "$1" ;;
25+
esac
26+
}
27+
28+
echo "<details>"
29+
echo "<summary>Query suites and query counts</summary>"
30+
echo
31+
echo "Counts are resolved with \`codeql resolve queries\` at this release's commit, including upstream queries selected by a suite. Suites can overlap; counts are not additive."
32+
echo
33+
echo "| Language | Suite | Queries |"
34+
echo "| --- | --- | ---: |"
35+
36+
FAILED=0
37+
shopt -s nullglob
38+
SUITES=(*/src/suites/*.qls)
39+
if [ "${#SUITES[@]}" -eq 0 ]; then
40+
echo "::error::No query suites found" >&2
41+
exit 1
42+
fi
43+
for suite in "${SUITES[@]}"; do
44+
language="${suite%%/*}"
45+
if queries=$(codeql resolve queries "${RESOLVE_OPTIONS[@]}" "$suite") &&
46+
count=$(jq -er 'if type == "array" and all(.[]; type == "string") then length else error("Expected a query path array") end' <<< "$queries"); then
47+
echo "| $(lang_label "$language") | [\`$(basename "$suite" .qls)\`](https://github.com/${REPO}/blob/${REPO_SHA}/${suite}) | $count |"
48+
else
49+
echo "::error::Could not count queries for $suite" >&2
50+
echo "| $(lang_label "$language") | [\`$(basename "$suite" .qls)\`](https://github.com/${REPO}/blob/${REPO_SHA}/${suite}) | Resolution failed |"
51+
FAILED=1
52+
fi
53+
done
54+
55+
echo
56+
echo "</details>"
57+
exit "$FAILED"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
from pathlib import Path
3+
import subprocess
4+
import tempfile
5+
import unittest
6+
7+
8+
SCRIPT = Path(__file__).resolve().parents[1] / "build-query-suite-table.sh"
9+
10+
11+
class QuerySuiteTableTests(unittest.TestCase):
12+
def run_table(self, response):
13+
with tempfile.TemporaryDirectory() as directory:
14+
root = Path(directory)
15+
for name in ("cpp", "cpp-audit"):
16+
suite = root / "cpp" / "src" / "suites" / f"{name}.qls"
17+
suite.parent.mkdir(parents=True, exist_ok=True)
18+
suite.touch()
19+
binary = root / "bin" / "codeql"
20+
binary.parent.mkdir()
21+
binary.write_text("#!/usr/bin/env bash\n" + response)
22+
binary.chmod(0o755)
23+
env = dict(os.environ, PATH=f"{binary.parent}:{os.environ['PATH']}",
24+
GITHUB_REPOSITORY="example/packs", GITHUB_SHA="abc123")
25+
return subprocess.run(
26+
["bash", str(SCRIPT), "upstream"],
27+
cwd=root, env=env, text=True, capture_output=True,
28+
)
29+
30+
def test_counts_and_commit_links(self):
31+
result = self.run_table(
32+
'[[ "$*" == "resolve queries --format=json --additional-packs=. '
33+
'--search-path=upstream cpp/src/suites/"* ]] || exit 2\n'
34+
'case "${@: -1}" in\n'
35+
' *-audit.qls) echo \'[]\' ;;\n'
36+
' *) echo \'["first.ql", "second.ql"]\' ;;\n'
37+
'esac\n'
38+
)
39+
self.assertEqual(result.returncode, 0, result.stderr)
40+
self.assertIn("<details>\n<summary>Query suites and query counts</summary>", result.stdout)
41+
self.assertIn(
42+
"| C++ | [`cpp-audit`](https://github.com/example/packs/blob/"
43+
"abc123/cpp/src/suites/cpp-audit.qls) | 0 |", result.stdout,
44+
)
45+
self.assertIn(
46+
"| C++ | [`cpp`](https://github.com/example/packs/blob/"
47+
"abc123/cpp/src/suites/cpp.qls) | 2 |", result.stdout,
48+
)
49+
self.assertTrue(result.stdout.endswith("</details>\n"))
50+
51+
def test_resolution_failure_does_not_skip_later_suites(self):
52+
result = self.run_table(
53+
'case "${@: -1}" in\n'
54+
' *-audit.qls) echo "resolver error" >&2; exit 1 ;;\n'
55+
' *) echo \'["query.ql"]\' ;;\n'
56+
'esac\n'
57+
)
58+
self.assertEqual(result.returncode, 1)
59+
self.assertIn("::error::Could not count queries for cpp/src/suites/cpp-audit.qls",
60+
result.stderr)
61+
self.assertIn("| Resolution failed |", result.stdout)
62+
self.assertIn("abc123/cpp/src/suites/cpp.qls) | 1 |", result.stdout)
63+
64+
def test_invalid_resolver_output_is_not_a_count(self):
65+
result = self.run_table('echo \'{"error": "not a query array"}\'\n')
66+
self.assertEqual(result.returncode, 1)
67+
self.assertEqual(result.stdout.count("| Resolution failed |"), 2)
68+
69+
70+
if __name__ == "__main__":
71+
unittest.main()

‎.github/workflows/publish.yml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,33 @@ jobs:
429429
run: |
430430
.github/scripts/upsert-release-table.sh summary.md publish-summary
431431
.github/scripts/upsert-release-table.sh codeql-lib-versions.md codeql-lib-versions
432+
433+
- name: Setup CodeQL for suite counts
434+
id: suite_codeql
435+
uses: ./.github/actions/install-codeql
436+
437+
- name: Checkout matching upstream suites and queries
438+
uses: actions/checkout@v7
439+
with:
440+
repository: github/codeql
441+
ref: codeql-cli/v${{ steps.suite_codeql.outputs.codeql-cli-version }}
442+
path: codeql-upstream
443+
sparse-checkout-cone-mode: false
444+
sparse-checkout: |
445+
/*/ql/src/
446+
/*/ql/lib/
447+
/shared/
448+
/misc/suite-helpers/
449+
/codeql-workspace.yml
450+
persist-credentials: false
451+
452+
- name: Build query suite counts
453+
run: |
454+
bash .github/scripts/build-query-suite-table.sh codeql-upstream > query-suite-counts.md
455+
cat query-suite-counts.md >> "$GITHUB_STEP_SUMMARY"
456+
457+
- name: Upsert release suite counts
458+
if: github.event_name == 'push'
459+
env:
460+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
461+
run: .github/scripts/upsert-release-table.sh query-suite-counts.md query-suite-counts

‎CONTRIBUTING.md‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ process for each; most of it is manual today.
316316
(`src` includes Actions, Rust, and Swift; `lib` is only created where there is shared library code; `ext` runs for `cpp`/`csharp`/`go`/`java`/`javascript`/`python`/`ruby`/`rust`; `ext-library-sources` only runs for `csharp`/`java`, the only languages with that pack) - plus a fifth
317317
`summary` job that runs after the other four (`if: always()`, so it still runs even if one of them
318318
fails), aggregates their per-pack results into the publish-summary and CodeQL library/query pack
319-
version tables, and (on the release-cut `push` trigger only) upserts both tables into the GitHub
320-
Release's notes.
319+
version tables, resolves query suite counts, and (on the release-cut `push` trigger only) upserts
320+
these sections into the GitHub Release's notes.
321321

322322
Each published/up-to-date cell in the publish-summary table is a shields.io **Dynamic Regex
323323
Badge**, labeled with the version and showing that pack version's download count (e.g. `0.6.0 |
@@ -536,12 +536,18 @@ supported way to bump `.release.yml`:
536536
- [ ] Merging it is what changes `.release.yml` on `main`, which auto-triggers
537537
[`publish.yml`][publish-workflow] for a real batch publish: every pack whose version actually
538538
changed gets published to [GHCR][ghcr-packages] in that one run.
539-
- [ ] The run's `summary` job posts two markdown tables to the job summary **and** upserts both into
539+
- [ ] The run's `summary` job posts three markdown tables to the job summary **and** upserts them into
540540
the matching GitHub Release (`vX.Y.Z`), creating it if it doesn't exist yet: a publish summary
541541
(what published) and a CodeQL standard library & query pack versions table (whether each
542542
language's locked `codeql/*` dependencies match what the pinned CodeQL CLI actually ships
543543
upstream — see the note under
544-
[Updating the pinned CodeQL CLI/library version](#updating-the-pinned-codeql-clilibrary-version)).
544+
[Updating the pinned CodeQL CLI/library version](#updating-the-pinned-codeql-clilibrary-version)),
545+
plus a collapsible query suite inventory with per-language query counts. Each suite links to
546+
its `.qls` file at the release commit. Counts use `codeql resolve queries --format=json`
547+
and include upstream queries selected by the suite, resolved from the `github/codeql` source
548+
at the pinned CLI tag. Suites may overlap, so their counts should not be added together.
549+
A failed resolution is reported as an error, never as a zero count, and prevents updating
550+
the inventory for that run.
545551

546552
> [!NOTE]
547553
> **Pre-release vs. full release.** The GitHub Release created above is a **full release by
@@ -584,7 +590,8 @@ consider before using it:
584590

585591
The [Releases][releases] tab (`v0.2.0`, `v0.2.1`, ...) is a repo-wide changelog tied to cutting a
586592
release as described above. Each release's auto-generated notes are supplemented with the publish
587-
summary table and the CodeQL standard library & query pack versions table (see
593+
summary table, the CodeQL standard library & query pack versions table, and a collapsible query
594+
suite inventory (see
588595
[Cutting a release](#cutting-a-release)), so you can see exactly which packs published at that
589596
version — and whether the library/query pack versions they're compiled against are still in sync
590597
with the pinned CodeQL CLI, with a direct link to the exact upstream file/line — without

0 commit comments

Comments
 (0)