Skip to content
Merged
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
134 changes: 90 additions & 44 deletions crates/squabble-cli/src/triage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,71 @@ pub fn run(args: &[String]) -> ExitCode {
}
}

fn run_inner(args: &[String]) -> Result<(), String> {
let (mut log, mut step, mut lock, mut callers, mut workflow) = (None, None, None, None, None);
let mut json = false;
/// What the command line asked for.
#[derive(Default)]
struct Args {
log: Option<String>,
step: Option<String>,
lock: Option<String>,
callers: Option<String>,
workflow: Option<String>,
json: bool,
}

fn parse_args(args: &[String]) -> Result<Args, String> {
let mut a = Args::default();
let mut it = args.iter();
while let Some(a) = it.next() {
while let Some(arg) = it.next() {
let mut val = |name: &str| it.next().cloned().ok_or(format!("{name} needs a value"));
match a.as_str() {
"--log" => log = Some(val("--log")?),
"--step" => step = Some(val("--step")?),
"--lock" => lock = Some(val("--lock")?),
"--callers" => callers = Some(val("--callers")?),
"--workflow" => workflow = Some(val("--workflow")?),
"--json" => json = true,
match arg.as_str() {
"--log" => a.log = Some(val("--log")?),
"--step" => a.step = Some(val("--step")?),
"--lock" => a.lock = Some(val("--lock")?),
"--callers" => a.callers = Some(val("--callers")?),
"--workflow" => a.workflow = Some(val("--workflow")?),
"--json" => a.json = true,
other => return Err(format!("unknown argument `{other}`")),
}
}
if log.is_none() && lock.is_none() && workflow.is_none() {
if a.log.is_none() && a.lock.is_none() && a.workflow.is_none() {
return Err("give at least one of --log, --lock, --workflow".into());
}
if step.is_some() && log.is_none() {
if a.step.is_some() && a.log.is_none() {
return Err("--step needs --log".into());
}
if callers.is_some() && lock.is_none() {
if a.callers.is_some() && a.lock.is_none() {
return Err("--callers needs --lock".into());
}
Ok(a)
}

/// The rules found, in discovery order, one entry per rule id, each with an
/// optional detail line; and the last lines of the log, for a referral.
#[derive(Default)]
struct Findings {
found: Vec<(&'static str, Option<String>)>,
signature: Option<String>,
}

// (rule id, detail) in discovery order, one row per rule id.
let mut signature: Option<String> = None;
let mut found: Vec<(&'static str, Option<String>)> = Vec::new();
let mut add = |id: &'static str, detail: Option<String>| {
if !found.iter().any(|(i, _)| *i == id) {
found.push((id, detail));
impl Findings {
fn add(&mut self, id: &'static str, detail: Option<String>) {
if !self.found.iter().any(|(i, _)| *i == id) {
self.found.push((id, detail));
}
};
if let Some(p) = &log {
let text = read(p)?;
signature = Some(tail_lines(&text, 3));
for id in match_log(&text, step.as_deref()) {
add(id, None);
}

fn scan_log(&mut self, path: &str, step: Option<&str>) -> Result<(), String> {
let text = read(path)?;
self.signature = Some(tail_lines(&text, 3));
for id in match_log(&text, step) {
self.add(id, None);
}
Ok(())
}
if let Some(p) = &lock {
let text = read(p)?;
let callers: Vec<String> = match &callers {

fn scan_lock(&mut self, path: &str, callers_path: Option<&str>) -> Result<(), String> {
let text = read(path)?;
let callers: Vec<String> = match callers_path {
Some(c) => read(c)?
.lines()
.map(str::trim)
Expand All @@ -100,56 +121,63 @@ fn run_inner(args: &[String]) -> Result<(), String> {
};
let flagged = self_sha_lock_records(&text, &callers);
if !flagged.is_empty() {
add(
self.add(
"SQ-LOCK-01",
Some(format!("self-SHA records: {}", flagged.join(", "))),
);
}
Ok(())
}
if let Some(p) = &workflow {
let text = read(p)?;

fn scan_workflow(&mut self, path: &str) -> Result<(), String> {
let text = read(path)?;
if unpinned_setup_bun(&text) {
add(
self.add(
"SQ-PIN-03",
Some("setup-bun step without an exact bun-version".into()),
);
}
let jobs = always_after_cancel(&text);
if !jobs.is_empty() {
add(
self.add(
"SQ-GEN-03",
Some(format!(
"if: always() after needs, downloading an artifact: {}",
jobs.join(", ")
)),
);
}
Ok(())
}
}

/// One row per catalogue rule found, with its guard decision and, for a
/// referral, the message Hypatia would receive.
fn build_rows(findings: Findings, step: Option<&str>) -> Vec<Row> {
let guards = GuardInput {
autofix: std::env::var("ESTATE_AUTOFIX").ok(),
..GuardInput::default()
};
let rows: Vec<Row> = found
let Findings { found, signature } = findings;
found
.into_iter()
.filter_map(|(id, detail)| {
let rule = rules::rule(id)?;
let decision = decide(rule, &guards);
let referral = match (&decision, rule.class) {
(Decision::Refer { .. }, _) => Some(referral_message(
let referral = matches!(decision, Decision::Refer { .. }).then(|| {
referral_message(
&Referral {
rule_id: rule.id.to_string(),
problem: rule.summary.to_string(),
fix: Some(rule.fix.to_string()),
risk: rule.refer_when.to_string(),
step_name: step.clone(),
step_name: step.map(String::from),
signature: signature.clone(),
..Referral::default()
},
false,
)),
_ => None,
};
)
});
Some(Row {
rule: id,
class: rule.class,
Expand All @@ -158,17 +186,19 @@ fn run_inner(args: &[String]) -> Result<(), String> {
referral,
})
})
.collect();
.collect()
}

fn print_rows(rows: &[Row], json: bool) -> Result<(), String> {
if json {
let s = serde_json::to_string_pretty(&rows).map_err(|e| e.to_string())?;
let s = serde_json::to_string_pretty(rows).map_err(|e| e.to_string())?;
println!("{s}");
return Ok(());
}
if rows.is_empty() {
println!("no catalogue rule matched");
}
for r in &rows {
for r in rows {
println!("{}\t{:?}\t{:?}", r.rule, r.class, r.decision);
if let Some(d) = &r.detail {
println!(" {d}");
Expand All @@ -181,3 +211,19 @@ fn run_inner(args: &[String]) -> Result<(), String> {
}
Ok(())
}

fn run_inner(args: &[String]) -> Result<(), String> {
let a = parse_args(args)?;
let mut findings = Findings::default();
if let Some(p) = &a.log {
findings.scan_log(p, a.step.as_deref())?;
}
if let Some(p) = &a.lock {
findings.scan_lock(p, a.callers.as_deref())?;
}
if let Some(p) = &a.workflow {
findings.scan_workflow(p)?;
}
let rows = build_rows(findings, a.step.as_deref());
print_rows(&rows, a.json)
}
5 changes: 3 additions & 2 deletions crates/squabble-core/data/first-line-rules.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ SQ-CANON-01 FIX an instance's canon-lockstep job is an old inline copy and goes
SQ-PERM-01 FIX Resource not accessible by integration at get-a-workflow-run in a job that uploads SARIF on a private repository Add actions: read to that job's permissions, with a comment that names the reason The upload step passes on a re-run The permission is already present, or the repository is public
SQ-RUN-01 ACT a setup or download step fails with API rate limit exceeded and the same workflow passes on an adjacent commit Re-run the failed jobs once after the rate-limit reset, only if that commit is still the head The re-run passes It fails twice on the same commit, the log has no rate-limit text, or the step is not a setup or download step
SQ-RUN-02 ACT a PR is blocked with required checks green under a strict ruleset and its base is behind the default branch Update the branch by merging the base; never rebase a branch the squabbler did not open The required checks pass on the new head Merge conflict, or still blocked on an up-to-date head
SQ-RUN-03 ACT a step fails on a transient host or API error: gh: HTTP 5xx, gh: Server Error (HTTP 5xx), a git remote returning 5xx, Could not resolve host, or Failed to connect Re-run the failed job once, only if that commit is still the head The re-run passes It fails twice on the same commit, or the status is 401 or 403 (a token problem: SQ-OWN-09, or a rate limit: SQ-RUN-01)
SQ-RUN-03 ACT a step fails on a transient host or API error: gh: HTTP 5xx, gh: Server Error (HTTP 5xx), a git remote returning 5xx, or Could not resolve host or Failed to connect to a host name that contains a dot Re-run the failed job once, only if that commit is still the head The re-run passes It fails twice on the same commit, the status is 401 or 403 (a token problem: SQ-OWN-09, or a rate limit: SQ-RUN-01), or the host name has no dot (a service container or localhost is a configuration error a re-run cannot fix)
SQ-RUN-04 ACT gitleaks fails with fatal: ambiguous argument <sha>^..<sha>: unknown revision or path not in the working tree, because a shallow checkout lacks the range base commit; trufflehog reports commits: 0 for the same range Re-run the failed job once, only if that commit is still the head The re-run passes and the scan covers more than 0 commits It fails twice on the same commit: refer with the suggestion fetch-depth: 0 on the scan checkout
SQ-RUN-05 REFER a step fails with Could not resolve host or Failed to connect to a host name that has no dot (a service container, localhost) or no name at all Refer to Hypatia with the log line and the step: this is a configuration error (the service is not running or is named wrongly), so a re-run cannot fix it (owner) Always a referral: the name that does resolve in the workflow is Hypatia's to confirm
SQ-CODE-01 FIX a Rust readiness or integration test cannot find its binary under target/debug/build Use PathBuf::from(env!("CARGO_BIN_EXE_<bin>")); a code fix, first-line only when the code-fix guard passes cargo test --test readiness passes The fix would hit the code-fix unsafe list, the code is vendored or third-party, or the check does not pass on the PR head
SQ-CODE-02 FIX Validate Hypatia Baseline fails on new code_safety findings Fix the code behind the finding; never touch .hypatia-baseline.json The baseline job passes with the baseline file unchanged The fix would hit the code-fix unsafe list, the code is vendored or third-party, or the check does not pass on the PR head
SQ-CODE-03 FIX Sonar S7688, S7679 or S131 in shell scripts Make the mechanical edit: for S7688 rewrite [ ] to [[ ]] only in files with a bash shebang (never /bin/sh or POSIX scripts); for S131 add a no-op default case *) ;;; decline S1192 in test files, where repetition is a convention bash -n, shellcheck and the repository's shell tests pass An S7688 test uses -a or -o or relies on word splitting, the fix would hit the code-fix unsafe list, the code is vendored or third-party, or the check does not pass on the PR head
Expand All @@ -58,7 +59,7 @@ SQ-OWN-12 OWNER the same scanner (panic-attack) is pinned at more than one commi
SQ-OWN-13 OWNER a workflow push is refused by a repository ruleset: gh: Repository rule violations found / Code coverage checks require a pull request. (HTTP 409) (Apodeixis.jl run 43257204071, Publish the lane badges, red on its base too) Report: a ruleset requires a pull request for that push, so the workflow cannot push to the protected branch. The owner changes the workflow to open a PR or adjusts the ruleset. The squabbler never changes a ruleset and never pushes around one (owner) (owner)
SQ-OWN-14 OWNER <file>: non-v8 UUID literal (<uuid>) / UUID check failed. Use ESTATE-UUID-V8 (ADR-008) (Apodeixis.jl run 43257204302, ./Project.toml) Report: an identifier needs the owner or the A7 identity process. The squabbler never mints, rewrites or retypes a UUID (owner) (owner)
SQ-OWN-15 OWNER SonarQube ERROR Fail to load the list of plugins installed on the server: Not authorized or organization not found, then sonar-scanner failed with exit code 3 (lithoglyph run 43152462261) Report: the Sonar token or organization is missing or wrong; the owner fixes the secret or the Sonar project. Never remove or skip the Sonar step (owner) (owner)
SQ-OWN-16 OWNER a workflow force- or mirror-pushes to a forge, gated on a *_MIRROR_ENABLED variable, with no check of the source repository visibility anywhere in the file (Hypatia WF030, found statically: standards mirror-reusable.yml gates the job at line 93 and runs git push --force gitlab main at line 144; github-backup-mirror-reusable.yml too) Report: the owner decides the guard (refuse, or skip loudly, when the source is private and the forge copy is not confirmed private; escalate when the forge visibility cannot be read). The squabbler never edits a mirror workflow, never pushes to a forge and never changes a visibility setting (owner) (owner)
SQ-OWN-16 OWNER a workflow force- or mirror-pushes to a forge, gated on a *_MIRROR_ENABLED variable, as Hypatia WF030 defines it (found statically, the definition is Hypatia's and not restated here: standards mirror-reusable.yml gates the job at line 93 and runs git push --force gitlab main at line 144; github-backup-mirror-reusable.yml too) Report: the owner decides the guard (refuse, or skip loudly, when the source is private and the forge copy is not confirmed private; escalate when the forge visibility cannot be read). The squabbler never edits a mirror workflow, never pushes to a forge and never changes a visibility setting (owner) (owner)
SQ-NOI-01 NOISE Fork governance failed (deed=cancelled) on a head that a newer commit replaced Label superseded; read the newest head The newest head is not deed=cancelled deed=cancelled on the newest head
SQ-NOI-02 NOISE couldn't find remote ref refs/pull/<N>/merge and the PR closed before the job started None: do not re-run a closed PR The PR API shows the PR closed The PR is still open (conflict or deleted ref)
SQ-NOI-03 NOISE Inbox Steward ends startup_failure only on runs triggered by chatgpt-codex-connector[bot] None Count of startup failures by actor Inbox Steward becomes a required check
Loading
Loading