diff options
-rw-r--r-- | models/webhook.go | 9 | ||||
-rw-r--r-- | models/webhook_test.go | 5 | ||||
-rw-r--r-- | modules/auth/repo_form.go | 14 | ||||
-rw-r--r-- | modules/setting/setting.go | 3 | ||||
-rw-r--r-- | public/img/gogs.ico | bin | 0 -> 29157 bytes | |||
-rw-r--r-- | routers/repo/webhook.go | 93 | ||||
-rw-r--r-- | routers/routes/routes.go | 12 | ||||
-rw-r--r-- | templates/org/settings/hook_new.tmpl | 5 | ||||
-rw-r--r-- | templates/repo/settings/hook_gitea.tmpl | 28 | ||||
-rw-r--r-- | templates/repo/settings/hook_list.tmpl | 5 | ||||
-rw-r--r-- | templates/repo/settings/hook_new.tmpl | 5 |
11 files changed, 167 insertions, 12 deletions
diff --git a/models/webhook.go b/models/webhook.go index 3fb1e57e78..5cb2e13a6d 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2017 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. @@ -312,9 +313,11 @@ type HookTaskType int const ( GOGS HookTaskType = iota + 1 SLACK + GITEA ) var hookTaskTypes = map[string]HookTaskType{ + "gitea": GITEA, "gogs": GOGS, "slack": SLACK, } @@ -327,6 +330,8 @@ func ToHookTaskType(name string) HookTaskType { // Name returns the name of an hook task type func (t HookTaskType) Name() string { switch t { + case GITEA: + return "gitea" case GOGS: return "gogs" case SLACK: @@ -503,7 +508,7 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err } } - // Use separate objects so modifications won't be made on payload on non-Gogs type hooks. + // Use separate objects so modifications won't be made on payload on non-Gogs/Gitea type hooks. switch w.HookTaskType { case SLACK: payloader, err = GetSlackPayload(p, event, w.Meta) @@ -536,6 +541,8 @@ func (t *HookTask) deliver() { timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second req := httplib.Post(t.URL).SetTimeout(timeout, timeout). + Header("X-Gitea-Delivery", t.UUID). + Header("X-Gitea-Event", string(t.EventType)). Header("X-Gogs-Delivery", t.UUID). Header("X-Gogs-Event", string(t.EventType)). Header("X-GitHub-Delivery", t.UUID). diff --git a/models/webhook_test.go b/models/webhook_test.go index 3f377eae7a..a637a16999 100644 --- a/models/webhook_test.go +++ b/models/webhook_test.go @@ -191,16 +191,19 @@ func TestDeleteWebhookByOrgID(t *testing.T) { func TestToHookTaskType(t *testing.T) { assert.Equal(t, GOGS, ToHookTaskType("gogs")) assert.Equal(t, SLACK, ToHookTaskType("slack")) + assert.Equal(t, GITEA, ToHookTaskType("gitea")) } func TestHookTaskType_Name(t *testing.T) { assert.Equal(t, "gogs", GOGS.Name()) assert.Equal(t, "slack", SLACK.Name()) + assert.Equal(t, "gitea", GITEA.Name()) } func TestIsValidHookTaskType(t *testing.T) { assert.True(t, IsValidHookTaskType("gogs")) assert.True(t, IsValidHookTaskType("slack")) + assert.True(t, IsValidHookTaskType("gitea")) assert.False(t, IsValidHookTaskType("invalid")) } @@ -221,7 +224,7 @@ func TestCreateHookTask(t *testing.T) { hookTask := &HookTask{ RepoID: 3, HookID: 3, - Type: GOGS, + Type: GITEA, URL: "http://www.example.com/unit_test", Payloader: &api.PushPayload{}, } diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index c56e76c871..58dcf468ef 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2017 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. @@ -154,6 +155,19 @@ func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) bin return validate(errs, ctx.Data, f, ctx.Locale) } +// NewGogshookForm form for creating gogs hook +type NewGogshookForm struct { + PayloadURL string `binding:"Required;ValidUrl"` + ContentType int `binding:"Required"` + Secret string + WebhookForm +} + +// Validate validates the fields +func (f *NewGogshookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) +} + // NewSlackHookForm form for creating slack hook type NewSlackHookForm struct { PayloadURL string `binding:"Required;ValidUrl"` diff --git a/modules/setting/setting.go b/modules/setting/setting.go index c3a829715b..a948527a2c 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -1,4 +1,5 @@ // Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2017 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. @@ -1325,7 +1326,7 @@ func newWebhookService() { Webhook.QueueLength = sec.Key("QUEUE_LENGTH").MustInt(1000) Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5) Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool() - Webhook.Types = []string{"gogs", "slack"} + Webhook.Types = []string{"gitea", "gogs", "slack"} Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10) } diff --git a/public/img/gogs.ico b/public/img/gogs.ico Binary files differnew file mode 100644 index 0000000000..3438553553 --- /dev/null +++ b/public/img/gogs.ico diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 754cf169b8..5489d71857 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -1,4 +1,5 @@ // Copyright 2015 The Gogs Authors. All rights reserved. +// Copyright 2017 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. @@ -124,6 +125,53 @@ func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) { ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} + ctx.Data["HookType"] = "gitea" + + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.Handle(500, "getOrgRepoCtx", err) + return + } + ctx.Data["BaseLink"] = orCtx.Link + + if ctx.HasError() { + ctx.HTML(200, orCtx.NewTemplate) + return + } + + contentType := models.ContentTypeJSON + if models.HookContentType(form.ContentType) == models.ContentTypeForm { + contentType = models.ContentTypeForm + } + + w := &models.Webhook{ + RepoID: orCtx.RepoID, + URL: form.PayloadURL, + ContentType: contentType, + Secret: form.Secret, + HookEvent: ParseHookEvent(form.WebhookForm), + IsActive: form.Active, + HookTaskType: models.GITEA, + OrgID: orCtx.OrgID, + } + if err := w.UpdateEvent(); err != nil { + ctx.Handle(500, "UpdateEvent", err) + return + } else if err := models.CreateWebhook(w); err != nil { + ctx.Handle(500, "CreateWebhook", err) + return + } + + ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) + ctx.Redirect(orCtx.Link + "/settings/hooks") +} + +// GogsHooksNewPost response for creating webhook +func GogsHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) { + ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook") + ctx.Data["PageIsSettingsHooks"] = true + ctx.Data["PageIsSettingsHooksNew"] = true + ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} ctx.Data["HookType"] = "gogs" orCtx, err := getOrgRepoCtx(ctx) @@ -150,7 +198,7 @@ func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) { Secret: form.Secret, HookEvent: ParseHookEvent(form.WebhookForm), IsActive: form.Active, - HookTaskType: models.GOGS, + HookTaskType: models.GITEA, OrgID: orCtx.OrgID, } if err := w.UpdateEvent(); err != nil { @@ -245,8 +293,10 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) { case models.SLACK: ctx.Data["SlackHook"] = w.GetSlackHook() ctx.Data["HookType"] = "slack" - default: + case models.GOGS: ctx.Data["HookType"] = "gogs" + default: + ctx.Data["HookType"] = "gitea" } ctx.Data["History"], err = w.History(1) @@ -310,6 +360,45 @@ func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) { ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) } +// GogsHooksEditPost response for editing gogs hook +func GogsHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) { + ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook") + ctx.Data["PageIsSettingsHooks"] = true + ctx.Data["PageIsSettingsHooksEdit"] = true + + orCtx, w := checkWebhook(ctx) + if ctx.Written() { + return + } + ctx.Data["Webhook"] = w + + if ctx.HasError() { + ctx.HTML(200, orCtx.NewTemplate) + return + } + + contentType := models.ContentTypeJSON + if models.HookContentType(form.ContentType) == models.ContentTypeForm { + contentType = models.ContentTypeForm + } + + w.URL = form.PayloadURL + w.ContentType = contentType + w.Secret = form.Secret + w.HookEvent = ParseHookEvent(form.WebhookForm) + w.IsActive = form.Active + if err := w.UpdateEvent(); err != nil { + ctx.Handle(500, "UpdateEvent", err) + return + } else if err := models.UpdateWebhook(w); err != nil { + ctx.Handle(500, "GogsHooksEditPost", err) + return + } + + ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) + ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) +} + // SlackHooksEditPost response for editing slack hook func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings") diff --git a/routers/routes/routes.go b/routers/routes/routes.go index f1c55e1705..0781aef89d 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -379,10 +379,12 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("", org.Webhooks) m.Post("/delete", org.DeleteWebhook) m.Get("/:type/new", repo.WebhooksNew) - m.Post("/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost) + m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost) + m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost) m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost) m.Get("/:id", repo.WebHooksEdit) - m.Post("/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) + m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) + m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksEditPost) m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost) }) @@ -423,11 +425,13 @@ func RegisterRoutes(m *macaron.Macaron) { m.Get("", repo.Webhooks) m.Post("/delete", repo.DeleteWebhook) m.Get("/:type/new", repo.WebhooksNew) - m.Post("/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost) + m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost) + m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost) m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost) m.Get("/:id", repo.WebHooksEdit) m.Post("/:id/test", repo.TestWebhook) - m.Post("/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) + m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) + m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost) m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost) m.Group("/git", func() { diff --git a/templates/org/settings/hook_new.tmpl b/templates/org/settings/hook_new.tmpl index 2324788d33..ee3fd2c506 100644 --- a/templates/org/settings/hook_new.tmpl +++ b/templates/org/settings/hook_new.tmpl @@ -9,14 +9,17 @@ <h4 class="ui top attached header"> {{if .PageIsSettingsHooksNew}}{{.i18n.Tr "repo.settings.add_webhook"}}{{else}}{{.i18n.Tr "repo.settings.update_webhook"}}{{end}} <div class="ui right"> - {{if eq .HookType "gogs"}} + {{if eq .HookType "gitea"}} <img class="img-13" src="{{AppSubUrl}}/img/favicon.png"> + {{else if eq .HookType "gogs"}} + <img class="img-13" src="{{AppSubUrl}}/img/gogs.ico"> {{else if eq .HookType "slack"}} <img class="img-13" src="{{AppSubUrl}}/img/slack.png"> {{end}} </div> </h4> <div class="ui attached segment"> + {{template "repo/settings/hook_gitea" .}} {{template "repo/settings/hook_gogs" .}} {{template "repo/settings/hook_slack" .}} </div> diff --git a/templates/repo/settings/hook_gitea.tmpl b/templates/repo/settings/hook_gitea.tmpl new file mode 100644 index 0000000000..4ac6d718c9 --- /dev/null +++ b/templates/repo/settings/hook_gitea.tmpl @@ -0,0 +1,28 @@ +{{if eq .HookType "gitea"}}
+ <p>{{.i18n.Tr "repo.settings.add_webhook_desc" "https://docs.gitea.io/features/webhook.html" | Str2html}}</p>
+ <form class="ui form" action="{{.BaseLink}}/settings/hooks/gitea/{{if .PageIsSettingsHooksNew}}new{{else}}{{.Webhook.ID}}{{end}}" method="post">
+ {{.CsrfTokenHtml}}
+ <div class="required field {{if .Err_PayloadURL}}error{{end}}">
+ <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.content_type"}}</label>
+ <div class="ui selection dropdown">
+ <input type="hidden" id="content_type" name="content_type" value="{{if .Webhook.ContentType}}{{.Webhook.ContentType}}{{else}}application/json{{end}}">
+ <div class="default text"></div>
+ <i class="dropdown icon"></i>
+ <div class="menu">
+ <div class="item" data-value="1">application/json</div>
+ <div class="item" data-value="2">application/x-www-form-urlencoded</div>
+ </div>
+ </div>
+ </div>
+ <input class="fake" type="password">
+ <div class="field {{if .Err_Secret}}error{{end}}">
+ <label for="secret">{{.i18n.Tr "repo.settings.secret"}}</label>
+ <input id="secret" name="secret" type="password" value="{{.Webhook.Secret}}" autocomplete="off">
+ </div>
+ {{template "repo/settings/hook_settings" .}}
+ </form>
+{{end}}
diff --git a/templates/repo/settings/hook_list.tmpl b/templates/repo/settings/hook_list.tmpl index 0b9c4d5057..aba948b952 100644 --- a/templates/repo/settings/hook_list.tmpl +++ b/templates/repo/settings/hook_list.tmpl @@ -5,9 +5,12 @@ <div class="ui floating1 jump dropdown"> <div class="ui blue tiny button">{{.i18n.Tr "repo.settings.add_webhook"}}</div> <div class="menu"> - <a class="item" href="{{.BaseLink}}/settings/hooks/gogs/new"> + <a class="item" href="{{.BaseLink}}/settings/hooks/gitea/new"> <img class="img-10" src="{{AppSubUrl}}/img/favicon.png">Gitea </a> + <a class="item" href="{{.BaseLink}}/settings/hooks/gogs/new"> + <img class="img-10" src="{{AppSubUrl}}/img/gogs.ico">Gogs + </a> <a class="item" href="{{.BaseLink}}/settings/hooks/slack/new"> <img class="img-10" src="{{AppSubUrl}}/img/slack.png">Slack </a> diff --git a/templates/repo/settings/hook_new.tmpl b/templates/repo/settings/hook_new.tmpl index 8f7f0026f2..48322e47f8 100644 --- a/templates/repo/settings/hook_new.tmpl +++ b/templates/repo/settings/hook_new.tmpl @@ -7,14 +7,17 @@ <h4 class="ui top attached header"> {{if .PageIsSettingsHooksNew}}{{.i18n.Tr "repo.settings.add_webhook"}}{{else}}{{.i18n.Tr "repo.settings.update_webhook"}}{{end}} <div class="ui right"> - {{if eq .HookType "gogs"}} + {{if eq .HookType "gitea"}} <img class="img-13" src="{{AppSubUrl}}/img/favicon.png"> + {{else if eq .HookType "gogs"}} + <img class="img-13" src="{{AppSubUrl}}/img/gogs.ico"> {{else if eq .HookType "slack"}} <img class="img-13" src="{{AppSubUrl}}/img/slack.png"> {{end}} </div> </h4> <div class="ui attached segment"> + {{template "repo/settings/hook_gitea" .}} {{template "repo/settings/hook_gogs" .}} {{template "repo/settings/hook_slack" .}} </div> |