Repository: jenkins-x/lighthouse
Pull request #872 (2020-07-30 17:07:02)

fix(tekton): Better PR and stage naming, set owner refs for cleanup
Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>

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 **abayer**
You can assign the PR to them by writing `/assign @abayer` 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":["abayer"]} -->
jenkins-x-bot: @abayer: The following test **failed**, say `/retest` to rerun them all:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pr-build | 6d0f94b534ffbc86cb0aa3f4942c9fb408b8f7e5 | [link](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-872/1) | `/test pr-build`

[View all Builds for this Pull Request](https://dashboard-jx.jenkins-x.live/teams/jx/projects/jenkins-x/lighthouse/PR-872) 

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [jenkins-x/lighthouse](https://github.com/jenkins-x/lighthouse/issues/new?title=Command%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->

Patch:
diff --git a/pkg/apis/lighthouse/register.go b/pkg/apis/lighthouse/register.go
index 969588b8..6cf420c1 100644
--- a/pkg/apis/lighthouse/register.go
+++ b/pkg/apis/lighthouse/register.go
@@ -3,4 +3,10 @@ package lighthouse
 const (
 	// GroupName defines the API group identifier for Lighthouse types.
 	GroupName = "lighthouse.jenkins.io"
+
+	// Version is the Lighthouse API group version
+	Version = "v1"
+
+	// GroupAndVersion is the Lighthouse API Group and version
+	GroupAndVersion = GroupName + "/" + Version
 )
diff --git a/pkg/apis/lighthouse/v1alpha1/types.go b/pkg/apis/lighthouse/v1alpha1/types.go
index 19efb60f..07579bff 100644
--- a/pkg/apis/lighthouse/v1alpha1/types.go
+++ b/pkg/apis/lighthouse/v1alpha1/types.go
@@ -345,6 +345,7 @@ func (prs ByNum) Less(i, j int) bool { return prs[i].Number < prs[j].Number }
 // ActivityRecord is a struct for reporting information on a pipeline, build, or other activity triggered by Lighthouse
 type ActivityRecord struct {
 	Name            string                 `json:"name"`
+	JobID           string                 `json:"jobId,omitempty"`
 	Owner           string                 `json:"owner,omitempty"`
 	Repo            string                 `json:"repo,omitempty"`
 	Branch          string                 `json:"branch,omitempty"`
diff --git a/pkg/engines/tekton/activity.go b/pkg/engines/tekton/activity.go
index 521cfe7a..8aa78f91 100644
--- a/pkg/engines/tekton/activity.go
+++ b/pkg/engines/tekton/activity.go
@@ -4,6 +4,7 @@ import (
 	"strings"
 
 	"github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1"
+	"github.com/jenkins-x/lighthouse/pkg/config"
 	"github.com/jenkins-x/lighthouse/pkg/util"
 	"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
 	corev1 "k8s.io/api/core/v1"
@@ -22,6 +23,7 @@ func ConvertPipelineRun(pr *v1beta1.PipelineRun) *v1alpha1.ActivityRecord {
 
 	record.Name = pr.Name
 
+	record.JobID = pr.Labels[config.LighthouseJobIDLabel]
 	record.BaseSHA = pr.Labels[util.BaseSHALabel]
 	record.Repo = pr.Labels[util.RepoLabel]
 	record.Context = pr.Labels[util.ContextLabel]
@@ -40,7 +42,7 @@ func ConvertPipelineRun(pr *v1beta1.PipelineRun) *v1alpha1.ActivityRecord {
 
 	for _, taskName := range sets.StringKeySet(pr.Status.TaskRuns).List() {
 		task := pr.Status.TaskRuns[taskName]
-		cleanedUpTaskName := strings.TrimPrefix(taskName, pr.Name+"-")
+		cleanedUpTaskName := strings.TrimPrefix(taskName[:len(taskName)-6], pr.Name+"-")
 		t := &v1alpha1.ActivityStageOrStep{
 			Name:           cleanedUpTaskName,
 			Status:         convertTektonStatus(task.Status.GetCondition(apis.ConditionSucceeded), task.Status.StartTime, task.Status.CompletionTime),
diff --git a/pkg/engines/tekton/controller.go b/pkg/engines/tekton/controller.go
index 6c78598a..e046eb47 100644
--- a/pkg/engines/tekton/controller.go
+++ b/pkg/engines/tekton/controller.go
@@ -14,6 +14,7 @@ import (
 	"time"
 
 	jsonpatch "github.com/evanphx/json-patch"
+	"github.com/jenkins-x/lighthouse/pkg/apis/lighthouse"
 	"github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1"
 	clientset "github.com/jenkins-x/lighthouse/pkg/client/clientset/versioned"
 	lhinformers "github.com/jenkins-x/lighthouse/pkg/client/informers/externalversions/lighthouse/v1alpha1"
@@ -413,7 +414,7 @@ func (c *Controller) syncPipelineRun(namespace, name, key string) error {
 
 	// To be safe, find the job with the activity's name in its status.
 	for _, j := range possibleJobs {
-		if j.Name == activityRecord.Name {
+		if j.Name == activityRecord.JobID {
 			job = j
 		}
 	}
@@ -521,13 +522,19 @@ func (c *Controller) makePipelineRun(lj v1alpha1.LighthouseJob) (*tektonv1beta1.
 	specCopy := lj.Spec.PipelineRunSpec.DeepCopy()
 	p := tektonv1beta1.PipelineRun{
 		ObjectMeta: metav1.ObjectMeta{
-			Annotations: annotations,
-			Name:        lj.Name,
-			Namespace:   lj.Spec.Namespace,
-			Labels:      prLabels,
+			Annotations:  annotations,
+			GenerateName: fmt.Sprintf("%s-", lj.Spec.Job),
+			Namespace:    lj.Spec.Namespace,
+			Labels:       prLabels,
 		},
 		Spec: *specCopy,
 	}
+	p.OwnerReferences = []metav1.OwnerReference{{
+		APIVersion: lighthouse.GroupAndVersion,
+		Kind:       "LighthouseJob",
+		Name:       lj.Name,
+		UID:        lj.UID,
+	}}
 
 	if err := validatePipelineRunSpec(lj.Spec.Type, lj.Spec.ExtraRefs, lj.Spec.PipelineRunSpec); err != nil {
 		return nil, fmt.Errorf("invalid pipeline_run_spec: %v", err)


Test patch:
diff --git a/pkg/engines/tekton/controller_test.go b/pkg/engines/tekton/controller_test.go
index 61d46287..33343860 100644
--- a/pkg/engines/tekton/controller_test.go
+++ b/pkg/engines/tekton/controller_test.go
@@ -135,6 +135,8 @@ func TestSyncHandler(t *testing.T) {
 				updatedPR := prs.Items[0].DeepCopy()
 				if d := cmp.Diff(expectedPR, updatedPR); d != "" {
 					t.Errorf("PipelineRun did not match expected: %s", d)
+					py, _ := yaml.Marshal(updatedPR)
+					t.Logf("pr:\n%s", string(py))
 				}
 			}
 			if expectedJob != nil {
diff --git a/pkg/engines/tekton/test_data/activity/failed_single_task/pr.yaml b/pkg/engines/tekton/test_data/activity/failed_single_task/pr.yaml
index 4eb1c619..12dbe8e8 100644
--- a/pkg/engines/tekton/test_data/activity/failed_single_task/pr.yaml
+++ b/pkg/engines/tekton/test_data/activity/failed_single_task/pr.yaml
@@ -14,6 +14,7 @@ metadata:
     lighthouse.jenkins-x.io/branch: PR-1533
     lighthouse.jenkins-x.io/buildNum: "7"
     lighthouse.jenkins-x.io/context: pr-build
+    lighthouse.jenkins-x.io/id: f46327af-b47e-11ea-b797-9256b7b8d9b0
     lighthouse.jenkins-x.io/lastCommitSHA: 3bb45bf8478b267bc38e8ad5ad6356cfb8a97d0f
     lighthouse.jenkins-x.io/refs.org: jenkins-x-charts
     lighthouse.jenkins-x.io/refs.repo: jx-build-templates
diff --git a/pkg/engines/tekton/test_data/activity/failed_single_task/record.yaml b/pkg/engines/tekton/test_data/activity/failed_single_task/record.yaml
index 0388f134..0ae27bcb 100644
--- a/pkg/engines/tekton/test_data/activity/failed_single_task/record.yaml
+++ b/pkg/engines/tekton/test_data/activity/failed_single_task/record.yaml
@@ -4,13 +4,14 @@ buildId: "7"
 completionTime: "2020-07-20T18:50:43Z"
 context: pr-build
 gitURL: https://github.com/jenkins-x-charts/jx-build-templates.git
+jobId: f46327af-b47e-11ea-b797-9256b7b8d9b0
 lastCommitSHA: 3bb45bf8478b267bc38e8ad5ad6356cfb8a97d0f
 name: jenkins-x-charts-jx-build-templ-wbbx6-7
 owner: jenkins-x-charts
 repo: jx-build-templates
 stages:
   - completionTime: "2020-07-20T18:50:43Z"
-    name: from-build-pack-zjcjs
+    name: from-build-pack
     startTime: "2020-07-20T18:50:22Z"
     status: failure
     steps:
diff --git a/pkg/engines/tekton/test_data/activity/running_multiple_tasks/pr.yaml b/pkg/engines/tekton/test_data/activity/running_multiple_tasks/pr.yaml
index 7e2815a6..76508923 100644
--- a/pkg/engines/tekton/test_data/activity/running_multiple_tasks/pr.yaml
+++ b/pkg/engines/tekton/test_data/activity/running_multiple_tasks/pr.yaml
@@ -14,6 +14,7 @@ metadata:
     lighthouse.jenkins-x.io/branch: PR-7463
     lighthouse.jenkins-x.io/buildNum: "17"
     lighthouse.jenkins-x.io/context: boot-vault
+    lighthouse.jenkins-x.io/id: f46327af-b47e-11ea-b797-9256b7b8d9b0
     lighthouse.jenkins-x.io/lastCommitSHA: 8a3c5f595cdc3162e46390c72a5d22fb9c89d1df
     lighthouse.jenkins-x.io/refs.org: jenkins-x
     lighthouse.jenkins-x.io/refs.repo: jx
diff --git a/pkg/engines/tekton/test_data/activity/running_multiple_tasks/record.yaml b/pkg/engines/tekton/test_data/activity/running_multiple_tasks/record.yaml
index cd885853..a10d78af 100644
--- a/pkg/engines/tekton/test_data/activity/running_multiple_tasks/record.yaml
+++ b/pkg/engines/tekton/test_data/activity/running_multiple_tasks/record.yaml
@@ -3,13 +3,14 @@ branch: PR-7463
 buildId: "17"
 context: boot-vault
 gitURL: https://github.com/jenkins-x/jx.git
+jobId: f46327af-b47e-11ea-b797-9256b7b8d9b0
 lastCommitSHA: 8a3c5f595cdc3162e46390c72a5d22fb9c89d1df
 name: jenkins-x-jx-pr-7463-boot-vault-9mbgb-17
 owner: jenkins-x
 repo: jx
 stages:
   - completionTime: "2020-07-20T19:56:53Z"
-    name: build-2jnvr
+    name: build
     startTime: "2020-07-20T19:50:21Z"
     status: success
     steps:
@@ -65,7 +66,7 @@ stages:
         name: build-and-push-maven
         startTime: "2020-07-20T19:55:52Z"
         status: success
-  - name: e2e-tests-ds74m
+  - name: e2e-tests
     startTime: "2020-07-20T19:56:53Z"
     status: running
     steps:
diff --git a/pkg/engines/tekton/test_data/activity/running_single_task/pr.yaml b/pkg/engines/tekton/test_data/activity/running_single_task/pr.yaml
index 001be343..a57b71a2 100644
--- a/pkg/engines/tekton/test_data/activity/running_single_task/pr.yaml
+++ b/pkg/engines/tekton/test_data/activity/running_single_task/pr.yaml
@@ -14,6 +14,7 @@ metadata:
     lighthouse.jenkins-x.io/branch: PR-7463
     lighthouse.jenkins-x.io/buildNum: "18"
     lighthouse.jenkins-x.io/context: unit
+    lighthouse.jenkins-x.io/id: f46327af-b47e-11ea-b797-9256b7b8d9b0
     lighthouse.jenkins-x.io/lastCommitSHA: 8a3c5f595cdc3162e46390c72a5d22fb9c89d1df
     lighthouse.jenkins-x.io/refs.org: jenkins-x
     lighthouse.jenkins-x.io/refs.repo: jx
diff --git a/pkg/engines/tekton/test_data/activity/running_single_task/record.yaml b/pkg/engines/tekton/test_data/activity/running_single_task/record.yaml
index dcf47f10..2e720501 100644
--- a/pkg/engines/tekton/test_data/activity/running_single_task/record.yaml
+++ b/pkg/engines/tekton/test_data/activity/running_single_task/record.yaml
@@ -3,12 +3,13 @@ branch: PR-7463
 buildId: "18"
 context: unit
 gitURL: https://github.com/jenkins-x/jx.git
+jobId: f46327af-b47e-11ea-b797-9256b7b8d9b0
 lastCommitSHA: 8a3c5f595cdc3162e46390c72a5d22fb9c89d1df
 name: jenkins-x-jx-pr-7463-unit-wbf7v-18
 owner: jenkins-x
 repo: jx
 stages:
-  - name: ci-8r5qw
+  - name: ci
     startTime: "2020-07-20T20:15:20Z"
     status: running
     steps:
diff --git a/pkg/engines/tekton/test_data/activity/successful_multiple_tasks/pr.yaml b/pkg/engines/tekton/test_data/activity/successful_multiple_tasks/pr.yaml
index 92e0c16c..bc5c9812 100644
--- a/pkg/engines/tekton/test_data/activity/successful_multiple_tasks/pr.yaml
+++ b/pkg/engines/tekton/test_data/activity/successful_multiple_tasks/pr.yaml
@@ -14,6 +14,7 @@ metadata:
     lighthouse.jenkins-x.io/branch: PR-7463
     lighthouse.jenkins-x.io/buildNum: "17"
     lighthouse.jenkins-x.io/context: boot-vault
+    lighthouse.jenkins-x.io/id: f46327af-b47e-11ea-b797-9256b7b8d9b0
     lighthouse.jenkins-x.io/lastCommitSHA: 8a3c5f595cdc3162e46390c72a5d22fb9c89d1df
     lighthouse.jenkins-x.io/refs.org: jenkins-x
     lighthouse.jenkins-x.io/refs.repo: jx
diff --git a/pkg/engines/tekton/test_data/activity/successful_multiple_tasks/record.yaml b/pkg/engines/tekton/test_data/activity/successful_multiple_tasks/record.yaml
index d8d29c94..2668dae4 100644
--- a/pkg/engines/tekton/test_data/activity/successful_multiple_tasks/record.yaml
+++ b/pkg/engines/tekton/test_data/activity/successful_multiple_tasks/record.yaml
@@ -4,13 +4,14 @@ buildId: "17"
 completionTime: "2020-07-20T20:50:27Z"
 context: boot-vault
 gitURL: https://github.com/jenkins-x/jx.git
+jobId: f46327af-b47e-11ea-b797-9256b7b8d9b0
 lastCommitSHA: 8a3c5f595cdc3162e46390c72a5d22fb9c89d1df
 name: jenkins-x-jx-pr-7463-boot-vault-9mbgb-17
 owner: jenkins-x
 repo: jx
 stages:
   - completionTime: "2020-07-20T19:56:53Z"
-    name: build-2jnvr
+    name: build
     startTime: "2020-07-20T19:50:21Z"
     status: success
     steps:
@@ -67,7 +68,7 @@ stages:
         startTime: "2020-07-20T19:55:52Z"
         status: success
   - completionTime: "2020-07-20T20:50:27Z"
-    name: e2e-tests-ds74m
+    name: e2e-tests
     startTime: "2020-07-20T19:56:53Z"
     status: success
     steps:
diff --git a/pkg/engines/tekton/test_data/activity/successful_single_task/pr.yaml b/pkg/engines/tekton/test_data/activity/successful_single_task/pr.yaml
index 56c976fb..df7a1277 100644
--- a/pkg/engines/tekton/test_data/activity/successful_single_task/pr.yaml
+++ b/pkg/engines/tekton/test_data/activity/successful_single_task/pr.yaml
@@ -14,6 +14,7 @@ metadata:
     lighthouse.jenkins-x.io/branch: PR-854
     lighthouse.jenkins-x.io/buildNum: "4"
     lighthouse.jenkins-x.io/context: github
+    lighthouse.jenkins-x.io/id: f46327af-b47e-11ea-b797-9256b7b8d9b0
     lighthouse.jenkins-x.io/lastCommitSHA: feb00a651ac860c842d02b9eac3cc2fc9eb06c10
     lighthouse.jenkins-x.io/refs.org: jenkins-x
     lighthouse.jenkins-x.io/refs.repo: lighthouse
diff --git a/pkg/engines/tekton/test_data/activity/successful_single_task/record.yaml b/pkg/engines/tekton/test_data/activity/successful_single_task/record.yaml
index 85766c48..51cc518a 100644
--- a/pkg/engines/tekton/test_data/activity/successful_single_task/record.yaml
+++ b/pkg/engines/tekton/test_data/activity/successful_single_task/record.yaml
@@ -4,13 +4,14 @@ buildId: "4"
 completionTime: "2020-07-20T18:00:05Z"
 context: github
 gitURL: https://github.com/jenkins-x/lighthouse.git
+jobId: f46327af-b47e-11ea-b797-9256b7b8d9b0
 lastCommitSHA: feb00a651ac860c842d02b9eac3cc2fc9eb06c10
 name: jenkins-x-lighthouse-pr-854-bbs-d5q9c-4
 owner: jenkins-x
 repo: lighthouse
 stages:
   - completionTime: "2020-07-20T18:00:05Z"
-    name: ci-wbnrf
+    name: ci
     startTime: "2020-07-20T17:29:24Z"
     status: success
     steps:
diff --git a/pkg/engines/tekton/test_data/controller/start-pullrequest/expected-pr.yml b/pkg/engines/tekton/test_data/controller/start-pullrequest/expected-pr.yml
index fb7aeef8..233a066c 100644
--- a/pkg/engines/tekton/test_data/controller/start-pullrequest/expected-pr.yml
+++ b/pkg/engines/tekton/test_data/controller/start-pullrequest/expected-pr.yml
@@ -15,8 +15,12 @@ metadata:
     lighthouse.jenkins-x.io/refs.pull: "813"
     lighthouse.jenkins-x.io/refs.repo: lighthouse
     lighthouse.jenkins-x.io/type: presubmit
-  name: f46327af-b47e-11ea-b797-9256b7b8d9b0
+  generateName: github-
   namespace: jx
+  ownerReferences:
+    - apiVersion: lighthouse.jenkins.io/v1
+      kind: LighthouseJob
+      name: f46327af-b47e-11ea-b797-9256b7b8d9b0
 spec:
   params:
     - name: BUILD_ID
diff --git a/pkg/engines/tekton/test_data/controller/update-job/expected-lhjob.yml b/pkg/engines/tekton/test_data/controller/update-job/expected-lhjob.yml
index 6a582c64..d87e9ba3 100644
--- a/pkg/engines/tekton/test_data/controller/update-job/expected-lhjob.yml
+++ b/pkg/engines/tekton/test_data/controller/update-job/expected-lhjob.yml
@@ -58,12 +58,13 @@ status:
     buildId: "7828158075477027098"
     context: github
     gitURL: https://github.com/jenkins-x/lighthouse.git
+    jobId: f46327af-b47e-11ea-b797-9256b7b8d9b0
     lastCommitSHA: dd64c739442d505cf5381e2a14b60968e8a0d86e
     name: f46327af-b47e-11ea-b797-9256b7b8d9b0
     owner: jenkins-x
     repo: lighthouse
     stages:
-      - name: jenkins-x-jx-pr-7463-unit-wbf7v-18-ci-8r5qw
+      - name: jenkins-x-jx-pr-7463-unit-wbf7v-18-ci
         startTime: "2020-07-20T20:15:20Z"
         status: running
         steps: