From 721857e4a076934b75db5570af78110644a32ad4 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Fri, 21 Apr 2023 20:21:15 +0800 Subject: [PATCH] Remove empty steps when decoding `Job` (#49) Follow #48 Empty steps are invalid, so remove them when decoding `Job` from YAML. Reviewed-on: https://gitea.com/gitea/act/pulls/49 Reviewed-by: Jason Song Co-authored-by: Zettat123 Co-committed-by: Zettat123 --- pkg/jobparser/jobparser_test.go | 5 +++++ pkg/jobparser/model.go | 7 +++++++ pkg/jobparser/testdata/empty_step.in.yaml | 8 ++++++++ pkg/jobparser/testdata/empty_step.out.yaml | 7 +++++++ 4 files changed, 27 insertions(+) create mode 100644 pkg/jobparser/testdata/empty_step.in.yaml create mode 100644 pkg/jobparser/testdata/empty_step.out.yaml diff --git a/pkg/jobparser/jobparser_test.go b/pkg/jobparser/jobparser_test.go index 6725785..454d9e4 100644 --- a/pkg/jobparser/jobparser_test.go +++ b/pkg/jobparser/jobparser_test.go @@ -42,6 +42,11 @@ func TestParse(t *testing.T) { options: nil, wantErr: false, }, + { + name: "empty_step", + 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 4cbdc6d..3236704 100644 --- a/pkg/jobparser/model.go +++ b/pkg/jobparser/model.go @@ -40,6 +40,13 @@ func (w *SingleWorkflow) jobs() ([]string, []*Job, error) { if err := item.Decode(job); err != nil { return nil, nil, fmt.Errorf("yaml.Unmarshal: %w", err) } + steps := make([]*Step, 0, len(job.Steps)) + for _, s := range job.Steps { + if s != nil { + steps = append(steps, s) + } + } + job.Steps = steps jobs = append(jobs, job) expectKey = true } diff --git a/pkg/jobparser/testdata/empty_step.in.yaml b/pkg/jobparser/testdata/empty_step.in.yaml new file mode 100644 index 0000000..737ac0b --- /dev/null +++ b/pkg/jobparser/testdata/empty_step.in.yaml @@ -0,0 +1,8 @@ +name: test +jobs: + job1: + name: job1 + runs-on: linux + steps: + - run: echo job-1 + - diff --git a/pkg/jobparser/testdata/empty_step.out.yaml b/pkg/jobparser/testdata/empty_step.out.yaml new file mode 100644 index 0000000..06828e0 --- /dev/null +++ b/pkg/jobparser/testdata/empty_step.out.yaml @@ -0,0 +1,7 @@ +name: test +jobs: + job1: + name: job1 + runs-on: linux + steps: + - run: echo job-1