diff --git a/pkg/matcher/annotation_matcher.go b/pkg/matcher/annotation_matcher.go index eaa11f1fe..0801085bb 100644 --- a/pkg/matcher/annotation_matcher.go +++ b/pkg/matcher/annotation_matcher.go @@ -92,6 +92,9 @@ func getAnnotationValues(annotation string) ([]string, error) { func getTargetBranch(prun *tektonv1.PipelineRun, event *info.Event) (bool, string, string, error) { var targetEvent, targetBranch string if key, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnEvent]; ok { + if key == "[]" { + return false, "", "", fmt.Errorf("annotation %s is empty", keys.OnEvent) + } targetEvents := []string{event.TriggerTarget.String()} if event.EventType == triggertype.Incoming.String() { // if we have a incoming event, we want to match pipelineruns on both incoming and push @@ -107,6 +110,9 @@ func getTargetBranch(prun *tektonv1.PipelineRun, event *info.Event) (bool, strin } } if key, ok := prun.GetObjectMeta().GetAnnotations()[keys.OnTargetBranch]; ok { + if key == "[]" { + return false, "", "", fmt.Errorf("annotation %s is empty", keys.OnTargetBranch) + } targetEvents := []string{event.BaseBranch} matched, err := matchOnAnnotation(key, targetEvents, true) targetBranch = key diff --git a/pkg/matcher/annotation_matcher_test.go b/pkg/matcher/annotation_matcher_test.go index b975134bd..211a0dbc4 100644 --- a/pkg/matcher/annotation_matcher_test.go +++ b/pkg/matcher/annotation_matcher_test.go @@ -2295,6 +2295,7 @@ func TestGetTargetBranch(t *testing.T) { expectedMatch bool expectedEvent string expectedBranch string + expectedError string }{ { name: "Test with pull_request event", @@ -2353,11 +2354,50 @@ func TestGetTargetBranch(t *testing.T) { expectedEvent: "", expectedBranch: "", }, + { + name: "Test empty array onEvent", + prun: &tektonv1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + keys.OnEvent: "[]", + keys.OnTargetBranch: "main", + }, + }, + }, + event: &info.Event{ + TriggerTarget: triggertype.PullRequest, + EventType: "pull_request", + BaseBranch: "main", + }, + expectedError: fmt.Sprintf("annotation %s is empty", keys.OnEvent), + }, + { + name: "Test empty array onTargetBranch", + prun: &tektonv1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + keys.OnEvent: "pull_request", + keys.OnTargetBranch: "[]", + }, + }, + }, + event: &info.Event{ + TriggerTarget: triggertype.PullRequest, + EventType: "pull_request", + BaseBranch: "main", + }, + expectedError: fmt.Sprintf("annotation %s is empty", keys.OnTargetBranch), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { matched, targetEvent, targetBranch, err := getTargetBranch(tt.prun, tt.event) + if tt.expectedError != "" { + assert.Assert(t, err != nil) + assert.Error(t, err, tt.expectedError, err.Error()) + return + } assert.NilError(t, err) assert.Equal(t, tt.expectedMatch, matched) assert.Equal(t, tt.expectedEvent, targetEvent)