Skip to content

Webhook Logs show proper HTTP Method, and allow change HTTP method in form #6953

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

Merged
merged 4 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,28 +755,26 @@ func prepareWebhooks(e Engine, repo *Repository, event HookEventType, p api.Payl

func (t *HookTask) deliver() {
t.IsDelivered = true
t.RequestInfo = &HookRequest{
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moving these back to the original spot before the GET PR was merged.

Headers: map[string]string{},
}
t.ResponseInfo = &HookResponse{
Headers: map[string]string{},
}

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

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)
switch t.ContentType {
case ContentTypeJSON:
req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
case ContentTypeForm:
req.Param("payload", t.PayloadContent)
}
} else if t.HTTPMethod == http.MethodGet {
case http.MethodGet:
req = httplib.Get(t.URL).Param("payload", t.PayloadContent)
} else {
t.ResponseInfo.Body = fmt.Sprintf("Invalid http method: %v", t.HTTPMethod)
default:
log.Error("Invalid http method for webhook: [%d] %v", t.ID, t.HTTPMethod)
return
}

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

// Record delivery information.
t.RequestInfo = &HookRequest{
Headers: map[string]string{},
}
for k, vals := range req.Headers() {
t.RequestInfo.Headers[k] = strings.Join(vals, ",")
}

t.ResponseInfo = &HookResponse{
Headers: map[string]string{},
}

defer func() {
t.Delivered = time.Now().UnixNano()
if t.IsSucceed {
Expand Down
1 change: 1 addition & 0 deletions routers/api/v1/utils/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
URL: form.Config["url"],
ContentType: models.ToHookContentType(form.Config["content_type"]),
Secret: form.Config["secret"],
HTTPMethod: "POST",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I've hardcoded this as POST for now. Currently there are several checks that essentially force POST to be the method, but I want to keep this PR as simple as possible. This of course will need to be changed in future.

HookEvent: &models.HookEvent{
ChooseEvents: true,
HookEvents: models.HookEvents{
Expand Down
1 change: 1 addition & 0 deletions routers/repo/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
w.Secret = form.Secret
w.HookEvent = ParseHookEvent(form.WebhookForm)
w.IsActive = form.Active
w.HTTPMethod = form.HTTPMethod
if err := w.UpdateEvent(); err != nil {
ctx.ServerError("UpdateEvent", err)
return
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/settings/webhook/history.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
{{if .RequestInfo}}
<h5>{{$.i18n.Tr "repo.settings.webhook.headers"}}</h5>
<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}}
{{end}}</pre>
<h5>{{$.i18n.Tr "repo.settings.webhook.payload"}}</h5>
Expand Down