Skip to content

Commit ea914d0

Browse files
authored
Fix bug for webhook and feishu caused by API changed (#13937)
* Fix bug for webhook * Fix bug for feishu
1 parent b355466 commit ea914d0

File tree

3 files changed

+37
-57
lines changed

3 files changed

+37
-57
lines changed

models/webhook.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package models
88
import (
99
"encoding/json"
1010
"fmt"
11+
"strings"
1112
"time"
1213

1314
"code.gitea.io/gitea/modules/log"
@@ -310,6 +311,7 @@ func CreateWebhook(w *Webhook) error {
310311
}
311312

312313
func createWebhook(e Engine, w *Webhook) error {
314+
w.Type = strings.TrimSpace(w.Type)
313315
_, err := e.Insert(w)
314316
return err
315317
}
@@ -547,7 +549,7 @@ func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
547549
// \/ \/ \/ \/ \/
548550

549551
// HookTaskType is the type of an hook task
550-
type HookTaskType string
552+
type HookTaskType = string
551553

552554
// Types of hook tasks
553555
const (

services/webhook/feishu.go

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@ import (
1717
type (
1818
// FeishuPayload represents
1919
FeishuPayload struct {
20-
Title string `json:"title"`
21-
Text string `json:"text"`
20+
MsgType string `json:"msg_type"` // text / post / image / share_chat / interactive
21+
Content struct {
22+
Text string `json:"text"`
23+
} `json:"content"`
2224
}
2325
)
2426

27+
func newFeishuTextPayload(text string) *FeishuPayload {
28+
return &FeishuPayload{
29+
MsgType: "text",
30+
Content: struct {
31+
Text string `json:"text"`
32+
}{
33+
Text: text,
34+
},
35+
}
36+
}
37+
2538
// SetSecret sets the Feishu secret
2639
func (f *FeishuPayload) SetSecret(_ string) {}
2740

@@ -42,34 +55,25 @@ var (
4255
func (f *FeishuPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
4356
// created tag/branch
4457
refName := git.RefEndName(p.Ref)
45-
title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
58+
text := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
4659

47-
return &FeishuPayload{
48-
Text: title,
49-
Title: title,
50-
}, nil
60+
return newFeishuTextPayload(text), nil
5161
}
5262

5363
// Delete implements PayloadConvertor Delete method
5464
func (f *FeishuPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
5565
// created tag/branch
5666
refName := git.RefEndName(p.Ref)
57-
title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
67+
text := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
5868

59-
return &FeishuPayload{
60-
Text: title,
61-
Title: title,
62-
}, nil
69+
return newFeishuTextPayload(text), nil
6370
}
6471

6572
// Fork implements PayloadConvertor Fork method
6673
func (f *FeishuPayload) Fork(p *api.ForkPayload) (api.Payloader, error) {
67-
title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
74+
text := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
6875

69-
return &FeishuPayload{
70-
Text: title,
71-
Title: title,
72-
}, nil
76+
return newFeishuTextPayload(text), nil
7377
}
7478

7579
// Push implements PayloadConvertor Push method
@@ -79,9 +83,7 @@ func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) {
7983
commitDesc string
8084
)
8185

82-
title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc)
83-
84-
var text string
86+
var text = fmt.Sprintf("[%s:%s] %s\n", p.Repo.FullName, branchName, commitDesc)
8587
// for each commit, generate attachment text
8688
for i, commit := range p.Commits {
8789
var authorName string
@@ -96,40 +98,28 @@ func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) {
9698
}
9799
}
98100

99-
return &FeishuPayload{
100-
Text: text,
101-
Title: title,
102-
}, nil
101+
return newFeishuTextPayload(text), nil
103102
}
104103

105104
// Issue implements PayloadConvertor Issue method
106105
func (f *FeishuPayload) Issue(p *api.IssuePayload) (api.Payloader, error) {
107106
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
108107

109-
return &FeishuPayload{
110-
Text: text + "\r\n\r\n" + attachmentText,
111-
Title: issueTitle,
112-
}, nil
108+
return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText), nil
113109
}
114110

115111
// IssueComment implements PayloadConvertor IssueComment method
116112
func (f *FeishuPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) {
117113
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
118114

119-
return &FeishuPayload{
120-
Text: text + "\r\n\r\n" + p.Comment.Body,
121-
Title: issueTitle,
122-
}, nil
115+
return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + p.Comment.Body), nil
123116
}
124117

125118
// PullRequest implements PayloadConvertor PullRequest method
126119
func (f *FeishuPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) {
127120
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
128121

129-
return &FeishuPayload{
130-
Text: text + "\r\n\r\n" + attachmentText,
131-
Title: issueTitle,
132-
}, nil
122+
return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText), nil
133123
}
134124

135125
// Review implements PayloadConvertor Review method
@@ -147,28 +137,19 @@ func (f *FeishuPayload) Review(p *api.PullRequestPayload, event models.HookEvent
147137

148138
}
149139

150-
return &FeishuPayload{
151-
Text: title + "\r\n\r\n" + text,
152-
Title: title,
153-
}, nil
140+
return newFeishuTextPayload(title + "\r\n\r\n" + text), nil
154141
}
155142

156143
// Repository implements PayloadConvertor Repository method
157144
func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, error) {
158-
var title string
145+
var text string
159146
switch p.Action {
160147
case api.HookRepoCreated:
161-
title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
162-
return &FeishuPayload{
163-
Text: title,
164-
Title: title,
165-
}, nil
148+
text = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
149+
return newFeishuTextPayload(text), nil
166150
case api.HookRepoDeleted:
167-
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
168-
return &FeishuPayload{
169-
Title: title,
170-
Text: title,
171-
}, nil
151+
text = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
152+
return newFeishuTextPayload(text), nil
172153
}
173154

174155
return nil, nil
@@ -178,10 +159,7 @@ func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, err
178159
func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
179160
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
180161

181-
return &FeishuPayload{
182-
Text: text,
183-
Title: text,
184-
}, nil
162+
return newFeishuTextPayload(text), nil
185163
}
186164

187165
// GetFeishuPayload converts a ding talk webhook into a FeishuPayload

services/webhook/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo
144144

145145
var payloader api.Payloader
146146
var err error
147-
webhook, ok := webhooks[w.Type]
147+
webhook, ok := webhooks[strings.TrimSpace(w.Type)] // NOTICE: w.Type maynot be trimmed before store into database
148148
if ok {
149149
payloader, err = webhook.payloadCreator(p, event, w.Meta)
150150
if err != nil {

0 commit comments

Comments
 (0)