Skip to content

Commit 8216360

Browse files
committed
webhook: support color for Discord
1 parent c5b85f2 commit 8216360

File tree

6 files changed

+17
-2
lines changed

6 files changed

+17
-2
lines changed

gogs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/gogits/gogs/modules/setting"
1717
)
1818

19-
const APP_VER = "0.9.163.0220"
19+
const APP_VER = "0.9.164.0220"
2020

2121
func init() {
2222
setting.AppVer = APP_VER

models/webhook_discord.go

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package models
77
import (
88
"encoding/json"
99
"fmt"
10+
"strconv"
1011
"strings"
1112

1213
"github.com/gogits/git-module"
@@ -32,6 +33,7 @@ type DiscordEmbedObject struct {
3233
Title string `json:"title"`
3334
Description string `json:"description"`
3435
URL string `json:"url"`
36+
Color int `json:"color"`
3537
Footer *DiscordEmbedFooterObject `json:"footer"`
3638
Author *DiscordEmbedAuthorObject `json:"author"`
3739
Fields []*DiscordEmbedFieldObject `json:"fields"`
@@ -70,11 +72,13 @@ func getDiscordCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*DiscordPa
7072
refLink := DiscordLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName)
7173
content := fmt.Sprintf("Created new %s: %s/%s", p.RefType, repoLink, refLink)
7274

75+
color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
7376
return &DiscordPayload{
7477
Username: slack.Username,
7578
AvatarURL: slack.IconURL,
7679
Embeds: []*DiscordEmbedObject{{
7780
Description: content,
81+
Color: int(color),
7882
Author: &DiscordEmbedAuthorObject{
7983
Name: p.Sender.UserName,
8084
IconURL: p.Sender.AvatarUrl,
@@ -116,11 +120,13 @@ func getDiscordPushPayload(p *api.PushPayload, slack *SlackMeta) (*DiscordPayloa
116120
}
117121
}
118122

123+
color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
119124
return &DiscordPayload{
120125
Username: slack.Username,
121126
AvatarURL: slack.IconURL,
122127
Embeds: []*DiscordEmbedObject{{
123128
Description: content,
129+
Color: int(color),
124130
Author: &DiscordEmbedAuthorObject{
125131
Name: p.Sender.UserName,
126132
IconURL: p.Sender.AvatarUrl,
@@ -173,13 +179,15 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (
173179
title = "Pull request synchronized: " + title
174180
}
175181

182+
color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
176183
return &DiscordPayload{
177184
Username: slack.Username,
178185
AvatarURL: slack.IconURL,
179186
Embeds: []*DiscordEmbedObject{{
180187
Title: title,
181188
Description: content,
182189
URL: url,
190+
Color: int(color),
183191
Footer: &DiscordEmbedFooterObject{
184192
Text: p.Repository.FullName,
185193
},

modules/auth/repo_form.go

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ type NewDiscordHookForm struct {
177177
PayloadURL string `binding:"Required;Url"`
178178
Username string
179179
IconURL string
180+
Color string
180181
WebhookForm
181182
}
182183

routers/repo/webhook.go

+2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
232232
meta, err := json.Marshal(&models.SlackMeta{
233233
Username: form.Username,
234234
IconURL: form.IconURL,
235+
Color: form.Color,
235236
})
236237
if err != nil {
237238
ctx.Handle(500, "Marshal", err)
@@ -418,6 +419,7 @@ func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
418419
meta, err := json.Marshal(&models.SlackMeta{
419420
Username: form.Username,
420421
IconURL: form.IconURL,
422+
Color: form.Color,
421423
})
422424
if err != nil {
423425
ctx.Handle(500, "Marshal", err)

templates/.VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.9.163.0220
1+
0.9.164.0220

templates/repo/settings/hook_discord.tmpl

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<label for="icon_url">{{.i18n.Tr "repo.settings.slack_icon_url"}}</label>
1616
<input id="icon_url" name="icon_url" value="{{.SlackHook.IconURL}}" placeholder="e.g. https://example.com/img/favicon.png">
1717
</div>
18+
<div class="field">
19+
<label for="color">{{.i18n.Tr "repo.settings.slack_color"}}</label>
20+
<input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. #dd4b39">
21+
</div>
1822
{{template "repo/settings/hook_settings" .}}
1923
</form>
2024
{{end}}

0 commit comments

Comments
 (0)