Skip to content
Draft
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
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
6 changes: 6 additions & 0 deletions crates/sourceorigin-standard/src/rules/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>, 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.
Expand Down
7 changes: 7 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod level;
mod manifest;
mod modules;
mod nextest_config;
mod pr_template;
mod recipes;
mod recipes_only;
mod report;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -446,6 +450,8 @@ struct Repository {
t3: Option<T3>,
/// The workflows and composite actions.
workflows: Vec<Workflow>,
/// The pull request template.
pr_template: Option<TextFile>,
}

impl Repository {
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions crates/sourceorigin-standard/src/rules/pr_template.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 6 additions & 1 deletion crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand All @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/pr_template.rs
Original file line number Diff line number Diff line change
@@ -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<!-- What changes. -->\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::<String>::new());
}