Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET requests to webhook #6771

Merged
merged 11 commits into from
May 5, 2019
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions models/migrations/v85.go
Original file line number Diff line number Diff line change
@@ -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))
}
26 changes: 18 additions & 8 deletions models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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" {
shilch marked this conversation as resolved.
Show resolved Hide resolved
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 {
shilch marked this conversation as resolved.
Show resolved Hide resolved
req = httplib.Get(t.URL).Param("payload", t.PayloadContent)
jonasfranz marked this conversation as resolved.
Show resolved Hide resolved
}

req = req.SetTimeout(timeout, timeout).
Header("X-Gitea-Delivery", t.UUID).
Header("X-Gitea-Event", string(t.EventType)).
Header("X-Gitea-Signature", t.Signature).
Expand All @@ -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{},
Expand Down
1 change: 1 addition & 0 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ settings.githook_content = Hook Content
settings.update_githook = Update Hook
settings.add_webhook_desc = Gitea will send <code>POST</code> requests with a specified content type to the target URL. Read more in the <a target="_blank" rel="noopener noreferrer" href="%s">webhooks guide</a>.
settings.payload_url = Target URL
settings.http_method = HTTP Method
settings.content_type = POST Content Type
settings.secret = Secret
settings.slack_username = Username
Expand Down
9 changes: 9 additions & 0 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions routers/repo/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
12 changes: 12 additions & 0 deletions templates/repo/settings/webhook/gitea.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
<label for="payload_url">{{.i18n.Tr "repo.settings.payload_url"}}</label>
<input id="payload_url" name="payload_url" type="url" value="{{.Webhook.URL}}" autofocus required>
</div>
<div class="field">
<label>{{.i18n.Tr "repo.settings.http_method"}}</label>
<div class="ui selection dropdown">
<input type="hidden" id="http_method" name="http_method" value="{{if .Webhook.HTTPMethod}}{{.Webhook.HTTPMethod}}{{else}}POST{{end}}">
<div class="default text"></div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="post">POST</div>
<div class="item" data-value="get">GET</div>
</div>
</div>
</div>
<div class="field">
<label>{{.i18n.Tr "repo.settings.content_type"}}</label>
<div class="ui selection dropdown">
Expand Down