Explorar o código

webhook: also only enable certain types (#3356)

Add new config option '[webhook] TYPES’.
Unknwon %!s(int64=8) %!d(string=hai) anos
pai
achega
60aca9ea18

+ 2 - 0
conf/app.ini

@@ -181,6 +181,8 @@ ENABLE_REVERSE_PROXY_AUTO_REGISTRATION = false
 ENABLE_CAPTCHA = true
 
 [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
 QUEUE_LENGTH = 1000
 ; Deliver timeout in seconds

+ 1 - 1
gogs.go

@@ -16,7 +16,7 @@ import (
 	"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() {
 	setting.AppVer = APP_VER

+ 7 - 4
models/webhook.go

@@ -118,7 +118,7 @@ func (w *Webhook) AfterSet(colName string, _ xorm.Cell) {
 	case "events":
 		w.HookEvent = &HookEvent{}
 		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":
 		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 {
 	s := &SlackMeta{}
 	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
 }
@@ -293,8 +293,9 @@ const (
 )
 
 var hookTaskTypes = map[string]HookTaskType{
-	"gogs":  GOGS,
-	"slack": SLACK,
+	"gogs":    GOGS,
+	"slack":   SLACK,
+	"discord": DISCORD,
 }
 
 // ToHookTaskType returns HookTaskType by given name.
@@ -308,6 +309,8 @@ func (t HookTaskType) Name() string {
 		return "gogs"
 	case SLACK:
 		return "slack"
+	case DISCORD:
+		return "discord"
 	}
 	return ""
 }

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
modules/bindata/bindata.go


+ 11 - 19
modules/setting/setting.go

@@ -107,15 +107,6 @@ var (
 	UsePostgreSQL bool
 	UseMSSQL      bool
 
-	// Webhook settings
-	Webhook struct {
-		QueueLength    int
-		DeliverTimeout int
-		SkipTLSVerify  bool
-		Types          []string
-		PagingNum      int
-	}
-
 	// Repository settings
 	Repository struct {
 		AnsiCharset              string
@@ -146,6 +137,15 @@ var (
 	RepoRootPath string
 	ScriptType   string
 
+	// Webhook settings
+	Webhook struct {
+		Types          []string
+		QueueLength    int
+		DeliverTimeout int
+		SkipTLSVerify  bool
+		PagingNum      int
+	}
+
 	// Markdown sttings
 	Markdown struct {
 		EnableHardLineBreak bool
@@ -579,6 +579,8 @@ func NewContext() {
 
 	if err = Cfg.Section("http").MapTo(&HTTP); err != nil {
 		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 {
 		log.Fatal(2, "Fail to map Markdown settings: %v", err)
 	} else if err = Cfg.Section("admin").MapTo(&Admin); err != nil {
@@ -822,15 +824,6 @@ func newNotifyMailService() {
 	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() {
 	newService()
 }
@@ -843,5 +836,4 @@ func NewServices() {
 	newMailService()
 	newRegisterMailService()
 	newNotifyMailService()
-	newWebhookService()
 }

+ 5 - 4
routers/org/setting.go

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

+ 7 - 6
routers/repo/webhook.go

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

+ 1 - 1
templates/.VERSION

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

+ 4 - 4
templates/org/settings/hook_new.tmpl → templates/org/settings/webhook_new.tmpl

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

+ 1 - 1
templates/org/settings/hooks.tmpl → templates/org/settings/webhooks.tmpl

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

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


+ 1 - 1
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>
 			<input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. #dd4b39">
 		</div>
-		{{template "repo/settings/hook_settings" .}}
+		{{template "repo/settings/webhook_settings" .}}
 	</form>
 {{end}}

+ 1 - 1
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>
 			<input id="secret" name="secret" type="password" value="{{.Webhook.Secret}}" autocomplete="off">
 		</div>
-		{{template "repo/settings/hook_settings" .}}
+		{{template "repo/settings/webhook_settings" .}}
 	</form>
 {{end}}

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


+ 21 - 13
templates/repo/settings/hook_list.tmpl → templates/repo/settings/webhook_list.tmpl

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

+ 4 - 4
templates/repo/settings/hook_new.tmpl → templates/repo/settings/webhook_new.tmpl

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

+ 1 - 1
templates/repo/settings/hook_settings.tmpl → templates/repo/settings/webhook_settings.tmpl

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

+ 1 - 1
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>
 			<input id="color" name="color" value="{{.SlackHook.Color}}" placeholder="e.g. #dd4b39, good, warning, danger">
 		</div>
-		{{template "repo/settings/hook_settings" .}}
+		{{template "repo/settings/webhook_settings" .}}
 	</form>
 {{end}}

+ 1 - 1
templates/repo/settings/hooks.tmpl → templates/repo/settings/webhooks.tmpl

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

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio