@@ -136,3 +136,98 @@ func TestDetectMatched(t *testing.T) {
136136 })
137137 }
138138}
139+
140+ func TestMatchIssuesEvent (t * testing.T ) {
141+ testCases := []struct {
142+ desc string
143+ payload * api.IssuePayload
144+ yamlOn string
145+ expected bool
146+ eventType string
147+ }{
148+ {
149+ desc : "Label deletion should trigger unlabeled event" ,
150+ payload : & api.IssuePayload {
151+ Action : api .HookIssueLabelUpdated ,
152+ Issue : & api.Issue {
153+ Labels : []* api.Label {},
154+ },
155+ RemovedLabels : []* api.Label {
156+ {ID : 123 , Name : "deleted-label" },
157+ },
158+ },
159+ yamlOn : "on:\n issues:\n types: [unlabeled]" ,
160+ expected : true ,
161+ eventType : "unlabeled" ,
162+ },
163+ {
164+ desc : "Label deletion with existing labels should trigger unlabeled event" ,
165+ payload : & api.IssuePayload {
166+ Action : api .HookIssueLabelUpdated ,
167+ Issue : & api.Issue {
168+ Labels : []* api.Label {
169+ {ID : 456 , Name : "existing-label" },
170+ },
171+ },
172+ RemovedLabels : []* api.Label {
173+ {ID : 123 , Name : "deleted-label" },
174+ },
175+ },
176+ yamlOn : "on:\n issues:\n types: [unlabeled]" ,
177+ expected : true ,
178+ eventType : "unlabeled" ,
179+ },
180+ {
181+ desc : "Label addition should trigger labeled event" ,
182+ payload : & api.IssuePayload {
183+ Action : api .HookIssueLabelUpdated ,
184+ Issue : & api.Issue {
185+ Labels : []* api.Label {
186+ {ID : 123 , Name : "new-label" },
187+ },
188+ },
189+ RemovedLabels : []* api.Label {}, // Empty array, no labels removed
190+ },
191+ yamlOn : "on:\n issues:\n types: [labeled]" ,
192+ expected : true ,
193+ eventType : "labeled" ,
194+ },
195+ {
196+ desc : "Label clear should trigger unlabeled event" ,
197+ payload : & api.IssuePayload {
198+ Action : api .HookIssueLabelCleared ,
199+ Issue : & api.Issue {
200+ Labels : []* api.Label {},
201+ },
202+ },
203+ yamlOn : "on:\n issues:\n types: [unlabeled]" ,
204+ expected : true ,
205+ eventType : "unlabeled" ,
206+ },
207+ }
208+
209+ for _ , tc := range testCases {
210+ t .Run (tc .desc , func (t * testing.T ) {
211+ evts , err := GetEventsFromContent ([]byte (tc .yamlOn ))
212+ assert .NoError (t , err )
213+ assert .Len (t , evts , 1 )
214+
215+ // Test if the event matches as expected
216+ assert .Equal (t , tc .expected , matchIssuesEvent (tc .payload , evts [0 ]))
217+
218+ // For extra validation, use a direct call to test the actual mapping
219+ action := tc .payload .Action
220+ switch action {
221+ case api .HookIssueLabelUpdated :
222+ if len (tc .payload .RemovedLabels ) > 0 {
223+ action = "unlabeled"
224+ } else {
225+ action = "labeled"
226+ }
227+ case api .HookIssueLabelCleared :
228+ action = "unlabeled"
229+ }
230+ assert .Equal (t , tc .eventType , string (action ), "Event type should match expected" )
231+ })
232+ }
233+ }
0 commit comments