Browse Source

webhook: also only enable certain types (#3356)

Add new config option '[webhook] TYPES’.
pull/4170/head
Unknwon 8 years ago
parent
commit
60aca9ea18
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 2
      conf/app.ini
  2. 2
      gogs.go
  3. 11
      models/webhook.go
  4. 50
      modules/bindata/bindata.go
  5. 30
      modules/setting/setting.go
  6. 9
      routers/org/setting.go
  7. 13
      routers/repo/webhook.go
  8. 2
      templates/.VERSION
  9. 8
      templates/org/settings/webhook_new.tmpl
  10. 2
      templates/org/settings/webhooks.tmpl
  11. 0
      templates/repo/settings/webhook_delete_modal.tmpl
  12. 2
      templates/repo/settings/webhook_discord.tmpl
  13. 2
      templates/repo/settings/webhook_gogs.tmpl
  14. 0
      templates/repo/settings/webhook_history.tmpl
  15. 34
      templates/repo/settings/webhook_list.tmpl
  16. 8
      templates/repo/settings/webhook_new.tmpl
  17. 2
      templates/repo/settings/webhook_settings.tmpl
  18. 2
      templates/repo/settings/webhook_slack.tmpl
  19. 2
      templates/repo/settings/webhooks.tmpl

2
conf/app.ini

@ -181,6 +181,8 @@ ENABLE_REVERSE_PROXY_AUTO_REGISTRATION = false
ENABLE_CAPTCHA = true ENABLE_CAPTCHA = true
[webhook] [webhook]
; Types are enabled for users to use, can be "gogs", "slack", "discord"
TYPES = gogs, slack, discord
; Hook task queue length, increase if webhook shooting starts hanging ; Hook task queue length, increase if webhook shooting starts hanging
QUEUE_LENGTH = 1000 QUEUE_LENGTH = 1000
; Deliver timeout in seconds ; Deliver timeout in seconds

2
gogs.go

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

11
models/webhook.go

@ -118,7 +118,7 @@ func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
case "events": case "events":
w.HookEvent = &HookEvent{} w.HookEvent = &HookEvent{}
if err = json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { if err = json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
log.Error(3, "Unmarshal[%d]: %v", w.ID, err) log.Error(3, "Unmarshal [%d]: %v", w.ID, err)
} }
case "created_unix": case "created_unix":
w.Created = time.Unix(w.CreatedUnix, 0).Local() w.Created = time.Unix(w.CreatedUnix, 0).Local()
@ -130,7 +130,7 @@ func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
func (w *Webhook) GetSlackHook() *SlackMeta { func (w *Webhook) GetSlackHook() *SlackMeta {
s := &SlackMeta{} s := &SlackMeta{}
if err := json.Unmarshal([]byte(w.Meta), s); err != nil { if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
log.Error(4, "webhook.GetSlackHook(%d): %v", w.ID, err) log.Error(2, "GetSlackHook [%d]: %v", w.ID, err)
} }
return s return s
} }
@ -293,8 +293,9 @@ const (
) )
var hookTaskTypes = map[string]HookTaskType{ var hookTaskTypes = map[string]HookTaskType{
"gogs": GOGS, "gogs": GOGS,
"slack": SLACK, "slack": SLACK,
"discord": DISCORD,
} }
// ToHookTaskType returns HookTaskType by given name. // ToHookTaskType returns HookTaskType by given name.
@ -308,6 +309,8 @@ func (t HookTaskType) Name() string {
return "gogs" return "gogs"
case SLACK: case SLACK:
return "slack" return "slack"
case DISCORD:
return "discord"
} }
return "" return ""
} }

50
modules/bindata/bindata.go

File diff suppressed because one or more lines are too long

30
modules/setting/setting.go

@ -107,15 +107,6 @@ var (
UsePostgreSQL bool UsePostgreSQL bool
UseMSSQL bool UseMSSQL bool
// Webhook settings
Webhook struct {
QueueLength int
DeliverTimeout int
SkipTLSVerify bool
Types []string
PagingNum int
}
// Repository settings // Repository settings
Repository struct { Repository struct {
AnsiCharset string AnsiCharset string
@ -146,6 +137,15 @@ var (
RepoRootPath string RepoRootPath string
ScriptType string ScriptType string
// Webhook settings
Webhook struct {
Types []string
QueueLength int
DeliverTimeout int
SkipTLSVerify bool
PagingNum int
}
// Markdown sttings // Markdown sttings
Markdown struct { Markdown struct {
EnableHardLineBreak bool EnableHardLineBreak bool
@ -579,6 +579,8 @@ func NewContext() {
if err = Cfg.Section("http").MapTo(&HTTP); err != nil { if err = Cfg.Section("http").MapTo(&HTTP); err != nil {
log.Fatal(2, "Fail to map HTTP settings: %v", err) log.Fatal(2, "Fail to map HTTP settings: %v", err)
} else if err = Cfg.Section("webhook").MapTo(&Webhook); err != nil {
log.Fatal(2, "Fail to map Webhook settings: %v", err)
} else if err = Cfg.Section("markdown").MapTo(&Markdown); err != nil { } else if err = Cfg.Section("markdown").MapTo(&Markdown); err != nil {
log.Fatal(2, "Fail to map Markdown settings: %v", err) log.Fatal(2, "Fail to map Markdown settings: %v", err)
} else if err = Cfg.Section("admin").MapTo(&Admin); err != nil { } else if err = Cfg.Section("admin").MapTo(&Admin); err != nil {
@ -822,15 +824,6 @@ func newNotifyMailService() {
log.Info("Notify Mail Service Enabled") log.Info("Notify Mail Service Enabled")
} }
func newWebhookService() {
sec := Cfg.Section("webhook")
Webhook.QueueLength = sec.Key("QUEUE_LENGTH").MustInt(1000)
Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5)
Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool()
Webhook.Types = []string{"gogs", "slack", "discord"}
Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10)
}
func NewService() { func NewService() {
newService() newService()
} }
@ -843,5 +836,4 @@ func NewServices() {
newMailService() newMailService()
newRegisterMailService() newRegisterMailService()
newNotifyMailService() newNotifyMailService()
newWebhookService()
} }

9
routers/org/setting.go

@ -18,9 +18,9 @@ import (
) )
const ( const (
SETTINGS_OPTIONS base.TplName = "org/settings/options" SETTINGS_OPTIONS base.TplName = "org/settings/options"
SETTINGS_DELETE base.TplName = "org/settings/delete" SETTINGS_DELETE base.TplName = "org/settings/delete"
SETTINGS_HOOKS base.TplName = "org/settings/hooks" SETTINGS_WEBHOOKS base.TplName = "org/settings/webhooks"
) )
func Settings(ctx *context.Context) { func Settings(ctx *context.Context) {
@ -140,6 +140,7 @@ func Webhooks(ctx *context.Context) {
ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["BaseLink"] = ctx.Org.OrgLink ctx.Data["BaseLink"] = ctx.Org.OrgLink
ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc") ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
ctx.Data["Types"] = setting.Webhook.Types
ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID) ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID)
if err != nil { if err != nil {
@ -148,7 +149,7 @@ func Webhooks(ctx *context.Context) {
} }
ctx.Data["Webhooks"] = ws ctx.Data["Webhooks"] = ws
ctx.HTML(200, SETTINGS_HOOKS) ctx.HTML(200, SETTINGS_WEBHOOKS)
} }
func DeleteWebhook(ctx *context.Context) { func DeleteWebhook(ctx *context.Context) {

13
routers/repo/webhook.go

@ -23,9 +23,9 @@ import (
) )
const ( const (
HOOKS base.TplName = "repo/settings/hooks" WEBHOOKS base.TplName = "repo/settings/webhooks"
HOOK_NEW base.TplName = "repo/settings/hook_new" WEBHOOK_NEW base.TplName = "repo/settings/webhook_new"
ORG_HOOK_NEW base.TplName = "org/settings/hook_new" ORG_WEBHOOK_NEW base.TplName = "org/settings/webhook_new"
) )
func Webhooks(ctx *context.Context) { func Webhooks(ctx *context.Context) {
@ -33,6 +33,7 @@ func Webhooks(ctx *context.Context) {
ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["BaseLink"] = ctx.Repo.RepoLink ctx.Data["BaseLink"] = ctx.Repo.RepoLink
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://github.com/gogits/go-gogs-client/wiki/Repositories-Webhooks") ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://github.com/gogits/go-gogs-client/wiki/Repositories-Webhooks")
ctx.Data["Types"] = setting.Webhook.Types
ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID) ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
if err != nil { if err != nil {
@ -41,7 +42,7 @@ func Webhooks(ctx *context.Context) {
} }
ctx.Data["Webhooks"] = ws ctx.Data["Webhooks"] = ws
ctx.HTML(200, HOOKS) ctx.HTML(200, WEBHOOKS)
} }
type OrgRepoCtx struct { type OrgRepoCtx struct {
@ -57,7 +58,7 @@ func getOrgRepoCtx(ctx *context.Context) (*OrgRepoCtx, error) {
return &OrgRepoCtx{ return &OrgRepoCtx{
RepoID: ctx.Repo.Repository.ID, RepoID: ctx.Repo.Repository.ID,
Link: ctx.Repo.RepoLink, Link: ctx.Repo.RepoLink,
NewTemplate: HOOK_NEW, NewTemplate: WEBHOOK_NEW,
}, nil }, nil
} }
@ -65,7 +66,7 @@ func getOrgRepoCtx(ctx *context.Context) (*OrgRepoCtx, error) {
return &OrgRepoCtx{ return &OrgRepoCtx{
OrgID: ctx.Org.Organization.ID, OrgID: ctx.Org.Organization.ID,
Link: ctx.Org.OrgLink, Link: ctx.Org.OrgLink,
NewTemplate: ORG_HOOK_NEW, NewTemplate: ORG_WEBHOOK_NEW,
}, nil }, nil
} }

2
templates/.VERSION

@ -1 +1 @@
0.9.165.0220 / 0.10 RC 0.9.165.0221 / 0.10 RC

8
templates/org/settings/hook_new.tmpl → templates/org/settings/webhook_new.tmpl

@ -17,12 +17,12 @@
</div> </div>
</h4> </h4>
<div class="ui attached segment"> <div class="ui attached segment">
{{template "repo/settings/hook_gogs" .}} {{template "repo/settings/webhook_gogs" .}}
{{template "repo/settings/hook_slack" .}} {{template "repo/settings/webhook_slack" .}}
{{template "repo/settings/hook_discord" .}} {{template "repo/settings/webhook_discord" .}}
</div> </div>
{{template "repo/settings/hook_history" .}} {{template "repo/settings/webhook_history" .}}
</div> </div>
</div> </div>
</div> </div>

2
templates/org/settings/hooks.tmpl → templates/org/settings/webhooks.tmpl

@ -4,7 +4,7 @@
<div class="ui container"> <div class="ui container">
<div class="ui grid"> <div class="ui grid">
{{template "org/settings/navbar" .}} {{template "org/settings/navbar" .}}
{{template "repo/settings/hook_list" .}} {{template "repo/settings/webhook_list" .}}
</div> </div>
</div> </div>
</div> </div>

0
templates/repo/settings/hook_delete_modal.tmpl → templates/repo/settings/webhook_delete_modal.tmpl

2
templates/repo/settings/hook_discord.tmpl → templates/repo/settings/webhook_discord.tmpl

@ -19,6 +19,6 @@
<label for="color">{{.i18n.Tr "repo.settings.slack_color"}}</label> <label for="color">{{.i18n.Tr "repo.settings.slack_color"}}</label>
<input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. #dd4b39"> <input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. #dd4b39">
</div> </div>
{{template "repo/settings/hook_settings" .}} {{template "repo/settings/webhook_settings" .}}
</form> </form>
{{end}} {{end}}

2
templates/repo/settings/hook_gogs.tmpl → templates/repo/settings/webhook_gogs.tmpl

@ -23,6 +23,6 @@
<label for="secret">{{.i18n.Tr "repo.settings.secret"}}</label> <label for="secret">{{.i18n.Tr "repo.settings.secret"}}</label>
<input id="secret" name="secret" type="password" value="{{.Webhook.Secret}}" autocomplete="off"> <input id="secret" name="secret" type="password" value="{{.Webhook.Secret}}" autocomplete="off">
</div> </div>
{{template "repo/settings/hook_settings" .}} {{template "repo/settings/webhook_settings" .}}
</form> </form>
{{end}} {{end}}

0
templates/repo/settings/hook_history.tmpl → templates/repo/settings/webhook_history.tmpl

34
templates/repo/settings/hook_list.tmpl → templates/repo/settings/webhook_list.tmpl

@ -4,18 +4,26 @@
{{.i18n.Tr "repo.settings.hooks"}} {{.i18n.Tr "repo.settings.hooks"}}
<div class="ui right"> <div class="ui right">
<div class="ui types jump dropdown"> <div class="ui types jump dropdown">
<div class="ui blue tiny button">{{.i18n.Tr "repo.settings.add_webhook"}}</div> {{if .Types}}
<div class="menu"> <div class="ui blue tiny button">{{.i18n.Tr "repo.settings.add_webhook"}}</div>
<a class="item" href="{{.BaseLink}}/settings/hooks/gogs/new"> <div class="menu">
<img class="img-12" src="{{AppSubUrl}}/img/favicon.png">Gogs {{range .Types}}
</a> {{if eq . "gogs"}}
<a class="item" href="{{.BaseLink}}/settings/hooks/slack/new"> <a class="item" href="{{$.BaseLink}}/settings/hooks/gogs/new">
<img class="img-12" src="{{AppSubUrl}}/img/slack.png">Slack <img class="img-12" src="{{AppSubUrl}}/img/favicon.png">Gogs
</a> </a>
<a class="item" href="{{.BaseLink}}/settings/hooks/discord/new"> {{else if eq . "slack"}}
<img class="img-12" src="{{AppSubUrl}}/img/discord.png">Discord <a class="item" href="{{$.BaseLink}}/settings/hooks/slack/new">
</a> <img class="img-12" src="{{AppSubUrl}}/img/slack.png">Slack
</div> </a>
{{else if eq . "discord"}}
<a class="item" href="{{$.BaseLink}}/settings/hooks/discord/new">
<img class="img-12" src="{{AppSubUrl}}/img/discord.png">Discord
</a>
{{end}}
{{end}}
</div>
{{end}}
</div> </div>
</div> </div>
</h4> </h4>
@ -44,4 +52,4 @@
</div> </div>
</div> </div>
{{template "repo/settings/hook_delete_modal" .}} {{template "repo/settings/webhook_delete_modal" .}}

8
templates/repo/settings/hook_new.tmpl → templates/repo/settings/webhook_new.tmpl

@ -17,12 +17,12 @@
</div> </div>
</h4> </h4>
<div class="ui attached segment"> <div class="ui attached segment">
{{template "repo/settings/hook_gogs" .}} {{template "repo/settings/webhook_gogs" .}}
{{template "repo/settings/hook_slack" .}} {{template "repo/settings/webhook_slack" .}}
{{template "repo/settings/hook_discord" .}} {{template "repo/settings/webhook_discord" .}}
</div> </div>
{{template "repo/settings/hook_history" .}} {{template "repo/settings/webhook_history" .}}
</div> </div>
</div> </div>
</div> </div>

2
templates/repo/settings/hook_settings.tmpl → templates/repo/settings/webhook_settings.tmpl

@ -73,4 +73,4 @@
{{end}} {{end}}
</div> </div>
{{template "repo/settings/hook_delete_modal" .}} {{template "repo/settings/webhook_delete_modal" .}}

2
templates/repo/settings/hook_slack.tmpl → templates/repo/settings/webhook_slack.tmpl

@ -23,6 +23,6 @@
<label for="color">{{.i18n.Tr "repo.settings.slack_color"}}</label> <label for="color">{{.i18n.Tr "repo.settings.slack_color"}}</label>
<input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. #dd4b39, good, warning, danger"> <input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. #dd4b39, good, warning, danger">
</div> </div>
{{template "repo/settings/hook_settings" .}} {{template "repo/settings/webhook_settings" .}}
</form> </form>
{{end}} {{end}}

2
templates/repo/settings/hooks.tmpl → templates/repo/settings/webhooks.tmpl

@ -4,7 +4,7 @@
<div class="ui container"> <div class="ui container">
<div class="ui grid"> <div class="ui grid">
{{template "repo/settings/navbar" .}} {{template "repo/settings/navbar" .}}
{{template "repo/settings/hook_list" .}} {{template "repo/settings/webhook_list" .}}
</div> </div>
</div> </div>
</div> </div>
Loading…
Cancel
Save