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 @@ -56,6 +56,7 @@ A repository adopts the standard in four steps:
- `actions-pinned`: Every `uses:` of a workflow or composite action naming another repository's action or workflow pins it to a 40-character commit SHA, followed by a `# v…` comment giving its version.
- `workflow-permissions`: Every workflow's top-level `permissions` is exactly `contents: read`; a job that needs more sets its own.
- `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`.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
57 changes: 57 additions & 0 deletions crates/sourceorigin-standard/src/rules/cache_saves.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! `cache-saves`: caches are saved from `main`, which every branch reads, so
//! a pull request never evicts them with entries no other branch can read.

use super::{Finding, Repository};

/// The events whose caches only their own ref can read.
const SCOPED: [&str; 3] = ["pull_request", "pull_request_target", "merge_group"];

impl Repository {
/// Each `Swatinem/rust-cache` step whose `save-if` isn't `false` and
/// doesn't limit saving to `main`, and each `actions/cache` step, which saves on a miss, in
/// a composite action or a workflow pull requests or the merge queue
/// run.
pub(super) fn cache_saves(&self) -> Vec<Finding> {
let mut findings = Vec::new();
for workflow in &self.workflows {
let scoped = workflow.is_action()
|| workflow
.triggers()
.iter()
.any(|trigger| SCOPED.contains(trigger));
for step in workflow.steps() {
let Some(uses) = step.get("uses") else {
continue;
};
let action = uses.as_str().unwrap_or_default();
let message = if action.starts_with("Swatinem/rust-cache@") {
let save_if = step.get("with").and_then(|with| with.get("save-if"));
let main_only =
save_if
.and_then(|save_if| save_if.as_str())
.is_some_and(|condition| {
condition == "false" || condition.contains("refs/heads/main")
});
(!main_only).then_some(
"the Rust cache is saved off `main`: set \
`save-if: ${{ github.ref == 'refs/heads/main' }}`",
)
} else if action.starts_with("actions/cache@") && scoped {
Some(
"`actions/cache` saves on a miss, here off `main`: restore with \
`actions/cache/restore`, and save with `actions/cache/save` from `main` \
only",
)
} else {
None
};
findings.extend(message.map(|message| Finding {
path: workflow.path.clone(),
line: uses.line,
message: message.to_owned(),
}));
}
}
findings
}
}
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 @@ -9,6 +9,7 @@ mod agent_files;
mod agents_md;
mod anyhow_edges;
mod budget;
mod cache_saves;
mod cargo_locked;
mod ci_shape;
mod clippy_config;
Expand Down Expand Up @@ -233,6 +234,12 @@ rules! {
/// Every job of a workflow sets `timeout-minutes`, except one calling a
/// reusable workflow.
JobTimeouts = "job-timeouts" => job_timeouts,
/// 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`.
CacheSaves = "cache-saves" => cache_saves,
}

/// Why the rules couldn't be checked.
Expand Down
50 changes: 50 additions & 0 deletions crates/sourceorigin-standard/src/rules/tests/cache_saves.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use super::{Fixture, INHERITS};
use crate::rules::Rule;

/// Steps saving caches from `main` only, and steps saving them anywhere.
const STEPS: &str = "\
jobs:
rust:
steps:
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2
with:
save-if: ${{ github.ref == 'refs/heads/main' }}
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2
with:
save-if: false
- uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
";

#[test]
fn caches_are_saved_from_main_only() {
let fixture = Fixture::new();
fixture
.add_crate("core", INHERITS)
.write(
".github/workflows/ci.yml",
&format!("on: [pull_request]\n{STEPS}"),
)
.write(
".github/workflows/main.yml",
&format!("on:\n push:\n{STEPS}"),
);
let rust_cache = |path: &str| {
format!(
"{path}:8: the Rust cache is saved off `main`: set `save-if: ${{{{ github.ref == \
'refs/heads/main' }}}}` (cache-saves)"
)
};
assert_eq!(
fixture.violations(Rule::CacheSaves),
[
rust_cache(".github/workflows/ci.yml"),
".github/workflows/ci.yml:13: `actions/cache` saves on a miss, here off `main`: \
restore with `actions/cache/restore`, and save with `actions/cache/save` from \
`main` only (cache-saves)"
.to_owned(),
rust_cache(".github/workflows/main.yml").replace(":8:", ":9:"),
]
);
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod agent_files;
mod agents_md;
mod anyhow_edges;
mod budget;
mod cache_saves;
mod cargo_locked;
mod ci_shape;
mod clippy_config;
Expand Down
Loading