diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff27c939..88078dab 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: ${{ !cancelled() && 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: ${{ !cancelled() && 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: ${{ !cancelled() && needs.changes.outputs.docs_only != 'true' }} runs-on: ubuntu-24.04 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -59,6 +128,8 @@ jobs: repository: name: repository + needs: [changes] + if: ${{ !cancelled() }} runs-on: ubuntu-24.04 steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 @@ -80,18 +151,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 +203,8 @@ jobs: budget: name: budget - if: always() && github.event_name == 'pull_request' - needs: [rust, windows, performance, repository, risk-tier] + 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: actions: read @@ -174,4 +251,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..0a09fdbc --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,73 @@ +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: + 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..76b32210 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,30 @@ 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 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 excluded = vec!["CodeQL".to_string()]; + assert_eq!( + 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 bddd0e65..30f09188 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}")) } @@ -378,22 +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 jobs = budget::fetch_ci_gate_jobs(root, &flag(args, "--run-id")?)?; - 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()); +}