Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion pkg/provider/gitlab/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (v *Provider) Detect(req *http.Request, payload string, logger *zap.Sugared
func hasOnlyLabelsChanged(gitEvent *gitlab.MergeEvent) bool {
changes := gitEvent.Changes

labelsChanged := len(changes.Labels.Previous) > 0 || len(changes.Labels.Current) > 0
labelsChanged := isNewLabelAdded(changes.Labels.Previous, changes.Labels.Current)

// Only Labels can change β€” everything else must be zero or nil
onlyUpdatedAtOrLabels := labelsChanged &&
Expand All @@ -108,3 +108,21 @@ func hasOnlyLabelsChanged(gitEvent *gitlab.MergeEvent) bool {

return onlyUpdatedAtOrLabels
}

func isNewLabelAdded(previous, current []*gitlab.EventLabel) bool {
newLabels := 0
for _, label := range current {
found := false
for _, previousLabel := range previous {
if label.Title == previousLabel.Title {
found = true
break
}
}
if !found {
newLabels++
Comment on lines +122 to +123

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can return early after the first new label found, no need to keep the total count.

}
}

return newLabels > 0
}
37 changes: 37 additions & 0 deletions pkg/provider/gitlab/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (

const largeComment = "/Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"

func mrEventWithChanges(sample thelp.TEvent, changesJSON string) string {
base := sample.MREventAsJSON("update", "")
idx := strings.LastIndex(base, "}")
return base[:idx] + `,"changes": {` + changesJSON + `}}`
}

func TestProviderDetect(t *testing.T) {
sample := thelp.TEvent{
Username: "foo",
Expand Down Expand Up @@ -173,6 +179,37 @@ func TestProviderDetect(t *testing.T) {
isGL: true,
processReq: true,
},
{
name: "good/mergeRequest update Event with label addition",
event: mrEventWithChanges(sample, `"labels": {"previous": [], "current": [{"id": 1, "title": "bug"}]}`),
eventType: gitlab.EventTypeMergeRequest,
isGL: true,
processReq: true,
},
Comment on lines +183 to +188

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Add a test case to verify that simultaneous label addition and removal (where the total label count remains unchanged) is correctly processed as a valid label addition event.

		{
			name:       "good/mergeRequest update Event with label addition",
			event:      mrEventWithChanges(sample, "\"labels\": {\"previous\": [], \"current\": [{\"id\": 1, \"title\": \"bug\"}]}"),
			eventType:  gitlab.EventTypeMergeRequest,
			isGL:       true,
			processReq: true,
		},
		{
			name:       "good/mergeRequest update Event with label addition and removal simultaneously",
			event:      mrEventWithChanges(sample, "\"labels\": {\"previous\": [{\"id\": 2, \"title\": \"feature\"}], \"current\": [{\"id\": 1, \"title\": \"bug\"}]}"),
			eventType:  gitlab.EventTypeMergeRequest,
			isGL:       true,
			processReq: true,
		},

{
name: "bad/mergeRequest update Event with label removal",
event: mrEventWithChanges(sample, `"labels": {"previous": [{"id": 1, "title": "bug"}], "current": []}`),
eventType: gitlab.EventTypeMergeRequest,
isGL: true,
processReq: false,
wantReason: "this 'Merge Request' update event changes are not supported",
},
{
name: "bad/mergeRequest update Event with label removal partial",
event: mrEventWithChanges(sample, `"labels": {"previous": [{"id": 1, "title": "bug"}, {"id": 2, "title": "feature"}], "current": [{"id": 1, "title": "bug"}]}`),
eventType: gitlab.EventTypeMergeRequest,
isGL: true,
processReq: false,
wantReason: "this 'Merge Request' update event changes are not supported",
},
{
name: "bad/mergeRequest update Event with label addition and description change",
event: mrEventWithChanges(sample, `"labels": {"previous": [], "current": [{"id": 1, "title": "bug"}]}, "description": {"previous": "old", "current": "new"}`),
eventType: gitlab.EventTypeMergeRequest,
isGL: true,
processReq: false,
wantReason: "this 'Merge Request' update event changes are not supported",
},
{
name: "good/commit comment /retest command",
event: sample.CommitNoteEventAsJSON("/retest", "create", "null"),
Expand Down
Loading