Repository: jenkins-x/lighthouse
Pull request #1371 (2021-08-16 14:57:13)

fix: better support for pipeline branch names
so we can filter by PR numbers or release branches for breakpoints


Patch:
diff --git a/pkg/engines/tekton/utils.go b/pkg/engines/tekton/utils.go
index 492c280f..ef1b3744 100644
--- a/pkg/engines/tekton/utils.go
+++ b/pkg/engines/tekton/utils.go
@@ -3,6 +3,7 @@ package tekton
 import (
 	"bytes"
 	"context"
+	"fmt"
 	"regexp"
 	"strconv"
 	"strings"
@@ -194,14 +195,36 @@ func resolveBreakpointFilter(p *tektonv1beta1.PipelineRun, name string, lj v1alp
 		Type:    lj.Spec.Type,
 		Context: lj.Spec.Context,
 	}
+	branch := ""
+	if lj.Labels != nil {
+		branch = lj.Labels[util.BranchLabel]
+	}
 	refs := lj.Spec.Refs
 	if refs != nil {
 		filterValues.Owner = refs.Org
 		filterValues.Repository = refs.Repo
-		filterValues.Branch = refs.BaseRef
+		if branch == "" {
+			for _, p := range refs.Pulls {
+				if p.Number > 0 {
+					branch = fmt.Sprintf("PR-%d", p.Number)
+					break
+				}
+			}
+		}
+		if branch == "" {
+			branch = refs.BaseRef
+		}
 	}
+	filterValues.Branch = branch
 
-	// TODO if refs isn't populated should we try labels?
+	if lj.Labels != nil {
+		if filterValues.Owner == "" {
+			filterValues.Owner = lj.Labels[util.OrgLabel]
+		}
+		if filterValues.Repository == "" {
+			filterValues.Repository = lj.Labels[util.RepoLabel]
+		}
+	}
 	return filterValues
 }
 


Test patch:
diff --git a/pkg/engines/tekton/controller_test.go b/pkg/engines/tekton/controller_test.go
index 2d4687ba..862c89a1 100644
--- a/pkg/engines/tekton/controller_test.go
+++ b/pkg/engines/tekton/controller_test.go
@@ -51,8 +51,8 @@ func (s *seededRandIDGenerator) GenerateBuildID() string {
 
 func TestReconcile(t *testing.T) {
 	testCases := []string{
-		"debug-pr",
 		"debug-pr-no-taskRunSpecs",
+		"debug-pr",
 		"update-job",
 		"start-pullrequest",
 		"start-batch-pullrequest",
@@ -102,6 +102,10 @@ func TestReconcile(t *testing.T) {
 			lhClient := fakelh.NewSimpleClientset()
 
 			if strings.HasPrefix(tc, "debug") {
+				branch := "master"
+				if tc == "debug-pr-no-taskRunSpecs" {
+					branch = "PR-813"
+				}
 				lhClient = fakelh.NewSimpleClientset(
 					&lighthousev1alpha1.LighthouseBreakpoint{
 						ObjectMeta: metav1.ObjectMeta{
@@ -112,7 +116,7 @@ func TestReconcile(t *testing.T) {
 							Filter: lighthousev1alpha1.LighthousePipelineFilter{
 								Owner:      "jenkins-x",
 								Repository: "lighthouse",
-								Branch:     "master",
+								Branch:     branch,
 								Context:    "github",
 								Task:       "",
 							},
diff --git a/pkg/engines/tekton/test_data/controller/debug-pr/expected-lhjob.yml b/pkg/engines/tekton/test_data/controller/debug-pr/expected-lhjob.yml
index f90b1f1c..1de57726 100644
--- a/pkg/engines/tekton/test_data/controller/debug-pr/expected-lhjob.yml
+++ b/pkg/engines/tekton/test_data/controller/debug-pr/expected-lhjob.yml
@@ -6,7 +6,7 @@ metadata:
   creationTimestamp: null
   labels:
     created-by-lighthouse: "true"
-    lighthouse.jenkins-x.io/branch: PR-813
+    lighthouse.jenkins-x.io/branch: master
     lighthouse.jenkins-x.io/buildNum: "7828158075477027098"
     lighthouse.jenkins-x.io/context: github
     lighthouse.jenkins-x.io/id: f46327af-b47e-11ea-b797-9256b7b8d9b0
diff --git a/pkg/engines/tekton/test_data/controller/debug-pr/expected-pr.yml b/pkg/engines/tekton/test_data/controller/debug-pr/expected-pr.yml
index 105dd723..173004d1 100644
--- a/pkg/engines/tekton/test_data/controller/debug-pr/expected-pr.yml
+++ b/pkg/engines/tekton/test_data/controller/debug-pr/expected-pr.yml
@@ -7,7 +7,7 @@ metadata:
   labels:
     created-by-lighthouse: "true"
     lighthouse.jenkins-x.io/baseSHA: e8d56b5ee9671599c75644af574a251dd3b94a5c
-    lighthouse.jenkins-x.io/branch: PR-813
+    lighthouse.jenkins-x.io/branch: master
     lighthouse.jenkins-x.io/buildNum: "7828158075477027098"
     lighthouse.jenkins-x.io/context: github
     lighthouse.jenkins-x.io/id: f46327af-b47e-11ea-b797-9256b7b8d9b0
diff --git a/pkg/engines/tekton/test_data/controller/debug-pr/observed-lhjob.yml b/pkg/engines/tekton/test_data/controller/debug-pr/observed-lhjob.yml
index 86cc4409..6812ac0b 100644
--- a/pkg/engines/tekton/test_data/controller/debug-pr/observed-lhjob.yml
+++ b/pkg/engines/tekton/test_data/controller/debug-pr/observed-lhjob.yml
@@ -5,7 +5,7 @@ metadata:
     lighthouse.jenkins-x.io/job: github
   labels:
     created-by-lighthouse: "true"
-    lighthouse.jenkins-x.io/branch: PR-813
+    lighthouse.jenkins-x.io/branch: master
     lighthouse.jenkins-x.io/buildNum: "7828158075477027098"
     lighthouse.jenkins-x.io/context: github
     lighthouse.jenkins-x.io/id: f46327af-b47e-11ea-b797-9256b7b8d9b0