Skip to content

Commit 803d9c1

Browse files
committed
Added tests for Feishu.
1 parent 04b32b4 commit 803d9c1

File tree

2 files changed

+147
-13
lines changed

2 files changed

+147
-13
lines changed

services/webhook/feishu.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) {
8484
commitDesc string
8585
)
8686

87-
var text = fmt.Sprintf("[%s:%s] %s\n", p.Repo.FullName, branchName, commitDesc)
87+
var text = fmt.Sprintf("[%s:%s] %s\r\n", p.Repo.FullName, branchName, commitDesc)
8888
// for each commit, generate attachment text
8989
for i, commit := range p.Commits {
9090
var authorName string
@@ -95,7 +95,7 @@ func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) {
9595
strings.TrimRight(commit.Message, "\r\n")) + authorName
9696
// add linebreak to each commit but the last
9797
if i < len(p.Commits)-1 {
98-
text += "\n"
98+
text += "\r\n"
9999
}
100100
}
101101

@@ -125,19 +125,14 @@ func (f *FeishuPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, e
125125

126126
// Review implements PayloadConvertor Review method
127127
func (f *FeishuPayload) Review(p *api.PullRequestPayload, event models.HookEventType) (api.Payloader, error) {
128-
var text, title string
129-
switch p.Action {
130-
case api.HookIssueSynchronized:
131-
action, err := parseHookPullRequestEventType(event)
132-
if err != nil {
133-
return nil, err
134-
}
135-
136-
title = fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
137-
text = p.Review.Content
138-
128+
action, err := parseHookPullRequestEventType(event)
129+
if err != nil {
130+
return nil, err
139131
}
140132

133+
title := fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
134+
text := p.Review.Content
135+
141136
return newFeishuTextPayload(title + "\r\n\r\n" + text), nil
142137
}
143138

services/webhook/feishu_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package webhook
6+
7+
import (
8+
"testing"
9+
10+
"code.gitea.io/gitea/models"
11+
api "code.gitea.io/gitea/modules/structs"
12+
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestFeishuPayload(t *testing.T) {
18+
t.Run("Create", func(t *testing.T) {
19+
p := createTestPayload()
20+
21+
d := new(FeishuPayload)
22+
pl, err := d.Create(p)
23+
require.NoError(t, err)
24+
require.NotNil(t, pl)
25+
require.IsType(t, &FeishuPayload{}, pl)
26+
27+
assert.Equal(t, `[test/repo] branch test created`, pl.(*FeishuPayload).Content.Text)
28+
})
29+
30+
t.Run("Delete", func(t *testing.T) {
31+
p := deleteTestPayload()
32+
33+
d := new(FeishuPayload)
34+
pl, err := d.Delete(p)
35+
require.NoError(t, err)
36+
require.NotNil(t, pl)
37+
require.IsType(t, &FeishuPayload{}, pl)
38+
39+
assert.Equal(t, `[test/repo] branch test deleted`, pl.(*FeishuPayload).Content.Text)
40+
})
41+
42+
t.Run("Fork", func(t *testing.T) {
43+
p := forkTestPayload()
44+
45+
d := new(FeishuPayload)
46+
pl, err := d.Fork(p)
47+
require.NoError(t, err)
48+
require.NotNil(t, pl)
49+
require.IsType(t, &FeishuPayload{}, pl)
50+
51+
assert.Equal(t, `test/repo2 is forked to test/repo`, pl.(*FeishuPayload).Content.Text)
52+
})
53+
54+
t.Run("Push", func(t *testing.T) {
55+
p := pushTestPayload()
56+
57+
d := new(FeishuPayload)
58+
pl, err := d.Push(p)
59+
require.NoError(t, err)
60+
require.NotNil(t, pl)
61+
require.IsType(t, &FeishuPayload{}, pl)
62+
63+
assert.Equal(t, "[test/repo:test] \r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1\r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1", pl.(*FeishuPayload).Content.Text)
64+
})
65+
66+
t.Run("Issue", func(t *testing.T) {
67+
p := issueTestPayload()
68+
69+
d := new(FeishuPayload)
70+
p.Action = api.HookIssueOpened
71+
pl, err := d.Issue(p)
72+
require.NoError(t, err)
73+
require.NotNil(t, pl)
74+
require.IsType(t, &FeishuPayload{}, pl)
75+
76+
assert.Equal(t, "#2 crash\r\n[test/repo] Issue opened: #2 crash by user1\r\n\r\nissue body", pl.(*FeishuPayload).Content.Text)
77+
})
78+
79+
t.Run("IssueComment", func(t *testing.T) {
80+
p := issueCommentTestPayload()
81+
82+
d := new(FeishuPayload)
83+
pl, err := d.IssueComment(p)
84+
require.NoError(t, err)
85+
require.NotNil(t, pl)
86+
require.IsType(t, &FeishuPayload{}, pl)
87+
88+
assert.Equal(t, "#2 crash\r\n[test/repo] New comment on issue #2 crash by user1\r\n\r\nmore info needed", pl.(*FeishuPayload).Content.Text)
89+
})
90+
91+
t.Run("PullRequest", func(t *testing.T) {
92+
p := pullRequestTestPayload()
93+
94+
d := new(FeishuPayload)
95+
pl, err := d.PullRequest(p)
96+
require.NoError(t, err)
97+
require.NotNil(t, pl)
98+
require.IsType(t, &FeishuPayload{}, pl)
99+
100+
assert.Equal(t, "#12 Fix bug\r\n[test/repo] Pull request opened: #12 Fix bug by user1\r\n\r\nfixes bug #2", pl.(*FeishuPayload).Content.Text)
101+
})
102+
103+
t.Run("Review", func(t *testing.T) {
104+
p := pullRequestTestPayload()
105+
p.Action = api.HookIssueReviewed
106+
107+
d := new(FeishuPayload)
108+
pl, err := d.Review(p, models.HookEventPullRequestReviewApproved)
109+
require.NoError(t, err)
110+
require.NotNil(t, pl)
111+
require.IsType(t, &FeishuPayload{}, pl)
112+
113+
assert.Equal(t, "[test/repo] Pull request review approved : #12 Fix bug\r\n\r\ngood job", pl.(*FeishuPayload).Content.Text)
114+
})
115+
116+
t.Run("Repository", func(t *testing.T) {
117+
p := repositoryTestPayload()
118+
119+
d := new(FeishuPayload)
120+
pl, err := d.Repository(p)
121+
require.NoError(t, err)
122+
require.NotNil(t, pl)
123+
require.IsType(t, &FeishuPayload{}, pl)
124+
125+
assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text)
126+
})
127+
128+
t.Run("Release", func(t *testing.T) {
129+
p := pullReleaseTestPayload()
130+
131+
d := new(FeishuPayload)
132+
pl, err := d.Release(p)
133+
require.NoError(t, err)
134+
require.NotNil(t, pl)
135+
require.IsType(t, &FeishuPayload{}, pl)
136+
137+
assert.Equal(t, "[test/repo] Release created: v1.0 by user1", pl.(*FeishuPayload).Content.Text)
138+
})
139+
}

0 commit comments

Comments
 (0)