Skip to content

Commit

Permalink
Merge pull request #1562 from JordanGoasdoue/fix/allow-tasks-results-…
Browse files Browse the repository at this point in the history
…params

fix: allow params from previous tasks results
  • Loading branch information
jenkins-x-bot authored Jul 24, 2023
2 parents 8b483bb + d2263d9 commit a072a17
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 9 deletions.
37 changes: 29 additions & 8 deletions pkg/triggerconfig/inrepo/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,37 @@ type UseLocation struct {
TaskSpec *v1beta1.TaskSpec
}

func getParamsFromTasksResults(loc *UseLocation) map[string]bool {
areParamsFromTasksResults := make(map[string]bool)
pipelineTasksAndFinally := loc.PipelineSpec.Tasks
pipelineTasksAndFinally = append(pipelineTasksAndFinally, loc.PipelineSpec.Finally...)
for _, pipelineTask := range pipelineTasksAndFinally {
params := pipelineTask.Params
for _, param := range params {
paramValStr := param.Value.StringVal
if strings.HasPrefix(paramValStr, "$(tasks.") && strings.Contains(paramValStr, ".results.") && strings.HasSuffix(paramValStr, ")") {
areParamsFromTasksResults[param.Name] = true
}
}
}
return areParamsFromTasksResults
}

// UseParametersAndResults adds the parameters from the used Task to the PipelineSpec if specified and the PipelineTask
func UseParametersAndResults(ctx context.Context, loc *UseLocation, uses *v1beta1.TaskSpec) error {
parameterSpecs := uses.Params
parameters := ToParams(parameterSpecs)
results := uses.Results
areParamsFromTasksResults := getParamsFromTasksResults(loc)

prs := loc.PipelineRunSpec
if prs != nil {
prs.Params = useParameters(prs.Params, ToDefaultParams(parameterSpecs))
prs.Params = useParameters(prs.Params, ToDefaultParams(parameterSpecs), areParamsFromTasksResults)
prs.Workspaces = useWorkspaceBindings(prs.Workspaces, ToWorkspaceBindings(uses.Workspaces))
}
ps := loc.PipelineSpec
if ps != nil {
ps.Params = useParameterSpecs(ctx, ps.Params, parameterSpecs)
ps.Params = useParameterSpecs(ctx, ps.Params, parameterSpecs, areParamsFromTasksResults)
ps.Results = usePipelineResults(ps.Results, results, loc.TaskName)
ps.Workspaces = usePipelineWorkspaces(ps.Workspaces, uses.Workspaces)
}
Expand All @@ -43,11 +60,11 @@ func UseParametersAndResults(ctx context.Context, loc *UseLocation, uses *v1beta
}
trs := loc.TaskRunSpec
if trs != nil {
trs.Params = useParameters(trs.Params, parameters)
trs.Params = useParameters(trs.Params, parameters, areParamsFromTasksResults)
}
ts := loc.TaskSpec
if ts != nil {
ts.Params = useParameterSpecs(ctx, ts.Params, parameterSpecs)
ts.Params = useParameterSpecs(ctx, ts.Params, parameterSpecs, areParamsFromTasksResults)
ts.Results = useResults(ts.Results, results)
ts.Sidecars = useSidecars(ts.Sidecars, uses.Sidecars)
ts.Workspaces = useWorkspaces(ts.Workspaces, uses.Workspaces)
Expand Down Expand Up @@ -89,7 +106,7 @@ func ToDefaultParams(params []v1beta1.ParamSpec) []v1beta1.Param {
return answer
}

func useParameterSpecs(ctx context.Context, params []v1beta1.ParamSpec, uses []v1beta1.ParamSpec) []v1beta1.ParamSpec {
func useParameterSpecs(ctx context.Context, params []v1beta1.ParamSpec, uses []v1beta1.ParamSpec, areParamsFromTasksResults map[string]bool) []v1beta1.ParamSpec {
for _, u := range uses {
found := false
for i := range params {
Expand All @@ -105,13 +122,15 @@ func useParameterSpecs(ctx context.Context, params []v1beta1.ParamSpec, uses []v
}
if !found {
u.SetDefaults(ctx)
params = append(params, u)
if areParamsFromTasksResults == nil || !areParamsFromTasksResults[u.Name] {
params = append(params, u)
}
}
}
return params
}

func useParameters(params []v1beta1.Param, uses []v1beta1.Param) []v1beta1.Param {
func useParameters(params []v1beta1.Param, uses []v1beta1.Param, areParamsFromTasksResults map[string]bool) []v1beta1.Param {
for _, u := range uses {
found := false
for i := range params {
Expand All @@ -134,7 +153,9 @@ func useParameters(params []v1beta1.Param, uses []v1beta1.Param) []v1beta1.Param
}
}
if !found {
params = append(params, u)
if areParamsFromTasksResults == nil || !areParamsFromTasksResults[u.Name] {
params = append(params, u)
}
}
}
return params
Expand Down
Loading

0 comments on commit a072a17

Please sign in to comment.