diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12a784a..c260da5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,10 +1,12 @@ # CI runs the `just` recipes grouped by what they compile: `checks` compiles # nothing, `rust` builds once for the rest. Pull requests run only the jobs -# whose inputs changed (see `changes`); `ci` is the single required status. +# whose inputs changed (see `changes`); `ci` sums them up, and is one of the +# two required checks with `standard / pr-checks`. name: CI on: + merge_group: pull_request: push: branches: [main] diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 7bec7bf..7a7997c 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -52,6 +52,7 @@ A repository adopts the standard in four steps: - `recipes`: The justfile has the recipes `validate`, `fmt`, `fmt-check`, `lint`, `standard`, `test`, `doc`, `doc-test`, `audit`, `unused` and `check-changed`. `just` alone runs `validate` or `check-changed`; `validate` runs `fmt` first, then at least `lint`, `standard`, `test`, `doc`, `doc-test`, `audit` and `unused`, and never `fmt-check`; `test` takes `*ARGS`, defaulting to `--workspace`. - `recipes-only`: Every command of `t3.json` runs `just`, and no line of a workflow's or composite action's `run:` starts with `cargo` unless `standard.toml`'s `cargo-in-ci` lists it; each listed line is one a `run:` holds. - `t3-setup`: A recipe `t3.json` runs on worktree creation that runs Cargo, itself or through the recipes it runs, sets `CARGO_BUILD_JOBS` to a default, half the cores, that a set one overrides, and runs the build with `nice -n 10`, both in its own body. +- `ci-shape`: `.github/workflows/ci.yml` runs on `merge_group`, and has a `ci` job that needs every other job and runs `if: always()`: with `standard / pr-checks`, it is one of the repository's two required checks. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/ci_shape.rs b/crates/sourceorigin-standard/src/rules/ci_shape.rs new file mode 100644 index 0000000..e1145ed --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/ci_shape.rs @@ -0,0 +1,133 @@ +//! `ci-shape`: CI sums its jobs up in one `ci` status, which the merge +//! queue waits on. The `changes` job and its filters are `check-changed`'s +//! drift check's. + +use std::path::Path; + +use super::fix::{Checked, Fix}; +use super::workflows::Workflow; +use super::yaml::Node; +use super::{Finding, Repository}; + +/// The CI workflow, from the repository's root. +const PATH: &str = ".github/workflows/ci.yml"; + +/// The job summing the others up. +const CI: &str = "ci"; + +impl Workflow { + /// The job `key`. + fn job(&self, key: &str) -> Option<&Node> { + self.jobs() + .iter() + .find(|(name, _)| name.as_str() == Some(key)) + .map(|(_, job)| job) + } + + /// Where the CI workflow breaks the standard's shape: each line with + /// its message. + fn shape(&self) -> Vec<(usize, String)> { + let mut found = Vec::new(); + if !self.triggers().contains(&"merge_group") { + found.push(( + self.document.get("on").map_or(1, |on| on.line), + "CI doesn't run on `merge_group`: add the trigger, so the merge queue runs it" + .to_owned(), + )); + } + let Some(ci) = self.job(CI) else { + found.push(( + 1, + format!( + "no `{CI}` job: add one that needs every other job, runs `if: always()`, \ + and fails if any of them failed; with `standard / pr-checks`, it is one \ + of the repository's two required checks" + ), + )); + return found; + }; + if ci.get("if").and_then(Node::as_str) != Some("always()") { + found.push(( + ci.line, + format!( + "the `{CI}` job doesn't run `if: always()`: add it, so a failure upstream \ + fails it" + ), + )); + } + let needs = ci.get("needs"); + let needed: Vec<&str> = needs + .into_iter() + .flat_map(|needs| { + needs + .as_str() + .into_iter() + .chain(needs.items().iter().filter_map(Node::as_str)) + }) + .collect(); + let missing = self + .jobs() + .iter() + .filter_map(|(name, _)| name.as_str()) + .filter(|&name| name != CI && !needed.contains(&name)); + for name in missing { + found.push(( + needs.map_or(ci.line, |needs| needs.line), + format!("the `{CI}` job doesn't need `{name}`: add it to `needs`"), + )); + } + found + } + + /// The fix of a missing `merge_group` trigger, when `on:` is a mapping + /// on its own line. + fn merge_group_fix(&self) -> Option { + let mapping = self + .document + .get("on") + .is_some_and(|on| !on.entries().is_empty()); + if self.triggers().contains(&"merge_group") || !mapping { + return None; + } + let index = self + .text + .lines() + .position(|line| line.trim_end() == "on:")?; + Some(Fix::line( + PATH, + "run CI on `merge_group`".to_owned(), + index + 1, + "on:\n merge_group:".to_owned(), + )) + } +} + +impl Repository { + /// Where the CI workflow breaks the standard's shape, or a missing one. + pub(super) fn ci_shape(&self) -> Checked { + let Some(ci) = self + .workflows + .iter() + .find(|workflow| workflow.path == Path::new(PATH)) + else { + return vec![Finding { + path: PATH.into(), + line: 1, + message: format!("no {PATH}: add the CI workflow, with a `{CI}` job"), + }] + .into(); + }; + Checked { + findings: ci + .shape() + .into_iter() + .map(|(line, message)| Finding { + path: PATH.into(), + line, + message, + }) + .collect(), + fixes: ci.merge_group_fix().into_iter().collect(), + } + } +} diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index 0ea6162..e1e9174 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -9,6 +9,7 @@ mod agents_md; mod anyhow_edges; mod budget; mod cargo_locked; +mod ci_shape; mod clippy_config; mod comments; mod config_comments; @@ -214,6 +215,11 @@ rules! { /// default, half the cores, that a set one overrides, and runs the build /// with `nice -n 10`, both in its own body. T3Setup = "t3-setup" => t3_setup, + /// `.github/workflows/ci.yml` runs on `merge_group`, and has a `ci` job + /// that needs every other job and runs `if: always()`: with + /// `standard / pr-checks`, it is one of the repository's two required + /// checks. + CiShape = "ci-shape" => ci_shape, } /// Why the rules couldn't be checked. diff --git a/crates/sourceorigin-standard/src/rules/tests/ci_shape.rs b/crates/sourceorigin-standard/src/rules/tests/ci_shape.rs new file mode 100644 index 0000000..8508494 --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/ci_shape.rs @@ -0,0 +1,51 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +/// A CI workflow of the standard's shape. +const CI: &str = "\ +on: + pull_request: + merge_group: +jobs: + changes: + steps: + - id: filter + uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3 + with: + filters: .github/filters.yml + rust: + needs: changes + ci: + if: always() + needs: [changes, rust] +"; + +#[test] +fn ci_has_a_changes_job_and_one_status_the_queue_waits_on() { + let fixture = Fixture::new(); + fixture + .add_crate("core", INHERITS) + .write(".github/workflows/ci.yml", CI); + assert_eq!(fixture.violations(Rule::CiShape), Vec::::new()); + + fixture.write( + ".github/workflows/ci.yml", + "on:\n pull_request:\njobs:\n changes:\n runs-on: ubuntu-latest\n rust:\n \ + needs: changes\n docs:\n needs: changes\n ci:\n needs:\n - changes\n", + ); + assert_eq!( + fixture.violations(Rule::CiShape), + [ + ".github/workflows/ci.yml:2: CI doesn't run on `merge_group`: add the trigger, so \ + the merge queue runs it (ci-shape)", + ".github/workflows/ci.yml:11: the `ci` job doesn't run `if: always()`: add it, so a \ + failure upstream fails it (ci-shape)", + ".github/workflows/ci.yml:12: the `ci` job doesn't need `rust`: add it to `needs` \ + (ci-shape)", + ".github/workflows/ci.yml:12: the `ci` job doesn't need `docs`: add it to `needs` \ + (ci-shape)", + ] + ); + fixture.fix_rule(Rule::CiShape); + assert_eq!(fixture.violations(Rule::CiShape).len(), 3); +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index 39355c8..17ffce5 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -5,6 +5,7 @@ mod agents_md; mod anyhow_edges; mod budget; mod cargo_locked; +mod ci_shape; mod clippy_config; mod comments; mod config_comments; diff --git a/crates/sourceorigin-standard/src/rules/workflows.rs b/crates/sourceorigin-standard/src/rules/workflows.rs index bc02e60..473ef1b 100644 --- a/crates/sourceorigin-standard/src/rules/workflows.rs +++ b/crates/sourceorigin-standard/src/rules/workflows.rs @@ -18,6 +18,8 @@ const FILES: [&str; 4] = [ pub(super) struct Workflow { /// From the repository's root. pub(super) path: PathBuf, + /// Its text, whose comments the document drops. + pub(super) text: String, pub(super) document: Node, } @@ -30,6 +32,7 @@ impl Workflow { Ok(document) => Some(Ok(Self { document: document?, path: file.path, + text: file.text, })), Err(source) => Some(Err(Error::Yaml { path: file.path, @@ -39,6 +42,19 @@ impl Workflow { .collect() } + /// The events that trigger it, from `on` as a name, a list or a + /// mapping. + pub(super) fn triggers(&self) -> Vec<&str> { + let Some(on) = self.document.get("on") else { + return Vec::new(); + }; + on.as_str() + .into_iter() + .chain(on.items().iter().filter_map(Node::as_str)) + .chain(on.entries().iter().filter_map(|(event, _)| event.as_str())) + .collect() + } + /// Its jobs, each with its key; none for an action. pub(super) fn jobs(&self) -> &[(Node, Node)] { self.document.get("jobs").map_or(&[], Node::entries)