Repository: jenkins-x/lighthouse
Pull request #940 (2020-08-19 17:08:55)

fix: Wrap payload passed to pipeline_run_params
This PR wraps the payload passed to the template when resolving params in `pipeline_run_params`.

Instead of passing directly `spec.Refs` of the underlying `LighthouseJob`, it changes the payload to wrap `spec.Refs` into a `Refs` property.

This way, if we want to pass other data to the template it will be easier, just add a new property to the payload for the new data to be passed.

jenkins-x-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **garethjevans**
You can assign the PR to them by writing `/assign @garethjevans` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/jenkins-x/lighthouse/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["garethjevans"]} -->
abayer: /lgtm
jenkins-x-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *[abayer](https://github.com/jenkins-x/lighthouse/pull/940#issuecomment-676556836 "LGTM")*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=jenkins-x%2Flighthouse).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com#issuecomment-676556836/jenkins-x/lighthouse/blob/master/OWNERS)~~ [abayer]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/engines/tekton/utils.go b/pkg/engines/tekton/utils.go
index 4ab4bf90..117dd9cf 100644
--- a/pkg/engines/tekton/utils.go
+++ b/pkg/engines/tekton/utils.go
@@ -83,13 +83,16 @@ func makePipelineRun(ctx context.Context, lj v1alpha1.LighthouseJob, namespace s
 		env[v1alpha1.PullPullRefEnv] = strings.Join(batchedRefsVals, " ")
 	}
 	if len(lj.Spec.PipelineRunParams) > 0 {
+		payload := map[string]interface{}{
+			"Refs": lj.Spec.Refs,
+		}
 		for _, param := range lj.Spec.PipelineRunParams {
 			parsedTemplate, err := template.New(param.Name).Parse(param.ValueTemplate)
 			if err != nil {
 				return nil, err
 			}
 			var msgBuffer bytes.Buffer
-			err = parsedTemplate.Execute(&msgBuffer, lj.Spec.Refs)
+			err = parsedTemplate.Execute(&msgBuffer, payload)
 			if err != nil {
 				return nil, err
 			}


Test patch:
diff --git a/pkg/engines/tekton/test_data/controller/start-batch-pullrequest/expected-lhjob.yml b/pkg/engines/tekton/test_data/controller/start-batch-pullrequest/expected-lhjob.yml
index f6593a5f..6533180f 100644
--- a/pkg/engines/tekton/test_data/controller/start-batch-pullrequest/expected-lhjob.yml
+++ b/pkg/engines/tekton/test_data/controller/start-batch-pullrequest/expected-lhjob.yml
@@ -31,11 +31,11 @@ spec:
     serviceAccountName: tekton-bot
   pipeline_run_params:
     - name: batch-refs
-      value_template: '{{ range $i, $v := .Pulls }}{{if $i}} {{end}}{{ $v.Ref }}{{ end }}'
+      value_template: '{{ range $i, $v := .Refs.Pulls }}{{if $i}} {{end}}{{ $v.Ref }}{{ end }}'
     - name: branch-name
-      value_template: '{{ .BaseRef }}'
+      value_template: '{{ .Refs.BaseRef }}'
     - name: repo-url
-      value_template: '{{ .CloneURI }}'
+      value_template: '{{ .Refs.CloneURI }}'
   refs:
     base_link: https://github.com/jenkins-x/lighthouse/commit/e8d56b5ee9671599c75644af574a251dd3b94a5c
     base_ref: master
diff --git a/pkg/engines/tekton/test_data/controller/start-batch-pullrequest/observed-lhjob.yml b/pkg/engines/tekton/test_data/controller/start-batch-pullrequest/observed-lhjob.yml
index a889a789..dab28784 100644
--- a/pkg/engines/tekton/test_data/controller/start-batch-pullrequest/observed-lhjob.yml
+++ b/pkg/engines/tekton/test_data/controller/start-batch-pullrequest/observed-lhjob.yml
@@ -28,11 +28,11 @@ spec:
     serviceAccountName: tekton-bot
   pipeline_run_params:
     - name: batch-refs
-      value_template: '{{ range $i, $v := .Pulls }}{{if $i}} {{end}}{{ $v.Ref }}{{ end }}'
+      value_template: '{{ range $i, $v := .Refs.Pulls }}{{if $i}} {{end}}{{ $v.Ref }}{{ end }}'
     - name: branch-name
-      value_template: '{{ .BaseRef }}'
+      value_template: '{{ .Refs.BaseRef }}'
     - name: repo-url
-      value_template: '{{ .CloneURI }}'
+      value_template: '{{ .Refs.CloneURI }}'
   refs:
     base_link: https://github.com/jenkins-x/lighthouse/commit/e8d56b5ee9671599c75644af574a251dd3b94a5c
     base_ref: master
diff --git a/pkg/engines/tekton/test_data/controller/start-pullrequest/expected-lhjob.yml b/pkg/engines/tekton/test_data/controller/start-pullrequest/expected-lhjob.yml
index 8a78c970..be10b152 100644
--- a/pkg/engines/tekton/test_data/controller/start-pullrequest/expected-lhjob.yml
+++ b/pkg/engines/tekton/test_data/controller/start-pullrequest/expected-lhjob.yml
@@ -31,9 +31,9 @@ spec:
     serviceAccountName: tekton-bot
   pipeline_run_params:
     - name: branch-name
-      value_template: '{{ range $i, $v := .Pulls }}{{if $i}} {{end}}{{ $v.SHA }}{{ end }}'
+      value_template: '{{ range $i, $v := .Refs.Pulls }}{{if $i}} {{end}}{{ $v.SHA }}{{ end }}'
     - name: repo-url
-      value_template: '{{ .CloneURI }}'
+      value_template: '{{ .Refs.CloneURI }}'
   refs:
     base_link: https://github.com/jenkins-x/lighthouse/commit/e8d56b5ee9671599c75644af574a251dd3b94a5c
     base_ref: master
diff --git a/pkg/engines/tekton/test_data/controller/start-pullrequest/observed-lhjob.yml b/pkg/engines/tekton/test_data/controller/start-pullrequest/observed-lhjob.yml
index 1473904c..699d6c32 100644
--- a/pkg/engines/tekton/test_data/controller/start-pullrequest/observed-lhjob.yml
+++ b/pkg/engines/tekton/test_data/controller/start-pullrequest/observed-lhjob.yml
@@ -28,9 +28,9 @@ spec:
     serviceAccountName: tekton-bot
   pipeline_run_params:
     - name: branch-name
-      value_template: '{{ range $i, $v := .Pulls }}{{if $i}} {{end}}{{ $v.SHA }}{{ end }}'
+      value_template: '{{ range $i, $v := .Refs.Pulls }}{{if $i}} {{end}}{{ $v.SHA }}{{ end }}'
     - name: repo-url
-      value_template: '{{ .CloneURI }}'
+      value_template: '{{ .Refs.CloneURI }}'
   refs:
     base_link: https://github.com/jenkins-x/lighthouse/commit/e8d56b5ee9671599c75644af574a251dd3b94a5c
     base_ref: master
diff --git a/test/e2e/tekton_test.go b/test/e2e/tekton_test.go
index 166f96c1..a5c3d1f2 100644
--- a/test/e2e/tekton_test.go
+++ b/test/e2e/tekton_test.go
@@ -114,15 +114,15 @@ func ChatOpsTests() bool {
 			cfg.Presubmits[repoFullName][0].PipelineRunParams = []config.PipelineRunParam{
 				{
 					Name:          "batch-refs",
-					ValueTemplate: "{{ range $i, $v := .Pulls }}{{if $i}} {{end}}{{ $v.Ref }}{{ end }}",
+					ValueTemplate: "{{ range $i, $v := .Refs.Pulls }}{{if $i}} {{end}}{{ $v.Ref }}{{ end }}",
 				},
 				{
 					Name:          "branch-name",
-					ValueTemplate: "{{ .BaseRef }}",
+					ValueTemplate: "{{ .Refs.BaseRef }}",
 				},
 				{
 					Name:          "repo-url",
-					ValueTemplate: "{{ .CloneURI }}",
+					ValueTemplate: "{{ .Refs.CloneURI }}",
 				},
 			}