From 823a9bcd8fc4e3456fb5e618cb68b858224d4d57 Mon Sep 17 00:00:00 2001 From: Christopher Homberger Date: Mon, 27 Oct 2025 20:33:27 +0100 Subject: [PATCH] Fix yaml with prefixed newline broken setjob --- pkg/jobparser/jobparser_test.go | 5 +++++ pkg/jobparser/model.go | 14 ++++++++------ pkg/jobparser/testdata/prefixed_newline.in.yaml | 14 ++++++++++++++ pkg/jobparser/testdata/prefixed_newline.out.yaml | 15 +++++++++++++++ 4 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 pkg/jobparser/testdata/prefixed_newline.in.yaml create mode 100644 pkg/jobparser/testdata/prefixed_newline.out.yaml diff --git a/pkg/jobparser/jobparser_test.go b/pkg/jobparser/jobparser_test.go index 800e19a..e510500 100644 --- a/pkg/jobparser/jobparser_test.go +++ b/pkg/jobparser/jobparser_test.go @@ -52,6 +52,11 @@ func TestParse(t *testing.T) { options: nil, wantErr: false, }, + { + name: "prefixed_newline", + options: nil, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/jobparser/model.go b/pkg/jobparser/model.go index e7ca0df..ae71698 100644 --- a/pkg/jobparser/model.go +++ b/pkg/jobparser/model.go @@ -50,16 +50,18 @@ func (w *SingleWorkflow) SetJob(id string, job *Job) error { m := map[string]*Job{ id: job, } - out, err := yaml.Marshal(m) - if err != nil { - return err - } + var buf bytes.Buffer + encoder := yaml.NewEncoder(&buf) + encoder.SetIndent(2) + encoder.Encode(m) + encoder.Close() + node := yaml.Node{} - if err := yaml.Unmarshal(out, &node); err != nil { + if err := yaml.Unmarshal(buf.Bytes(), &node); err != nil { return err } if len(node.Content) != 1 || node.Content[0].Kind != yaml.MappingNode { - return fmt.Errorf("can not set job: %q", out) + return fmt.Errorf("can not set job: %s", buf.String()) } w.RawJobs = *node.Content[0] return nil diff --git a/pkg/jobparser/testdata/prefixed_newline.in.yaml b/pkg/jobparser/testdata/prefixed_newline.in.yaml new file mode 100644 index 0000000..91f8530 --- /dev/null +++ b/pkg/jobparser/testdata/prefixed_newline.in.yaml @@ -0,0 +1,14 @@ +name: Step with leading new line + +on: + push: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: "\nExtract tag for variant" + id: extract_tag + run: | + + echo Test diff --git a/pkg/jobparser/testdata/prefixed_newline.out.yaml b/pkg/jobparser/testdata/prefixed_newline.out.yaml new file mode 100644 index 0000000..027e9df --- /dev/null +++ b/pkg/jobparser/testdata/prefixed_newline.out.yaml @@ -0,0 +1,15 @@ +name: Step with leading new line +"on": + push: +jobs: + test: + name: test + runs-on: ubuntu-latest + steps: + - id: extract_tag + name: |2- + + Extract tag for variant + run: |2 + + echo Test