From bcea2aa28810273589b2e6f503b0cfdb38980565 Mon Sep 17 00:00:00 2001 From: John Michael Manlupig Date: Wed, 25 Nov 2026 02:30:48 +0800 Subject: [PATCH 1/2] ci(ci): skip code CI checks and CodeQL on docs-only pull requests --- .github/workflows/ci.yml | 94 +++++++++++++++++++++++++++++--- .github/workflows/codeint.yml | 14 +++++ .github/workflows/codeql.yml | 74 +++++++++++++++++++++++++ docs/checks/feedback-budget.md | 4 +- xtask/src/checks/budget.rs | 22 +++++++- xtask/src/checks/budget/tests.rs | 23 +++++++- xtask/src/runner.rs | 24 +++++++- 7 files changed, 239 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff27c939..82089f1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,8 +20,73 @@ env: CARGO_TERM_COLOR: never jobs: + changes: + name: changes + runs-on: ubuntu-24.04 + outputs: + docs_only: ${{ steps.classify.outputs.docs_only }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + with: + fetch-depth: 0 + - id: classify + env: + EVENT: ${{ github.event_name }} + BASE_REF: ${{ github.base_ref }} + BEFORE: ${{ github.event.before }} + run: | + is_doc_path() { + case "$1" in + README.md|CHANGELOG.md|CONTRIBUTING.md|GLOSSARY.md|LICENSE|llms.txt) + return 0 + ;; + docs/reference.md|docs/checks/*|docs/agents/review-and-merge.md|docs/agents/security.md) + return 1 + ;; + docs/*.md) + return 0 + ;; + *) + return 1 + ;; + esac + } + + if [ "$EVENT" = pull_request ]; then + range="origin/$BASE_REF...HEAD" + elif [ -n "$BEFORE" ] && [ "$BEFORE" != 0000000000000000000000000000000000000000 ] && git cat-file -e "$BEFORE^{commit}" 2>/dev/null; then + range="$BEFORE..HEAD" + else + echo "docs_only=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + if ! git diff --name-only --no-renames -z "$range" > "$RUNNER_TEMP/changed.bin"; then + echo "docs_only=false" >> "$GITHUB_OUTPUT" + exit 0 + fi + + count=0 + docs_only=true + while IFS= read -r -d '' path; do + count=$((count + 1)) + if ! is_doc_path "$path"; then + docs_only=false + break + fi + done < "$RUNNER_TEMP/changed.bin" + + if [ "$count" -eq 0 ]; then + docs_only=false + fi + + echo "docs_only=$docs_only" >> "$GITHUB_OUTPUT" + echo "Classified $count changed file(s): docs_only=$docs_only" + rust: name: rust + needs: [changes] + if: needs.changes.outputs.docs_only != 'true' runs-on: ubuntu-24.04 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -41,6 +106,8 @@ jobs: windows: name: windows + needs: [changes] + if: needs.changes.outputs.docs_only != 'true' runs-on: windows-2025 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -50,6 +117,8 @@ jobs: performance: name: performance + needs: [changes] + if: needs.changes.outputs.docs_only != 'true' runs-on: ubuntu-24.04 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -59,6 +128,7 @@ jobs: repository: name: repository + needs: [changes] runs-on: ubuntu-24.04 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -80,18 +150,24 @@ jobs: else echo "BASE=$(git rev-list --max-parents=0 HEAD | tail -n 1)" >> "$GITHUB_ENV" fi - - run: cargo xtask no-comments --base "$BASE" + - if: needs.changes.outputs.docs_only != 'true' + run: cargo xtask no-comments --base "$BASE" - run: cargo xtask commit-message --range "$BASE" - - run: npm run --silent duplicates + - if: needs.changes.outputs.docs_only != 'true' + run: npm run --silent duplicates - run: npx --no-install markdownlint-cli2 - uses: ./.github/actions/lychee - run: git ls-files -z '*.md' .rules ':!:.agents/skills/**' | xargs -0 lychee --config lychee.toml - - uses: taiki-e/install-action@75a7c0f87bd666af3cdb75c5b9e9d03ab6044e6f + - if: needs.changes.outputs.docs_only != 'true' + uses: taiki-e/install-action@75a7c0f87bd666af3cdb75c5b9e9d03ab6044e6f with: tool: cargo-shear@1.14.1 - - run: cargo shear - - uses: ./.github/actions/actionlint - - run: actionlint + - if: needs.changes.outputs.docs_only != 'true' + run: cargo shear + - if: needs.changes.outputs.docs_only != 'true' + uses: ./.github/actions/actionlint + - if: needs.changes.outputs.docs_only != 'true' + run: actionlint - run: gitleaks git --redact --no-banner risk-tier: @@ -126,8 +202,8 @@ jobs: budget: name: budget - if: always() && github.event_name == 'pull_request' - needs: [rust, windows, performance, repository, risk-tier] + if: always() && github.event_name == 'pull_request' && needs.changes.outputs.docs_only != 'true' + needs: [changes, rust, windows, performance, repository, risk-tier] runs-on: ubuntu-24.04 permissions: actions: read @@ -174,4 +250,4 @@ jobs: run: | report=() if [ "$SAME_REPOSITORY" = true ]; then report=(--report); fi - cargo xtask feedback-budget --loop ci-gate --run-id "$RUN_ID" --exclude-job budget "${report[@]}" + cargo xtask feedback-budget --loop ci-gate --run-id "$RUN_ID" --exclude-job budget --exclude-workflow CodeQL "${report[@]}" diff --git a/.github/workflows/codeint.yml b/.github/workflows/codeint.yml index 21b96e4e..be33aa94 100644 --- a/.github/workflows/codeint.yml +++ b/.github/workflows/codeint.yml @@ -2,6 +2,20 @@ name: codeint on: pull_request: + paths: + - '**' + - '!README.md' + - '!CHANGELOG.md' + - '!CONTRIBUTING.md' + - '!GLOSSARY.md' + - '!LICENSE' + - '!llms.txt' + - '!docs/*.md' + - '!docs/**/*.md' + - 'docs/reference.md' + - 'docs/checks/**' + - 'docs/agents/review-and-merge.md' + - 'docs/agents/security.md' permissions: contents: read diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 00000000..ff926dba --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,74 @@ +name: CodeQL + +on: + push: + branches: [main] + paths: + - '**' + - '!README.md' + - '!CHANGELOG.md' + - '!CONTRIBUTING.md' + - '!GLOSSARY.md' + - '!LICENSE' + - '!llms.txt' + - '!docs/*.md' + - '!docs/**/*.md' + - 'docs/reference.md' + - 'docs/checks/**' + - 'docs/agents/review-and-merge.md' + - 'docs/agents/security.md' + pull_request: + branches: [main] + paths: + - '**' + - '!README.md' + - '!CHANGELOG.md' + - '!CONTRIBUTING.md' + - '!GLOSSARY.md' + - '!LICENSE' + - '!llms.txt' + - '!docs/*.md' + - '!docs/**/*.md' + - 'docs/reference.md' + - 'docs/checks/**' + - 'docs/agents/review-and-merge.md' + - 'docs/agents/security.md' + schedule: + - cron: '30 1 * * 1' + +permissions: {} + +concurrency: + group: codeql-${{ github.ref }} + cancel-in-progress: true + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + runs-on: ubuntu-24.04 + permissions: + actions: read + contents: read + security-events: write + strategy: + fail-fast: false + matrix: + include: + - language: actions + build-mode: none + - language: javascript-typescript + build-mode: none + - language: rust + build-mode: none + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 + - name: Initialize CodeQL + uses: github/codeql-action/init@1bd86f2fdb4b86187fa4059f16f31a3202acc9ea + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@1bd86f2fdb4b86187fa4059f16f31a3202acc9ea + with: + category: "/language:${{ matrix.language }}" diff --git a/docs/checks/feedback-budget.md b/docs/checks/feedback-budget.md index 933b776a..dbfd21aa 100644 --- a/docs/checks/feedback-budget.md +++ b/docs/checks/feedback-budget.md @@ -12,8 +12,8 @@ The budget file is the `feedback_budget` table in [checks.toml](../../checks.tom At 80% of a budget it opens or updates one issue labelled `budget`; over the budget it fails. **Runs in**: -CI, `budget` job of the `ci` workflow, on every pull request. -It times the Git hook on the changed files, `cargo xtask affected-tests`, and the wall clock across every gating workflow run on the pull request's head commit, excluding the `budget` job. +CI, `budget` job of the `ci` workflow, on every pull request that changes non-documentation files (skipped on docs-only pull requests). +It times the Git hook on the changed files, `cargo xtask affected-tests`, and the wall clock across every gating workflow run on the pull request's head commit, excluding the `budget` job and non-gating `CodeQL` workflow. It opens or updates the `budget` issue only for pull requests from this repository, because a fork's token cannot write issues. **Error text**: diff --git a/xtask/src/checks/budget.rs b/xtask/src/checks/budget.rs index a8296edf..13db350b 100644 --- a/xtask/src/checks/budget.rs +++ b/xtask/src/checks/budget.rs @@ -145,10 +145,11 @@ fn workflow_key(run: &serde_json::Value) -> Option { }) } -pub(crate) fn gating_run_ids( +pub(crate) fn gating_run_ids_excluding( primary_run: &serde_json::Value, sha_runs: Option<&serde_json::Value>, primary_run_id: &str, + excluded_workflows: &[String], ) -> Vec { let mut run_ids = vec![primary_run_id.to_string()]; if pull_request_head_sha(primary_run).is_none() { @@ -169,6 +170,13 @@ pub(crate) fn gating_run_ids( if run.get("conclusion").and_then(serde_json::Value::as_str) == Some("cancelled") { continue; } + if run + .get("name") + .and_then(serde_json::Value::as_str) + .is_some_and(|name| excluded_workflows.iter().any(|excluded| excluded == name)) + { + continue; + } let Some(id) = run.get("id").and_then(serde_json::Value::as_u64) else { continue; }; @@ -190,6 +198,15 @@ pub(crate) fn gating_run_ids( run_ids } +#[cfg(test)] +pub(crate) fn gating_run_ids( + primary_run: &serde_json::Value, + sha_runs: Option<&serde_json::Value>, + primary_run_id: &str, +) -> Vec { + gating_run_ids_excluding(primary_run, sha_runs, primary_run_id, &[]) +} + fn fetch_json(root: &Path, endpoint: &str, description: &str) -> Result { let output = gh(root, &["api", endpoint])?; serde_json::from_str(&output).map_err(|error| format!("cannot parse {description}: {error}")) @@ -198,6 +215,7 @@ fn fetch_json(root: &Path, endpoint: &str, description: &str) -> Result Result, String> { let primary_run = fetch_json( root, @@ -214,7 +232,7 @@ pub(crate) fn fetch_ci_gate_jobs( )?), None => None, }; - gating_run_ids(&primary_run, sha_runs.as_ref(), run_id) + gating_run_ids_excluding(&primary_run, sha_runs.as_ref(), run_id, excluded_workflows) .iter() .map(|id| { fetch_json( diff --git a/xtask/src/checks/budget/tests.rs b/xtask/src/checks/budget/tests.rs index 909e1311..3cee0a82 100644 --- a/xtask/src/checks/budget/tests.rs +++ b/xtask/src/checks/budget/tests.rs @@ -1,5 +1,6 @@ use super::{ - Verdict, ci_gate_seconds, gating_run_ids, parse_timestamp, pull_request_head_sha, verdict, + Verdict, ci_gate_seconds, gating_run_ids, gating_run_ids_excluding, parse_timestamp, + pull_request_head_sha, verdict, }; #[test] @@ -116,3 +117,23 @@ fn gating_run_ids_handles_pull_request_with_only_ci_run() { vec!["400".to_string()] ); } + +#[test] +fn gating_run_ids_excludes_named_non_gating_workflows() { + let primary = serde_json::json!({ + "id": 500, + "workflow_id": 10, + "name": "ci", + "event": "pull_request", + "head_sha": "deadbeef" + }); + let sha_runs = serde_json::json!({"workflow_runs": [ + {"id": 500, "workflow_id": 10, "name": "ci", "event": "pull_request", "conclusion": null}, + {"id": 501, "workflow_id": 20, "name": "pull-request", "event": "pull_request", "conclusion": "success"}, + {"id": 502, "workflow_id": 30, "name": "CodeQL", "event": "pull_request", "conclusion": "success"} + ]}); + assert_eq!( + gating_run_ids_excluding(&primary, Some(&sha_runs), "500", &["CodeQL".to_string()]), + vec!["500".to_string(), "501".to_string()] + ); +} diff --git a/xtask/src/runner.rs b/xtask/src/runner.rs index bddd0e65..52a6c941 100644 --- a/xtask/src/runner.rs +++ b/xtask/src/runner.rs @@ -34,7 +34,7 @@ Loops: hook the pre-commit checks on staged files affected-tests [--base {ref}] full every check and every test - feedback-budget --loop {hook|relevant|ci-gate} (--seconds {n} | --run-id {id} --exclude-job {name}) [--report] + feedback-budget --loop {hook|relevant|ci-gate} (--seconds {n} | --run-id {id} --exclude-job {name} [--exclude-workflow {name}]) [--report] perf-check [--binary {path}]"; pub(crate) fn run(command: &str, args: &[String]) -> Result { @@ -80,6 +80,24 @@ fn flag(args: &[String], name: &str) -> Result { .ok_or_else(|| format!("missing {name} {{value}}")) } +fn repeated_flag(args: &[String], name: &str) -> Result, String> { + let mut values = Vec::new(); + let mut index = 0; + while index < args.len() { + if args[index] == name { + let value = args + .get(index + 1) + .cloned() + .ok_or_else(|| format!("missing {name} {{value}}"))?; + values.push(value); + index += 2; + } else { + index += 1; + } + } + Ok(values) +} + fn read_file(path: &str) -> Result { fs::read_to_string(path).map_err(|error| format!("cannot read {path}: {error}")) } @@ -389,7 +407,9 @@ fn feedback_budget( .parse::() .map_err(|_| format!("--seconds must be a number, not {seconds}"))?, Err(_) => { - let jobs = budget::fetch_ci_gate_jobs(root, &flag(args, "--run-id")?)?; + let excluded_workflows = repeated_flag(args, "--exclude-workflow")?; + let jobs = + budget::fetch_ci_gate_jobs(root, &flag(args, "--run-id")?, &excluded_workflows)?; budget::ci_gate_seconds(&jobs, &flag(args, "--exclude-job")?) .ok_or("the run has no completed jobs")? } From 18cdc4ac2624a1d567de693f0521b5d09367fee6 Mon Sep 17 00:00:00 2001 From: John Michael Manlupig Date: Wed, 25 Nov 2026 02:42:31 +0800 Subject: [PATCH 2/2] ci(ci): fail closed when changes job fails and resolve codeint diff findings --- .github/workflows/ci.yml | 9 +++++---- .github/workflows/codeql.yml | 1 - xtask/src/checks/budget/tests.rs | 31 +++++++++++++++++++------------ xtask/src/runner.rs | 25 +++++++++++++------------ xtask/src/runner/tests.rs | 26 +++++++++++++++++++++++++- 5 files changed, 62 insertions(+), 30 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 82089f1f..88078dab 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -86,7 +86,7 @@ jobs: rust: name: rust needs: [changes] - if: needs.changes.outputs.docs_only != 'true' + if: ${{ !cancelled() && needs.changes.outputs.docs_only != 'true' }} runs-on: ubuntu-24.04 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -107,7 +107,7 @@ jobs: windows: name: windows needs: [changes] - if: needs.changes.outputs.docs_only != 'true' + if: ${{ !cancelled() && needs.changes.outputs.docs_only != 'true' }} runs-on: windows-2025 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -118,7 +118,7 @@ jobs: performance: name: performance needs: [changes] - if: needs.changes.outputs.docs_only != 'true' + if: ${{ !cancelled() && needs.changes.outputs.docs_only != 'true' }} runs-on: ubuntu-24.04 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -129,6 +129,7 @@ jobs: repository: name: repository needs: [changes] + if: ${{ !cancelled() }} runs-on: ubuntu-24.04 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -202,7 +203,7 @@ jobs: budget: name: budget - if: always() && github.event_name == 'pull_request' && needs.changes.outputs.docs_only != 'true' + if: ${{ !cancelled() && github.event_name == 'pull_request' && needs.changes.outputs.docs_only != 'true' }} needs: [changes, rust, windows, performance, repository, risk-tier] runs-on: ubuntu-24.04 permissions: diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index ff926dba..0a09fdbc 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -18,7 +18,6 @@ on: - 'docs/agents/review-and-merge.md' - 'docs/agents/security.md' pull_request: - branches: [main] paths: - '**' - '!README.md' diff --git a/xtask/src/checks/budget/tests.rs b/xtask/src/checks/budget/tests.rs index 3cee0a82..76b32210 100644 --- a/xtask/src/checks/budget/tests.rs +++ b/xtask/src/checks/budget/tests.rs @@ -120,20 +120,27 @@ fn gating_run_ids_handles_pull_request_with_only_ci_run() { #[test] fn gating_run_ids_excludes_named_non_gating_workflows() { - let primary = serde_json::json!({ - "id": 500, - "workflow_id": 10, - "name": "ci", - "event": "pull_request", - "head_sha": "deadbeef" + let make_run = |id: u64, workflow_id: u64, name: &str, conclusion: Option<&str>| { + serde_json::json!({ + "id": id, + "workflow_id": workflow_id, + "name": name, + "event": "pull_request", + "head_sha": "deadbeef", + "conclusion": conclusion, + }) + }; + let primary = make_run(500, 10, "ci", None); + let sha_runs = serde_json::json!({ + "workflow_runs": [ + make_run(500, 10, "ci", None), + make_run(501, 20, "pull-request", Some("success")), + make_run(502, 30, "CodeQL", Some("success")), + ] }); - let sha_runs = serde_json::json!({"workflow_runs": [ - {"id": 500, "workflow_id": 10, "name": "ci", "event": "pull_request", "conclusion": null}, - {"id": 501, "workflow_id": 20, "name": "pull-request", "event": "pull_request", "conclusion": "success"}, - {"id": 502, "workflow_id": 30, "name": "CodeQL", "event": "pull_request", "conclusion": "success"} - ]}); + let excluded = vec!["CodeQL".to_string()]; assert_eq!( - gating_run_ids_excluding(&primary, Some(&sha_runs), "500", &["CodeQL".to_string()]), + gating_run_ids_excluding(&primary, Some(&sha_runs), "500", &excluded), vec!["500".to_string(), "501".to_string()] ); } diff --git a/xtask/src/runner.rs b/xtask/src/runner.rs index 52a6c941..30f09188 100644 --- a/xtask/src/runner.rs +++ b/xtask/src/runner.rs @@ -396,24 +396,25 @@ fn affected_tests(root: &Path, args: &[String]) -> Result { }) } +fn feedback_loop_seconds(root: &Path, args: &[String]) -> Result { + if let Ok(seconds) = flag(args, "--seconds") { + return seconds + .parse::() + .map_err(|_| format!("--seconds must be a number, not {seconds}")); + } + let excluded_workflows = repeated_flag(args, "--exclude-workflow")?; + let jobs = budget::fetch_ci_gate_jobs(root, &flag(args, "--run-id")?, &excluded_workflows)?; + budget::ci_gate_seconds(&jobs, &flag(args, "--exclude-job")?) + .ok_or_else(|| "the run has no completed jobs".to_string()) +} + fn feedback_budget( root: &Path, settings: &Settings, args: &[String], ) -> Result { let feedback_loop = FeedbackLoop::from_name(&flag(args, "--loop")?)?; - let seconds = match flag(args, "--seconds") { - Ok(seconds) => seconds - .parse::() - .map_err(|_| format!("--seconds must be a number, not {seconds}"))?, - Err(_) => { - let excluded_workflows = repeated_flag(args, "--exclude-workflow")?; - let jobs = - budget::fetch_ci_gate_jobs(root, &flag(args, "--run-id")?, &excluded_workflows)?; - budget::ci_gate_seconds(&jobs, &flag(args, "--exclude-job")?) - .ok_or("the run has no completed jobs")? - } - }; + let seconds = feedback_loop_seconds(root, args)?; let budget_seconds = feedback_loop.budget_seconds(settings); println!( "{} took {seconds:.0} seconds of a {budget_seconds:.0}-second budget.", diff --git a/xtask/src/runner/tests.rs b/xtask/src/runner/tests.rs index b885e0a7..467f8698 100644 --- a/xtask/src/runner/tests.rs +++ b/xtask/src/runner/tests.rs @@ -1,7 +1,7 @@ use std::collections::BTreeSet; use std::path::{Path, PathBuf}; -use super::affected_packages; +use super::{affected_packages, repeated_flag}; fn packages(root: &Path) -> Vec<(String, PathBuf, BTreeSet)> { [ @@ -54,3 +54,27 @@ fn documents_affect_nothing_and_the_lockfile_affects_everything() { names(&["cli", "metrics", "model", "xtask"]) ); } + +#[test] +fn repeated_flag_collects_zero_or_more_values_and_rejects_dangling_flag() { + let to_args = + |items: &[&str]| -> Vec { items.iter().map(|s| (*s).to_string()).collect() }; + assert_eq!( + repeated_flag(&to_args(&["--loop", "ci-gate"]), "--exclude-workflow"), + Ok(Vec::new()) + ); + assert_eq!( + repeated_flag( + &to_args(&[ + "--exclude-workflow", + "CodeQL", + "--report", + "--exclude-workflow", + "codeint" + ]), + "--exclude-workflow" + ), + Ok(vec!["CodeQL".to_string(), "codeint".to_string()]) + ); + assert!(repeated_flag(&to_args(&["--exclude-workflow"]), "--exclude-workflow").is_err()); +}