Skip to content

Commit 5fb1ad7

Browse files
techknowlogicklunny
authored andcommitted
Webhook Logs show proper HTTP Method, and allow change HTTP method in form (#6953)
* Fix #6951 - logs show proper HTTP Method, and allow change HTTP method in form * enforce POST method for webhook * set default if method is empty
1 parent 710245e commit 5fb1ad7

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

models/webhook.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -755,28 +755,26 @@ func prepareWebhooks(e Engine, repo *Repository, event HookEventType, p api.Payl
755755

756756
func (t *HookTask) deliver() {
757757
t.IsDelivered = true
758-
t.RequestInfo = &HookRequest{
759-
Headers: map[string]string{},
760-
}
761-
t.ResponseInfo = &HookResponse{
762-
Headers: map[string]string{},
763-
}
764758

765759
timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
766760

767761
var req *httplib.Request
768-
if t.HTTPMethod == http.MethodPost {
762+
switch t.HTTPMethod {
763+
case "":
764+
log.Info("HTTP Method for webhook %d empty, setting to POST as default", t.ID)
765+
fallthrough
766+
case http.MethodPost:
769767
req = httplib.Post(t.URL)
770768
switch t.ContentType {
771769
case ContentTypeJSON:
772770
req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
773771
case ContentTypeForm:
774772
req.Param("payload", t.PayloadContent)
775773
}
776-
} else if t.HTTPMethod == http.MethodGet {
774+
case http.MethodGet:
777775
req = httplib.Get(t.URL).Param("payload", t.PayloadContent)
778-
} else {
779-
t.ResponseInfo.Body = fmt.Sprintf("Invalid http method: %v", t.HTTPMethod)
776+
default:
777+
log.Error("Invalid http method for webhook: [%d] %v", t.ID, t.HTTPMethod)
780778
return
781779
}
782780

@@ -792,10 +790,17 @@ func (t *HookTask) deliver() {
792790
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify})
793791

794792
// Record delivery information.
793+
t.RequestInfo = &HookRequest{
794+
Headers: map[string]string{},
795+
}
795796
for k, vals := range req.Headers() {
796797
t.RequestInfo.Headers[k] = strings.Join(vals, ",")
797798
}
798799

800+
t.ResponseInfo = &HookResponse{
801+
Headers: map[string]string{},
802+
}
803+
799804
defer func() {
800805
t.Delivered = time.Now().UnixNano()
801806
if t.IsSucceed {

routers/api/v1/utils/hook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
9898
URL: form.Config["url"],
9999
ContentType: models.ToHookContentType(form.Config["content_type"]),
100100
Secret: form.Config["secret"],
101+
HTTPMethod: "POST",
101102
HookEvent: &models.HookEvent{
102103
ChooseEvents: true,
103104
HookEvents: models.HookEvents{

routers/repo/webhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
564564
w.Secret = form.Secret
565565
w.HookEvent = ParseHookEvent(form.WebhookForm)
566566
w.IsActive = form.Active
567+
w.HTTPMethod = form.HTTPMethod
567568
if err := w.UpdateEvent(); err != nil {
568569
ctx.ServerError("UpdateEvent", err)
569570
return

templates/repo/settings/webhook/history.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
{{if .RequestInfo}}
4646
<h5>{{$.i18n.Tr "repo.settings.webhook.headers"}}</h5>
4747
<pre class="raw"><strong>Request URL:</strong> {{.URL}}
48-
<strong>Request method:</strong> POST
48+
<strong>Request method:</strong> {{if .HTTPMethod}}{{.HTTPMethod}}{{else}}POST{{end}}
4949
{{ range $key, $val := .RequestInfo.Headers }}<strong>{{$key}}:</strong> {{$val}}
5050
{{end}}</pre>
5151
<h5>{{$.i18n.Tr "repo.settings.webhook.payload"}}</h5>

0 commit comments

Comments
 (0)