Browse Source

webhook: support color for Discord

pull/4170/head
Unknwon 8 years ago
parent
commit
8216360da8
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 2
      gogs.go
  2. 8
      models/webhook_discord.go
  3. 1
      modules/auth/repo_form.go
  4. 2
      routers/repo/webhook.go
  5. 2
      templates/.VERSION
  6. 4
      templates/repo/settings/hook_discord.tmpl

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/modules/setting"
)
const APP_VER = "0.9.163.0220"
const APP_VER = "0.9.164.0220"
func init() {
setting.AppVer = APP_VER

8
models/webhook_discord.go

@ -7,6 +7,7 @@ package models
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/gogits/git-module"
@ -32,6 +33,7 @@ type DiscordEmbedObject struct {
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
Color int `json:"color"`
Footer *DiscordEmbedFooterObject `json:"footer"`
Author *DiscordEmbedAuthorObject `json:"author"`
Fields []*DiscordEmbedFieldObject `json:"fields"`
@ -70,11 +72,13 @@ func getDiscordCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*DiscordPa
refLink := DiscordLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName)
content := fmt.Sprintf("Created new %s: %s/%s", p.RefType, repoLink, refLink)
color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
return &DiscordPayload{
Username: slack.Username,
AvatarURL: slack.IconURL,
Embeds: []*DiscordEmbedObject{{
Description: content,
Color: int(color),
Author: &DiscordEmbedAuthorObject{
Name: p.Sender.UserName,
IconURL: p.Sender.AvatarUrl,
@ -116,11 +120,13 @@ func getDiscordPushPayload(p *api.PushPayload, slack *SlackMeta) (*DiscordPayloa
}
}
color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
return &DiscordPayload{
Username: slack.Username,
AvatarURL: slack.IconURL,
Embeds: []*DiscordEmbedObject{{
Description: content,
Color: int(color),
Author: &DiscordEmbedAuthorObject{
Name: p.Sender.UserName,
IconURL: p.Sender.AvatarUrl,
@ -173,6 +179,7 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (
title = "Pull request synchronized: " + title
}
color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
return &DiscordPayload{
Username: slack.Username,
AvatarURL: slack.IconURL,
@ -180,6 +187,7 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (
Title: title,
Description: content,
URL: url,
Color: int(color),
Footer: &DiscordEmbedFooterObject{
Text: p.Repository.FullName,
},

1
modules/auth/repo_form.go

@ -177,6 +177,7 @@ type NewDiscordHookForm struct {
PayloadURL string `binding:"Required;Url"`
Username string
IconURL string
Color string
WebhookForm
}

2
routers/repo/webhook.go

@ -232,6 +232,7 @@ func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
meta, err := json.Marshal(&models.SlackMeta{
Username: form.Username,
IconURL: form.IconURL,
Color: form.Color,
})
if err != nil {
ctx.Handle(500, "Marshal", err)
@ -418,6 +419,7 @@ func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
meta, err := json.Marshal(&models.SlackMeta{
Username: form.Username,
IconURL: form.IconURL,
Color: form.Color,
})
if err != nil {
ctx.Handle(500, "Marshal", err)

2
templates/.VERSION

@ -1 +1 @@
0.9.163.0220
0.9.164.0220

4
templates/repo/settings/hook_discord.tmpl

@ -15,6 +15,10 @@
<label for="icon_url">{{.i18n.Tr "repo.settings.slack_icon_url"}}</label>
<input id="icon_url" name="icon_url" value="{{.SlackHook.IconURL}}" placeholder="e.g. https://example.com/img/favicon.png">
</div>
<div class="field">
<label for="color">{{.i18n.Tr "repo.settings.slack_color"}}</label>
<input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. #dd4b39">
</div>
{{template "repo/settings/hook_settings" .}}
</form>
{{end}}

Loading…
Cancel
Save