Skip to content

Commit 74ef25b

Browse files
committed
fix webhooks
1 parent abe3230 commit 74ef25b

File tree

5 files changed

+104
-82
lines changed

5 files changed

+104
-82
lines changed

models/webhook.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ func (w *Webhook) GetSlackHook() *SlackMeta {
150150
return s
151151
}
152152

153+
// GetDiscordHook returns discord metadata
154+
func (w *Webhook) GetDiscordHook() *DiscordMeta {
155+
s := &DiscordMeta{}
156+
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
157+
log.Error(4, "webhook.GetDiscordHook(%d): %v", w.ID, err)
158+
}
159+
return s
160+
}
161+
153162
// History returns history of webhook by given conditions.
154163
func (w *Webhook) History(page int) ([]*HookTask, error) {
155164
return HookTasks(w.ID, page)

models/webhook_discord.go

Lines changed: 83 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"strconv"
78
"strings"
89

910
"code.gitea.io/git"
@@ -56,10 +57,26 @@ type (
5657
DiscordMeta struct {
5758
Username string `json:"username"`
5859
IconURL string `json:"icon_url"`
59-
Color int `json:"color"`
6060
}
6161
)
6262

63+
func color(clr string) int {
64+
if clr != "" {
65+
clr = strings.TrimLeft(clr, "#")
66+
if s, err := strconv.ParseInt(clr, 16, 32); err == nil {
67+
return int(s)
68+
}
69+
}
70+
71+
return 0
72+
}
73+
74+
var (
75+
successColor = color("#1ac600")
76+
warnColor = color("#ffd930")
77+
failedColor = color("#ff3232")
78+
)
79+
6380
// SetSecret sets the slack secret
6481
func (p *DiscordPayload) SetSecret(_ string) {}
6582

@@ -72,84 +89,71 @@ func (p *DiscordPayload) JSONPayload() ([]byte, error) {
7289
return data, nil
7390
}
7491

75-
func replaceBadCharsForDiscord(in string) string {
76-
return strings.NewReplacer("[", "", "]", ":", ":", "/").Replace(in)
77-
}
78-
7992
func getDiscordCreatePayload(p *api.CreatePayload, meta *DiscordMeta) (*DiscordPayload, error) {
8093
// created tag/branch
8194
refName := git.RefEndName(p.Ref)
82-
83-
repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
84-
refLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName)
85-
86-
format := "[%s:%s] %s created by %s"
87-
format = replaceBadCharsForDiscord(format)
88-
text := fmt.Sprintf(format, repoLink, refLink, p.RefType, p.Sender.UserName)
89-
90-
var username = meta.Username
91-
if username == "" {
92-
username = "Gitea"
93-
}
95+
title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
9496

9597
return &DiscordPayload{
96-
Content: text,
97-
Username: username,
98+
Username: meta.Username,
9899
AvatarURL: meta.IconURL,
100+
Embeds: []DiscordEmbed{
101+
{
102+
Title: title,
103+
URL: p.Repo.HTMLURL + "/src/" + refName,
104+
Color: successColor,
105+
Author: DiscordEmbedAuthor{
106+
Name: p.Sender.UserName,
107+
URL: setting.AppURL + p.Sender.UserName,
108+
IconURL: p.Sender.AvatarURL,
109+
},
110+
},
111+
},
99112
}, nil
100113
}
101114

102115
func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPayload, error) {
103-
// n new commits
104116
var (
105-
branchName = git.RefEndName(p.Ref)
106-
commitDesc string
107-
commitString string
117+
branchName = git.RefEndName(p.Ref)
118+
commitDesc string
108119
)
109120

121+
var titleLink string
110122
if len(p.Commits) == 1 {
111123
commitDesc = "1 new commit"
124+
titleLink = p.Commits[0].URL
112125
} else {
113126
commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
127+
titleLink = p.CompareURL
114128
}
115-
if len(p.CompareURL) > 0 {
116-
commitString = SlackLinkFormatter(p.CompareURL, commitDesc)
117-
} else {
118-
commitString = commitDesc
129+
if titleLink == "" {
130+
titleLink = p.Repo.HTMLURL + "/src/" + branchName
119131
}
120132

121-
repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
122-
branchLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+branchName, branchName)
123-
124-
format := "[%s:%s] %s pushed by %s"
125-
format = replaceBadCharsForDiscord(format)
126-
text := fmt.Sprintf(format, repoLink, branchLink, commitString, p.Pusher.UserName)
133+
title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc)
127134

128-
var attachmentText string
135+
var text string
129136
// for each commit, generate attachment text
130137
for i, commit := range p.Commits {
131-
attachmentText += fmt.Sprintf("%s: %s - %s", SlackLinkFormatter(commit.URL, commit.ID[:7]), SlackShortTextFormatter(commit.Message), SlackTextFormatter(commit.Author.Name))
138+
text += fmt.Sprintf("[%s](%s) %s - %s", commit.ID[:7], commit.URL,
139+
strings.TrimRight(commit.Message, "\r\n"), commit.Author.Name)
132140
// add linebreak to each commit but the last
133141
if i < len(p.Commits)-1 {
134-
attachmentText += "\n"
142+
text += "\n"
135143
}
136144
}
137145

138-
var username = meta.Username
139-
if username == "" {
140-
username = "Gitea"
141-
}
146+
fmt.Println(text)
142147

143148
return &DiscordPayload{
144-
//Content: text,
145-
Username: username,
149+
Username: meta.Username,
146150
AvatarURL: meta.IconURL,
147151
Embeds: []DiscordEmbed{
148152
{
149-
Title: text,
150-
Description: attachmentText,
151-
//URL: branchLink, // FIXME
152-
Color: meta.Color,
153+
Title: title,
154+
Description: text,
155+
URL: titleLink,
156+
Color: successColor,
153157
Author: DiscordEmbedAuthor{
154158
Name: p.Sender.UserName,
155159
URL: setting.AppURL + p.Sender.UserName,
@@ -161,55 +165,62 @@ func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPaylo
161165
}
162166

163167
func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta) (*DiscordPayload, error) {
164-
senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
165-
titleLink := SlackLinkFormatter(fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index),
166-
fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
167168
var text, title string
169+
var color int
168170
switch p.Action {
169171
case api.HookIssueOpened:
170-
text = fmt.Sprintf("[%s] Pull request submitted by %s", p.Repository.FullName, senderLink)
171-
title = titleLink
172-
//attachmentText = SlackTextFormatter(p.PullRequest.Body)
172+
title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
173+
text = p.PullRequest.Body
174+
color = warnColor
173175
case api.HookIssueClosed:
174176
if p.PullRequest.HasMerged {
175-
text = fmt.Sprintf("[%s] Pull request merged: %s by %s", p.Repository.FullName, titleLink, senderLink)
177+
title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
178+
color = successColor
176179
} else {
177-
text = fmt.Sprintf("[%s] Pull request closed: %s by %s", p.Repository.FullName, titleLink, senderLink)
180+
title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
181+
color = failedColor
178182
}
183+
text = p.PullRequest.Body
179184
case api.HookIssueReOpened:
180-
text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", p.Repository.FullName, titleLink, senderLink)
185+
title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
186+
text = p.PullRequest.Body
187+
color = warnColor
181188
case api.HookIssueEdited:
182-
text = fmt.Sprintf("[%s] Pull request edited: %s by %s", p.Repository.FullName, titleLink, senderLink)
183-
//attachmentText = SlackTextFormatter(p.PullRequest.Body)
189+
title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
190+
text = p.PullRequest.Body
191+
color = warnColor
184192
case api.HookIssueAssigned:
185-
text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", p.Repository.FullName,
186-
SlackLinkFormatter(setting.AppURL+p.PullRequest.Assignee.UserName, p.PullRequest.Assignee.UserName),
187-
titleLink, senderLink)
193+
title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName,
194+
p.PullRequest.Assignee.UserName, p.Index, p.PullRequest.Title)
195+
text = p.PullRequest.Body
196+
color = successColor
188197
case api.HookIssueUnassigned:
189-
text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", p.Repository.FullName, titleLink, senderLink)
198+
title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
199+
text = p.PullRequest.Body
200+
color = warnColor
190201
case api.HookIssueLabelUpdated:
191-
text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", p.Repository.FullName, titleLink, senderLink)
202+
title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
203+
text = p.PullRequest.Body
204+
color = warnColor
192205
case api.HookIssueLabelCleared:
193-
text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", p.Repository.FullName, titleLink, senderLink)
206+
title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
207+
text = p.PullRequest.Body
208+
color = warnColor
194209
case api.HookIssueSynchronized:
195-
text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", p.Repository.FullName, titleLink, senderLink)
196-
}
197-
198-
var username = meta.Username
199-
if username == "" {
200-
username = "Gitea"
210+
title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title)
211+
text = p.PullRequest.Body
212+
color = warnColor
201213
}
202214

203215
return &DiscordPayload{
204-
//Content: text,
205-
Username: username,
216+
Username: meta.Username,
206217
AvatarURL: meta.IconURL,
207218
Embeds: []DiscordEmbed{
208219
{
209220
Title: title,
210221
Description: text,
211222
URL: p.PullRequest.HTMLURL,
212-
Color: meta.Color,
223+
Color: color,
213224
Author: DiscordEmbedAuthor{
214225
Name: p.Sender.UserName,
215226
URL: setting.AppURL + p.Sender.UserName,

modules/auth/repo_form.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ type NewDiscordHookForm struct {
188188
PayloadURL string `binding:"Required;ValidUrl"`
189189
Username string
190190
IconURL string
191-
Color int
192191
WebhookForm
193192
}
194193

routers/repo/webhook.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,18 @@ func WebhooksNew(ctx *context.Context) {
9696
return
9797
}
9898

99-
ctx.Data["HookType"] = checkHookType(ctx)
99+
hookType := checkHookType(ctx)
100+
ctx.Data["HookType"] = hookType
100101
if ctx.Written() {
101102
return
102103
}
104+
if hookType == "discord" {
105+
ctx.Data["DiscordHook"] = map[string]interface{}{
106+
"Username": "Gitea",
107+
"IconURL": setting.AppURL + "img/favicon.png",
108+
"Color": 16724530,
109+
}
110+
}
103111
ctx.Data["BaseLink"] = orCtx.Link
104112

105113
ctx.HTML(200, orCtx.NewTemplate)
@@ -234,7 +242,6 @@ func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
234242
meta, err := json.Marshal(&models.DiscordMeta{
235243
Username: form.Username,
236244
IconURL: form.IconURL,
237-
Color: form.Color,
238245
})
239246
if err != nil {
240247
ctx.Handle(500, "Marshal", err)
@@ -346,6 +353,7 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
346353
case models.GOGS:
347354
ctx.Data["HookType"] = "gogs"
348355
case models.DISCORD:
356+
ctx.Data["DiscordHook"] = w.GetDiscordHook()
349357
ctx.Data["HookType"] = "discord"
350358
default:
351359
ctx.Data["HookType"] = "gitea"
@@ -515,7 +523,6 @@ func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
515523
meta, err := json.Marshal(&models.DiscordMeta{
516524
Username: form.Username,
517525
IconURL: form.IconURL,
518-
Color: form.Color,
519526
})
520527
if err != nil {
521528
ctx.Handle(500, "Marshal", err)

templates/repo/settings/hook_discord.tmpl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@
88
</div>
99
<div class="field">
1010
<label for="username">{{.i18n.Tr "repo.settings.slack_username"}}</label>
11-
<input id="username" name="username" value="{{.SlackHook.Username}}" placeholder="e.g. Gitea">
11+
<input id="username" name="username" value="{{.DiscordHook.Username}}" placeholder="e.g. Gitea">
1212
</div>
1313
<div class="field">
1414
<label for="icon_url">{{.i18n.Tr "repo.settings.slack_icon_url"}}</label>
15-
<input id="icon_url" name="icon_url" value="{{.SlackHook.IconURL}}" placeholder="e.g. https://example.com/img/favicon.png">
16-
</div>
17-
<div class="field">
18-
<label for="color">{{.i18n.Tr "repo.settings.slack_color"}}</label>
19-
<input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. dd4b39">
15+
<input id="icon_url" name="icon_url" value="{{.DiscordHook.IconURL}}" placeholder="e.g. https://example.com/img/favicon.png">
2016
</div>
2117
{{template "repo/settings/hook_settings" .}}
2218
</form>

0 commit comments

Comments
 (0)