Skip to content

Commit

Permalink
Make sure we don't have empty values in key
Browse files Browse the repository at this point in the history
if the user has an annoation for onEvent and onTargetBranch with `[]` it
would be impossible to match anything so we should return an error
since onTargetBranch and onEvent are required.

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel committed Dec 6, 2024
1 parent 78b2ea7 commit 425d4f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/matcher/annotation_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions pkg/matcher/annotation_matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,7 @@ func TestGetTargetBranch(t *testing.T) {
expectedMatch bool
expectedEvent string
expectedBranch string
expectedError string
}{
{
name: "Test with pull_request event",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 425d4f2

Please sign in to comment.