Repository: openshift-pipelines/pipelines-as-code
Pull request #136 (2021-06-17 08:22:22)

Better debug message when we cannot match a PR to event
When we cannot match a PipelineRun from the tekton directory to event we print
as much as possible in the logs with the configuration available and what we are
supposed to match on.

This would help a lot for debugging pipelineruns.



Patch:
diff --git a/pkg/config/annotation_matcher.go b/pkg/config/annotation_matcher.go
index 79311340..31652df8 100644
--- a/pkg/config/annotation_matcher.go
+++ b/pkg/config/annotation_matcher.go
@@ -41,7 +41,9 @@ func getAnnotationValues(annotation string) ([]string, error) {
 
 func MatchPipelinerunByAnnotation(pruns []*v1beta1.PipelineRun, cs *cli.Clients,
 	runinfo *webvcs.RunInfo) (*v1beta1.PipelineRun, error) {
+	configurations := map[string]map[string]string{}
 	for _, prun := range pruns {
+		configurations[prun.GetGenerateName()] = map[string]string{}
 		if prun.GetObjectMeta().GetAnnotations() == nil {
 			cs.Log.Warnf("PipelineRun %s does not have any annotations", prun.GetName())
 			continue
@@ -50,6 +52,7 @@ func MatchPipelinerunByAnnotation(pruns []*v1beta1.PipelineRun, cs *cli.Clients,
 		if targetEvent, ok := prun.GetObjectMeta().GetAnnotations()[pipelinesascode.
 			GroupName+"/"+onEventAnnotation]; ok {
 			matched, err := matchOnAnnotation(targetEvent, runinfo.EventType, false)
+			configurations[prun.GetGenerateName()]["target-event"] = targetEvent
 			if err != nil {
 				return nil, err
 			}
@@ -61,6 +64,7 @@ func MatchPipelinerunByAnnotation(pruns []*v1beta1.PipelineRun, cs *cli.Clients,
 		if targetBranch, ok := prun.GetObjectMeta().GetAnnotations()[pipelinesascode.
 			GroupName+"/"+onTargetBranchAnnotation]; ok {
 			matched, err := matchOnAnnotation(targetBranch, runinfo.BaseBranch, true)
+			configurations[prun.GetGenerateName()]["target-branch"] = targetBranch
 			if err != nil {
 				return nil, err
 			}
@@ -71,8 +75,20 @@ func MatchPipelinerunByAnnotation(pruns []*v1beta1.PipelineRun, cs *cli.Clients,
 
 		return prun, nil
 	}
+
+	cs.Log.Infof("cannot match between event and pipelineRuns: URL=%s baseBranch=%s, "+
+		"eventType=%s", runinfo.URL,
+		runinfo.BaseBranch,
+		runinfo.EventType)
+
+	cs.Log.Info("available configuration in pipelineRuns annotations")
+	for prunname, maps := range configurations {
+		cs.Log.Infof("pipelineRun: %s, baseBranch=%s, targetEvent=%s",
+			prunname, maps["target-branch"], maps["target-event"])
+	}
+
 	// TODO: more descriptive error message
-	return nil, fmt.Errorf("cannot match any pipeline on EventType: %s, TargetBranch: %s", runinfo.EventType, runinfo.BaseBranch)
+	return nil, fmt.Errorf("cannot match pipeline from webhook to pipelineruns")
 }
 
 func matchOnAnnotation(annotations string, runinfoValue string, branchMatching bool) (bool, error) {


Test patch:
diff --git a/pkg/config/annotation_matcher_test.go b/pkg/config/annotation_matcher_test.go
index f9f3e381..1f0c9478 100644
--- a/pkg/config/annotation_matcher_test.go
+++ b/pkg/config/annotation_matcher_test.go
@@ -100,7 +100,7 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
 				runinfo: &webvcs.RunInfo{EventType: "push", BaseBranch: "main"},
 			},
 			wantErr: true,
-			wantLog: "does not have any annotations",
+			wantLog: "cannot match between event and pipelineRuns",
 		},
 		{
 			name: "bad-event-annotation",
@@ -225,8 +225,8 @@ func TestMatchPipelinerunByAnnotation(t *testing.T) {
 			}
 			if tt.wantLog != "" {
 				logmsg := log.TakeAll()
-				assert.Assert(t, len(logmsg) > 0)
-				assert.Assert(t, strings.Contains(logmsg[0].Message, tt.wantLog))
+				assert.Assert(t, len(logmsg) > 0, "We didn't get any log message")
+				assert.Assert(t, strings.Contains(logmsg[0].Message, tt.wantLog), logmsg[0].Message, tt.wantLog)
 			}
 		})
 	}
diff --git a/pkg/pipelineascode/pipelineascode_test.go b/pkg/pipelineascode/pipelineascode_test.go
index 42413e19..ba4bb822 100644
--- a/pkg/pipelineascode/pipelineascode_test.go
+++ b/pkg/pipelineascode/pipelineascode_test.go
@@ -136,7 +136,7 @@ func TestRun(t *testing.T) {
 				EventType:  "pull_request",
 			},
 			tektondir:   "testdata/pull_request",
-			wantErr:     "cannot match any pipeline",
+			wantErr:     "cannot match pipeline from webhook to pipelineruns",
 			finalStatus: "neutral",
 		},
 		{