Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/scripts/verify-codeql.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const LANGUAGES = ['actions', 'javascript-typescript', 'python']

async function requireCodeQLAnalyses({ github, owner, repo, sha, ref }) {
const analyses = await github.paginate(github.rest.codeScanning.listRecentAnalyses, {
owner, repo, ref, tool_name: 'CodeQL', per_page: 100,
})
for (const language of LANGUAGES) {
const latest = analyses.filter(analysis => analysis.ref === ref
&& analysis.commit_sha === sha && analysis.tool?.name === 'CodeQL'
&& analysis.analysis_key === '.github/workflows/codeql.yml:analyze'
&& analysis.category === `/language:${language}`)
.sort((a, b) => b.id - a.id)[0]
if (!latest || latest.error !== '') {
throw new Error(`CodeQL 分析 ${language} 缺失或处理失败,禁止通过检查`)
}
}
}

module.exports = requireCodeQLAnalyses
11 changes: 8 additions & 3 deletions .github/scripts/verify-release.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const requireCodeQLAnalyses = require('./verify-codeql.cjs')

const REQUIREMENTS = [
{
path: '.github/workflows/checks.yml',
Expand All @@ -6,10 +8,11 @@ const REQUIREMENTS = [
jobs: ['Backend lint, types and tests', 'Frontend lint, types, build and regressions'],
},
{
path: 'dynamic/github-code-scanning/codeql',
path: '.github/workflows/codeql.yml',
name: 'CodeQL',
events: ['dynamic'],
jobs: ['Analyze (actions)', 'Analyze (javascript-typescript)', 'Analyze (python)'],
events: ['push', 'workflow_dispatch'],
jobs: ['Analyze (actions)', 'Analyze (javascript-typescript)', 'Analyze (python)',
'CodeQL required'],
},
]

Expand Down Expand Up @@ -75,6 +78,8 @@ async function verifyRelease({ github, context, core }) {
selected.push({ required, run })
}

await requireCodeQLAnalyses({ github, owner, repo, sha, ref: context.ref })

const latestRuns = await listRuns()
for (const { required, run } of selected) {
assertUnchanged(required, run, latestRun(latestRuns, required))
Expand Down
156 changes: 152 additions & 4 deletions .github/scripts/verify-release.test.cjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const assert = require('node:assert/strict')
const { test } = require('node:test')
const verifyRelease = require('./verify-release.cjs')
const requireCodeQLAnalyses = require('./verify-codeql.cjs')

const SHA = 'a'.repeat(40)
const NAMES = {
10: ['Backend lint, types and tests', 'Frontend lint, types, build and regressions'],
20: ['Analyze (actions)', 'Analyze (javascript-typescript)', 'Analyze (python)'],
20: ['Analyze (actions)', 'Analyze (javascript-typescript)', 'Analyze (python)',
'CodeQL required'],
}

function fixture(branch = 'main') {
const runs = [
{ id: 10, path: '.github/workflows/checks.yml', event: 'push' },
{ id: 20, path: 'dynamic/github-code-scanning/codeql', event: 'dynamic' },
{ id: 20, path: '.github/workflows/codeql.yml', event: 'push' },
].map(run => ({
...run, head_sha: SHA, head_branch: branch, run_attempt: 1,
head_repository: { full_name: 'example/project' },
Expand All @@ -21,9 +23,14 @@ function fixture(branch = 'main') {
names.map(name => ({ name, head_sha: SHA, run_attempt: 1,
status: 'completed', conclusion: 'success' })),
]))
const analyses = ['actions', 'javascript-typescript', 'python'].map((language, index) => ({
id: 100 + index, ref: `refs/heads/${branch}`, commit_sha: SHA,
tool: { name: 'CodeQL' }, analysis_key: '.github/workflows/codeql.yml:analyze',
category: `/language:${language}`, error: '',
}))
const outputs = {}
const requests = []
const state = { runs, jobs, outputs, requests, current: run => run }
const state = { runs, jobs, analyses, outputs, requests, current: run => run }
const actions = {
listWorkflowRunsForRepo: 'runs', listJobsForWorkflowRun: 'jobs',
getWorkflowRun: async ({ run_id }) => ({
Expand All @@ -38,10 +45,14 @@ function fixture(branch = 'main') {
ref: `refs/heads/${branch}`, eventName: 'workflow_dispatch' },
core: { info() {}, setOutput(name, value) { outputs[name] = value }, summary },
github: {
rest: { actions },
rest: { actions, codeScanning: { listRecentAnalyses: 'analyses' } },
async paginate(endpoint, params) {
requests.push([endpoint, params])
if (state.error) throw state.error
if (endpoint === 'analyses') {
if (state.analysisError) throw state.analysisError
return structuredClone(state.analyses)
}
return structuredClone(endpoint === 'runs' ? state.runs : state.jobs[params.run_id])
},
},
Expand All @@ -58,6 +69,14 @@ for (const branch of ['main', 'agent']) {
owner: 'example', repo: 'project', branch, head_sha: SHA, per_page: 100,
}])
assert.equal(state.requests[1][1].filter, 'all')
const analysisIndex = state.requests.findIndex(([endpoint]) => endpoint === 'analyses')
assert.deepEqual(state.requests[analysisIndex], ['analyses', {
owner: 'example', repo: 'project', ref: `refs/heads/${branch}`,
tool_name: 'CodeQL', per_page: 100,
}])
assert.deepEqual(state.requests.slice(0, analysisIndex).map(([endpoint]) => endpoint),
['runs', 'jobs', 'jobs'])
assert.equal(state.requests[analysisIndex + 1][0], 'runs')
})
}

Expand Down Expand Up @@ -99,6 +118,28 @@ test('CodeQL 缺失不放行', async () => {
assert.deepEqual(state.outputs, {})
})

for (const change of [
{ path: 'dynamic/github-code-scanning/codeql', event: 'dynamic' },
{ path: 'dynamic/github-code-scanning/codeql', event: 'push' },
{ event: 'pull_request' }, { event: 'schedule' }, { event: 'dynamic' },
]) {
test(`CodeQL 非可信路径或事件成功不能放行 ${JSON.stringify(change)}`, async () => {
const state = fixture()
Object.assign(state.runs[1], change)
await assert.rejects(verifyRelease(state.args), /CodeQL 缺少/)
assert.deepEqual(state.outputs, {})
})
}

for (const branch of ['main', 'agent']) {
test(`允许 ${branch} 同提交的 CodeQL 手动检查成功发布`, async () => {
const state = fixture(branch)
state.runs[1].event = 'workflow_dispatch'
await verifyRelease(state.args)
assert.deepEqual(state.outputs, { sha: SHA })
})
}

for (const [status, conclusion] of [
['completed', 'failure'], ['completed', 'cancelled'], ['completed', 'skipped'],
['completed', 'neutral'], ['completed', 'timed_out'], ['queued', null],
Expand Down Expand Up @@ -249,3 +290,110 @@ for (const id of [10, 20]) {
assert.deepEqual(state.outputs, {})
})
}

test('所有 jobs 成功但 CodeQL 分析记录为空仍拒绝发布', async () => {
const state = fixture()
state.analyses = []
await assert.rejects(verifyRelease(state.args), /CodeQL 分析 actions 缺失或处理失败/)
assert.deepEqual(state.outputs, {})
})

for (const language of ['actions', 'javascript-typescript', 'python']) {
test(`所有 jobs 成功但缺少 ${language} 分析仍拒绝发布`, async () => {
const state = fixture()
state.analyses = state.analyses.filter(analysis => analysis.category !== `/language:${language}`)
await assert.rejects(verifyRelease(state.args), new RegExp(`CodeQL 分析 ${language} 缺失`))
assert.deepEqual(state.outputs, {})
})
}

for (const change of [
{ commit_sha: 'b'.repeat(40) }, { ref: 'refs/heads/agent' },
{ analysis_key: 'dynamic/github-code-scanning/codeql:analyze' },
{ analysis_key: '.github/workflows/codeql.yml:other' },
{ tool: { name: 'Other' } }, { tool: null }, { category: '/language:other' },
]) {
test(`CodeQL 分析不匹配提交、引用或来源必须拒绝 ${JSON.stringify(change)}`, async () => {
const state = fixture()
Object.assign(state.analyses[0], change)
await assert.rejects(verifyRelease(state.args), /CodeQL 分析 actions 缺失/)
assert.deepEqual(state.outputs, {})
})
}

for (const error of ['fixture processing failed', null, undefined]) {
test(`CodeQL 分析 error 非空字符串必须拒绝 ${String(error)}`, async () => {
const state = fixture()
state.analyses[0].error = error
await assert.rejects(verifyRelease(state.args), /CodeQL 分析 actions 缺失或处理失败/)
assert.deepEqual(state.outputs, {})
})
}

test('CodeQL 分析 API 异常必须阻止发布', async () => {
const state = fixture()
state.analysisError = new Error('fixture code scanning forbidden')
await assert.rejects(verifyRelease(state.args), /code scanning forbidden/)
assert.deepEqual(state.outputs, {})
})

test('CodeQL 最新分析失败不能使用旧成功记录', async () => {
const state = fixture()
state.analyses.unshift({ ...state.analyses[0], id: 200, error: 'fixture processing failed' })
await assert.rejects(verifyRelease(state.args), /CodeQL 分析 actions 缺失或处理失败/)
assert.deepEqual(state.outputs, {})
})

test('CodeQL 最新分析成功可以替代旧失败记录', async () => {
const state = fixture()
state.analyses.push({ ...state.analyses[0], id: 99, error: 'fixture processing failed' })
await verifyRelease(state.args)
assert.deepEqual(state.outputs, { sha: SHA })
})

test('分析 helper 使用调用者传入的 PR 合并引用与合并提交', async () => {
const state = fixture()
const ref = 'refs/pull/21/merge'
const sha = 'b'.repeat(40)
state.analyses = state.analyses.map(analysis => ({ ...analysis, ref, commit_sha: sha }))
await requireCodeQLAnalyses({ github: state.args.github,
owner: 'example', repo: 'project', sha, ref })
assert.deepEqual(state.requests, [['analyses', {
owner: 'example', repo: 'project', ref, tool_name: 'CodeQL', per_page: 100,
}]])
})

for (const conclusion of ['skipped', 'neutral', 'missing']) {
test(`CodeQL required 任务 ${conclusion} 必须拒绝发布`, async () => {
const state = fixture()
if (conclusion === 'missing') state.jobs[20].pop()
else state.jobs[20].find(job => job.name === 'CodeQL required').conclusion = conclusion
await assert.rejects(verifyRelease(state.args), /必需任务 CodeQL required 缺失或未成功/)
assert.deepEqual(state.outputs, {})
})
}

for (const id of [10, 20]) {
for (const change of ['new run', 'rerun']) {
test(`分析 API 查询期间工作流 ${id} 出现 ${change} 必须被末尾复核拒绝`, async () => {
const state = fixture()
const paginate = state.args.github.paginate
state.args.github.paginate = async (endpoint, params) => {
const result = await paginate(endpoint, params)
if (endpoint === 'analyses') {
const index = state.runs.findIndex(run => run.id === id)
if (change === 'new run') {
state.runs.push({ ...state.runs[index], id: id + 1,
status: 'queued', conclusion: null })
} else {
state.runs[index] = { ...state.runs[index], run_attempt: 2,
status: 'in_progress', conclusion: null }
}
}
return result
}
await assert.rejects(verifyRelease(state.args), /检查状态已变化/)
assert.deepEqual(state.outputs, {})
})
}
}
83 changes: 83 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: CodeQL

on:
push:
branches: [main, agent]
pull_request:
branches: [main]
types: [opened, synchronize, reopened, edited, ready_for_review]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: codeql-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
analyze:
name: Analyze (${{ matrix.language }})
runs-on: ubuntu-24.04
timeout-minutes: 30
permissions:
actions: read
contents: read
packages: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [actions, javascript-typescript, python]
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: none
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@v4
with:
category: /language:${{ matrix.language }}
wait-for-processing: true

verify:
name: CodeQL required
needs: analyze
if: ${{ always() }}
runs-on: ubuntu-24.04
timeout-minutes: 5
permissions:
contents: read
actions: read
security-events: read
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Require successful analyses and uploaded results
uses: actions/github-script@v7
env:
ANALYZE_RESULT: ${{ needs.analyze.result }}
with:
script: |
if (process.env.ANALYZE_RESULT !== 'success') {
throw new Error('CodeQL analysis did not succeed');
}
const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, {
...context.repo, run_id: context.runId, filter: 'all', per_page: 100,
});
for (const language of ['actions', 'javascript-typescript', 'python']) {
const candidates = jobs.filter(job => job.name === `Analyze (${language})`);
const attempt = Math.max(...candidates.map(job => job.run_attempt));
const latest = candidates.filter(job => job.run_attempt === attempt);
if (latest.length !== 1 || latest[0].status !== 'completed'
|| latest[0].conclusion !== 'success') {
throw new Error(`CodeQL ${language} must complete successfully`);
}
}
const requireAnalyses = require('./.github/scripts/verify-codeql.cjs');
await requireAnalyses({github, ...context.repo, sha: context.sha, ref: context.ref});
2 changes: 2 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
permissions:
contents: read
actions: read
security-events: read

concurrency:
group: pages
Expand Down Expand Up @@ -64,6 +65,7 @@ jobs:
permissions:
contents: read
actions: read
security-events: read
pages: write
id-token: write
environment:
Expand Down
Loading
Loading