Skip to content

Commit 2474d63

Browse files
committed
Added general tests.
1 parent c6a2454 commit 2474d63

File tree

2 files changed

+302
-4
lines changed

2 files changed

+302
-4
lines changed

services/webhook/general.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, with
4444
case api.HookIssueEdited:
4545
text = fmt.Sprintf("[%s] Issue edited: %s", repoLink, titleLink)
4646
case api.HookIssueAssigned:
47-
text = fmt.Sprintf("[%s] Issue assigned to %s: %s", repoLink,
48-
linkFormatter(setting.AppURL+p.Issue.Assignee.UserName, p.Issue.Assignee.UserName), titleLink)
47+
list := make([]string, len(p.Issue.Assignees))
48+
for i, user := range p.Issue.Assignees {
49+
list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName)
50+
}
51+
text = fmt.Sprintf("[%s] Issue assigned to %s: %s", repoLink, strings.Join(list, ", "), titleLink)
4952
color = greenColor
5053
case api.HookIssueUnassigned:
5154
text = fmt.Sprintf("[%s] Issue unassigned: %s", repoLink, titleLink)
@@ -102,7 +105,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
102105
for i, user := range p.PullRequest.Assignees {
103106
list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName)
104107
}
105-
text = fmt.Sprintf("[%s] Pull request assigned: %s to %s", repoLink,
108+
text = fmt.Sprintf("[%s] Pull request assigned to %s: %s", repoLink,
106109
strings.Join(list, ", "), titleLink)
107110
color = greenColor
108111
case api.HookIssueUnassigned:
@@ -115,7 +118,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
115118
text = fmt.Sprintf("[%s] Pull request synchronized: %s", repoLink, titleLink)
116119
case api.HookIssueMilestoned:
117120
mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID)
118-
text = fmt.Sprintf("[%s] Pull request milestoned: %s to %s", repoLink,
121+
text = fmt.Sprintf("[%s] Pull request milestoned to %s: %s", repoLink,
119122
linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink)
120123
case api.HookIssueDemilestoned:
121124
text = fmt.Sprintf("[%s] Pull request milestone cleared: %s", repoLink, titleLink)

services/webhook/general_test.go

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
package webhook
66

77
import (
8+
"testing"
9+
810
api "code.gitea.io/gitea/modules/structs"
11+
12+
"github.com/stretchr/testify/assert"
913
)
1014

1115
func createTestPayload() *api.CreatePayload {
@@ -119,6 +123,17 @@ func issueTestPayload() *api.IssuePayload {
119123
HTMLURL: "http://localhost:3000/test/repo/issues/2",
120124
Title: "crash",
121125
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+
},
122137
},
123138
}
124139
}
@@ -223,6 +238,17 @@ func pullRequestTestPayload() *api.PullRequestPayload {
223238
Title: "Fix bug",
224239
Body: "fixes bug #2",
225240
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+
},
226252
},
227253
Review: &api.ReviewPayload{
228254
Content: "good job",
@@ -244,3 +270,272 @@ func repositoryTestPayload() *api.RepositoryPayload {
244270
},
245271
}
246272
}
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

Comments
 (0)