From a0dc5e79b359c29beb053a5dbdbd04a340759409 Mon Sep 17 00:00:00 2001 From: Simon Hilchenbach Date: Sat, 27 Apr 2019 00:20:55 +0200 Subject: [PATCH 1/6] Add GET requests to webhook --- models/migrations/migrations.go | 2 ++ models/migrations/v85.go | 17 ++++++++++++++ models/webhook.go | 26 +++++++++++++++------- modules/auth/repo_form.go | 1 + options/locale/locale_en-US.ini | 1 + public/js/index.js | 9 ++++++++ routers/repo/webhook.go | 1 + templates/repo/settings/webhook/gitea.tmpl | 12 ++++++++++ 8 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 models/migrations/v85.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 62c41fb906e27..1671df96c03e2 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -223,6 +223,8 @@ var migrations = []Migration{ NewMigration("add uploader id for table attachment", addUploaderIDForAttachment), // v84 -> v85 NewMigration("add table to store original imported gpg keys", addGPGKeyImport), + // v85 -> v86 + NewMigration("add http method to webhook", addHTTPMethodToWebhook), } // Migrate database to current version diff --git a/models/migrations/v85.go b/models/migrations/v85.go new file mode 100644 index 0000000000000..44932646660fa --- /dev/null +++ b/models/migrations/v85.go @@ -0,0 +1,17 @@ +// 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 migrations + +import ( + "github.com/go-xorm/xorm" +) + +func addHTTPMethodToWebhook(x *xorm.Engine) error { + type Webhook struct { + HTTPMethod string `xorm:"http_method DEFAULT 'post'"` + } + + return x.Sync2(new(Webhook)) +} diff --git a/models/webhook.go b/models/webhook.go index 9be89241a4c82..c2b7860cdc8f2 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -105,6 +105,7 @@ type Webhook struct { OrgID int64 `xorm:"INDEX"` URL string `xorm:"url TEXT"` Signature string `xorm:"TEXT"` + HTTPMethod string `xorm:"http_method"` ContentType HookContentType Secret string `xorm:"TEXT"` Events string `xorm:"TEXT"` @@ -553,6 +554,7 @@ type HookTask struct { Signature string `xorm:"TEXT"` api.Payloader `xorm:"-"` PayloadContent string `xorm:"TEXT"` + HTTPMethod string `xorm:"http_method"` ContentType HookContentType EventType HookEventType IsSSL bool @@ -707,6 +709,7 @@ func prepareWebhook(e Engine, w *Webhook, repo *Repository, event HookEventType, URL: w.URL, Signature: signature, Payloader: payloader, + HTTPMethod: w.HTTPMethod, ContentType: w.ContentType, EventType: event, IsSSL: w.IsSSL, @@ -753,7 +756,21 @@ func (t *HookTask) deliver() { t.IsDelivered = true timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second - req := httplib.Post(t.URL).SetTimeout(timeout, timeout). + + var req *httplib.Request + if t.HTTPMethod == "post" { + req = httplib.Post(t.URL) + switch t.ContentType { + case ContentTypeJSON: + req = req.Header("Content-Type", "application/json").Body(t.PayloadContent) + case ContentTypeForm: + req.Param("payload", t.PayloadContent) + } + } else { + req = httplib.Get(t.URL).Param("payload", t.PayloadContent) + } + + req = req.SetTimeout(timeout, timeout). Header("X-Gitea-Delivery", t.UUID). Header("X-Gitea-Event", string(t.EventType)). Header("X-Gitea-Signature", t.Signature). @@ -764,13 +781,6 @@ func (t *HookTask) deliver() { HeaderWithSensitiveCase("X-GitHub-Event", string(t.EventType)). SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify}) - switch t.ContentType { - case ContentTypeJSON: - req = req.Header("Content-Type", "application/json").Body(t.PayloadContent) - case ContentTypeForm: - req.Param("payload", t.PayloadContent) - } - // Record delivery information. t.RequestInfo = &HookRequest{ Headers: map[string]string{}, diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index fd6891288d998..33bdf764805d6 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -196,6 +196,7 @@ func (f WebhookForm) ChooseEvents() bool { // NewWebhookForm form for creating web hook type NewWebhookForm struct { PayloadURL string `binding:"Required;ValidUrl"` + HTTPMethod string `binding:"Required;In(post,get)"` ContentType int `binding:"Required"` Secret string WebhookForm diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index eedede2a05859..6c1cea0371c22 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1171,6 +1171,7 @@ settings.githook_content = Hook Content settings.update_githook = Update Hook settings.add_webhook_desc = Gitea will send POST requests with a specified content type to the target URL. Read more in the webhooks guide. settings.payload_url = Target URL +settings.http_method = HTTP Method settings.content_type = POST Content Type settings.secret = Secret settings.slack_username = Username diff --git a/public/js/index.js b/public/js/index.js index 062ed7ce4d791..e16d31b8ae58e 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -1430,6 +1430,15 @@ function initWebhook() { } }); + var updateContentType = function () { + var visible = $('#http_method').val() === 'post'; + $('#content_type').parent().parent()[visible ? 'show' : 'hide'](); + }; + updateContentType(); + $('#http_method').change(function () { + updateContentType(); + }); + // Test delivery $('#test-delivery').click(function () { var $this = $(this); diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 6cf636f2470ac..2e76cbfe02946 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -176,6 +176,7 @@ func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) { w := &models.Webhook{ RepoID: orCtx.RepoID, URL: form.PayloadURL, + HTTPMethod: form.HTTPMethod, ContentType: contentType, Secret: form.Secret, HookEvent: ParseHookEvent(form.WebhookForm), diff --git a/templates/repo/settings/webhook/gitea.tmpl b/templates/repo/settings/webhook/gitea.tmpl index 605022256d778..fd2d1672888ae 100644 --- a/templates/repo/settings/webhook/gitea.tmpl +++ b/templates/repo/settings/webhook/gitea.tmpl @@ -6,6 +6,18 @@ +
+ + +
From 693c8644ff62737b5b341063446c5d10dce2c658 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Sat, 4 May 2019 11:47:05 -0400 Subject: [PATCH 5/6] Rename v85.go to v86.go --- models/migrations/{v85.go => v86.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/{v85.go => v86.go} (100%) diff --git a/models/migrations/v85.go b/models/migrations/v86.go similarity index 100% rename from models/migrations/v85.go rename to models/migrations/v86.go From b13603278bf3d4b50c224456e96925b4b5d2a3a6 Mon Sep 17 00:00:00 2001 From: techknowlogick Date: Sat, 4 May 2019 13:13:52 -0400 Subject: [PATCH 6/6] make fmt --- models/migrations/migrations.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 4ded89705fd00..b86c20576f7e8 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -225,7 +225,7 @@ var migrations = []Migration{ NewMigration("add table to store original imported gpg keys", addGPGKeyImport), // v85 -> v86 NewMigration("hash application token", hashAppToken), - // v86 -> v87 + // v86 -> v87 NewMigration("add http method to webhook", addHTTPMethodToWebhook), }