0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-10-24 14:20:22 +02:00

Enhance matchIssuesEvent function to support multiple label actions and add corresponding tests

This commit is contained in:
Sumit Paul 2025-04-29 23:32:08 +05:30
parent 15e707c0c8
commit cecac31f60
2 changed files with 93 additions and 18 deletions

View File

@ -367,21 +367,35 @@ func matchIssuesEvent(issuePayload *api.IssuePayload, evt *jobparser.Event) bool
// Unsupported activity types: // Unsupported activity types:
// deleted, transferred, pinned, unpinned, locked, unlocked // deleted, transferred, pinned, unpinned, locked, unlocked
action := issuePayload.Action actions := []string{}
switch action { switch issuePayload.Action {
case api.HookIssueLabelUpdated: case api.HookIssueLabelUpdated:
// Check if any labels were removed to determine if this should be "labeled" or "unlabeled" // Check if both labels were added and removed to determine events to fire
if len(issuePayload.RemovedLabels) > 0 { if len(issuePayload.Issue.Labels) > 0 && len(issuePayload.RemovedLabels) > 0 {
action = "unlabeled" // Both labeled and unlabeled events should be triggered
actions = append(actions, "labeled", "unlabeled")
} else if len(issuePayload.RemovedLabels) > 0 {
// Only labels were removed
actions = append(actions, "unlabeled")
} else { } else {
action = "labeled" // Only labels were added
actions = append(actions, "labeled")
} }
case api.HookIssueLabelCleared: case api.HookIssueLabelCleared:
action = "unlabeled" actions = append(actions, "unlabeled")
default:
actions = append(actions, string(issuePayload.Action))
} }
for _, val := range vals { for _, val := range vals {
if glob.MustCompile(val, '/').Match(string(action)) { for _, action := range actions {
matchTimes++ if glob.MustCompile(val, '/').Match(action) {
matchTimes++
break
}
}
// Once a match is found for this value, we can move to the next one
if matchTimes > 0 {
break break
} }
} }

View File

@ -204,6 +204,57 @@ func TestMatchIssuesEvent(t *testing.T) {
expected: true, expected: true,
eventType: "unlabeled", eventType: "unlabeled",
}, },
{
desc: "Both adding and removing labels should trigger labeled event",
payload: &api.IssuePayload{
Action: api.HookIssueLabelUpdated,
Issue: &api.Issue{
Labels: []*api.Label{
{ID: 789, Name: "new-label"},
},
},
RemovedLabels: []*api.Label{
{ID: 123, Name: "deleted-label"},
},
},
yamlOn: "on:\n issues:\n types: [labeled]",
expected: true,
eventType: "labeled",
},
{
desc: "Both adding and removing labels should trigger unlabeled event",
payload: &api.IssuePayload{
Action: api.HookIssueLabelUpdated,
Issue: &api.Issue{
Labels: []*api.Label{
{ID: 789, Name: "new-label"},
},
},
RemovedLabels: []*api.Label{
{ID: 123, Name: "deleted-label"},
},
},
yamlOn: "on:\n issues:\n types: [unlabeled]",
expected: true,
eventType: "unlabeled",
},
{
desc: "Both adding and removing labels should trigger both events",
payload: &api.IssuePayload{
Action: api.HookIssueLabelUpdated,
Issue: &api.Issue{
Labels: []*api.Label{
{ID: 789, Name: "new-label"},
},
},
RemovedLabels: []*api.Label{
{ID: 123, Name: "deleted-label"},
},
},
yamlOn: "on:\n issues:\n types: [labeled, unlabeled]",
expected: true,
eventType: "multiple",
},
} }
for _, tc := range testCases { for _, tc := range testCases {
@ -215,19 +266,29 @@ func TestMatchIssuesEvent(t *testing.T) {
// Test if the event matches as expected // Test if the event matches as expected
assert.Equal(t, tc.expected, matchIssuesEvent(tc.payload, evts[0])) assert.Equal(t, tc.expected, matchIssuesEvent(tc.payload, evts[0]))
// For extra validation, use a direct call to test the actual mapping // For extra validation, check that action mapping works correctly
action := tc.payload.Action if tc.eventType == "multiple" {
switch action { // Skip direct action mapping validation for multiple events case
// as one action can map to multiple event types
return
}
// Determine expected action for single event case
var expectedAction string
switch tc.payload.Action {
case api.HookIssueLabelUpdated: case api.HookIssueLabelUpdated:
if len(tc.payload.RemovedLabels) > 0 { if tc.eventType == "labeled" {
action = "unlabeled" expectedAction = "labeled"
} else { } else if tc.eventType == "unlabeled" {
action = "labeled" expectedAction = "unlabeled"
} }
case api.HookIssueLabelCleared: case api.HookIssueLabelCleared:
action = "unlabeled" expectedAction = "unlabeled"
default:
expectedAction = string(tc.payload.Action)
} }
assert.Equal(t, tc.eventType, string(action), "Event type should match expected")
assert.Equal(t, tc.eventType, expectedAction, "Event type should match expected")
}) })
} }
} }