@@ -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