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
This commit is contained in:
techknowlogick 2019-05-15 08:01:53 -04:00 committed by Lunny Xiao
parent 710245e81e
commit 5fb1ad7011
4 changed files with 18 additions and 11 deletions

View File

@ -755,17 +755,15 @@ func prepareWebhooks(e Engine, repo *Repository, event HookEventType, p api.Payl
func (t *HookTask) deliver() { func (t *HookTask) deliver() {
t.IsDelivered = true t.IsDelivered = true
t.RequestInfo = &HookRequest{
Headers: map[string]string{},
}
t.ResponseInfo = &HookResponse{
Headers: map[string]string{},
}
timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
var req *httplib.Request var req *httplib.Request
if t.HTTPMethod == http.MethodPost { switch t.HTTPMethod {
case "":
log.Info("HTTP Method for webhook %d empty, setting to POST as default", t.ID)
fallthrough
case http.MethodPost:
req = httplib.Post(t.URL) req = httplib.Post(t.URL)
switch t.ContentType { switch t.ContentType {
case ContentTypeJSON: case ContentTypeJSON:
@ -773,10 +771,10 @@ func (t *HookTask) deliver() {
case ContentTypeForm: case ContentTypeForm:
req.Param("payload", t.PayloadContent) req.Param("payload", t.PayloadContent)
} }
} else if t.HTTPMethod == http.MethodGet { case http.MethodGet:
req = httplib.Get(t.URL).Param("payload", t.PayloadContent) req = httplib.Get(t.URL).Param("payload", t.PayloadContent)
} else { default:
t.ResponseInfo.Body = fmt.Sprintf("Invalid http method: %v", t.HTTPMethod) log.Error("Invalid http method for webhook: [%d] %v", t.ID, t.HTTPMethod)
return return
} }
@ -792,10 +790,17 @@ func (t *HookTask) deliver() {
SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify}) SetTLSClientConfig(&tls.Config{InsecureSkipVerify: setting.Webhook.SkipTLSVerify})
// Record delivery information. // Record delivery information.
t.RequestInfo = &HookRequest{
Headers: map[string]string{},
}
for k, vals := range req.Headers() { for k, vals := range req.Headers() {
t.RequestInfo.Headers[k] = strings.Join(vals, ",") t.RequestInfo.Headers[k] = strings.Join(vals, ",")
} }
t.ResponseInfo = &HookResponse{
Headers: map[string]string{},
}
defer func() { defer func() {
t.Delivered = time.Now().UnixNano() t.Delivered = time.Now().UnixNano()
if t.IsSucceed { if t.IsSucceed {

View File

@ -98,6 +98,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
URL: form.Config["url"], URL: form.Config["url"],
ContentType: models.ToHookContentType(form.Config["content_type"]), ContentType: models.ToHookContentType(form.Config["content_type"]),
Secret: form.Config["secret"], Secret: form.Config["secret"],
HTTPMethod: "POST",
HookEvent: &models.HookEvent{ HookEvent: &models.HookEvent{
ChooseEvents: true, ChooseEvents: true,
HookEvents: models.HookEvents{ HookEvents: models.HookEvents{

View File

@ -564,6 +564,7 @@ func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
w.Secret = form.Secret w.Secret = form.Secret
w.HookEvent = ParseHookEvent(form.WebhookForm) w.HookEvent = ParseHookEvent(form.WebhookForm)
w.IsActive = form.Active w.IsActive = form.Active
w.HTTPMethod = form.HTTPMethod
if err := w.UpdateEvent(); err != nil { if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err) ctx.ServerError("UpdateEvent", err)
return return

View File

@ -45,7 +45,7 @@
{{if .RequestInfo}} {{if .RequestInfo}}
<h5>{{$.i18n.Tr "repo.settings.webhook.headers"}}</h5> <h5>{{$.i18n.Tr "repo.settings.webhook.headers"}}</h5>
<pre class="raw"><strong>Request URL:</strong> {{.URL}} <pre class="raw"><strong>Request URL:</strong> {{.URL}}
<strong>Request method:</strong> POST <strong>Request method:</strong> {{if .HTTPMethod}}{{.HTTPMethod}}{{else}}POST{{end}}
{{ range $key, $val := .RequestInfo.Headers }}<strong>{{$key}}:</strong> {{$val}} {{ range $key, $val := .RequestInfo.Headers }}<strong>{{$key}}:</strong> {{$val}}
{{end}}</pre> {{end}}</pre>
<h5>{{$.i18n.Tr "repo.settings.webhook.payload"}}</h5> <h5>{{$.i18n.Tr "repo.settings.webhook.payload"}}</h5>