Repository: openshift-pipelines/pipelines-as-code
Pull request #147 (2021-06-23 09:43:57)

Add support for inlining finally tasks in resolver
We were not inlining finally tasks, let's make sure we do.

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>



Patch:
diff --git a/pkg/resolve/resolve.go b/pkg/resolve/resolve.go
index e2c2c41b..547259b1 100644
--- a/pkg/resolve/resolve.go
+++ b/pkg/resolve/resolve.go
@@ -76,6 +76,23 @@ func skippingTask(taskName string, skippedTasks []string) bool {
 	return false
 }
 
+func inlineTasks(tasks []tektonv1beta1.PipelineTask, ropt *Opts, types Types) ([]tektonv1beta1.PipelineTask,
+	error) {
+	pipelineTasks := []tektonv1beta1.PipelineTask{}
+	for _, task := range tasks {
+		if task.TaskRef != nil && task.TaskRef.Bundle == "" && !skippingTask(task.TaskRef.Name, ropt.SkipInlining) {
+			taskResolved, err := getTaskByName(task.TaskRef.Name, types.Tasks)
+			if err != nil {
+				return nil, err
+			}
+			task.TaskRef = nil
+			task.TaskSpec = &tektonv1beta1.EmbeddedTask{TaskSpec: taskResolved.Spec}
+		}
+		pipelineTasks = append(pipelineTasks, task)
+	}
+	return pipelineTasks, nil
+}
+
 type Opts struct {
 	GenerateName bool     // wether to GenerateName
 	RemoteTasks  bool     // wether to parse annotation to fetch tasks from remote
@@ -114,39 +131,35 @@ func Resolve(ctx context.Context, cs *cli.Clients, runinfo *webvcs.RunInfo, data
 		}
 	}
 
-	// Resolve TaskRef inside Pipeline
+	// Resolve {Finally/Task}Ref inside Pipeline
 	for _, pipeline := range types.Pipelines {
-		var pipelineTasks []tektonv1beta1.PipelineTask
-		for _, task := range pipeline.Spec.Tasks {
-			if task.TaskRef != nil && task.TaskRef.Bundle == "" && !skippingTask(task.TaskRef.Name, ropt.SkipInlining) {
-				taskResolved, err := getTaskByName(task.TaskRef.Name, types.Tasks)
-				if err != nil {
-					return []*tektonv1beta1.PipelineRun{}, err
-				}
-				task.TaskRef = nil
-				task.TaskSpec = &tektonv1beta1.EmbeddedTask{TaskSpec: taskResolved.Spec}
-			}
-			pipelineTasks = append(pipelineTasks, task)
+		pipelineTasks, err := inlineTasks(pipeline.Spec.Tasks, ropt, types)
+		if err != nil {
+			return nil, err
 		}
 		pipeline.Spec.Tasks = pipelineTasks
+
+		finallyTasks, err := inlineTasks(pipeline.Spec.Finally, ropt, types)
+		if err != nil {
+			return nil, err
+		}
+		pipeline.Spec.Finally = finallyTasks
 	}
 
 	for _, pipelinerun := range types.PipelineRuns {
-		// Resolve taskRef inside PipelineSpec inside PipelineRun
+		// Resolve {Finally/Task}Ref inside PipelineSpec inside PipelineRun
 		if pipelinerun.Spec.PipelineSpec != nil {
-			var pipelineTasksResolve []tektonv1beta1.PipelineTask
-			for _, task := range pipelinerun.Spec.PipelineSpec.Tasks {
-				if task.TaskRef != nil && task.TaskRef.Bundle == "" && !skippingTask(task.TaskRef.Name, ropt.SkipInlining) {
-					taskResolved, err := getTaskByName(task.TaskRef.Name, types.Tasks)
-					if err != nil {
-						return []*tektonv1beta1.PipelineRun{}, err
-					}
-					task.TaskRef = nil
-					task.TaskSpec = &tektonv1beta1.EmbeddedTask{TaskSpec: taskResolved.Spec}
-				}
-				pipelineTasksResolve = append(pipelineTasksResolve, task)
+			truns, err := inlineTasks(pipelinerun.Spec.PipelineSpec.Tasks, ropt, types)
+			if err != nil {
+				return nil, err
+			}
+			pipelinerun.Spec.PipelineSpec.Tasks = truns
+
+			fruns, err := inlineTasks(pipelinerun.Spec.PipelineSpec.Finally, ropt, types)
+			if err != nil {
+				return nil, err
 			}
-			pipelinerun.Spec.PipelineSpec.Tasks = pipelineTasksResolve
+			pipelinerun.Spec.PipelineSpec.Finally = fruns
 		}
 
 		// Resolve PipelineRef inside PipelineRef


Test patch:
diff --git a/pkg/resolve/resolve_test.go b/pkg/resolve/resolve_test.go
index f0ea0507..16a5f017 100644
--- a/pkg/resolve/resolve_test.go
+++ b/pkg/resolve/resolve_test.go
@@ -86,6 +86,19 @@ func TestPipelineRunPipelineSpecTaskSpec(t *testing.T) {
 	assert.Equal(t, resolved.Spec.PipelineSpec.Tasks[0].TaskSpec.Steps[0].Name, "hello-moto")
 }
 
+func TestPipelineRunWithFinally(t *testing.T) {
+	resolved, _, err := readTDfile(t, "pipelinerun-finally", false)
+	assert.NilError(t, err)
+	assert.Equal(t, resolved.Spec.PipelineSpec.Finally[0].TaskSpec.Steps[0].Name, "finally-task")
+}
+
+func TestPipelineWithFinally(t *testing.T) {
+	resolved, _, err := readTDfile(t, "pipeline-finally", false)
+	assert.NilError(t, err)
+	assert.Equal(t, resolved.Spec.PipelineSpec.Tasks[0].TaskSpec.Steps[0].Name, "normal-task")
+	assert.Equal(t, resolved.Spec.PipelineSpec.Finally[0].TaskSpec.Steps[0].Name, "finally-task")
+}
+
 func TestPipelineRunPipelineSpecTaskRef(t *testing.T) {
 	resolved, _, err := readTDfile(t, "pipelinerun-pipelinespec-taskref", false)
 	assert.NilError(t, err)
diff --git a/pkg/resolve/testdata/pipeline-finally.yaml b/pkg/resolve/testdata/pipeline-finally.yaml
new file mode 100644
index 00000000..e1d14e64
--- /dev/null
+++ b/pkg/resolve/testdata/pipeline-finally.yaml
@@ -0,0 +1,49 @@
+---
+apiVersion: tekton.dev/v1beta1
+kind: PipelineRun
+metadata:
+  name: pr-test1
+spec:
+  pipelineRef:
+    name: pipeline-with-finally
+  params:
+    - name: key
+      value: "{{value}}"
+---
+apiVersion: tekton.dev/v1beta1
+kind: Pipeline
+metadata:
+  name: pipeline-with-finally
+spec:
+  params:
+    - name: repo_url
+    - name: revision
+  finally:
+    - name: finally-task
+      taskRef:
+        name: finally-task
+  tasks:
+    - name: normal-task
+      taskRef:
+        name: normal-task
+  steps:
+    - name: first-step
+      image: image
+---
+apiVersion: tekton.dev/v1beta1
+kind: Task
+metadata:
+  name: normal-task
+spec:
+  steps:
+    - name: normal-task
+      image: image
+---
+apiVersion: tekton.dev/v1beta1
+kind: Task
+metadata:
+  name: finally-task
+spec:
+  steps:
+    - name: finally-task
+      image: image
diff --git a/pkg/resolve/testdata/pipelinerun-finally.yaml b/pkg/resolve/testdata/pipelinerun-finally.yaml
new file mode 100644
index 00000000..7472924c
--- /dev/null
+++ b/pkg/resolve/testdata/pipelinerun-finally.yaml
@@ -0,0 +1,27 @@
+---
+apiVersion: tekton.dev/v1beta1
+kind: Task
+metadata:
+  name: finally-task
+spec:
+  steps:
+    - name: finally-task
+      image: finally-image
+
+---
+apiVersion: tekton.dev/v1beta1
+kind: PipelineRun
+metadata:
+  name: pipelinespec-taskspecs-embedded
+spec:
+  pipelineSpec:
+    finally:
+      - name: finally-task
+        taskRef:
+          name: finally-task
+    tasks:
+      - name: hello1
+        taskSpec:
+          steps:
+            - name: hello-moto
+              image: scratch