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 @@ -53,6 +53,7 @@ A repository adopts the standard in four steps:
- `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.
- `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.
<!-- rules:end -->

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
47 changes: 47 additions & 0 deletions crates/sourceorigin-standard/src/rules/actions_pinned.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! `actions-pinned`: a workflow runs the exact code of each action it
//! reviewed.

use super::{Finding, Repository};

impl Repository {
/// Each `uses:` of a step or a job naming an action or workflow of
/// another repository by anything but a commit SHA with its version in
/// a comment.
pub(super) fn actions_pinned(&self) -> Vec<Finding> {
let mut findings = Vec::new();
for workflow in &self.workflows {
let jobs = workflow.jobs().iter().map(|(_, job)| job);
let uses = workflow
.steps()
.chain(jobs)
.filter_map(|node| node.get("uses"));
for used in uses {
let Some(action) = used.as_str() else {
continue;
};
if action.starts_with("./") || action.starts_with("docker://") {
continue;
}
let pinned = action.rsplit_once('@').is_some_and(|(_, sha)| {
sha.len() == 40 && sha.bytes().all(|b| b.is_ascii_hexdigit())
});
let line = workflow.text.lines().nth(used.line - 1).unwrap_or_default();
let versioned = line
.split_once(" #")
.is_some_and(|(_, comment)| comment.trim_start().starts_with('v'));
if !(pinned && versioned) {
findings.push(Finding {
path: workflow.path.clone(),
line: used.line,
message: format!(
"`{action}` isn't pinned: name the commit of its release, \
`{}@<40-character SHA> # v<version>`",
action.split('@').next().unwrap_or(action)
),
});
}
}
}
findings
}
}
5 changes: 5 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! rules `standard.toml` deviates from. [`describe`] lists the rules with
//! their docs.

mod actions_pinned;
mod agent_files;
mod agents_md;
mod anyhow_edges;
Expand Down Expand Up @@ -220,6 +221,10 @@ rules! {
/// `standard / pr-checks`, it is one of the repository's two required
/// checks.
CiShape = "ci-shape" => ci_shape,
/// 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.
ActionsPinned = "actions-pinned" => actions_pinned,
}

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

#[test]
fn actions_are_pinned_to_a_commit_with_their_version() {
let fixture = Fixture::new();
fixture.add_crate("core", INHERITS).write(
".github/workflows/ci.yml",
"jobs:\n rust:\n steps:\n \
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7\n \
- uses: actions/cache@v4\n \
- uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6\n \
- uses: ./.github/actions/ui\n \
release:\n uses: owner/repo/.github/workflows/release.yml@main\n",
);
assert_eq!(
fixture.violations(Rule::ActionsPinned),
[
".github/workflows/ci.yml:5: `actions/cache@v4` isn't pinned: name the commit of its \
release, `actions/cache@<40-character SHA> # v<version>` (actions-pinned)",
".github/workflows/ci.yml:6: `Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6` \
isn't pinned: name the commit of its release, `Swatinem/rust-cache@<40-character \
SHA> # v<version>` (actions-pinned)",
".github/workflows/ci.yml:9: `owner/repo/.github/workflows/release.yml@main` isn't \
pinned: name the commit of its release, \
`owner/repo/.github/workflows/release.yml@<40-character SHA> # v<version>` \
(actions-pinned)",
]
);
}
1 change: 1 addition & 0 deletions crates/sourceorigin-standard/src/rules/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! A fixture workspace the rules check, shared by the tests of each rule.

mod actions_pinned;
mod agent_files;
mod agents_md;
mod anyhow_edges;
Expand Down
Loading