5
5
package webhook
6
6
7
7
import (
8
+ "testing"
9
+
8
10
api "code.gitea.io/gitea/modules/structs"
11
+
12
+ "github.com/stretchr/testify/assert"
9
13
)
10
14
11
15
func createTestPayload () * api.CreatePayload {
@@ -119,6 +123,17 @@ func issueTestPayload() *api.IssuePayload {
119
123
HTMLURL : "http://localhost:3000/test/repo/issues/2" ,
120
124
Title : "crash" ,
121
125
Body : "issue body" ,
126
+ Assignees : []* api.User {
127
+ {
128
+ UserName : "user1" ,
129
+ AvatarURL : "http://localhost:3000/user1/avatar" ,
130
+ },
131
+ },
132
+ Milestone : & api.Milestone {
133
+ ID : 1 ,
134
+ Title : "Milestone Title" ,
135
+ Description : "Milestone Description" ,
136
+ },
122
137
},
123
138
}
124
139
}
@@ -223,6 +238,17 @@ func pullRequestTestPayload() *api.PullRequestPayload {
223
238
Title : "Fix bug" ,
224
239
Body : "fixes bug #2" ,
225
240
Mergeable : true ,
241
+ Assignees : []* api.User {
242
+ {
243
+ UserName : "user1" ,
244
+ AvatarURL : "http://localhost:3000/user1/avatar" ,
245
+ },
246
+ },
247
+ Milestone : & api.Milestone {
248
+ ID : 1 ,
249
+ Title : "Milestone Title" ,
250
+ Description : "Milestone Description" ,
251
+ },
226
252
},
227
253
Review : & api.ReviewPayload {
228
254
Content : "good job" ,
@@ -244,3 +270,272 @@ func repositoryTestPayload() *api.RepositoryPayload {
244
270
},
245
271
}
246
272
}
273
+
274
+ func TestGetIssuesPayloadInfo (t * testing.T ) {
275
+ p := issueTestPayload ()
276
+
277
+ cases := []struct {
278
+ action api.HookIssueAction
279
+ text string
280
+ issueTitle string
281
+ attachmentText string
282
+ color int
283
+ }{
284
+ {
285
+ api .HookIssueOpened ,
286
+ "[test/repo] Issue opened: #2 crash by user1" ,
287
+ "#2 crash" ,
288
+ "issue body" ,
289
+ orangeColor ,
290
+ },
291
+ {
292
+ api .HookIssueClosed ,
293
+ "[test/repo] Issue closed: #2 crash by user1" ,
294
+ "#2 crash" ,
295
+ "" ,
296
+ redColor ,
297
+ },
298
+ {
299
+ api .HookIssueReOpened ,
300
+ "[test/repo] Issue re-opened: #2 crash by user1" ,
301
+ "#2 crash" ,
302
+ "" ,
303
+ yellowColor ,
304
+ },
305
+ {
306
+ api .HookIssueEdited ,
307
+ "[test/repo] Issue edited: #2 crash by user1" ,
308
+ "#2 crash" ,
309
+ "issue body" ,
310
+ yellowColor ,
311
+ },
312
+ {
313
+ api .HookIssueAssigned ,
314
+ "[test/repo] Issue assigned to user1: #2 crash by user1" ,
315
+ "#2 crash" ,
316
+ "" ,
317
+ greenColor ,
318
+ },
319
+ {
320
+ api .HookIssueUnassigned ,
321
+ "[test/repo] Issue unassigned: #2 crash by user1" ,
322
+ "#2 crash" ,
323
+ "" ,
324
+ yellowColor ,
325
+ },
326
+ {
327
+ api .HookIssueLabelUpdated ,
328
+ "[test/repo] Issue labels updated: #2 crash by user1" ,
329
+ "#2 crash" ,
330
+ "" ,
331
+ yellowColor ,
332
+ },
333
+ {
334
+ api .HookIssueLabelCleared ,
335
+ "[test/repo] Issue labels cleared: #2 crash by user1" ,
336
+ "#2 crash" ,
337
+ "" ,
338
+ yellowColor ,
339
+ },
340
+ {
341
+ api .HookIssueSynchronized ,
342
+ "[test/repo] Issue synchronized: #2 crash by user1" ,
343
+ "#2 crash" ,
344
+ "" ,
345
+ yellowColor ,
346
+ },
347
+ {
348
+ api .HookIssueMilestoned ,
349
+ "[test/repo] Issue milestoned to Milestone Title: #2 crash by user1" ,
350
+ "#2 crash" ,
351
+ "" ,
352
+ yellowColor ,
353
+ },
354
+ {
355
+ api .HookIssueDemilestoned ,
356
+ "[test/repo] Issue milestone cleared: #2 crash by user1" ,
357
+ "#2 crash" ,
358
+ "" ,
359
+ yellowColor ,
360
+ },
361
+ }
362
+
363
+ for i , c := range cases {
364
+ p .Action = c .action
365
+ text , issueTitle , attachmentText , color := getIssuesPayloadInfo (p , noneLinkFormatter , true )
366
+ assert .Equal (t , c .text , text , "case %d" , i )
367
+ assert .Equal (t , c .issueTitle , issueTitle , "case %d" , i )
368
+ assert .Equal (t , c .attachmentText , attachmentText , "case %d" , i )
369
+ assert .Equal (t , c .color , color , "case %d" , i )
370
+ }
371
+ }
372
+
373
+ func TestGetPullRequestPayloadInfo (t * testing.T ) {
374
+ p := pullRequestTestPayload ()
375
+
376
+ cases := []struct {
377
+ action api.HookIssueAction
378
+ text string
379
+ issueTitle string
380
+ attachmentText string
381
+ color int
382
+ }{
383
+ {
384
+ api .HookIssueOpened ,
385
+ "[test/repo] Pull request opened: #12 Fix bug by user1" ,
386
+ "#12 Fix bug" ,
387
+ "fixes bug #2" ,
388
+ greenColor ,
389
+ },
390
+ {
391
+ api .HookIssueClosed ,
392
+ "[test/repo] Pull request closed: #12 Fix bug by user1" ,
393
+ "#12 Fix bug" ,
394
+ "" ,
395
+ redColor ,
396
+ },
397
+ {
398
+ api .HookIssueReOpened ,
399
+ "[test/repo] Pull request re-opened: #12 Fix bug by user1" ,
400
+ "#12 Fix bug" ,
401
+ "" ,
402
+ yellowColor ,
403
+ },
404
+ {
405
+ api .HookIssueEdited ,
406
+ "[test/repo] Pull request edited: #12 Fix bug by user1" ,
407
+ "#12 Fix bug" ,
408
+ "fixes bug #2" ,
409
+ yellowColor ,
410
+ },
411
+ {
412
+ api .HookIssueAssigned ,
413
+ "[test/repo] Pull request assigned to user1: #12 Fix bug by user1" ,
414
+ "#12 Fix bug" ,
415
+ "" ,
416
+ greenColor ,
417
+ },
418
+ {
419
+ api .HookIssueUnassigned ,
420
+ "[test/repo] Pull request unassigned: #12 Fix bug by user1" ,
421
+ "#12 Fix bug" ,
422
+ "" ,
423
+ yellowColor ,
424
+ },
425
+ {
426
+ api .HookIssueLabelUpdated ,
427
+ "[test/repo] Pull request labels updated: #12 Fix bug by user1" ,
428
+ "#12 Fix bug" ,
429
+ "" ,
430
+ yellowColor ,
431
+ },
432
+ {
433
+ api .HookIssueLabelCleared ,
434
+ "[test/repo] Pull request labels cleared: #12 Fix bug by user1" ,
435
+ "#12 Fix bug" ,
436
+ "" ,
437
+ yellowColor ,
438
+ },
439
+ {
440
+ api .HookIssueSynchronized ,
441
+ "[test/repo] Pull request synchronized: #12 Fix bug by user1" ,
442
+ "#12 Fix bug" ,
443
+ "" ,
444
+ yellowColor ,
445
+ },
446
+ {
447
+ api .HookIssueMilestoned ,
448
+ "[test/repo] Pull request milestoned to Milestone Title: #12 Fix bug by user1" ,
449
+ "#12 Fix bug" ,
450
+ "" ,
451
+ yellowColor ,
452
+ },
453
+ {
454
+ api .HookIssueDemilestoned ,
455
+ "[test/repo] Pull request milestone cleared: #12 Fix bug by user1" ,
456
+ "#12 Fix bug" ,
457
+ "" ,
458
+ yellowColor ,
459
+ },
460
+ }
461
+
462
+ for i , c := range cases {
463
+ p .Action = c .action
464
+ text , issueTitle , attachmentText , color := getPullRequestPayloadInfo (p , noneLinkFormatter , true )
465
+ assert .Equal (t , c .text , text , "case %d" , i )
466
+ assert .Equal (t , c .issueTitle , issueTitle , "case %d" , i )
467
+ assert .Equal (t , c .attachmentText , attachmentText , "case %d" , i )
468
+ assert .Equal (t , c .color , color , "case %d" , i )
469
+ }
470
+ }
471
+
472
+ func TestGetReleasePayloadInfo (t * testing.T ) {
473
+ p := pullReleaseTestPayload ()
474
+
475
+ cases := []struct {
476
+ action api.HookReleaseAction
477
+ text string
478
+ color int
479
+ }{
480
+ {
481
+ api .HookReleasePublished ,
482
+ "[test/repo] Release created: v1.0 by user1" ,
483
+ greenColor ,
484
+ },
485
+ {
486
+ api .HookReleaseUpdated ,
487
+ "[test/repo] Release updated: v1.0 by user1" ,
488
+ yellowColor ,
489
+ },
490
+ {
491
+ api .HookReleaseDeleted ,
492
+ "[test/repo] Release deleted: v1.0 by user1" ,
493
+ redColor ,
494
+ },
495
+ }
496
+
497
+ for i , c := range cases {
498
+ p .Action = c .action
499
+ text , color := getReleasePayloadInfo (p , noneLinkFormatter , true )
500
+ assert .Equal (t , c .text , text , "case %d" , i )
501
+ assert .Equal (t , c .color , color , "case %d" , i )
502
+ }
503
+ }
504
+
505
+ func TestGetIssueCommentPayloadInfo (t * testing.T ) {
506
+ p := pullRequestCommentTestPayload ()
507
+
508
+ cases := []struct {
509
+ action api.HookIssueCommentAction
510
+ text string
511
+ issueTitle string
512
+ color int
513
+ }{
514
+ {
515
+ api .HookIssueCommentCreated ,
516
+ "[test/repo] New comment on pull request #12 Fix bug by user1" ,
517
+ "#12 Fix bug" ,
518
+ greenColorLight ,
519
+ },
520
+ {
521
+ api .HookIssueCommentEdited ,
522
+ "[test/repo] Comment edited on pull request #12 Fix bug by user1" ,
523
+ "#12 Fix bug" ,
524
+ yellowColor ,
525
+ },
526
+ {
527
+ api .HookIssueCommentDeleted ,
528
+ "[test/repo] Comment deleted on pull request #12 Fix bug by user1" ,
529
+ "#12 Fix bug" ,
530
+ redColor ,
531
+ },
532
+ }
533
+
534
+ for i , c := range cases {
535
+ p .Action = c .action
536
+ text , issueTitle , color := getIssueCommentPayloadInfo (p , noneLinkFormatter , true )
537
+ assert .Equal (t , c .text , text , "case %d" , i )
538
+ assert .Equal (t , c .issueTitle , issueTitle , "case %d" , i )
539
+ assert .Equal (t , c .color , color , "case %d" , i )
540
+ }
541
+ }
0 commit comments