From 108af37cda1313a84aa329916a7d90996ab59459 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sun, 13 Feb 2022 03:03:58 -0500 Subject: [PATCH 01/10] Custom webhook stub --- models/webhook/hooktask.go | 3 + models/webhook/webhook.go | 1 + modules/setting/webhook.go | 2 +- options/locale/locale_en-US.ini | 2 + routers/web/repo/webhook.go | 71 +++++++++++++++++++ routers/web/web.go | 6 ++ services/forms/repo_form.go | 6 ++ services/webhook/custom.go | 35 +++++++++ services/webhook/custom_test.go | 51 +++++++++++++ services/webhook/deliver.go | 3 + services/webhook/webhook.go | 20 ++++-- templates/admin/hook_new.tmpl | 3 + templates/org/settings/hook_new.tmpl | 3 + .../repo/settings/webhook/base_list.tmpl | 3 + templates/repo/settings/webhook/custom.tmpl | 40 +++++++++++ templates/repo/settings/webhook/new.tmpl | 3 + templates/swagger/v1_json.tmpl | 3 +- 17 files changed, 249 insertions(+), 6 deletions(-) create mode 100644 services/webhook/custom.go create mode 100644 services/webhook/custom_test.go create mode 100644 templates/repo/settings/webhook/custom.tmpl diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go index 1d19ebd24e76b..8a9a3ffd1d6aa 100644 --- a/models/webhook/hooktask.go +++ b/models/webhook/hooktask.go @@ -116,6 +116,9 @@ type HookTask struct { RequestInfo *HookRequest `xorm:"-"` ResponseContent string `xorm:"TEXT"` ResponseInfo *HookResponse `xorm:"-"` + + // Used for Auth Headers. + BearerToken string } func init() { diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index ffc9b72b64d88..fba4a566e8510 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -162,6 +162,7 @@ const ( MATRIX HookType = "matrix" WECHATWORK HookType = "wechatwork" PACKAGIST HookType = "packagist" + CUSTOM HookType = "custom" ) // HookStatus is the status of a web hook diff --git a/modules/setting/webhook.go b/modules/setting/webhook.go index 0bfd7dcb4dd3f..8974f92e3876d 100644 --- a/modules/setting/webhook.go +++ b/modules/setting/webhook.go @@ -36,7 +36,7 @@ func newWebhookService() { Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5) Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool() Webhook.AllowedHostList = sec.Key("ALLOWED_HOST_LIST").MustString("") - Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams", "feishu", "matrix", "wechatwork", "packagist"} + Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams", "feishu", "matrix", "wechatwork", "packagist", "custom"} Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10) Webhook.ProxyURL = sec.Key("PROXY_URL").MustString("") if Webhook.ProxyURL != "" { diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index f52fef3c0590b..b888dd0a8f81b 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1957,6 +1957,7 @@ settings.hook_type = Hook Type settings.slack_token = Token settings.slack_domain = Domain settings.slack_channel = Channel +settings.custom_host_url = Host URL settings.add_web_hook_desc = Integrate %s into your repository. settings.web_hook_name_gitea = Gitea settings.web_hook_name_gogs = Gogs @@ -1971,6 +1972,7 @@ settings.web_hook_name_feishu = Feishu settings.web_hook_name_larksuite = Lark Suite settings.web_hook_name_wechatwork = WeCom (Wechat Work) settings.web_hook_name_packagist = Packagist +settings.web_hook_name_custom = Custom settings.packagist_username = Packagist username settings.packagist_api_token = API token settings.packagist_package_url = Packagist package URL diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index fb984de7f5857..4eef3ed5cbf72 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -774,6 +774,8 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) { ctx.Data["MatrixHook"] = webhook_service.GetMatrixHook(w) case webhook.PACKAGIST: ctx.Data["PackagistHook"] = webhook_service.GetPackagistHook(w) + case webhook.CUSTOM: + ctx.Data["CustomHook"] = webhook_service.GetCustomHook(w) } ctx.Data["History"], err = w.History(1) @@ -1236,6 +1238,75 @@ func PackagistHooksEditPost(ctx *context.Context) { ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } +// CustomHooksEditPost response for editing custom hook +func CustomHookEditPost(ctx *context.Context) { + form := web.GetForm(ctx).(*forms.NewCustomHookForm) + ctx.Data["Title"] = ctx.Tr("repo.settings") + ctx.Data["PageIsSettingHooks"] = true + ctx.Data["PageIsSettingHooksNew"] = true + ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}} + ctx.Data["HookType"] = webhook.CUSTOM + + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.ServerError("getOrgRepoCtx", err) + } + ctx.Data["BaseLink"] = orCtx.Link + + if ctx.HasError() { + ctx.HTML(200, orCtx.NewTemplate) + return + } + + meta, err := json.Marshal(&webhook_service.CustomMeta{ + HostURL: form.HostURL, + AuthToken: form.AuthToken, + }) + if err != nil { + ctx.ServerError("Marshal", err) + return + } + + payloadURL, err := buildCustomURL(form) + if err != nil { + ctx.ServerError("buildCustomURL", err) + return + } + + w := &webhook.Webhook{ + RepoID: orCtx.RepoID, + URL: payloadURL, + ContentType: webhook.ContentTypeForm, + HookEvent: ParseHookEvent(form.WebhookForm), + IsActive: form.Active, + Type: webhook.CUSTOM, + HTTPMethod: http.MethodPost, + Meta: string(meta), + OrgID: orCtx.OrgID, + IsSystemWebhook: orCtx.IsSystemWebhook, + } + if err := w.UpdateEvent(); err != nil { + ctx.ServerError("UpdateEvent", err) + return + } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil { + ctx.ServerError("CreateWebhook", err) + return + } + + ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) + ctx.Redirect(orCtx.Link) +} + +// buildCustomURL returns the correct REST API url for a Custom POST request. +func buildCustomURL(meta *forms.NewCustomHookForm) (string, error) { + tcURL, err := url.Parse(meta.HostURL) + if err != nil { + return "", err + } + + return fmt.Sprintf("%s", tcURL), nil +} + // TestWebhook test if web hook is work fine func TestWebhook(ctx *context.Context) { hookID := ctx.ParamsInt64(":id") diff --git a/routers/web/web.go b/routers/web/web.go index d8c197fb967e2..c7139718a2309 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -457,6 +457,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/feishu/{id}", bindIgnErr(forms.NewFeishuHookForm{}), repo.FeishuHooksEditPost) m.Post("/wechatwork/{id}", bindIgnErr(forms.NewWechatWorkHookForm{}), repo.WechatworkHooksEditPost) m.Post("/packagist/{id}", bindIgnErr(forms.NewPackagistHookForm{}), repo.PackagistHooksEditPost) + m.Post("/custom/{id}", bindIgnErr(forms.NewCustomHookForm{}), repo.CustomHookEditPost) }, webhooksEnabled) m.Group("/{configType:default-hooks|system-hooks}", func() { @@ -472,6 +473,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/feishu/new", bindIgnErr(forms.NewFeishuHookForm{}), repo.FeishuHooksNewPost) m.Post("/wechatwork/new", bindIgnErr(forms.NewWechatWorkHookForm{}), repo.WechatworkHooksNewPost) m.Post("/packagist/new", bindIgnErr(forms.NewPackagistHookForm{}), repo.PackagistHooksNewPost) + m.Post("/custom/{id}", bindIgnErr(forms.NewCustomHookForm{}), repo.CustomHooksNewPost) }) m.Group("/auths", func() { @@ -570,6 +572,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/msteams/new", bindIgnErr(forms.NewMSTeamsHookForm{}), repo.MSTeamsHooksNewPost) m.Post("/feishu/new", bindIgnErr(forms.NewFeishuHookForm{}), repo.FeishuHooksNewPost) m.Post("/wechatwork/new", bindIgnErr(forms.NewWechatWorkHookForm{}), repo.WechatworkHooksNewPost) + m.Post("/custom/new", bindIgnErr(forms.NewCustomHookForm{}), repo.CustomHooksNewPost) m.Group("/{id}", func() { m.Get("", repo.WebHooksEdit) m.Post("/replay/{uuid}", repo.ReplayWebhook) @@ -584,6 +587,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/msteams/{id}", bindIgnErr(forms.NewMSTeamsHookForm{}), repo.MSTeamsHooksEditPost) m.Post("/feishu/{id}", bindIgnErr(forms.NewFeishuHookForm{}), repo.FeishuHooksEditPost) m.Post("/wechatwork/{id}", bindIgnErr(forms.NewWechatWorkHookForm{}), repo.WechatworkHooksEditPost) + m.Post("/custom/{id}", bindIgnErr(forms.NewCustomHookForm{}), repo.CustomHookEditPost) }, webhooksEnabled) m.Group("/labels", func() { @@ -668,6 +672,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/feishu/new", bindIgnErr(forms.NewFeishuHookForm{}), repo.FeishuHooksNewPost) m.Post("/wechatwork/new", bindIgnErr(forms.NewWechatWorkHookForm{}), repo.WechatworkHooksNewPost) m.Post("/packagist/new", bindIgnErr(forms.NewPackagistHookForm{}), repo.PackagistHooksNewPost) + m.Post("/custom/new", bindIgnErr(forms.NewCustomHookForm{}), repo.) m.Group("/{id}", func() { m.Get("", repo.WebHooksEdit) m.Post("/test", repo.TestWebhook) @@ -684,6 +689,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/feishu/{id}", bindIgnErr(forms.NewFeishuHookForm{}), repo.FeishuHooksEditPost) m.Post("/wechatwork/{id}", bindIgnErr(forms.NewWechatWorkHookForm{}), repo.WechatworkHooksEditPost) m.Post("/packagist/{id}", bindIgnErr(forms.NewPackagistHookForm{}), repo.PackagistHooksEditPost) + m.Post("/custom/{id}", bindIgnErr(forms.NewCustomHookForm{}), repo.CustomHookEditPost) }, webhooksEnabled) m.Group("/keys", func() { diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index da709ef800240..9739f31925a73 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -404,6 +404,12 @@ type NewPackagistHookForm struct { WebhookForm } +type NewCustomHookForm struct { + HostURL string `binding:"Required;ValidUrl"` + AuthToken string + WebhookForm +} + // Validate validates the fields func (f *NewPackagistHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors { ctx := context.GetContext(req) diff --git a/services/webhook/custom.go b/services/webhook/custom.go new file mode 100644 index 0000000000000..3147e12110c60 --- /dev/null +++ b/services/webhook/custom.go @@ -0,0 +1,35 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file.package webhook + +package webhook + +import ( + webhook_model "code.gitea.io/gitea/models/webhook" + "code.gitea.io/gitea/modules/json" + "code.gitea.io/gitea/modules/log" + api "code.gitea.io/gitea/modules/structs" +) + +type ( + // Custom contains metadata for the Custom WebHook + CustomMeta struct { + HostURL string `json:"host_url"` + AuthToken string `json:"auth_token,omitempty"` + } +) + +// GetCustomPayload returns the payload as-is +func GetCustomPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) { + // TODO: add optional body on POST. + return p, nil +} + +// GetCustomHook returns Custom metadata +func GetCustomHook(w *webhook_model.Webhook) *CustomMeta { + s := &CustomMeta{} + if err := json.Unmarshal([]byte(w.Meta), s); err != nil { + log.Error("webhook.GetCustomHook(%d): %v", w.ID, err) + } + return s +} diff --git a/services/webhook/custom_test.go b/services/webhook/custom_test.go new file mode 100644 index 0000000000000..c3562ea26e653 --- /dev/null +++ b/services/webhook/custom_test.go @@ -0,0 +1,51 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file.package webhook + +package webhook + +import ( + "testing" + + webhook_model "code.gitea.io/gitea/models/webhook" + + _ "github.com/mattn/go-sqlite3" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetCustomPayload(t *testing.T) { + t.Run("Payload isn't altered.", func(t *testing.T) { + p := createTestPayload() + + pl, err := GetCustomPayload(p, webhook_model.HookEventPush, "") + require.NoError(t, err) + require.Equal(t, p, pl) + }) +} + +func TestWebhook_GetCustomHook(t *testing.T) { + // Run with bearer token + t.Run("GetCustomHook", func(t *testing.T) { + w := &webhook_model.Webhook{ + Meta: `{"host_url": "http://localhost.com", "auth_token": "testToken"}`, + } + + customHook := GetCustomHook(w) + assert.Equal(t, *customHook, CustomMeta{ + HostURL: "http://localhost.com", + AuthToken: "testToken", + }) + }) + // Run without bearer token + t.Run("GetCustomHook", func(t *testing.T) { + w := &webhook_model.Webhook{ + Meta: `{"host_url": "http://localhost.com"}`, + } + + customHook := GetCustomHook(w) + assert.Equal(t, *customHook, CustomMeta{ + HostURL: "http://localhost.com", + }) + }) +} diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index 88b709cb41e74..4f95105468cb5 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -116,6 +116,9 @@ func Deliver(t *webhook_model.HookTask) error { event := t.EventType.Event() eventType := string(t.EventType) + if t.BearerToken != "" { + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", t.BearerToken)) + } req.Header.Add("X-Gitea-Delivery", t.UUID) req.Header.Add("X-Gitea-Event", event) req.Header.Add("X-Gitea-Event-Type", eventType) diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index 607fac963452f..bcd13d3a911cc 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -62,6 +62,10 @@ var webhooks = map[webhook_model.HookType]*webhook{ name: webhook_model.PACKAGIST, payloadCreator: GetPackagistPayload, }, + webhook_model.CUSTOM: { + name: webhook_model.CUSTOM, + payloadCreator: GetCustomPayload, + }, } // RegisterWebhook registers a webhook @@ -170,11 +174,19 @@ func prepareWebhook(w *webhook_model.Webhook, repo *repo_model.Repository, event payloader = p } + // Load bearer token + var authToken string + switch w.Type { + case webhook_model.CUSTOM: + authToken = GetCustomHook(w).AuthToken + } + if err = webhook_model.CreateHookTask(&webhook_model.HookTask{ - RepoID: repo.ID, - HookID: w.ID, - Payloader: payloader, - EventType: event, + RepoID: repo.ID, + HookID: w.ID, + Payloader: payloader, + EventType: event, + BearerToken: authToken, }); err != nil { return fmt.Errorf("CreateHookTask: %v", err) } diff --git a/templates/admin/hook_new.tmpl b/templates/admin/hook_new.tmpl index 049e54ef833cf..a1cf44543f9d2 100644 --- a/templates/admin/hook_new.tmpl +++ b/templates/admin/hook_new.tmpl @@ -36,6 +36,8 @@ {{else if eq .HookType "packagist"}} + {{else if eq .HookType "custom"}} + {{end}} @@ -51,6 +53,7 @@ {{template "repo/settings/webhook/matrix" .}} {{template "repo/settings/webhook/wechatwork" .}} {{template "repo/settings/webhook/packagist" .}} + {{template "repo/settings/webhook/custom" .}} {{template "repo/settings/webhook/history" .}} diff --git a/templates/org/settings/hook_new.tmpl b/templates/org/settings/hook_new.tmpl index 5e8ebb51e9427..469d94828efcd 100644 --- a/templates/org/settings/hook_new.tmpl +++ b/templates/org/settings/hook_new.tmpl @@ -31,6 +31,8 @@ {{else if eq .HookType "packagist"}} + {{else if eq .HookType "custom"}} + {{end}} @@ -46,6 +48,7 @@ {{template "repo/settings/webhook/matrix" .}} {{template "repo/settings/webhook/wechatwork" .}} {{template "repo/settings/webhook/packagist" .}} + {{template "repo/settings/webhook/custom" .}} {{template "repo/settings/webhook/history" .}} diff --git a/templates/repo/settings/webhook/base_list.tmpl b/templates/repo/settings/webhook/base_list.tmpl index b1a3771bdba11..8e036416f4839 100644 --- a/templates/repo/settings/webhook/base_list.tmpl +++ b/templates/repo/settings/webhook/base_list.tmpl @@ -37,6 +37,9 @@ {{.i18n.Tr "repo.settings.web_hook_name_packagist"}} + + {{.i18n.Tr "repo.settings.web_hook_name_custom"}} + diff --git a/templates/repo/settings/webhook/custom.tmpl b/templates/repo/settings/webhook/custom.tmpl new file mode 100644 index 0000000000000..6cd5ad8e4f34e --- /dev/null +++ b/templates/repo/settings/webhook/custom.tmpl @@ -0,0 +1,40 @@ +{{if eq .HookType "gitea"}} +

{{.i18n.Tr "repo.settings.add_web_hook_desc" "https://docs.gitea.io/en-us/webhooks/" (.i18n.Tr "repo.settings.web_hook_name_gitea") | Str2html}}

+
+ {{template "base/disable_form_autofill"}} + {{.CsrfTokenHtml}} +
+ + +
+ + +
+ + +
+ {{template "repo/settings/webhook/settings" .}} +
+{{end}} diff --git a/templates/repo/settings/webhook/new.tmpl b/templates/repo/settings/webhook/new.tmpl index a438a4c71a3d7..7bb3d35c6e22f 100644 --- a/templates/repo/settings/webhook/new.tmpl +++ b/templates/repo/settings/webhook/new.tmpl @@ -29,6 +29,8 @@ {{else if eq .HookType "packagist"}} + {{else if eq .HookType "custom"}} + {{end}} @@ -44,6 +46,7 @@ {{template "repo/settings/webhook/matrix" .}} {{template "repo/settings/webhook/wechatwork" .}} {{template "repo/settings/webhook/packagist" .}} + {{template "repo/settings/webhook/custom" .}} {{template "repo/settings/webhook/history" .}} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 0b0b83ebbc16f..4844b4284daee 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -13562,7 +13562,8 @@ "telegram", "feishu", "wechatwork", - "packagist" + "packagist", + "custom" ], "x-go-name": "Type" } From 92ca748b14ddfc90150f4afe3089a60bb86207a0 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sun, 13 Feb 2022 03:34:47 -0500 Subject: [PATCH 02/10] fix --- routers/web/repo/webhook.go | 59 +++++++++++++++++++++++++++++++++++++ routers/web/web.go | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index 4eef3ed5cbf72..f0a05d7fce4d7 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -735,6 +735,65 @@ func PackagistHooksNewPost(ctx *context.Context) { ctx.Redirect(orCtx.Link) } +// CustomHooksNewPost response for creating Custom hook +func CustomHooksNewPost(ctx *context.Context) { + form := web.GetForm(ctx).(*forms.NewCustomHookForm) + ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook") + ctx.Data["PageIsSettingHooks"] = true + ctx.Data["PageIsSettingHooksNew"] = true + ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}} + ctx.Data["HookType"] = webhook.CUSTOM + + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.ServerError("getOrgRepoCtx", err) + } + ctx.Data["BaseLink"] = orCtx.Link + + if ctx.HasError() { + ctx.HTML(200, orCtx.NewTemplate) + return + } + + meta, err := json.Marshal(&webhook_service.CustomMeta{ + HostURL: form.HostURL, + AuthToken: form.AuthToken, + }) + if err != nil { + ctx.ServerError("Marshal", err) + return + } + + payloadURL, err := buildCustomURL(form) + if err != nil { + ctx.ServerError("buildCustomURL", err) + return + } + + w := &webhook.Webhook{ + RepoID: orCtx.RepoID, + URL: payloadURL, + ContentType: webhook.ContentTypeForm, + HookEvent: ParseHookEvent(form.WebhookForm), + IsActive: form.Active, + Type: webhook.CUSTOM, + HTTPMethod: http.MethodPost, + Meta: string(meta), + OrgID: orCtx.OrgID, + IsSystemWebhook: orCtx.IsSystemWebhook, + } + if err := w.UpdateEvent(); err != nil { + ctx.ServerError("UpdateEvent", err) + return + } else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil { + ctx.ServerError("CreateWebhook", err) + return + } + + ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) + ctx.Redirect(orCtx.Link) +} + func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) { ctx.Data["RequireHighlightJS"] = true diff --git a/routers/web/web.go b/routers/web/web.go index c7139718a2309..909fe23f66df5 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -672,7 +672,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/feishu/new", bindIgnErr(forms.NewFeishuHookForm{}), repo.FeishuHooksNewPost) m.Post("/wechatwork/new", bindIgnErr(forms.NewWechatWorkHookForm{}), repo.WechatworkHooksNewPost) m.Post("/packagist/new", bindIgnErr(forms.NewPackagistHookForm{}), repo.PackagistHooksNewPost) - m.Post("/custom/new", bindIgnErr(forms.NewCustomHookForm{}), repo.) + m.Post("/custom/new", bindIgnErr(forms.NewCustomHookForm{}), repo.CustomHooksNewPost) m.Group("/{id}", func() { m.Get("", repo.WebHooksEdit) m.Post("/test", repo.TestWebhook) From da69a0c8c9eb54a6bdfcb874907794e1fc36fe51 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sun, 13 Feb 2022 10:01:44 -0500 Subject: [PATCH 03/10] Update webhook.go --- routers/web/repo/webhook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index f0a05d7fce4d7..a2e0649103224 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -1363,7 +1363,7 @@ func buildCustomURL(meta *forms.NewCustomHookForm) (string, error) { return "", err } - return fmt.Sprintf("%s", tcURL), nil + return tcURL.String(), nil } // TestWebhook test if web hook is work fine From 33482321609d6bd77d188dc7649420de6da0eca8 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sun, 13 Feb 2022 10:13:36 -0500 Subject: [PATCH 04/10] Update custom.tmpl --- templates/repo/settings/webhook/custom.tmpl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/repo/settings/webhook/custom.tmpl b/templates/repo/settings/webhook/custom.tmpl index 6cd5ad8e4f34e..996594f754492 100644 --- a/templates/repo/settings/webhook/custom.tmpl +++ b/templates/repo/settings/webhook/custom.tmpl @@ -31,10 +31,10 @@ --> -
- - -
+
+ + +
{{template "repo/settings/webhook/settings" .}} {{end}} From 15cd15bb2f77599ef9a840033fdd99a2e70bac71 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 13 Feb 2022 15:13:06 +0000 Subject: [PATCH 05/10] Fix fmt Signed-off-by: Andrew Thornton --- services/webhook/custom_test.go | 3 ++- templates/repo/settings/webhook/custom.tmpl | 24 --------------------- templates/swagger/v1_json.tmpl | 3 +-- 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/services/webhook/custom_test.go b/services/webhook/custom_test.go index c3562ea26e653..b81331900245c 100644 --- a/services/webhook/custom_test.go +++ b/services/webhook/custom_test.go @@ -9,9 +9,10 @@ import ( webhook_model "code.gitea.io/gitea/models/webhook" - _ "github.com/mattn/go-sqlite3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + _ "github.com/mattn/go-sqlite3" ) func TestGetCustomPayload(t *testing.T) { diff --git a/templates/repo/settings/webhook/custom.tmpl b/templates/repo/settings/webhook/custom.tmpl index 996594f754492..24f4ada6a31df 100644 --- a/templates/repo/settings/webhook/custom.tmpl +++ b/templates/repo/settings/webhook/custom.tmpl @@ -7,30 +7,6 @@ - -
diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 4844b4284daee..0b0b83ebbc16f 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -13562,8 +13562,7 @@ "telegram", "feishu", "wechatwork", - "packagist", - "custom" + "packagist" ], "x-go-name": "Type" } From a4ef755c22bad03a3992c27228f7cd8fac106027 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sat, 19 Feb 2022 07:57:26 -0500 Subject: [PATCH 06/10] fix buttons --- templates/repo/settings/webhook/base_list.tmpl | 2 +- templates/repo/settings/webhook/custom.tmpl | 6 +++--- templates/repo/settings/webhook/new.tmpl | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/repo/settings/webhook/base_list.tmpl b/templates/repo/settings/webhook/base_list.tmpl index 8e036416f4839..4770cf2080b90 100644 --- a/templates/repo/settings/webhook/base_list.tmpl +++ b/templates/repo/settings/webhook/base_list.tmpl @@ -38,7 +38,7 @@ {{.i18n.Tr "repo.settings.web_hook_name_packagist"}} - {{.i18n.Tr "repo.settings.web_hook_name_custom"}} + {{.i18n.Tr "repo.settings.web_hook_name_custom"}}
diff --git a/templates/repo/settings/webhook/custom.tmpl b/templates/repo/settings/webhook/custom.tmpl index 24f4ada6a31df..c01263910d84a 100644 --- a/templates/repo/settings/webhook/custom.tmpl +++ b/templates/repo/settings/webhook/custom.tmpl @@ -1,6 +1,6 @@ -{{if eq .HookType "gitea"}} -

{{.i18n.Tr "repo.settings.add_web_hook_desc" "https://docs.gitea.io/en-us/webhooks/" (.i18n.Tr "repo.settings.web_hook_name_gitea") | Str2html}}

-
+{{if eq .HookType "custom"}} +

{{.i18n.Tr "repo.settings.add_web_hook_desc" "https://docs.gitea.io/en-us/webhooks/" (.i18n.Tr "repo.settings.web_hook_name_custom") | Str2html}}

+ {{template "base/disable_form_autofill"}} {{.CsrfTokenHtml}}
diff --git a/templates/repo/settings/webhook/new.tmpl b/templates/repo/settings/webhook/new.tmpl index 7bb3d35c6e22f..1d3242bc67d46 100644 --- a/templates/repo/settings/webhook/new.tmpl +++ b/templates/repo/settings/webhook/new.tmpl @@ -30,7 +30,7 @@ {{else if eq .HookType "packagist"}} {{else if eq .HookType "custom"}} - + {{end}}
From 2e1c04b399176b21028b7521743b3fa7ebc409c8 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sat, 19 Feb 2022 08:00:03 -0500 Subject: [PATCH 07/10] dates --- services/webhook/custom.go | 2 +- services/webhook/custom_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/webhook/custom.go b/services/webhook/custom.go index 3147e12110c60..9d525bf1898d2 100644 --- a/services/webhook/custom.go +++ b/services/webhook/custom.go @@ -1,4 +1,4 @@ -// Copyright 2019 The Gitea Authors. All rights reserved. +// Copyright 2022 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file.package webhook diff --git a/services/webhook/custom_test.go b/services/webhook/custom_test.go index b81331900245c..6af6b5d7edbf5 100644 --- a/services/webhook/custom_test.go +++ b/services/webhook/custom_test.go @@ -1,4 +1,4 @@ -// Copyright 2019 The Gitea Authors. All rights reserved. +// Copyright 2022 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file.package webhook From fdbb25d48e5cbd2e5bd74fdf93ae8c0346668910 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sat, 19 Feb 2022 08:05:24 -0500 Subject: [PATCH 08/10] Create recognizable custom svg --- public/img/gitea_custom.svg | 1 + templates/repo/settings/webhook/base_list.tmpl | 2 +- templates/repo/settings/webhook/new.tmpl | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 public/img/gitea_custom.svg diff --git a/public/img/gitea_custom.svg b/public/img/gitea_custom.svg new file mode 100644 index 0000000000000..e37776b506631 --- /dev/null +++ b/public/img/gitea_custom.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/templates/repo/settings/webhook/base_list.tmpl b/templates/repo/settings/webhook/base_list.tmpl index 4770cf2080b90..22b870b15bb04 100644 --- a/templates/repo/settings/webhook/base_list.tmpl +++ b/templates/repo/settings/webhook/base_list.tmpl @@ -38,7 +38,7 @@ {{.i18n.Tr "repo.settings.web_hook_name_packagist"}} - {{.i18n.Tr "repo.settings.web_hook_name_custom"}} + {{.i18n.Tr "repo.settings.web_hook_name_custom"}} diff --git a/templates/repo/settings/webhook/new.tmpl b/templates/repo/settings/webhook/new.tmpl index 1d3242bc67d46..7245827d6dad4 100644 --- a/templates/repo/settings/webhook/new.tmpl +++ b/templates/repo/settings/webhook/new.tmpl @@ -30,7 +30,7 @@ {{else if eq .HookType "packagist"}} {{else if eq .HookType "custom"}} - + {{end}} From 7d66a4fb5ecb0a1f2ab1ab0920b3e66558fb7b58 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sat, 19 Feb 2022 11:39:24 -0500 Subject: [PATCH 09/10] custom svg --- public/img/gitea_custom.svg | 2 +- templates/admin/hook_new.tmpl | 2 +- templates/org/settings/hook_new.tmpl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/public/img/gitea_custom.svg b/public/img/gitea_custom.svg index e37776b506631..d027534502820 100644 --- a/public/img/gitea_custom.svg +++ b/public/img/gitea_custom.svg @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/templates/admin/hook_new.tmpl b/templates/admin/hook_new.tmpl index a1cf44543f9d2..c47c326b353da 100644 --- a/templates/admin/hook_new.tmpl +++ b/templates/admin/hook_new.tmpl @@ -37,7 +37,7 @@ {{else if eq .HookType "packagist"}} {{else if eq .HookType "custom"}} - + {{end}} diff --git a/templates/org/settings/hook_new.tmpl b/templates/org/settings/hook_new.tmpl index 469d94828efcd..d0e7484b7e214 100644 --- a/templates/org/settings/hook_new.tmpl +++ b/templates/org/settings/hook_new.tmpl @@ -32,7 +32,7 @@ {{else if eq .HookType "packagist"}} {{else if eq .HookType "custom"}} - + {{end}} From bffa611737f54899061870cc192ccebb94313a58 Mon Sep 17 00:00:00 2001 From: Alex Daigle Date: Sat, 19 Feb 2022 11:39:35 -0500 Subject: [PATCH 10/10] fix --- options/locale/locale_en-US.ini | 1 + templates/repo/settings/webhook/custom.tmpl | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index b888dd0a8f81b..ed49cdf522c41 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1958,6 +1958,7 @@ settings.slack_token = Token settings.slack_domain = Domain settings.slack_channel = Channel settings.custom_host_url = Host URL +settings.custom_auth_token = Custom Auth Token settings.add_web_hook_desc = Integrate %s into your repository. settings.web_hook_name_gitea = Gitea settings.web_hook_name_gogs = Gogs diff --git a/templates/repo/settings/webhook/custom.tmpl b/templates/repo/settings/webhook/custom.tmpl index c01263910d84a..bbc5580ada6b7 100644 --- a/templates/repo/settings/webhook/custom.tmpl +++ b/templates/repo/settings/webhook/custom.tmpl @@ -1,15 +1,15 @@ {{if eq .HookType "custom"}}

{{.i18n.Tr "repo.settings.add_web_hook_desc" "https://docs.gitea.io/en-us/webhooks/" (.i18n.Tr "repo.settings.web_hook_name_custom") | Str2html}}

- {{template "base/disable_form_autofill"}} {{.CsrfTokenHtml}}
- - + +
-
+ +
- +
{{template "repo/settings/webhook/settings" .}}