diff --git a/pkg/reconciler/pipelinerun/resources/resultrefresolution.go b/pkg/reconciler/pipelinerun/resources/resultrefresolution.go index 146deca41a1..e441b8eb0b9 100644 --- a/pkg/reconciler/pipelinerun/resources/resultrefresolution.go +++ b/pkg/reconciler/pipelinerun/resources/resultrefresolution.go @@ -17,6 +17,7 @@ limitations under the License. package resources import ( + "encoding/json" "errors" "fmt" "sort" @@ -162,13 +163,28 @@ func resolveCustomResultRef(customRuns []*v1beta1.CustomRun, resultRef *v1.Resul return nil, err } return &ResolvedResultRef{ - Value: *v1.NewStructuredValues(runValue), + Value: *paramValueFromCustomRunResult(runValue), FromTaskRun: "", FromRun: runName, ResultReference: *resultRef, }, nil } +func paramValueFromCustomRunResult(result string) *v1.ParamValue { + var arrayResult []string + // for fan out array result, which is represented as string, we should make it to array type param value + if err := json.Unmarshal([]byte(result), &arrayResult); err == nil && len(arrayResult) > 0 { + if len(arrayResult) > 1 { + return v1.NewStructuredValues(arrayResult[0], arrayResult[1:]...) + } + return &v1.ParamValue{ + Type: v1.ParamTypeArray, + ArrayVal: []string{arrayResult[0]}, + } + } + return v1.NewStructuredValues(result) +} + func resolveResultRef(taskRuns []*v1.TaskRun, resultRef *v1.ResultRef) (*ResolvedResultRef, error) { taskRun := taskRuns[0] taskRunName := taskRun.Name diff --git a/pkg/reconciler/pipelinerun/resources/resultrefresolution_test.go b/pkg/reconciler/pipelinerun/resources/resultrefresolution_test.go index 8dc2b1b60de..1a298234b83 100644 --- a/pkg/reconciler/pipelinerun/resources/resultrefresolution_test.go +++ b/pkg/reconciler/pipelinerun/resources/resultrefresolution_test.go @@ -930,3 +930,53 @@ func TestValidateArrayResultsIndex(t *testing.T) { }) } } + +func TestParamValueFromCustomRunResult(t *testing.T) { + type args struct { + result string + } + tests := []struct { + name string + args args + want *v1.ParamValue + }{ + { + name: "multiple array elements result", + args: args{ + result: `["amd64", "arm64"]`, + }, + want: &v1.ParamValue{ + Type: "array", + ArrayVal: []string{"amd64", "arm64"}, + }, + }, + { + name: "single array elements result", + args: args{ + result: `[ "amd64" ]`, + }, + want: &v1.ParamValue{ + Type: "array", + ArrayVal: []string{"amd64"}, + }, + }, + { + name: "simple string result", + args: args{ + result: "amd64", + }, + want: &v1.ParamValue{ + Type: "string", + StringVal: "amd64", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := paramValueFromCustomRunResult(tt.args.result) + if d := cmp.Diff(tt.want, got); d != "" { + t.Fatalf("paramValueFromCustomRunResult %s", diff.PrintWantGot(d)) + } + }) + } +}