Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion pkg/apis/pipeline/v1/when_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1

import (
"encoding/json"
"fmt"

"github.com/tektoncd/pipeline/pkg/substitution"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (we *WhenExpression) isTrue() bool {
}

func (we *WhenExpression) applyReplacements(replacements map[string]string, arrayReplacements map[string][]string) WhenExpression {
replacedInput := substitution.ApplyReplacements(we.Input, replacements)
replacedInput := applyReplacementsAsString(we.Input, replacements, arrayReplacements)
replacedCEL := substitution.ApplyReplacements(we.CEL, replacements)

var replacedValues []string
Expand All @@ -83,6 +84,26 @@ func (we *WhenExpression) applyReplacements(replacements map[string]string, arra
return WhenExpression{Input: replacedInput, Operator: we.Operator, Values: replacedValues, CEL: replacedCEL}
}

func applyReplacementsAsString(s string, replacements map[string]string, arrayReplacements map[string][]string) string {
if _, ok := arrayReplacements[fmt.Sprintf("%s.%s", ParamsPrefix, ArrayReference(s))]; ok {
b, err := json.Marshal(substitution.ApplyArrayReplacements(s, replacements, arrayReplacements))
if err != nil {
return s
}
return string(b)
}

if _, ok := arrayReplacements[ResultsArrayReference(s)]; ok {
b, err := json.Marshal(substitution.ApplyArrayReplacements(s, replacements, arrayReplacements))
if err != nil {
return s
}
return string(b)
}

return substitution.ApplyReplacements(s, replacements)
}

// GetVarSubstitutionExpressions extracts all the values between "$(" and ")" in a When Expression
func (we *WhenExpression) GetVarSubstitutionExpressions() ([]string, bool) {
var allExpressions []string
Expand Down
64 changes: 63 additions & 1 deletion pkg/apis/pipeline/v1/when_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,69 @@ func TestApplyReplacements(t *testing.T) {
Operator: selection.In,
Values: []string{"dev", "stage", "foo.txt", "readme.md", "test.go"},
},
}}
}, {
name: "replace input with empty results array",
original: &WhenExpression{
Input: "$(tasks.foo.results.bar[*])",
Operator: selection.In,
Values: []string{"main"},
},
arrayReplacements: map[string][]string{
"tasks.foo.results.bar": {},
},
expected: &WhenExpression{
Input: "[]",
Operator: selection.In,
Values: []string{"main"},
},
}, {
name: "replace input with non-empty results array",
original: &WhenExpression{
Input: "$(tasks.foo.results.bar[*])",
Operator: selection.In,
Values: []string{"main"},
},
arrayReplacements: map[string][]string{
"tasks.foo.results.bar": {"main", "devel"},
},
expected: &WhenExpression{
Input: `["main","devel"]`,
Operator: selection.In,
Values: []string{"main"},
},
},
{
name: "replace input with empty params array",
original: &WhenExpression{
Input: "$(params.branches[*])",
Operator: selection.In,
Values: []string{"main"},
},
arrayReplacements: map[string][]string{
"params.branches": {},
},
expected: &WhenExpression{
Input: "[]",
Operator: selection.In,
Values: []string{"main"},
},
},
{
name: "replace input with non-empty params array",
original: &WhenExpression{
Input: "$(params.branches[*])",
Operator: selection.In,
Values: []string{"main"},
},
arrayReplacements: map[string][]string{
"params.branches": {"main", "devel"},
},
expected: &WhenExpression{
Input: `["main","devel"]`,
Operator: selection.In,
Values: []string{"main"},
},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := tc.original.applyReplacements(tc.replacements, tc.arrayReplacements)
Expand Down
Loading