Skip to content

Commit 66f13d4

Browse files
bobcatfishtekton-robot
authored andcommitted
cherry pick (ish) refactoring and fixes from master 🍒
In order to cherry pick in the fixes from #2393 and #2471, we first need to apply the refactoring/changes these were built on in a8946d4#diff-e2b4c96e5145aa139a97f864d9f9f2a1 but unfortunately this both refactored/changed the code, AND added pipeline results. Since we don't want to add a new feature in a patch release, this commit extracts the refactoring (and fix?) from that commit.
1 parent 33e0847 commit 66f13d4

File tree

9 files changed

+564
-484
lines changed

9 files changed

+564
-484
lines changed

pkg/apis/pipeline/v1alpha1/pipeline_types.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,12 @@ func (pt PipelineTask) Deps() []string {
164164
}
165165
// Add any dependents from task results
166166
for _, param := range pt.Params {
167-
if resultRefs, err := v1beta1.NewResultRefs(param); err == nil {
168-
for _, resultRef := range resultRefs {
169-
deps = append(deps, resultRef.PipelineTask)
167+
expressions, ok := v1beta1.GetVarSubstitutionExpressionsForParam(param)
168+
if ok {
169+
if resultRefs, err := v1beta1.NewResultRefs(expressions); err == nil {
170+
for _, resultRef := range resultRefs {
171+
deps = append(deps, resultRef.PipelineTask)
172+
}
170173
}
171174
}
172175
}

pkg/apis/pipeline/v1alpha1/pipeline_validation.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,12 @@ func validateGraph(tasks []PipelineTask) error {
140140
func validateParamResults(tasks []PipelineTask) error {
141141
for _, task := range tasks {
142142
for _, param := range task.Params {
143-
if v1beta1.LooksLikeContainsResultRefs(param) {
144-
if _, err := v1beta1.NewResultRefs(param); err != nil {
145-
return err
143+
expressions, ok := v1beta1.GetVarSubstitutionExpressionsForParam(param)
144+
if ok {
145+
if v1beta1.LooksLikeContainsResultRefs(expressions) {
146+
if _, err := v1beta1.NewResultRefs(expressions); err != nil {
147+
return err
148+
}
146149
}
147150
}
148151
}

pkg/apis/pipeline/v1beta1/param_types.go

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23-
"regexp"
24-
"strings"
2523

2624
resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
2725
)
@@ -142,108 +140,3 @@ func NewArrayOrString(value string, values ...string) ArrayOrString {
142140
StringVal: value,
143141
}
144142
}
145-
146-
// ResultRef is a type that represents a reference to a task run result
147-
type ResultRef struct {
148-
PipelineTask string
149-
Result string
150-
}
151-
152-
const (
153-
resultExpressionFormat = "tasks.<taskName>.results.<resultName>"
154-
// ResultTaskPart Constant used to define the "tasks" part of a pipeline result reference
155-
ResultTaskPart = "tasks"
156-
// ResultResultPart Constant used to define the "results" part of a pipeline result reference
157-
ResultResultPart = "results"
158-
variableSubstitutionFormat = `\$\([A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\)`
159-
)
160-
161-
var variableSubstitutionRegex = regexp.MustCompile(variableSubstitutionFormat)
162-
163-
// NewResultRefs extracts all ResultReferences from param.
164-
// If the ResultReference can be extracted, they are returned. Otherwise an error is returned
165-
func NewResultRefs(param Param) ([]*ResultRef, error) {
166-
substitutionExpressions, ok := getVarSubstitutionExpressions(param)
167-
if !ok {
168-
return nil, fmt.Errorf("Invalid result reference expression: must contain variable substitution %q", resultExpressionFormat)
169-
}
170-
var resultRefs []*ResultRef
171-
for _, expression := range substitutionExpressions {
172-
pipelineTask, result, err := parseExpression(expression)
173-
if err != nil {
174-
return nil, fmt.Errorf("Invalid result reference expression: %v", err)
175-
}
176-
resultRefs = append(resultRefs, &ResultRef{
177-
PipelineTask: pipelineTask,
178-
Result: result,
179-
})
180-
}
181-
return resultRefs, nil
182-
}
183-
184-
// LooksLikeContainsResultRefs attempts to check if param looks like it contains any
185-
// result references.
186-
// This is useful if we want to make sure the param looks like a ResultReference before
187-
// performing strict validation
188-
func LooksLikeContainsResultRefs(param Param) bool {
189-
extractedExpressions, ok := getVarSubstitutionExpressions(param)
190-
if !ok {
191-
return false
192-
}
193-
for _, expression := range extractedExpressions {
194-
if looksLikeResultRef(expression) {
195-
return true
196-
}
197-
}
198-
return false
199-
}
200-
201-
func looksLikeResultRef(expression string) bool {
202-
return strings.HasPrefix(expression, "task") && strings.Contains(expression, ".result")
203-
}
204-
205-
// getVarSubstitutionExpressions extracts all the value between "$(" and ")""
206-
func getVarSubstitutionExpressions(param Param) ([]string, bool) {
207-
var allExpressions []string
208-
switch param.Value.Type {
209-
case ParamTypeArray:
210-
// array type
211-
for _, value := range param.Value.ArrayVal {
212-
expressions := variableSubstitutionRegex.FindAllString(value, -1)
213-
if expressions == nil {
214-
continue
215-
}
216-
for _, expression := range expressions {
217-
allExpressions = append(allExpressions, stripVarSubExpression(expression))
218-
}
219-
}
220-
if len(allExpressions) == 0 {
221-
return nil, false
222-
}
223-
return allExpressions, true
224-
case ParamTypeString:
225-
// string type
226-
expressions := variableSubstitutionRegex.FindAllString(param.Value.StringVal, -1)
227-
if expressions == nil {
228-
return nil, false
229-
}
230-
for _, expression := range expressions {
231-
allExpressions = append(allExpressions, stripVarSubExpression(expression))
232-
}
233-
return allExpressions, true
234-
default:
235-
return nil, false
236-
}
237-
}
238-
239-
func stripVarSubExpression(expression string) string {
240-
return strings.TrimSuffix(strings.TrimPrefix(expression, "$("), ")")
241-
}
242-
243-
func parseExpression(substitutionExpression string) (string, string, error) {
244-
subExpressions := strings.Split(substitutionExpression, ".")
245-
if len(subExpressions) != 4 || subExpressions[0] != ResultTaskPart || subExpressions[2] != ResultResultPart {
246-
return "", "", fmt.Errorf("Must be of the form %q", resultExpressionFormat)
247-
}
248-
return subExpressions[1], subExpressions[3], nil
249-
}

0 commit comments

Comments
 (0)