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

Each rule is a variant of [`Rule`]: its name is the one `standard.toml` and
Expand Down
30 changes: 30 additions & 0 deletions crates/sourceorigin-standard/src/rules/job_timeouts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! `job-timeouts`: a hung job fails rather than running for six hours.

use super::{Finding, Repository};

impl Repository {
/// Each job without `timeout-minutes`, except one calling a reusable
/// workflow, which can't set it.
pub(super) fn job_timeouts(&self) -> Vec<Finding> {
self.workflows
.iter()
.flat_map(|workflow| {
workflow
.jobs()
.iter()
.filter(|(_, job)| {
job.get("timeout-minutes").is_none() && job.get("uses").is_none()
})
.map(|(name, _)| Finding {
path: workflow.path.clone(),
line: name.line,
message: format!(
"the job `{}` has no `timeout-minutes`: give it a little over its \
longest run",
name.as_str().unwrap_or_default()
),
})
})
.collect()
}
}
4 changes: 4 additions & 0 deletions crates/sourceorigin-standard/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod config_comments;
mod deny_config;
mod fix;
mod generated_lints;
mod job_timeouts;
mod justfile;
mod layout;
mod level;
Expand Down Expand Up @@ -229,6 +230,9 @@ rules! {
/// Every workflow's top-level `permissions` is exactly
/// `contents: read`; a job that needs more sets its own.
WorkflowPermissions = "workflow-permissions" => workflow_permissions,
/// Every job of a workflow sets `timeout-minutes`, except one calling a
/// reusable workflow.
JobTimeouts = "job-timeouts" => job_timeouts,
}

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

#[test]
fn every_job_times_out() {
let fixture = Fixture::new();
fixture.add_crate("core", INHERITS).write(
".github/workflows/ci.yml",
"jobs:\n rust:\n timeout-minutes: 20\n docs:\n runs-on: ubuntu-latest\n \
release:\n uses: ./.github/workflows/release.yml\n",
);
assert_eq!(
fixture.violations(Rule::JobTimeouts),
[
".github/workflows/ci.yml:4: the job `docs` has no `timeout-minutes`: give it a \
little over its longest run (job-timeouts)"
]
);
}
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 @@ -14,6 +14,7 @@ mod deny_config;
mod fix;
mod framework;
mod generated_lints;
mod job_timeouts;
mod layout;
mod nextest_config;
mod recipes;
Expand Down