Repository navigation
ci(codeql): run the analysis step again #1818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
auxesis
wants to merge
8
commits into
main
Choose a base branch
from
ci/codeql-analyze
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd35883
ci(codeql): run the analysis step again
auxesis a2686a7
test(ci): name the commit that dropped the CodeQL analysis
auxesis 0cef28b
test(ci): match each CodeQL step against the pattern once
auxesis 55cdf40
test(ci): move the composite action walker into the shared helpers
auxesis e3d0541
test(ci): follow local composite actions in the CodeQL guard
auxesis 6bd2b5c
test(ci): refuse a CodeQL analysis that never runs or never uploads
auxesis 6d074be
ci(codeql): pin both CodeQL steps to the commit of v4.38.0
auxesis 6bfbada
test(ci): count repeated composite calls and their conditions
auxesis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ jobs: | |
| analyze: | ||
| name: Analyze | ||
| runs-on: blacksmith-4vcpu-ubuntu-2404 | ||
| timeout-minutes: 10 | ||
| timeout-minutes: 15 | ||
|
|
||
| strategy: | ||
| fail-fast: false | ||
|
|
@@ -32,11 +32,11 @@ jobs: | |
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Initialize CodeQL | ||
| uses: github/codeql-action/[email protected] | ||
| uses: github/codeql-action/init@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0 | ||
| with: | ||
| languages: ${{ matrix.language }} | ||
|
|
||
| - name: Perform CodeQL Analysis | ||
| uses: github/codeql-action/init@v4.38.0 | ||
| uses: github/codeql-action/analyze@b96794f015dfd88f77b49b1c93e0fa7110f94c63 # v4.38.0 | ||
| with: | ||
| category: "/language:${{ matrix.language }}" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| flattenSteps, | ||
| readWorkflow, | ||
| resolveRepoComposite, | ||
| workflowFiles, | ||
| } from './lib/workflows.mjs' | ||
|
|
||
| /** | ||
| * A job that starts CodeQL must also run the analysis. | ||
| * | ||
| * `codeql-action/init` only prepares the database. `codeql-action/analyze` | ||
| * runs the queries and uploads the results. Commit `ed08dce88`, on 12 May | ||
| * 2026, changed the analysis step in `codeql.yml` to name `init` by mistake. | ||
| * Until November 2026 the job still passed, with only a warning about the | ||
| * unexpected `category` input, so nothing showed that no code on `main` was | ||
| * analysed. | ||
| */ | ||
| const CODEQL_STEP = /^github\/codeql-action\/(init|analyze)@/ | ||
|
|
||
| // A local action that cannot be opened is reported, because a CodeQL step | ||
| // could be inside it. | ||
| function codeqlSteps(steps, resolveComposite) { | ||
| const { flat, unresolved } = flattenSteps(steps, resolveComposite) | ||
| const found = flat.flatMap(({ step, label, conditions }) => { | ||
| const match = | ||
| typeof step?.uses === 'string' ? step.uses.match(CODEQL_STEP) : null | ||
| return match ? [{ action: match[1], step, label, conditions }] : [] | ||
| }) | ||
| return { | ||
| actions: found.map(({ action }) => action), | ||
| analyses: found.filter(({ action }) => action === 'analyze'), | ||
| unresolved, | ||
| } | ||
| } | ||
|
|
||
| // `if: false`, `if: 'false'` and `if: ${{ false }}` all skip the step. | ||
| const ALWAYS_FALSE = /^\s*(?:\$\{\{\s*false\s*\}\}|false)\s*$/ | ||
| const alwaysFalse = (condition) => | ||
| condition === false || | ||
| (typeof condition === 'string' && ALWAYS_FALSE.test(condition)) | ||
|
|
||
| // `enclosing` is the `if:` of each composite call around the step, from | ||
| // `flattenSteps`. The analyze action uploads to code scanning only when | ||
| // `upload` is `always`, which is its default. | ||
| function analysisProblem(step, enclosing = []) { | ||
| if (alwaysFalse(step?.if)) { | ||
| return '`if:` is always false, so the analysis never runs' | ||
| } | ||
| if (enclosing.some(alwaysFalse)) { | ||
| return 'the composite action around it is called with an `if:` that is always false, so the analysis never runs' | ||
| } | ||
| const upload = step?.with?.upload | ||
| if (upload !== undefined && upload !== 'always') { | ||
| return `\`upload: ${upload}\` keeps the results out of code scanning. Remove \`upload\`, or set it to \`always\`.` | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| const scanned = workflowFiles().flatMap((file) => | ||
| Object.entries(readWorkflow(file)?.jobs ?? {}).map(([id, job]) => ({ | ||
| ref: `${file}#${id}`, | ||
| ...codeqlSteps(job?.steps, resolveRepoComposite), | ||
| })), | ||
| ) | ||
| const codeqlJobs = scanned.filter(({ actions }) => actions.length > 0) | ||
|
|
||
| describe('CodeQL jobs run the analysis', () => { | ||
| it('finds the CodeQL job', () => { | ||
| // An empty scan would pass the check below. | ||
| expect(codeqlJobs.map(({ ref }) => ref)).toContain( | ||
| '.github/workflows/codeql.yml#analyze', | ||
| ) | ||
| }) | ||
|
|
||
| it('opens every local action that a job uses', () => { | ||
| const unresolved = scanned.flatMap(({ ref, unresolved }) => | ||
| unresolved.map((entry) => `${ref}: ${entry}`), | ||
| ) | ||
| expect(unresolved).toEqual([]) | ||
| }) | ||
|
|
||
| it('runs init once, then analyze, in every CodeQL job', () => { | ||
| const wrong = codeqlJobs | ||
| .filter(({ actions }) => actions.join(', ') !== 'init, analyze') | ||
| .map(({ ref, actions }) => `${ref}: ${actions.join(', ')}`) | ||
| expect(wrong).toEqual([]) | ||
| }) | ||
|
|
||
| it('runs every CodeQL analysis and uploads its results', () => { | ||
| const wrong = codeqlJobs.flatMap(({ ref, analyses }) => | ||
| analyses.flatMap(({ step, label, conditions }) => { | ||
| const problem = analysisProblem(step, conditions) | ||
| return problem ? [`${ref}: ${label}: ${problem}`] : [] | ||
| }), | ||
| ) | ||
| expect(wrong).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('the checker this guard depends on', () => { | ||
| const init = { uses: 'github/codeql-action/[email protected]' } | ||
| const analyze = { uses: 'github/codeql-action/[email protected]' } | ||
| const callComposite = { uses: './.github/actions/codeql' } | ||
| const noComposites = () => null | ||
| const composite = (steps) => (uses) => | ||
| uses === callComposite.uses ? { id: 'codeql/action.yml', steps } : null | ||
|
|
||
| it('reproduces the defect: init named twice', () => { | ||
| expect(codeqlSteps([init, init], noComposites).actions).toEqual([ | ||
| 'init', | ||
| 'init', | ||
| ]) | ||
| }) | ||
|
|
||
| it('finds init inside a composite action', () => { | ||
| expect( | ||
| codeqlSteps([callComposite, analyze], composite([init])).actions, | ||
| ).toEqual(['init', 'analyze']) | ||
| }) | ||
|
|
||
| it('finds a composite action that runs init with no analysis', () => { | ||
| expect(codeqlSteps([callComposite], composite([init])).actions).toEqual([ | ||
| 'init', | ||
| ]) | ||
| }) | ||
|
|
||
| it('counts each call to the same composite action', () => { | ||
| expect( | ||
| codeqlSteps([callComposite, callComposite, analyze], composite([init])) | ||
| .actions, | ||
| ).toEqual(['init', 'init', 'analyze']) | ||
| }) | ||
|
|
||
| it('reports a local action it cannot open', () => { | ||
| expect(codeqlSteps([callComposite], noComposites).unresolved).toHaveLength( | ||
| 1, | ||
| ) | ||
| }) | ||
|
|
||
| it('finds an analysis inside a composite action, to check its settings', () => { | ||
| const skipped = { ...analyze, if: false } | ||
| const { analyses } = codeqlSteps( | ||
| [init, callComposite], | ||
| composite([skipped]), | ||
| ) | ||
| expect(analyses.map(({ step }) => step)).toEqual([skipped]) | ||
| }) | ||
|
|
||
| // biome-ignore lint/suspicious/noTemplateCurlyInString: a literal Actions expression, as written in a workflow. | ||
| it.each([false, 'false', '${{ false }}'])( | ||
| 'refuses an analysis with `if: %j`', | ||
| (condition) => { | ||
| expect(analysisProblem({ ...analyze, if: condition })).toMatch(/`if:`/) | ||
| }, | ||
| ) | ||
|
|
||
| it('refuses an analysis inside a composite action called with `if: false`', () => { | ||
| const [analysis] = codeqlSteps( | ||
| [init, { ...callComposite, if: false }], | ||
| composite([analyze]), | ||
| ).analyses | ||
| expect(analysisProblem(analysis.step, analysis.conditions)).toMatch(/`if:`/) | ||
| }) | ||
|
|
||
| it('accepts an analysis inside a composite action called with no `if:`', () => { | ||
| const [analysis] = codeqlSteps( | ||
| [init, callComposite], | ||
| composite([analyze]), | ||
| ).analyses | ||
| expect(analysisProblem(analysis.step, analysis.conditions)).toBeNull() | ||
| }) | ||
|
|
||
| it('accepts an analysis whose `if:` can be true', () => { | ||
| expect( | ||
| analysisProblem({ ...analyze, if: "github.event_name == 'push'" }), | ||
| ).toBeNull() | ||
| }) | ||
|
|
||
| it.each(['never', 'failure-only', false])( | ||
| 'refuses an analysis with `upload: %j`', | ||
| (upload) => { | ||
| expect(analysisProblem({ ...analyze, with: { upload } })).toMatch( | ||
| /`upload: /, | ||
| ) | ||
| }, | ||
| ) | ||
|
|
||
| it('accepts an analysis that leaves `upload` unset, or sets it to always', () => { | ||
| expect(analysisProblem(analyze)).toBeNull() | ||
| expect( | ||
| analysisProblem({ ...analyze, with: { upload: 'always' } }), | ||
| ).toBeNull() | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.