Skip to content

Commit 83a8944

Browse files
authored
Add feishu webhook support (#10229)
Add feishu webhook support
1 parent ea7ad38 commit 83a8944

File tree

16 files changed

+327
-3
lines changed

16 files changed

+327
-3
lines changed

models/webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ const (
445445
DINGTALK
446446
TELEGRAM
447447
MSTEAMS
448+
FEISHU
448449
)
449450

450451
var hookTaskTypes = map[string]HookTaskType{
@@ -455,6 +456,7 @@ var hookTaskTypes = map[string]HookTaskType{
455456
"dingtalk": DINGTALK,
456457
"telegram": TELEGRAM,
457458
"msteams": MSTEAMS,
459+
"feishu": FEISHU,
458460
}
459461

460462
// ToHookTaskType returns HookTaskType by given name.
@@ -479,6 +481,8 @@ func (t HookTaskType) Name() string {
479481
return "telegram"
480482
case MSTEAMS:
481483
return "msteams"
484+
case FEISHU:
485+
return "feishu"
482486
}
483487
return ""
484488
}

modules/auth/repo_form.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,17 @@ func (f *NewMSTeamsHookForm) Validate(ctx *macaron.Context, errs binding.Errors)
313313
return validate(errs, ctx.Data, f, ctx.Locale)
314314
}
315315

316+
// NewFeishuHookForm form for creating feishu hook
317+
type NewFeishuHookForm struct {
318+
PayloadURL string `binding:"Required;ValidUrl"`
319+
WebhookForm
320+
}
321+
322+
// Validate validates the fields
323+
func (f *NewFeishuHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
324+
return validate(errs, ctx.Data, f, ctx.Locale)
325+
}
326+
316327
// .___
317328
// | | ______ ________ __ ____
318329
// | |/ ___// ___/ | \_/ __ \

modules/setting/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func newWebhookService() {
3636
Webhook.QueueLength = sec.Key("QUEUE_LENGTH").MustInt(1000)
3737
Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5)
3838
Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool()
39-
Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams"}
39+
Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams", "feishu"}
4040
Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10)
4141
Webhook.ProxyURL = sec.Key("PROXY_URL").MustString("")
4242
if Webhook.ProxyURL != "" {

modules/structs/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type CreateHookOptionConfig map[string]string
4141
// CreateHookOption options when create a hook
4242
type CreateHookOption struct {
4343
// required: true
44-
// enum: dingtalk,discord,gitea,gogs,msteams,slack,telegram
44+
// enum: dingtalk,discord,gitea,gogs,msteams,slack,telegram,feishu
4545
Type string `json:"type" binding:"Required"`
4646
// required: true
4747
Config CreateHookOptionConfig `json:"config" binding:"Required"`

modules/webhook/feishu.go

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// Copyright 2020 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+
"encoding/json"
9+
"fmt"
10+
"strings"
11+
12+
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/git"
14+
api "code.gitea.io/gitea/modules/structs"
15+
)
16+
17+
type (
18+
// FeishuPayload represents
19+
FeishuPayload struct {
20+
Title string `json:"title"`
21+
Text string `json:"text"`
22+
}
23+
)
24+
25+
// SetSecret sets the Feishu secret
26+
func (p *FeishuPayload) SetSecret(_ string) {}
27+
28+
// JSONPayload Marshals the FeishuPayload to json
29+
func (p *FeishuPayload) JSONPayload() ([]byte, error) {
30+
data, err := json.MarshalIndent(p, "", " ")
31+
if err != nil {
32+
return []byte{}, err
33+
}
34+
return data, nil
35+
}
36+
37+
func getFeishuCreatePayload(p *api.CreatePayload) (*FeishuPayload, error) {
38+
// created tag/branch
39+
refName := git.RefEndName(p.Ref)
40+
title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
41+
42+
return &FeishuPayload{
43+
Text: title,
44+
Title: title,
45+
}, nil
46+
}
47+
48+
func getFeishuDeletePayload(p *api.DeletePayload) (*FeishuPayload, error) {
49+
// created tag/branch
50+
refName := git.RefEndName(p.Ref)
51+
title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
52+
53+
return &FeishuPayload{
54+
Text: title,
55+
Title: title,
56+
}, nil
57+
}
58+
59+
func getFeishuForkPayload(p *api.ForkPayload) (*FeishuPayload, error) {
60+
title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
61+
62+
return &FeishuPayload{
63+
Text: title,
64+
Title: title,
65+
}, nil
66+
}
67+
68+
func getFeishuPushPayload(p *api.PushPayload) (*FeishuPayload, error) {
69+
var (
70+
branchName = git.RefEndName(p.Ref)
71+
commitDesc string
72+
)
73+
74+
title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc)
75+
76+
var text string
77+
// for each commit, generate attachment text
78+
for i, commit := range p.Commits {
79+
var authorName string
80+
if commit.Author != nil {
81+
authorName = " - " + commit.Author.Name
82+
}
83+
text += fmt.Sprintf("[%s](%s) %s", commit.ID[:7], commit.URL,
84+
strings.TrimRight(commit.Message, "\r\n")) + authorName
85+
// add linebreak to each commit but the last
86+
if i < len(p.Commits)-1 {
87+
text += "\n"
88+
}
89+
}
90+
91+
return &FeishuPayload{
92+
Text: text,
93+
Title: title,
94+
}, nil
95+
}
96+
97+
func getFeishuIssuesPayload(p *api.IssuePayload) (*FeishuPayload, error) {
98+
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
99+
100+
return &FeishuPayload{
101+
Text: text + "\r\n\r\n" + attachmentText,
102+
Title: issueTitle,
103+
}, nil
104+
}
105+
106+
func getFeishuIssueCommentPayload(p *api.IssueCommentPayload) (*FeishuPayload, error) {
107+
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
108+
109+
return &FeishuPayload{
110+
Text: text + "\r\n\r\n" + p.Comment.Body,
111+
Title: issueTitle,
112+
}, nil
113+
}
114+
115+
func getFeishuPullRequestPayload(p *api.PullRequestPayload) (*FeishuPayload, error) {
116+
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
117+
118+
return &FeishuPayload{
119+
Text: text + "\r\n\r\n" + attachmentText,
120+
Title: issueTitle,
121+
}, nil
122+
}
123+
124+
func getFeishuPullRequestApprovalPayload(p *api.PullRequestPayload, event models.HookEventType) (*FeishuPayload, error) {
125+
var text, title string
126+
switch p.Action {
127+
case api.HookIssueSynchronized:
128+
action, err := parseHookPullRequestEventType(event)
129+
if err != nil {
130+
return nil, err
131+
}
132+
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+
136+
}
137+
138+
return &FeishuPayload{
139+
Text: title + "\r\n\r\n" + text,
140+
Title: title,
141+
}, nil
142+
}
143+
144+
func getFeishuRepositoryPayload(p *api.RepositoryPayload) (*FeishuPayload, error) {
145+
var title string
146+
switch p.Action {
147+
case api.HookRepoCreated:
148+
title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
149+
return &FeishuPayload{
150+
Text: title,
151+
Title: title,
152+
}, nil
153+
case api.HookRepoDeleted:
154+
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
155+
return &FeishuPayload{
156+
Title: title,
157+
Text: title,
158+
}, nil
159+
}
160+
161+
return nil, nil
162+
}
163+
164+
func getFeishuReleasePayload(p *api.ReleasePayload) (*FeishuPayload, error) {
165+
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
166+
167+
return &FeishuPayload{
168+
Text: text,
169+
Title: text,
170+
}, nil
171+
}
172+
173+
// GetFeishuPayload converts a ding talk webhook into a FeishuPayload
174+
func GetFeishuPayload(p api.Payloader, event models.HookEventType, meta string) (*FeishuPayload, error) {
175+
s := new(FeishuPayload)
176+
177+
switch event {
178+
case models.HookEventCreate:
179+
return getFeishuCreatePayload(p.(*api.CreatePayload))
180+
case models.HookEventDelete:
181+
return getFeishuDeletePayload(p.(*api.DeletePayload))
182+
case models.HookEventFork:
183+
return getFeishuForkPayload(p.(*api.ForkPayload))
184+
case models.HookEventIssues:
185+
return getFeishuIssuesPayload(p.(*api.IssuePayload))
186+
case models.HookEventIssueComment:
187+
return getFeishuIssueCommentPayload(p.(*api.IssueCommentPayload))
188+
case models.HookEventPush:
189+
return getFeishuPushPayload(p.(*api.PushPayload))
190+
case models.HookEventPullRequest:
191+
return getFeishuPullRequestPayload(p.(*api.PullRequestPayload))
192+
case models.HookEventPullRequestApproved, models.HookEventPullRequestRejected, models.HookEventPullRequestComment:
193+
return getFeishuPullRequestApprovalPayload(p.(*api.PullRequestPayload), event)
194+
case models.HookEventRepository:
195+
return getFeishuRepositoryPayload(p.(*api.RepositoryPayload))
196+
case models.HookEventRelease:
197+
return getFeishuReleasePayload(p.(*api.ReleasePayload))
198+
}
199+
200+
return s, nil
201+
}

modules/webhook/webhook.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo
114114
if err != nil {
115115
return fmt.Errorf("GetMSTeamsPayload: %v", err)
116116
}
117+
case models.FEISHU:
118+
payloader, err = GetFeishuPayload(p, event, w.Meta)
119+
if err != nil {
120+
return fmt.Errorf("GetFeishuPayload: %v", err)
121+
}
117122
default:
118123
p.SetSecret(w.Secret)
119124
payloader = p

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,7 @@ settings.add_discord_hook_desc = Integrate <a href="%s">Discord</a> into your re
13981398
settings.add_dingtalk_hook_desc = Integrate <a href="%s">Dingtalk</a> into your repository.
13991399
settings.add_telegram_hook_desc = Integrate <a href="%s">Telegram</a> into your repository.
14001400
settings.add_msteams_hook_desc = Integrate <a href="%s">Microsoft Teams</a> into your repository.
1401+
settings.add_feishu_hook_desc = Integrate <a href="%s">Feishu</a> into your repository.
14011402
settings.deploy_keys = Deploy Keys
14021403
settings.add_deploy_key = Add Deploy Key
14031404
settings.deploy_key_desc = Deploy keys have read-only pull access to the repository.

public/img/feishu.png

1.94 KB
Loading

routers/repo/webhook.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,46 @@ func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
485485
ctx.Redirect(orCtx.Link)
486486
}
487487

488+
// FeishuHooksNewPost response for creating feishu hook
489+
func FeishuHooksNewPost(ctx *context.Context, form auth.NewFeishuHookForm) {
490+
ctx.Data["Title"] = ctx.Tr("repo.settings")
491+
ctx.Data["PageIsSettingsHooks"] = true
492+
ctx.Data["PageIsSettingsHooksNew"] = true
493+
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
494+
495+
orCtx, err := getOrgRepoCtx(ctx)
496+
if err != nil {
497+
ctx.ServerError("getOrgRepoCtx", err)
498+
return
499+
}
500+
501+
if ctx.HasError() {
502+
ctx.HTML(200, orCtx.NewTemplate)
503+
return
504+
}
505+
506+
w := &models.Webhook{
507+
RepoID: orCtx.RepoID,
508+
URL: form.PayloadURL,
509+
ContentType: models.ContentTypeJSON,
510+
HookEvent: ParseHookEvent(form.WebhookForm),
511+
IsActive: form.Active,
512+
HookTaskType: models.FEISHU,
513+
Meta: "",
514+
OrgID: orCtx.OrgID,
515+
}
516+
if err := w.UpdateEvent(); err != nil {
517+
ctx.ServerError("UpdateEvent", err)
518+
return
519+
} else if err := models.CreateWebhook(w); err != nil {
520+
ctx.ServerError("CreateWebhook", err)
521+
return
522+
}
523+
524+
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
525+
ctx.Redirect(orCtx.Link)
526+
}
527+
488528
func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
489529
ctx.Data["RequireHighlightJS"] = true
490530

@@ -819,6 +859,38 @@ func MSTeamsHooksEditPost(ctx *context.Context, form auth.NewMSTeamsHookForm) {
819859
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
820860
}
821861

862+
// FeishuHooksEditPost response for editing feishu hook
863+
func FeishuHooksEditPost(ctx *context.Context, form auth.NewFeishuHookForm) {
864+
ctx.Data["Title"] = ctx.Tr("repo.settings")
865+
ctx.Data["PageIsSettingsHooks"] = true
866+
ctx.Data["PageIsSettingsHooksEdit"] = true
867+
868+
orCtx, w := checkWebhook(ctx)
869+
if ctx.Written() {
870+
return
871+
}
872+
ctx.Data["Webhook"] = w
873+
874+
if ctx.HasError() {
875+
ctx.HTML(200, orCtx.NewTemplate)
876+
return
877+
}
878+
879+
w.URL = form.PayloadURL
880+
w.HookEvent = ParseHookEvent(form.WebhookForm)
881+
w.IsActive = form.Active
882+
if err := w.UpdateEvent(); err != nil {
883+
ctx.ServerError("UpdateEvent", err)
884+
return
885+
} else if err := models.UpdateWebhook(w); err != nil {
886+
ctx.ServerError("UpdateWebhook", err)
887+
return
888+
}
889+
890+
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
891+
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
892+
}
893+
822894
// TestWebhook test if web hook is work fine
823895
func TestWebhook(ctx *context.Context) {
824896
hookID := ctx.ParamsInt64(":id")

0 commit comments

Comments
 (0)