From f764805e17ec6a227050224725f1f9dc48bd7564 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 8 Apr 2026 06:17:55 +0000 Subject: [PATCH] refactor: extract writeStepsSection helper to eliminate pre/post-steps duplication generatePreSteps and generatePostSteps had identical implementations for stripping the YAML header line and normalising indentation. Extract a shared writeStepsSection helper so the logic lives in one place. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- pkg/workflow/compiler_yaml.go | 64 ++++++++++++----------------------- 1 file changed, 22 insertions(+), 42 deletions(-) diff --git a/pkg/workflow/compiler_yaml.go b/pkg/workflow/compiler_yaml.go index 1337e0300dc..b42a6f94ab5 100644 --- a/pkg/workflow/compiler_yaml.go +++ b/pkg/workflow/compiler_yaml.go @@ -604,51 +604,31 @@ func writePromptBashStep(yaml *strings.Builder, name, script string) { } func (c *Compiler) generatePreSteps(yaml *strings.Builder, data *WorkflowData) { - if data.PreSteps != "" { - // Remove "pre-steps:" line and adjust indentation, similar to PostSteps processing - lines := strings.Split(data.PreSteps, "\n") - if len(lines) > 1 { - for _, line := range lines[1:] { - // Trim trailing whitespace - trimmed := strings.TrimRight(line, " ") - // Skip empty lines - if strings.TrimSpace(trimmed) == "" { - yaml.WriteString("\n") - continue - } - // Steps need 6-space indentation ( - name:) - // Nested properties need 8-space indentation ( run:) - if strings.HasPrefix(line, " ") { - yaml.WriteString(" " + line[2:] + "\n") - } else { - yaml.WriteString(" " + line + "\n") - } - } - } - } + writeStepsSection(yaml, data.PreSteps) } func (c *Compiler) generatePostSteps(yaml *strings.Builder, data *WorkflowData) { - if data.PostSteps != "" { - // Remove "post-steps:" line and adjust indentation, similar to CustomSteps processing - lines := strings.Split(data.PostSteps, "\n") - if len(lines) > 1 { - for _, line := range lines[1:] { - // Trim trailing whitespace - trimmed := strings.TrimRight(line, " ") - // Skip empty lines - if strings.TrimSpace(trimmed) == "" { - yaml.WriteString("\n") - continue - } - // Steps need 6-space indentation ( - name:) - // Nested properties need 8-space indentation ( run:) - if strings.HasPrefix(line, " ") { - yaml.WriteString(" " + line[2:] + "\n") - } else { - yaml.WriteString(" " + line + "\n") - } - } + writeStepsSection(yaml, data.PostSteps) +} + +// writeStepsSection writes a steps section (pre-steps or post-steps) to the YAML builder, +// stripping the header line and normalising indentation to match the agent job step format: +// top-level items get 6-space indent ( - name:) and nested properties get 8-space indent ( run:). +func writeStepsSection(yaml *strings.Builder, stepsYAML string) { + if stepsYAML == "" { + return + } + lines := strings.Split(stepsYAML, "\n") + for _, line := range lines[1:] { // skip the "pre-steps:" / "post-steps:" header line + trimmed := strings.TrimRight(line, " ") + if strings.TrimSpace(trimmed) == "" { + yaml.WriteString("\n") + continue + } + if strings.HasPrefix(line, " ") { + yaml.WriteString(" " + line[2:] + "\n") + } else { + yaml.WriteString(" " + line + "\n") } } }