Skip to content

Commit

Permalink
Refactor extracting parameters from a PipelineTask
Browse files Browse the repository at this point in the history
This commit adds a PipelinTask method to extract all parameters and refactors validatePipelineContextVariablesInParamValues() using this method.
  • Loading branch information
EmmaMunley committed Mar 10, 2023
1 parent 5757700 commit 9ae6cc1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
23 changes: 23 additions & 0 deletions pkg/apis/pipeline/v1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,29 @@ func (pt *PipelineTask) IsMatrixed() bool {
return pt.Matrix != nil && (pt.Matrix.hasParams() || pt.Matrix.hasInclude())
}

// extractAllParams extracts all the parameter in a Pipeline Task including Matrix parameter
// and Matrix Include parameter
func (pt *PipelineTask) extractAllParams() Params {
allParams := pt.Params
if pt.Matrix.hasParams() {
allParams = append(allParams, pt.Matrix.Params...)
}
if pt.Matrix.hasInclude() {
for _, include := range pt.Matrix.Include {
allParams = append(allParams, include.Params...)
}
}
return allParams
}

// extractAllParamValues extracts all the parameter values in a Pipeline Task including Matrix parameter
// and Matrix Include parameter
func (pt *PipelineTask) extractAllParamValues() []string {
allParams := pt.extractAllParams()
paramValues := allParams.extractParamValuesFromParams()
return paramValues
}

func (pt *PipelineTask) validateMatrix(ctx context.Context) (errs *apis.FieldError) {
if pt.IsMatrixed() {
// This is an alpha feature and will fail validation if it's used in a pipeline spec
Expand Down
21 changes: 1 addition & 20 deletions pkg/apis/pipeline/v1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,7 @@ func validatePipelineContextVariables(tasks []PipelineTask) *apis.FieldError {
)
var paramValues []string
for _, task := range tasks {
var matrixParams []Param
var includeParams []Param
if task.IsMatrixed() {
matrixParams = task.Matrix.Params
if task.Matrix.hasInclude() {
for _, include := range task.Matrix.Include {
includeParams = include.Params
}
}
}
for _, param := range append(task.Params, matrixParams...) {
paramValues = append(paramValues, param.Value.StringVal)
paramValues = append(paramValues, param.Value.ArrayVal...)
}

if task.Matrix.hasInclude() {
for _, param := range append(task.Params, includeParams...) {
paramValues = append(paramValues, param.Value.StringVal)
}
}
paramValues = task.extractAllParamValues()
}
errs := validatePipelineContextVariablesInParamValues(paramValues, "context\\.pipelineRun", pipelineRunContextNames).
Also(validatePipelineContextVariablesInParamValues(paramValues, "context\\.pipeline", pipelineContextNames)).
Expand Down
22 changes: 22 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,28 @@ func (pt *PipelineTask) IsMatrixed() bool {
return pt.Matrix != nil && (pt.Matrix.hasParams() || pt.Matrix.hasInclude())
}

// extractAllParams extracts all the parameter in a Pipeline Task including Matrix parameter
// and Matrix Include parameter
func (pt *PipelineTask) extractAllParams() Params {
allParams := pt.Params
if pt.Matrix.hasParams() {
allParams = append(allParams, pt.Matrix.Params...)
}
if pt.Matrix.hasInclude() {
for _, include := range pt.Matrix.Include {
allParams = append(allParams, include.Params...)
}
}
return allParams
}

// extractAllParamValues extracts all the parameter values in a Pipeline Task including Matrix parameter
// and Matrix Include parameter
func (pt *PipelineTask) extractAllParamValues() []string {
allParams := pt.extractAllParams()
paramValues := allParams.extractParamValuesFromParams()
return paramValues
}
func (pt *PipelineTask) validateMatrix(ctx context.Context) (errs *apis.FieldError) {
if pt.IsMatrixed() {
// This is an alpha feature and will fail validation if it's used in a pipeline spec
Expand Down
21 changes: 1 addition & 20 deletions pkg/apis/pipeline/v1beta1/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,7 @@ func validatePipelineContextVariables(tasks []PipelineTask) *apis.FieldError {
)
var paramValues []string
for _, task := range tasks {
var matrixParams []Param
var includeParams []Param
if task.IsMatrixed() {
matrixParams = task.Matrix.Params
if task.Matrix.hasInclude() {
for _, include := range task.Matrix.Include {
includeParams = include.Params
}
}
}
for _, param := range append(task.Params, matrixParams...) {
paramValues = append(paramValues, param.Value.StringVal)
paramValues = append(paramValues, param.Value.ArrayVal...)
}

if task.Matrix.hasInclude() {
for _, param := range append(task.Params, includeParams...) {
paramValues = append(paramValues, param.Value.StringVal)
}
}
paramValues = task.extractAllParamValues()
}
errs := validatePipelineContextVariablesInParamValues(paramValues, "context\\.pipelineRun", pipelineRunContextNames).
Also(validatePipelineContextVariablesInParamValues(paramValues, "context\\.pipeline", pipelineContextNames)).
Expand Down

0 comments on commit 9ae6cc1

Please sign in to comment.