-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support referencing array params in when expression values #4075
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for this fix @pritidesai 😁
could we please add a task with array params to the example pipeline with when expressions? also would be great to have an test case invalidating specifying array parameters in the input field
@@ -51,6 +51,13 @@ func TestWhenExpressions_Valid(t *testing.T) { | |||
Operator: selection.In, | |||
Values: []string{""}, | |||
}}, | |||
}, { | |||
name: "valid input with string variable and values with array variables", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could also add the array in input test case here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the set of tests here is running positive tests. We could add it as one of the failure tests but nothing changed in terms of validating the input
.
These tests are validating WhenExpressions.validate()
defined here. The purpose of WhenExpressions.validate()
is to validate each field for non-empty values and validating task result pattern.
This test is also not necessary here and can be removed.
a72b11b
to
232ec0e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we support it for when expression, should we also support this on variable interpolation ? (otherwise might feel a bit inconsistent ).
I think we do support this on variable interpolations using the same syntax |
Ah I see 👌🏼 |
// one of the values could be a reference to an array param, such as, $(params.foo[*]) | ||
// extract the variable name from the pattern $(params.foo[*]), if the variable name matches with one of the array params | ||
// validate the param as an array variable otherwise, validate it as a string variable | ||
if arrayParamNames.Has(strings.TrimSuffix(strings.TrimPrefix(val, "$("+prefix+"."), "[*])")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're checking if the variable is an array param in two places is slightly different ways, wondering if we can create a common function to check for array param that can be used in both places
if _, ok := arrayReplacements[strings.TrimSuffix(strings.TrimPrefix(val, "$("), "[*])")]; ok { |
if arrayParamNames.Has(strings.TrimSuffix(strings.TrimPrefix(val, "$("+prefix+"."), "[*])")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, it's very similar but the first expression is returning params.arrayParam1
from $(params.arrayParam1[*])
while the second expression is returning arrayParam1
from $(params.arrayParam1[*])
.
The arrayReplacements
in the first expression hold a list of array parameters with params
prefix while the arrayParamNames
hold the list of parameters without such prefix.
I have created a common function to return just the parameter name which works for the second expression and added prefix
back for the first expression.
Validation and applying replacements works with a different patterns because the apply.go
adds the prefix
before creating a list of parameter names:
arrayReplacements[fmt.Sprintf("params.%s", p.Name)] = p.Default.ArrayVal |
We can refactor this further (in a separate PR) to have a consistent validation and replacements routines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the refactor, looks great!
The following is the coverage report on the affected files.
|
The following is the coverage report on the affected files.
|
/retest |
/test pull-tekton-pipeline-integration-tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @pritidesai!
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: jerop The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requesting one additional unit test case but otherwise this lgtm!
original: &WhenExpression{ | ||
Input: "$(params.path)", | ||
Operator: selection.In, | ||
Values: []string{"$(params.branches[*])", "$(params.files[*])"}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One additional test-case here for completeness would be Values
with string
and array
vars mixed together:
original: &WhenExpression{
Input: "$(params.path)",
Operator: selection.In,
Values: []string{"$(params.branches[*])", "$(params.matchPath)", "$(params.files[*])"},
},
replacements: map[string]string{
"params.path": "readme.md",
"params.matchPath": "foo.txt",
},
arrayReplacements: map[string][]string{
"params.branches": {"dev", "stage"},
"params.files": {"readme.md", "test.go"},
},
expected: &WhenExpression{
Input: "readme.md",
Operator: selection.In,
Values: []string{"dev", "stage", "foo.txt", "readme.md", "test.go"},
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @sbwsg for an additional unit test, done 👍
Array params are referenced using $(params.arrayParam[*]), when expressions values is a list of string and was designed to resolve an array parameter. But the implementation was limiting the array param usage in the values. Adding support for the array params in when expressions.
The following is the coverage report on the affected files.
|
/lgtm |
Changes
Array params are referenced using $(params.arrayParam[*]). When expressions
values
is a list ofstring
and was designed to reference and resolve an array parameter. But the implementation was limiting the array param usage in the when expressionvalues
. Adding support for the array params in when expressions./kind bug
Closes #4061
Submitter Checklist
As the author of this PR, please check off the items in this checklist:
functionality, content, code)
Release Notes