Skip to content

Commit 453b21e

Browse files
committed
ci(codeql): PR scan per language inside PR CI, full scan on main
Bring CodeQL back to pull requests as jobs of PR CI itself (needed by ci-ok, no polling), narrowed to first-party code, while push to main, the weekly run, the dry run and the release keep the full scan. - codeql.yml: one leg per language (c-cpp, javascript-typescript, python, actions), scope in .github/codeql/codeql-config.yml. The full scan traces the whole build, vendored C included. The PR scan (light: true) analyses each selected language's whole first-party scope; c-cpp compiles the third-party objects before tracing (scripts/build.sh --vendored-only) and then traces the first-party build plus the vendored files the PR changes (--reuse-vendored --retrace-from, make -W). Diff-informed analysis (codeql-action v4.38.1, forced on for PR calls) only restricts reported alerts. The alert check moves into each leg and reads the analysed ref. Push and PR runs cancel superseded runs per language; dispatched runs never cancel. Compiler cache off so every compile is traced. - pr.yml: a codeql job runs the legs of the selected codeql-* lanes; ci-ok needs it. - select-lanes.sh: codeql-cpp/-js/-py/-actions lanes, selected by a change in their scope (any C/C++ incl. vendored; JS/TS, Python and workflows inside the config's paths minus paths-ignore) and by every full run (undeterminable diff, workflows, build, CodeQL machinery). --codeql-scope LANGUAGE prints a list's in-scope paths. - codeql-retrace-vendored.sh: the vendored C files a PR changes (empty, never narrowing, when the diff cannot be determined). - Makefile.cbm: cbm-third-party builds the vendored prod objects only. - tests: the gate contract pins the new layout; the selector contract pins that a C change always selects the C scan, an undeterminable diff selects every language, and a vendored-only change retraces exactly the changed vendored files. Selector fixtures, ci-ok's job map, the replay's codeql-action failures (lane codeql-cpp) and the interface probes follow. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 5afdfca commit 453b21e

18 files changed

Lines changed: 696 additions & 160 deletions

‎.github/codeql/codeql-config.yml‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# CodeQL scope: the repository's first-party source.
2+
#
3+
# Read by .github/workflows/codeql.yml (every scan, full and PR) and by
4+
# scripts/ci/select-lanes.sh (which changed paths select which language's PR
5+
# scan). Keep the lists plain: one path prefix per line, no globs.
6+
#
7+
# paths / paths-ignore narrow what the INTERPRETED languages extract
8+
# (javascript-typescript, python, actions). For c-cpp they have no effect:
9+
# CodeQL extracts exactly what the traced build compiles. A PR's c-cpp scan
10+
# keeps vendored C out by compiling it before tracing starts (except vendored
11+
# files the PR changes); the full scan on main, the dry run and the release
12+
# traces everything, vendored C included.
13+
name: cbm first-party source
14+
paths:
15+
- src
16+
- internal
17+
- graph-ui
18+
- pkg
19+
- scripts
20+
- tests
21+
- .github
22+
paths-ignore:
23+
- internal/cbm/vendored
24+
- graph-ui/vendor
25+
- graph-ui/fixtures
26+
- graph-ui/verification
27+
- tests/fixtures

‎.github/workflows/codeql.yml‎

Lines changed: 105 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,150 @@
11
name: CodeQL SAST
22

3-
# CodeQL runs on main, on a schedule and inside the dry run and the release --
4-
# never per pull request. A per-PR analysis (40-120 min each) plus the PR-CI
5-
# job that waited up to 150 min for it held runners that the PR legs needed:
6-
# with ~30 PRs pushed at once, PR CI starved for hours.
3+
# One analysis per language: c-cpp, javascript-typescript (graph-ui, pkg/npm),
4+
# python, actions. Scope: .github/codeql/codeql-config.yml (first-party source).
75
#
8-
# push to main every merge is analysed; the `alerts` job fails the run
9-
# while any code-scanning alert is open (alert policy).
10-
# schedule weekly, so a CodeQL query update that finds something in
11-
# unchanged code is noticed without a push.
12-
# workflow_call dry-run.yml and release.yml analyse the exact commit they
13-
# test; their `codeql` job (analyze + alerts) must pass.
14-
# workflow_dispatch a manual re-run on any ref.
6+
# FULL scan -- push to main, weekly, dry run, release: every language; c-cpp
7+
# traces the whole production build, vendored C included.
8+
# PR scan -- pr.yml's `codeql` job (light: true), needed by ci-ok. One leg per
9+
# language whose scope the PR changes (codeql-* lanes of
10+
# scripts/ci/select-lanes.sh; an undeterminable diff or a change to the
11+
# workflows, the build or the CodeQL machinery selects every language).
12+
# Each leg analyses its language's WHOLE first-party scope, so cross-file
13+
# data flow is kept; c-cpp compiles the vendored objects before tracing
14+
# starts, except vendored files the PR changes. Diff-informed analysis then
15+
# restricts the REPORTED alerts to the changed lines; what is extracted and
16+
# analysed is never narrowed by the diff.
17+
# There is no pull_request trigger and no job anywhere that polls for a CodeQL
18+
# run: the former PR gate waited up to 150 min per PR for a full analysis and,
19+
# with ~30 PRs pushed at once, starved PR CI for hours.
1520
on:
1621
push:
1722
branches: [main]
1823
schedule:
1924
- cron: "23 5 * * 1"
2025
workflow_dispatch:
26+
inputs:
27+
light:
28+
description: "PR-style c-cpp build (vendored objects compiled untraced) -- to time it"
29+
type: boolean
30+
default: false
2131
workflow_call:
32+
inputs:
33+
light:
34+
description: "PR scan: vendored C compiled untraced except changed files; diff-informed results"
35+
type: boolean
36+
default: false
37+
matrix:
38+
description: 'The legs to run, {"include":[...]} (pr.yml: the selected codeql-* lanes); empty = every language'
39+
type: string
40+
default: ""
2241

2342
permissions:
2443
contents: read
2544

2645
jobs:
2746
analyze:
2847
runs-on: ubuntu-latest
29-
# A newer push to main cancels the analysis of an older main commit; only
30-
# the newest commit's results matter for main. Every other trigger gets a
31-
# group of its own and is never cancelled, so a dry run and a release
32-
# dispatched together cannot cancel each other's gate. In a called workflow
33-
# `github` is the CALLER's context: github.workflow names the dry run or
34-
# the release, github.run_id is the caller's run.
48+
strategy:
49+
fail-fast: false
50+
# The same legs (and keys) everywhere, so a PR's analysis and main's
51+
# share one code-scanning configuration per language.
52+
matrix: ${{ fromJSON(inputs.matrix || '{"include":[{"language":"c-cpp","build-mode":"manual","lane":"codeql-cpp"},{"language":"javascript-typescript","build-mode":"none","lane":"codeql-js"},{"language":"python","build-mode":"none","lane":"codeql-py"},{"language":"actions","build-mode":"none","lane":"codeql-actions"}]}') }}
53+
# Superseded runs are always cancelled: a newer push to main cancels the
54+
# analysis of an older main commit (batched merges -> only the newest main
55+
# commit is scanned), a newer push to a PR cancels that PR's scan. Every
56+
# other trigger gets a group of its own and is never cancelled: a dry run
57+
# or a release tests ONE commit, and two dispatched together must not
58+
# cancel each other's gate. Per language, so legs never cancel each other.
59+
# In a called workflow `github` is the CALLER's context: event_name is
60+
# pull_request for pr.yml, github.workflow names the dry run or release.
3561
concurrency:
36-
group: codeql-${{ github.event_name == 'push' && github.ref || format('{0}-{1}', github.workflow, github.run_id) }}
37-
cancel-in-progress: ${{ github.event_name == 'push' }}
38-
# CodeQL needs security-events: write to upload results; scoped to this job so
39-
# the workflow's top-level token stays read-only (Scorecard TokenPermissions).
62+
group: codeql-${{ matrix.language }}-${{ github.event_name == 'push' && github.ref || github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || format('{0}-{1}', github.workflow, github.run_id) }}
63+
cancel-in-progress: ${{ github.event_name == 'push' || github.event_name == 'pull_request' }}
64+
# CodeQL needs security-events: write to upload results (and read for the
65+
# alert check); scoped to this job so the workflow's top-level token stays
66+
# read-only (Scorecard TokenPermissions).
4067
permissions:
4168
security-events: write
4269
actions: read
4370
contents: read
4471
env:
4572
# One code-scanning configuration for every trigger. The action derives
4673
# the key from the RUN's workflow file, which for a called workflow is
47-
# the caller: dry-run.yml / release.yml would each open a configuration
48-
# of their own on main, and an alert fixed by a later push would stay
49-
# open in the stale one until the next dry run. The value is the key the
50-
# push-to-main analysis has always used (the action documents this
51-
# variable as settable for non-standard setups).
74+
# the caller: pr.yml / dry-run.yml / release.yml would each open a
75+
# configuration of their own, and on main an alert fixed by a later push
76+
# would stay open in a stale one. The value is the key the push-to-main
77+
# analysis has always used (the action documents this variable as
78+
# settable for non-standard setups).
5279
CODEQL_ACTION_ANALYSIS_KEY: .github/workflows/codeql.yml:analyze
80+
# Diff-informed analysis: codeql-action v4.38.1 (CLI 2.27.0) reports a
81+
# pull request's alerts only on its changed lines when the
82+
# diff_informed_queries feature is on (default on; this variable forces
83+
# it, src/feature-flags.ts). It applies only when the event payload is a
84+
# pull request -- pr.yml's call -- and is inert on main, dry run, release.
85+
CODEQL_ACTION_DIFF_INFORMED_QUERIES: ${{ inputs.light && 'true' || '' }}
86+
# No compiler cache: a cache hit skips the compiler, and CodeQL extracts
87+
# only what a traced compiler invocation actually compiles.
88+
CBM_NO_CCACHE: "1"
5389
steps:
5490
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
91+
with:
92+
# The PR scan reads the merge commit's first parent (the base) to
93+
# find the vendored files the PR changes.
94+
fetch-depth: ${{ inputs.light && 2 || 1 }}
5595

5696
- name: Install build dependencies
97+
if: ${{ matrix.language == 'c-cpp' }}
5798
run: sudo apt-get update && sudo apt-get install -y zlib1g-dev
5899

100+
# PR c-cpp: compile the third-party objects (tree-sitter grammars,
101+
# sqlite, mimalloc, lz4, zstd, the tree-sitter runtime: ~1.3 GB of
102+
# vendored C against ~45 MB first-party) BEFORE tracing starts, so the
103+
# traced build below compiles first-party code only ...
104+
- name: Build third-party objects (PR scan, untraced)
105+
if: ${{ matrix.language == 'c-cpp' && inputs.light }}
106+
run: scripts/build.sh --vendored-only
107+
108+
# ... except the vendored files this PR changes: those are rebuilt
109+
# under tracing (an undeterminable diff leaves the list empty; the scan
110+
# still covers all first-party C).
111+
- name: Vendored files this PR changes (traced too)
112+
if: ${{ matrix.language == 'c-cpp' && inputs.light }}
113+
run: scripts/ci/codeql-retrace-vendored.sh "$RUNNER_TEMP/codeql-retrace.txt"
114+
59115
- name: Initialize CodeQL
60116
uses: github/codeql-action/init@1c5b675653bb5c22dbe9b12b556ec555138e09fd # v4.38.1
61117
with:
62-
languages: c-cpp
63-
build-mode: manual
118+
languages: ${{ matrix.language }}
119+
build-mode: ${{ matrix.build-mode }}
120+
config-file: ./.github/codeql/codeql-config.yml
64121

65-
- name: Build for CodeQL analysis
122+
- name: Build for CodeQL analysis (full)
123+
if: ${{ matrix.language == 'c-cpp' && !inputs.light }}
66124
run: scripts/build.sh
67125

126+
- name: Build for CodeQL analysis (PR scan, first-party + changed vendored)
127+
if: ${{ matrix.language == 'c-cpp' && inputs.light }}
128+
run: scripts/build.sh --reuse-vendored --retrace-from "$RUNNER_TEMP/codeql-retrace.txt"
129+
68130
- name: Perform CodeQL Analysis
69131
uses: github/codeql-action/analyze@1c5b675653bb5c22dbe9b12b556ec555138e09fd # v4.38.1
70132
with:
71-
category: "/language:c-cpp"
133+
category: "/language:${{ matrix.language }}"
72134

73-
# Alert policy: the default branch carries ZERO open code-scanning alerts.
74-
# Any open alert -- a new finding from a merge, from the weekly run, or one
75-
# this commit adds -- fails this job: the main commit turns red, GitHub
76-
# notifies whoever triggered the run, and the dry run and the release (which
77-
# call this workflow) cannot pass until the alert is fixed or dismissed with
78-
# a reason in the Security tab. With a zero baseline, "any open alert" is
79-
# exactly "a new alert". Moved here unchanged from _security.yml's former
80-
# PR-CI codeql-gate job; it reads the default branch's alerts, as it did there.
81-
alerts:
82-
needs: [analyze]
83-
runs-on: ubuntu-latest
84-
timeout-minutes: 10
85-
# Without security-events:read this job inherits the workflow's
86-
# contents:read, the code-scanning alert API answers 403, and the former
87-
# gate reported "0 alerts" on every run it ever made.
88-
permissions:
89-
contents: read
90-
security-events: read
91-
steps:
135+
# Alert policy: the default branch carries ZERO open code-scanning
136+
# alerts. Any open alert on the analysed ref fails this leg: on main the
137+
# commit turns red and GitHub notifies whoever triggered the run; the
138+
# dry run and the release cannot pass; a PR (refs/pull/<n>/merge: what
139+
# its changed lines introduce) fails ci-ok. With a zero baseline "any
140+
# open alert" is exactly "a new alert"; a dismissal (with a reason, in
141+
# the Security tab) holds on every ref. Every leg checks after its own
142+
# upload, so each language's alerts are seen by at least its own leg.
143+
# Moved from _security.yml's former PR-CI codeql-gate job.
92144
- name: Check for open code scanning alerts
93145
env:
94146
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
147+
ALERT_REF: ${{ github.ref }}
95148
run: |
96149
# FAIL CLOSED. The previous form was `gh api ... 2>/dev/null || echo "0"`,
97150
# which looks defensive and is the opposite. gh writes the API error
@@ -108,7 +161,7 @@ jobs:
108161
# replaces. The callers below check the status explicitly.
109162
count_open_alerts() {
110163
local body status
111-
body=$(gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
164+
body=$(gh api "repos/${{ github.repository }}/code-scanning/alerts?state=open&ref=$ALERT_REF" \
112165
--jq 'length' 2>/dev/null)
113166
status=$?
114167
if [ $status -ne 0 ]; then
@@ -132,9 +185,9 @@ jobs:
132185
ALERTS2=$(count_open_alerts) || exit 1
133186
[ "$ALERTS" -lt "$ALERTS2" ] && ALERTS=$ALERTS2
134187
if [ "$ALERTS" -gt 0 ]; then
135-
echo "::error title=Open code scanning alerts::$ALERTS open alert(s) on the default branch -- fix, or dismiss with a reason in the Security tab"
188+
echo "::error title=Open code scanning alerts::$ALERTS open alert(s) on $ALERT_REF -- fix, or dismiss with a reason in the Security tab"
136189
echo "BLOCKED: $ALERTS open alert(s)"
137-
gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
190+
gh api "repos/${{ github.repository }}/code-scanning/alerts?state=open&ref=$ALERT_REF" \
138191
--jq '.[] | " #\(.number) [\(.rule.security_severity_level // .rule.severity)] \(.rule.id) — \(.most_recent_instance.location.path):\(.most_recent_instance.location.start_line)"' 2>/dev/null || true
139192
exit 1
140193
fi

‎.github/workflows/dry-run.yml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Pipeline: lint → test → build + candidate scan/select → smoke/soak
55
# Security: security-static + license-gate (independent island)
6-
# CodeQL: codeql.yml on this commit (analyze + alerts) (independent island)
6+
# CodeQL: codeql.yml full scan of this commit + alert check (independent island)
77
#
88
# Security and CodeQL do NOT block lint/test/build/smoke/soak. All jobs must pass
99
# for the overall workflow to be green.
@@ -69,11 +69,11 @@ jobs:
6969
contents: read
7070
secrets: inherit
7171

72-
# ── CodeQL: analysis of this exact commit + the open-alert check ──
73-
# PRs do not run CodeQL; the dry run and the release are where a CodeQL pass
74-
# is required. codeql.yml's analyze job uploads results and its alerts job
75-
# reads them: a called workflow cannot request more than its caller grants,
76-
# so withholding either permission fails the whole run at startup.
72+
# ── CodeQL: full scan of this exact commit + the open-alert check ──
73+
# Every language, vendored C included (PRs run the narrower PR scan).
74+
# codeql.yml uploads results and reads the alerts: a called workflow
75+
# cannot request more than its caller grants, so withholding either
76+
# permission fails the whole run at startup.
7777
codeql:
7878
uses: ./.github/workflows/codeql.yml
7979
permissions:

‎.github/workflows/pr.yml‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ jobs:
125125
full: ${{ steps.select.outputs.full }}
126126
lanes: ${{ steps.select.outputs.lanes }}
127127
smoke: ${{ steps.select.outputs.smoke }}
128+
codeql: ${{ steps.select.outputs.codeql }}
128129
steps:
129130
- name: List the changed files
130131
env:
@@ -177,6 +178,10 @@ jobs:
177178
SMOKE='[{"os":"ubuntu-latest","lane":"smoke-ubuntu"},{"os":"macos-14","lane":"smoke-mac"},{"os":"windows-latest","lane":"smoke-win"}]'
178179
LANES=$(sed -n 's/^lanes=//p' "$RUNNER_TEMP/selection.txt")
179180
echo "smoke={\"include\":$(jq -c --argjson sel "$LANES" 'map(select(.lane | IN($sel[])))' <<< "$SMOKE")}" >> "$GITHUB_OUTPUT"
181+
# The CodeQL legs, tagged the same way (codeql.yml's full matrix has
182+
# the same entries, so PR and main share a configuration).
183+
CODEQL='[{"language":"c-cpp","build-mode":"manual","lane":"codeql-cpp"},{"language":"javascript-typescript","build-mode":"none","lane":"codeql-js"},{"language":"python","build-mode":"none","lane":"codeql-py"},{"language":"actions","build-mode":"none","lane":"codeql-actions"}]'
184+
echo "codeql={\"include\":$(jq -c --argjson sel "$LANES" 'map(select(.lane | IN($sel[])))' <<< "$CODEQL")}" >> "$GITHUB_OUTPUT"
180185
181186
# ── Light smoke: build the PRODUCTION binary and run the core smoke on the
182187
# three RELIABLE NATIVE platforms. Catches built-binary regressions at PR
@@ -358,13 +363,34 @@ jobs:
358363
- name: Contract steps only
359364
run: scripts/test.sh --contracts-only
360365

366+
# ── CodeQL PR scan (lanes codeql-cpp / -js / -py / -actions): one leg per
367+
# language whose first-party scope this PR changes; each analyses that
368+
# language's whole first-party scope, c-cpp with the vendored objects
369+
# compiled before tracing (except vendored files the PR changes), and
370+
# reports alerts on the changed lines (diff-informed). A job of this run,
371+
# needed by ci-ok -- nothing waits for a separate CodeQL run. Push to
372+
# main, dry run and release run the full scan (codeql.yml). ──
373+
codeql:
374+
needs: [changes]
375+
if: ${{ !cancelled() && needs.changes.result == 'success' && (contains(fromJSON(needs.changes.outputs.lanes), 'codeql-cpp') || contains(fromJSON(needs.changes.outputs.lanes), 'codeql-js') || contains(fromJSON(needs.changes.outputs.lanes), 'codeql-py') || contains(fromJSON(needs.changes.outputs.lanes), 'codeql-actions')) }}
376+
uses: ./.github/workflows/codeql.yml
377+
# codeql.yml uploads results and reads alerts: a called workflow cannot
378+
# request more than its caller grants (startup failure otherwise).
379+
permissions:
380+
contents: read
381+
security-events: write
382+
actions: read
383+
with:
384+
light: true
385+
matrix: ${{ needs.changes.outputs.codeql }}
386+
361387
ci-ok:
362388
# The one required context (besides dco) — fails unless every PR stage
363389
# succeeded, so matrix renames can never silently deadlock merges, and
364390
# every lane the selector chose ran green: `skipped` is OK only for a lane
365391
# nobody selected (a selected lane that skipped is a silent gate loss).
366392
# memwaste and the test-impact shadow are report-only and not needed here.
367-
needs: [changes, security, lint, test, pr-smoke, contracts]
393+
needs: [changes, security, lint, test, pr-smoke, contracts, codeql]
368394
if: ${{ always() }}
369395
runs-on: ubuntu-latest
370396
timeout-minutes: 5

‎.github/workflows/release.yml‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Release pipeline: lint → test → build → smoke/soak → draft → verify → publish
22
#
3-
# Security (security-static + license-gate) and CodeQL (codeql.yml: analysis
3+
# Security (security-static + license-gate) and CodeQL (codeql.yml: full scan
44
# of this commit + the open-alert check) start immediately and run in
55
# parallel with lint/test/build/smoke/soak. They only block the final verify
66
# step — everything else proceeds independently.
@@ -52,11 +52,10 @@ jobs:
5252
secrets: inherit
5353

5454
# ── CodeQL (starts immediately, blocks only verify) ──────────────
55-
# The analysis of this exact commit + the open-alert check.
56-
# PRs do not run CodeQL; the dry run and the release are where a CodeQL pass
57-
# is required. codeql.yml's analyze job uploads results and its alerts job
58-
# reads them: a called workflow cannot request more than its caller grants,
59-
# so withholding either permission fails the whole run at startup.
55+
# Full scan of this exact commit (every language, vendored C included) +
56+
# the open-alert check. codeql.yml uploads results and reads the alerts:
57+
# a called workflow cannot request more than its caller grants, so
58+
# withholding either permission fails the whole run at startup.
6059
codeql:
6160
uses: ./.github/workflows/codeql.yml
6261
permissions:

0 commit comments

Comments
 (0)