mirror of
https://github.com/go-gitea/gitea.git
synced 2025-07-21 05:54:38 +02:00
Enhance label event handling in matchIssuesEvent function and add tests for label addition/removal scenarios
This commit is contained in:
parent
7bd2ce7109
commit
15e707c0c8
@ -362,7 +362,7 @@ func matchIssuesEvent(issuePayload *api.IssuePayload, evt *jobparser.Event) bool
|
||||
// Actions with the same name:
|
||||
// opened, edited, closed, reopened, assigned, unassigned, milestoned, demilestoned
|
||||
// Actions need to be converted:
|
||||
// label_updated -> labeled
|
||||
// label_updated -> labeled (when adding) or unlabeled (when removing)
|
||||
// label_cleared -> unlabeled
|
||||
// Unsupported activity types:
|
||||
// deleted, transferred, pinned, unpinned, locked, unlocked
|
||||
@ -370,7 +370,12 @@ func matchIssuesEvent(issuePayload *api.IssuePayload, evt *jobparser.Event) bool
|
||||
action := issuePayload.Action
|
||||
switch action {
|
||||
case api.HookIssueLabelUpdated:
|
||||
action = "labeled"
|
||||
// Check if any labels were removed to determine if this should be "labeled" or "unlabeled"
|
||||
if len(issuePayload.RemovedLabels) > 0 {
|
||||
action = "unlabeled"
|
||||
} else {
|
||||
action = "labeled"
|
||||
}
|
||||
case api.HookIssueLabelCleared:
|
||||
action = "unlabeled"
|
||||
}
|
||||
|
@ -136,3 +136,98 @@ func TestDetectMatched(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchIssuesEvent(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
payload *api.IssuePayload
|
||||
yamlOn string
|
||||
expected bool
|
||||
eventType string
|
||||
}{
|
||||
{
|
||||
desc: "Label deletion should trigger unlabeled event",
|
||||
payload: &api.IssuePayload{
|
||||
Action: api.HookIssueLabelUpdated,
|
||||
Issue: &api.Issue{
|
||||
Labels: []*api.Label{},
|
||||
},
|
||||
RemovedLabels: []*api.Label{
|
||||
{ID: 123, Name: "deleted-label"},
|
||||
},
|
||||
},
|
||||
yamlOn: "on:\n issues:\n types: [unlabeled]",
|
||||
expected: true,
|
||||
eventType: "unlabeled",
|
||||
},
|
||||
{
|
||||
desc: "Label deletion with existing labels should trigger unlabeled event",
|
||||
payload: &api.IssuePayload{
|
||||
Action: api.HookIssueLabelUpdated,
|
||||
Issue: &api.Issue{
|
||||
Labels: []*api.Label{
|
||||
{ID: 456, Name: "existing-label"},
|
||||
},
|
||||
},
|
||||
RemovedLabels: []*api.Label{
|
||||
{ID: 123, Name: "deleted-label"},
|
||||
},
|
||||
},
|
||||
yamlOn: "on:\n issues:\n types: [unlabeled]",
|
||||
expected: true,
|
||||
eventType: "unlabeled",
|
||||
},
|
||||
{
|
||||
desc: "Label addition should trigger labeled event",
|
||||
payload: &api.IssuePayload{
|
||||
Action: api.HookIssueLabelUpdated,
|
||||
Issue: &api.Issue{
|
||||
Labels: []*api.Label{
|
||||
{ID: 123, Name: "new-label"},
|
||||
},
|
||||
},
|
||||
RemovedLabels: []*api.Label{}, // Empty array, no labels removed
|
||||
},
|
||||
yamlOn: "on:\n issues:\n types: [labeled]",
|
||||
expected: true,
|
||||
eventType: "labeled",
|
||||
},
|
||||
{
|
||||
desc: "Label clear should trigger unlabeled event",
|
||||
payload: &api.IssuePayload{
|
||||
Action: api.HookIssueLabelCleared,
|
||||
Issue: &api.Issue{
|
||||
Labels: []*api.Label{},
|
||||
},
|
||||
},
|
||||
yamlOn: "on:\n issues:\n types: [unlabeled]",
|
||||
expected: true,
|
||||
eventType: "unlabeled",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
evts, err := GetEventsFromContent([]byte(tc.yamlOn))
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, evts, 1)
|
||||
|
||||
// Test if the event matches as expected
|
||||
assert.Equal(t, tc.expected, matchIssuesEvent(tc.payload, evts[0]))
|
||||
|
||||
// For extra validation, use a direct call to test the actual mapping
|
||||
action := tc.payload.Action
|
||||
switch action {
|
||||
case api.HookIssueLabelUpdated:
|
||||
if len(tc.payload.RemovedLabels) > 0 {
|
||||
action = "unlabeled"
|
||||
} else {
|
||||
action = "labeled"
|
||||
}
|
||||
case api.HookIssueLabelCleared:
|
||||
action = "unlabeled"
|
||||
}
|
||||
assert.Equal(t, tc.eventType, string(action), "Event type should match expected")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -310,13 +310,14 @@ const (
|
||||
|
||||
// IssuePayload represents the payload information that is sent along with an issue event.
|
||||
type IssuePayload struct {
|
||||
Action HookIssueAction `json:"action"`
|
||||
Index int64 `json:"number"`
|
||||
Changes *ChangesPayload `json:"changes,omitempty"`
|
||||
Issue *Issue `json:"issue"`
|
||||
Repository *Repository `json:"repository"`
|
||||
Sender *User `json:"sender"`
|
||||
CommitID string `json:"commit_id"`
|
||||
Action HookIssueAction `json:"action"`
|
||||
Index int64 `json:"number"`
|
||||
Changes *ChangesPayload `json:"changes,omitempty"`
|
||||
RemovedLabels []*Label `json:"removed_labels"`
|
||||
Issue *Issue `json:"issue"`
|
||||
Repository *Repository `json:"repository"`
|
||||
Sender *User `json:"sender"`
|
||||
CommitID string `json:"commit_id"`
|
||||
}
|
||||
|
||||
// JSONPayload encodes the IssuePayload to JSON, with an indentation of two spaces.
|
||||
|
Loading…
x
Reference in New Issue
Block a user