From 6abeff01658677ea6635546b70123596e3de211b Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 14 Feb 2024 23:22:31 +0100 Subject: [PATCH] Remove empty strings from slice before parsing agent config (#3387) Fixes: https://github.com/woodpecker-ci/woodpecker/issues/3385 --- cmd/agent/core/agent.go | 2 +- shared/utils/slices.go | 11 +++++++++++ shared/utils/slices_test.go | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cmd/agent/core/agent.go b/cmd/agent/core/agent.go index 8bfe00fe095..8a1c972cdde 100644 --- a/cmd/agent/core/agent.go +++ b/cmd/agent/core/agent.go @@ -275,7 +275,7 @@ func stringSliceAddToMap(sl []string, m map[string]string) error { if m == nil { m = make(map[string]string) } - for _, v := range sl { + for _, v := range utils.StringSliceDeleteEmpty(sl) { parts := strings.SplitN(v, "=", 2) switch len(parts) { case 2: diff --git a/shared/utils/slices.go b/shared/utils/slices.go index d0732c814db..6f5229a6174 100644 --- a/shared/utils/slices.go +++ b/shared/utils/slices.go @@ -69,3 +69,14 @@ func SliceToBoolMap(s []string) map[string]bool { } return v } + +// StringSliceDeleteEmpty removes empty strings from a string slice. +func StringSliceDeleteEmpty(s []string) []string { + r := make([]string, 0) + for _, str := range s { + if str != "" { + r = append(r, str) + } + } + return r +} diff --git a/shared/utils/slices_test.go b/shared/utils/slices_test.go index 3a499c4ab39..355ee2716f2 100644 --- a/shared/utils/slices_test.go +++ b/shared/utils/slices_test.go @@ -68,3 +68,24 @@ func TestSliceToBoolMap(t *testing.T) { assert.Equal(t, map[string]bool{}, SliceToBoolMap([]string{})) assert.Equal(t, map[string]bool{}, SliceToBoolMap([]string{""})) } + +func TestStringSliceDeleteEmpty(t *testing.T) { + tests := []struct { + in []string + out []string + }{{ + in: []string{"", "ab", "ab"}, + out: []string{"ab", "ab"}, + }, { + in: []string{"", "ab", ""}, + out: []string{"ab"}, + }, { + in: []string{""}, + out: []string{}, + }} + + for _, tc := range tests { + exp := StringSliceDeleteEmpty(tc.in) + assert.EqualValues(t, tc.out, exp, "got '%#v', expects %#v", exp, tc.out) + } +}