From 685106e7eb5da0fbd5cda2619c6eb5ddd5824bb6 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:47:28 +0100 Subject: [PATCH] feat: pr-template asks every pull request what, why and how it was tested --- crates/sourceorigin-standard/README.md | 1 + crates/sourceorigin-standard/src/rules/fix.rs | 6 +++ crates/sourceorigin-standard/src/rules/mod.rs | 7 ++++ .../src/rules/pr_template.rs | 39 +++++++++++++++++++ .../src/rules/tests/mod.rs | 7 +++- .../src/rules/tests/pr_template.rs | 20 ++++++++++ 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 crates/sourceorigin-standard/src/rules/pr_template.rs create mode 100644 crates/sourceorigin-standard/src/rules/tests/pr_template.rs diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 337e12d..e5acd8a 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -58,6 +58,7 @@ A repository adopts the standard in four steps: - `job-timeouts`: Every job of a workflow sets `timeout-minutes`, except one calling a reusable workflow. - `cache-saves`: Every `Swatinem/rust-cache` step's `save-if` is `false` or saves from `refs/heads/main` only, and no composite action, nor workflow run by `pull_request`, `pull_request_target` or `merge_group`, uses `actions/cache`, which saves on a miss: they use `actions/cache/restore`, saving with `actions/cache/save` from `main`. - `scheduled-audit`: A workflow runs `just audit` (or `cargo deny`) on a `schedule` at least weekly: a cron whose day of the month and month are `*`. +- `pr-template`: `.github/pull_request_template.md` has the headings `## What and why` and `## Testing`. Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and diff --git a/crates/sourceorigin-standard/src/rules/fix.rs b/crates/sourceorigin-standard/src/rules/fix.rs index 3a45948..88bfe5f 100644 --- a/crates/sourceorigin-standard/src/rules/fix.rs +++ b/crates/sourceorigin-standard/src/rules/fix.rs @@ -75,6 +75,12 @@ impl Fix { } } + /// A fix adding `text`, which may span lines, at the end of `path`, a + /// file that isn't TOML. + pub(super) fn append(path: impl Into, what: String, text: String) -> Self { + Self::line(path, what, usize::MAX, text) + } + /// Makes `fixes` under `root`: each file's once, in order, and drops a /// fix that repeats another. A missing file is written, from an empty /// document. diff --git a/crates/sourceorigin-standard/src/rules/mod.rs b/crates/sourceorigin-standard/src/rules/mod.rs index a0ab658..770bba1 100644 --- a/crates/sourceorigin-standard/src/rules/mod.rs +++ b/crates/sourceorigin-standard/src/rules/mod.rs @@ -25,6 +25,7 @@ mod level; mod manifest; mod modules; mod nextest_config; +mod pr_template; mod recipes; mod recipes_only; mod report; @@ -244,6 +245,9 @@ rules! { /// A workflow runs `just audit` (or `cargo deny`) on a `schedule` at /// least weekly: a cron whose day of the month and month are `*`. ScheduledAudit = "scheduled-audit" => scheduled_audit, + /// `.github/pull_request_template.md` has the headings `## What and + /// why` and `## Testing`. + PrTemplate = "pr-template" => pr_template, } /// Why the rules couldn't be checked. @@ -446,6 +450,8 @@ struct Repository { t3: Option, /// The workflows and composite actions. workflows: Vec, + /// The pull request template. + pr_template: Option, } impl Repository { @@ -474,6 +480,7 @@ impl Repository { justfile: Justfile::load(root)?, t3: T3::load(root)?, workflows: Workflow::load_all(root)?, + pr_template: TextFile::load_all(root, &[pr_template::PATH])?.pop(), metadata, sources, unparsed, diff --git a/crates/sourceorigin-standard/src/rules/pr_template.rs b/crates/sourceorigin-standard/src/rules/pr_template.rs new file mode 100644 index 0000000..c57b53b --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/pr_template.rs @@ -0,0 +1,39 @@ +//! `pr-template`: every pull request says what it changes, why, and how it +//! was tested. + +use super::fix::{Checked, Fix}; +use super::{Finding, Repository}; + +/// The template, from the repository's root. +pub(super) const PATH: &str = ".github/pull_request_template.md"; + +/// The headings it holds. +const HEADINGS: [&str; 2] = ["## What and why", "## Testing"]; + +impl Repository { + /// Each required heading the template lacks, fixed by adding it at the + /// end, which writes a missing template. + pub(super) fn pr_template(&self) -> Checked { + let text = self + .pr_template + .as_ref() + .map_or("", |file| file.text.as_str()); + let mut checked = Checked::default(); + for heading in HEADINGS { + if text.lines().any(|line| line.trim_end() == heading) { + continue; + } + checked.findings.push(Finding { + path: PATH.into(), + line: 1, + message: format!("the pull request template has no `{heading}`: add it"), + }); + checked.fixes.push(Fix::append( + PATH, + format!("add `{heading}`"), + format!("\n{heading}"), + )); + } + checked + } +} diff --git a/crates/sourceorigin-standard/src/rules/tests/mod.rs b/crates/sourceorigin-standard/src/rules/tests/mod.rs index d8ef909..d5888db 100644 --- a/crates/sourceorigin-standard/src/rules/tests/mod.rs +++ b/crates/sourceorigin-standard/src/rules/tests/mod.rs @@ -18,6 +18,7 @@ mod generated_lints; mod job_timeouts; mod layout; mod nextest_config; +mod pr_template; mod recipes; mod recipes_only; mod report; @@ -121,7 +122,7 @@ self_named_module_files = \"warn\" /// The configuration files every fixture holds, as the standard has them, /// so a rule's fixes touch only what a test writes. -const CONFIG: [(&str, &str); 4] = [ +const CONFIG: [(&str, &str); 5] = [ ( ".cargo/config.toml", "[alias]\nxtask = \"run --locked --package xtask --\"\n", @@ -138,6 +139,10 @@ const CONFIG: [(&str, &str); 4] = [ "clippy.toml", "allow-expect-in-tests = true\nallow-panic-in-tests = true\nallow-dbg-in-tests = true\n", ), + ( + ".github/pull_request_template.md", + "## What and why\n\n## Testing\n", + ), ]; /// A manifest tail inheriting the workspace's lints. diff --git a/crates/sourceorigin-standard/src/rules/tests/pr_template.rs b/crates/sourceorigin-standard/src/rules/tests/pr_template.rs new file mode 100644 index 0000000..34c44ed --- /dev/null +++ b/crates/sourceorigin-standard/src/rules/tests/pr_template.rs @@ -0,0 +1,20 @@ +use super::{Fixture, INHERITS}; +use crate::rules::Rule; + +#[test] +fn the_pull_request_template_asks_what_why_and_how_it_was_tested() { + let fixture = Fixture::new(); + fixture.add_crate("core", INHERITS).write( + ".github/pull_request_template.md", + "## What and why\n\n\n\n## Checklist\n", + ); + assert_eq!( + fixture.violations(Rule::PrTemplate), + [ + ".github/pull_request_template.md:1: the pull request template has no `## Testing`: \ + add it (pr-template)" + ] + ); + fixture.fix_rule(Rule::PrTemplate); + assert_eq!(fixture.violations(Rule::PrTemplate), Vec::::new()); +}