From 31dbde72c1cf4e9c1426b2ebe2c290ff2d3ada22 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 27 Jul 2026 10:08:33 +0000 Subject: [PATCH] refactor(ir): reduce complexity of add_edges_from_job in graph.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extract add_edges_for_condition and add_edges_for_env helpers to eliminate the repeated for-ref-loop pattern inside every Step arm. Before: 52 lines, 4 nested levels, each of 4 step variants repeated the same collect→loop→add_edge_for_ref pattern inline. After: 30-line match delegating to two focused helpers; each match arm is ≤2 lines. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/compile/ir/graph.rs | 66 +++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/src/compile/ir/graph.rs b/src/compile/ir/graph.rs index 68675079..f25408b0 100644 --- a/src/compile/ir/graph.rs +++ b/src/compile/ir/graph.rs @@ -277,49 +277,25 @@ fn collect_step_outputs(step: &Step) -> BTreeSet { fn add_edges_from_job(stage: Option, job: &super::job::Job, g: &mut Graph) -> Result<()> { // Walk job-level condition references. - if let Some(cond) = &job.condition { - for r in collect_condition_refs(cond) { - add_edge_for_ref(stage.as_ref(), &job.id, r, g)?; - } - } + add_edges_for_condition(stage.as_ref(), &job.id, job.condition.as_ref(), g)?; // Walk every step's env + condition. for step in &job.steps { match step { Step::Bash(b) => { - for r in collect_env_refs(b.env.values()) { - add_edge_for_ref(stage.as_ref(), &job.id, r, g)?; - } - if let Some(cond) = &b.condition { - for r in collect_condition_refs(cond) { - add_edge_for_ref(stage.as_ref(), &job.id, r, g)?; - } - } + add_edges_for_env(stage.as_ref(), &job.id, b.env.values(), g)?; + add_edges_for_condition(stage.as_ref(), &job.id, b.condition.as_ref(), g)?; } Step::Task(t) => { - for r in collect_env_refs(t.env.values()) { - add_edge_for_ref(stage.as_ref(), &job.id, r, g)?; - } - if let Some(cond) = &t.condition { - for r in collect_condition_refs(cond) { - add_edge_for_ref(stage.as_ref(), &job.id, r, g)?; - } - } + add_edges_for_env(stage.as_ref(), &job.id, t.env.values(), g)?; + add_edges_for_condition(stage.as_ref(), &job.id, t.condition.as_ref(), g)?; } - Step::Checkout(_) => {} Step::Download(d) => { - if let Some(cond) = &d.condition { - for r in collect_condition_refs(cond) { - add_edge_for_ref(stage.as_ref(), &job.id, r, g)?; - } - } + add_edges_for_condition(stage.as_ref(), &job.id, d.condition.as_ref(), g)?; } Step::Publish(p) => { - if let Some(cond) = &p.condition { - for r in collect_condition_refs(cond) { - add_edge_for_ref(stage.as_ref(), &job.id, r, g)?; - } - } + add_edges_for_condition(stage.as_ref(), &job.id, p.condition.as_ref(), g)?; } + Step::Checkout(_) => {} // `RawYaml` carries opaque user-authored YAML; the graph // pass cannot introspect it. Producers that need // cross-step refs must use a typed Bash/Task variant. @@ -329,6 +305,32 @@ fn add_edges_from_job(stage: Option, job: &super::job::Job, g: &mut Gra Ok(()) } +fn add_edges_for_condition( + stage: Option<&StageId>, + job: &JobId, + cond: Option<&Condition>, + g: &mut Graph, +) -> Result<()> { + if let Some(cond) = cond { + for r in collect_condition_refs(cond) { + add_edge_for_ref(stage, job, r, g)?; + } + } + Ok(()) +} + +fn add_edges_for_env<'a, I: IntoIterator>( + stage: Option<&StageId>, + job: &JobId, + values: I, + g: &mut Graph, +) -> Result<()> { + for r in collect_env_refs(values) { + add_edge_for_ref(stage, job, r, g)?; + } + Ok(()) +} + fn collect_env_refs<'a, I: IntoIterator>(values: I) -> Vec<&'a OutputRef> { let mut out = Vec::new(); for v in values {