diff --git a/cmd/web.go b/cmd/web.go index 855ce7721..be489c1be 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -174,6 +174,7 @@ func runWeb(*cli.Context) { r.Post("/settings/collaboration", repo.CollaborationPost) r.Get("/settings/hooks", repo.WebHooks) // TODO r.Get("/settings/hooks/add", repo.WebHooksAdd) + r.Post("/settings/hooks/add", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksAddPost) r.Get("/settings/hooks/id", repo.WebHooksEdit) }, reqSignIn, middleware.RepoAssignment(true), reqOwner()) diff --git a/models/models.go b/models/models.go index b565f22ab..ebb558fcc 100644 --- a/models/models.go +++ b/models/models.go @@ -34,7 +34,7 @@ var ( func init() { tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow), - new(Mirror), new(Release), new(LoginSource)) + new(Mirror), new(Release), new(LoginSource), new(Webhook)) } func LoadModelsConfig() { diff --git a/models/webhook.go b/models/webhook.go new file mode 100644 index 000000000..78d984cfe --- /dev/null +++ b/models/webhook.go @@ -0,0 +1,58 @@ +// Copyright 2014 The Gogs 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 models + +import ( + "encoding/json" + + "github.com/gogits/gogs/modules/log" +) + +// Content types. +const ( + CT_JSON = iota + 1 + CT_FORM +) + +type HookEvent struct { + PushOnly bool `json:"push_only"` +} + +type Webhook struct { + Id int64 + RepoId int64 + Payload string `xorm:"TEXT"` + ContentType int + Secret string `xorm:"TEXT"` + Events string `xorm:"TEXT"` + IsSsl bool + IsActive bool +} + +func (w *Webhook) GetEvent() *HookEvent { + h := &HookEvent{} + if err := json.Unmarshal([]byte(w.Events), h); err != nil { + log.Error("webhook.GetEvent(%d): %v", w.Id, err) + } + return h +} + +func (w *Webhook) SaveEvent(h *HookEvent) error { + data, err := json.Marshal(h) + w.Events = string(data) + return err +} + +// CreateWebhook creates new webhook. +func CreateWebhook(w *Webhook) error { + _, err := orm.Insert(w) + return err +} + +// GetWebhooksByRepoId returns all webhooks of repository. +func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) { + err = orm.Find(&ws, &Webhook{RepoId: repoId}) + return ws, err +} diff --git a/modules/auth/repo.go b/modules/auth/repo.go index 54fa99c83..37b9f1d1e 100644 --- a/modules/auth/repo.go +++ b/modules/auth/repo.go @@ -83,3 +83,24 @@ func (f *RepoSettingForm) Validate(errors *binding.BindingErrors, req *http.Requ data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) validate(errors, data, f) } + +type NewWebhookForm struct { + Url string `form:"url" binding:"Required;Url"` + ContentType string `form:"content_type" binding:"Required"` + Secret string `form:"secret""` + PushOnly bool `form:"push_only"` + Active bool `form:"active"` +} + +func (f *NewWebhookForm) Name(field string) string { + names := map[string]string{ + "Url": "Payload URL", + "ContentType": "Content type", + } + return names[field] +} + +func (f *NewWebhookForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) { + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + validate(errors, data, f) +} diff --git a/routers/repo/setting.go b/routers/repo/setting.go index c30873920..d49c93375 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -199,18 +199,62 @@ func CollaborationPost(ctx *middleware.Context) { func WebHooks(ctx *middleware.Context) { ctx.Data["IsRepoToolbarWebHooks"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Web Hooks" + ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhooks" + + ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.Id) + if err != nil { + ctx.Handle(500, "repo.WebHooks(GetWebhooksByRepoId)", err) + return + } + + ctx.Data["Webhooks"] = ws ctx.HTML(200, "repo/hooks") } func WebHooksAdd(ctx *middleware.Context) { ctx.Data["IsRepoToolbarWebHooks"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Web Hook" + ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook" ctx.HTML(200, "repo/hooks_add") } +func WebHooksAddPost(ctx *middleware.Context, form auth.NewWebhookForm) { + ctx.Data["IsRepoToolbarWebHooks"] = true + ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Add Webhook" + + if ctx.HasError() { + ctx.HTML(200, "repo/hooks_add") + return + } + + ct := models.CT_JSON + if form.ContentType == "form" { + ct = models.CT_FORM + } + + w := &models.Webhook{ + RepoId: ctx.Repo.Repository.Id, + Payload: form.Url, + ContentType: ct, + Secret: form.Secret, + IsActive: form.Active, + } + h := &models.HookEvent{ + PushOnly: form.PushOnly, + } + if err := w.SaveEvent(h); err != nil { + ctx.Handle(500, "repo.WebHooksAddPost(SaveEvent)", err) + return + } else if err := models.CreateWebhook(w); err != nil { + ctx.Handle(500, "repo.WebHooksAddPost(CreateWebhook)", err) + return + } + + ctx.Flash.Success("New webhook has been added.") + ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks") +} + func WebHooksEdit(ctx *middleware.Context) { ctx.Data["IsRepoToolbarWebHooks"] = true - ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Web Hook Name" + ctx.Data["Title"] = strings.TrimPrefix(ctx.Repo.RepoLink, "/") + " - Webhook" ctx.HTML(200, "repo/hooks_edit") } diff --git a/templates/repo/hooks.tmpl b/templates/repo/hooks.tmpl index 643686aae..eed35c358 100644 --- a/templates/repo/hooks.tmpl +++ b/templates/repo/hooks.tmpl @@ -13,22 +13,18 @@

Webhooks allow external services to be notified when certain events happen on GitHub. When the specified events happen, we'll send a POST request to each of the URLs you provide. Learn more in our Webhooks Guide.
 

diff --git a/templates/repo/hooks_add.tmpl b/templates/repo/hooks_add.tmpl index 3a377fda6..cd4e6e7a9 100644 --- a/templates/repo/hooks_add.tmpl +++ b/templates/repo/hooks_add.tmpl @@ -6,7 +6,8 @@ {{template "repo/setting_nav" .}}
{{template "base/alert" .}} -
+ + {{.CsrfTokenHtml}}
Add Webhook @@ -18,31 +19,31 @@
- +
- -
- +


We will deliver event details when this hook is triggered.

diff --git a/templates/repo/hooks_edit.tmpl b/templates/repo/hooks_edit.tmpl index 718336ec3..cac2ff653 100644 --- a/templates/repo/hooks_edit.tmpl +++ b/templates/repo/hooks_edit.tmpl @@ -36,7 +36,7 @@