Skip to content

Commit e0c6ab2

Browse files
DblKappleboy
authored andcommitted
Add Gitea Webhook (#1755)
* Replace Gogs by Gitea * Fix missing changes * Create Gitea webhook and put Gogs webhook apart.
1 parent e67ece2 commit e0c6ab2

File tree

11 files changed

+167
-12
lines changed

11 files changed

+167
-12
lines changed

models/webhook.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2017 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -312,9 +313,11 @@ type HookTaskType int
312313
const (
313314
GOGS HookTaskType = iota + 1
314315
SLACK
316+
GITEA
315317
)
316318

317319
var hookTaskTypes = map[string]HookTaskType{
320+
"gitea": GITEA,
318321
"gogs": GOGS,
319322
"slack": SLACK,
320323
}
@@ -327,6 +330,8 @@ func ToHookTaskType(name string) HookTaskType {
327330
// Name returns the name of an hook task type
328331
func (t HookTaskType) Name() string {
329332
switch t {
333+
case GITEA:
334+
return "gitea"
330335
case GOGS:
331336
return "gogs"
332337
case SLACK:
@@ -503,7 +508,7 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
503508
}
504509
}
505510

506-
// Use separate objects so modifications won't be made on payload on non-Gogs type hooks.
511+
// Use separate objects so modifications won't be made on payload on non-Gogs/Gitea type hooks.
507512
switch w.HookTaskType {
508513
case SLACK:
509514
payloader, err = GetSlackPayload(p, event, w.Meta)
@@ -536,6 +541,8 @@ func (t *HookTask) deliver() {
536541

537542
timeout := time.Duration(setting.Webhook.DeliverTimeout) * time.Second
538543
req := httplib.Post(t.URL).SetTimeout(timeout, timeout).
544+
Header("X-Gitea-Delivery", t.UUID).
545+
Header("X-Gitea-Event", string(t.EventType)).
539546
Header("X-Gogs-Delivery", t.UUID).
540547
Header("X-Gogs-Event", string(t.EventType)).
541548
Header("X-GitHub-Delivery", t.UUID).

models/webhook_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,19 @@ func TestDeleteWebhookByOrgID(t *testing.T) {
191191
func TestToHookTaskType(t *testing.T) {
192192
assert.Equal(t, GOGS, ToHookTaskType("gogs"))
193193
assert.Equal(t, SLACK, ToHookTaskType("slack"))
194+
assert.Equal(t, GITEA, ToHookTaskType("gitea"))
194195
}
195196

196197
func TestHookTaskType_Name(t *testing.T) {
197198
assert.Equal(t, "gogs", GOGS.Name())
198199
assert.Equal(t, "slack", SLACK.Name())
200+
assert.Equal(t, "gitea", GITEA.Name())
199201
}
200202

201203
func TestIsValidHookTaskType(t *testing.T) {
202204
assert.True(t, IsValidHookTaskType("gogs"))
203205
assert.True(t, IsValidHookTaskType("slack"))
206+
assert.True(t, IsValidHookTaskType("gitea"))
204207
assert.False(t, IsValidHookTaskType("invalid"))
205208
}
206209

@@ -221,7 +224,7 @@ func TestCreateHookTask(t *testing.T) {
221224
hookTask := &HookTask{
222225
RepoID: 3,
223226
HookID: 3,
224-
Type: GOGS,
227+
Type: GITEA,
225228
URL: "http://www.example.com/unit_test",
226229
Payloader: &api.PushPayload{},
227230
}

modules/auth/repo_form.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2017 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -154,6 +155,19 @@ func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) bin
154155
return validate(errs, ctx.Data, f, ctx.Locale)
155156
}
156157

158+
// NewGogshookForm form for creating gogs hook
159+
type NewGogshookForm struct {
160+
PayloadURL string `binding:"Required;ValidUrl"`
161+
ContentType int `binding:"Required"`
162+
Secret string
163+
WebhookForm
164+
}
165+
166+
// Validate validates the fields
167+
func (f *NewGogshookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
168+
return validate(errs, ctx.Data, f, ctx.Locale)
169+
}
170+
157171
// NewSlackHookForm form for creating slack hook
158172
type NewSlackHookForm struct {
159173
PayloadURL string `binding:"Required;ValidUrl"`

modules/setting/setting.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2017 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -1325,7 +1326,7 @@ func newWebhookService() {
13251326
Webhook.QueueLength = sec.Key("QUEUE_LENGTH").MustInt(1000)
13261327
Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5)
13271328
Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool()
1328-
Webhook.Types = []string{"gogs", "slack"}
1329+
Webhook.Types = []string{"gitea", "gogs", "slack"}
13291330
Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10)
13301331
}
13311332

public/img/gogs.ico

28.5 KB
Binary file not shown.

routers/repo/webhook.go

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2017 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -120,6 +121,53 @@ func ParseHookEvent(form auth.WebhookForm) *models.HookEvent {
120121

121122
// WebHooksNewPost response for creating webhook
122123
func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
124+
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
125+
ctx.Data["PageIsSettingsHooks"] = true
126+
ctx.Data["PageIsSettingsHooksNew"] = true
127+
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
128+
ctx.Data["HookType"] = "gitea"
129+
130+
orCtx, err := getOrgRepoCtx(ctx)
131+
if err != nil {
132+
ctx.Handle(500, "getOrgRepoCtx", err)
133+
return
134+
}
135+
ctx.Data["BaseLink"] = orCtx.Link
136+
137+
if ctx.HasError() {
138+
ctx.HTML(200, orCtx.NewTemplate)
139+
return
140+
}
141+
142+
contentType := models.ContentTypeJSON
143+
if models.HookContentType(form.ContentType) == models.ContentTypeForm {
144+
contentType = models.ContentTypeForm
145+
}
146+
147+
w := &models.Webhook{
148+
RepoID: orCtx.RepoID,
149+
URL: form.PayloadURL,
150+
ContentType: contentType,
151+
Secret: form.Secret,
152+
HookEvent: ParseHookEvent(form.WebhookForm),
153+
IsActive: form.Active,
154+
HookTaskType: models.GITEA,
155+
OrgID: orCtx.OrgID,
156+
}
157+
if err := w.UpdateEvent(); err != nil {
158+
ctx.Handle(500, "UpdateEvent", err)
159+
return
160+
} else if err := models.CreateWebhook(w); err != nil {
161+
ctx.Handle(500, "CreateWebhook", err)
162+
return
163+
}
164+
165+
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
166+
ctx.Redirect(orCtx.Link + "/settings/hooks")
167+
}
168+
169+
// GogsHooksNewPost response for creating webhook
170+
func GogsHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
123171
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
124172
ctx.Data["PageIsSettingsHooks"] = true
125173
ctx.Data["PageIsSettingsHooksNew"] = true
@@ -150,7 +198,7 @@ func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
150198
Secret: form.Secret,
151199
HookEvent: ParseHookEvent(form.WebhookForm),
152200
IsActive: form.Active,
153-
HookTaskType: models.GOGS,
201+
HookTaskType: models.GITEA,
154202
OrgID: orCtx.OrgID,
155203
}
156204
if err := w.UpdateEvent(); err != nil {
@@ -245,8 +293,10 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
245293
case models.SLACK:
246294
ctx.Data["SlackHook"] = w.GetSlackHook()
247295
ctx.Data["HookType"] = "slack"
248-
default:
296+
case models.GOGS:
249297
ctx.Data["HookType"] = "gogs"
298+
default:
299+
ctx.Data["HookType"] = "gitea"
250300
}
251301

252302
ctx.Data["History"], err = w.History(1)
@@ -310,6 +360,45 @@ func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
310360
ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
311361
}
312362

363+
// GogsHooksEditPost response for editing gogs hook
364+
func GogsHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
365+
ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
366+
ctx.Data["PageIsSettingsHooks"] = true
367+
ctx.Data["PageIsSettingsHooksEdit"] = true
368+
369+
orCtx, w := checkWebhook(ctx)
370+
if ctx.Written() {
371+
return
372+
}
373+
ctx.Data["Webhook"] = w
374+
375+
if ctx.HasError() {
376+
ctx.HTML(200, orCtx.NewTemplate)
377+
return
378+
}
379+
380+
contentType := models.ContentTypeJSON
381+
if models.HookContentType(form.ContentType) == models.ContentTypeForm {
382+
contentType = models.ContentTypeForm
383+
}
384+
385+
w.URL = form.PayloadURL
386+
w.ContentType = contentType
387+
w.Secret = form.Secret
388+
w.HookEvent = ParseHookEvent(form.WebhookForm)
389+
w.IsActive = form.Active
390+
if err := w.UpdateEvent(); err != nil {
391+
ctx.Handle(500, "UpdateEvent", err)
392+
return
393+
} else if err := models.UpdateWebhook(w); err != nil {
394+
ctx.Handle(500, "GogsHooksEditPost", err)
395+
return
396+
}
397+
398+
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
399+
ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
400+
}
401+
313402
// SlackHooksEditPost response for editing slack hook
314403
func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
315404
ctx.Data["Title"] = ctx.Tr("repo.settings")

routers/routes/routes.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,12 @@ func RegisterRoutes(m *macaron.Macaron) {
379379
m.Get("", org.Webhooks)
380380
m.Post("/delete", org.DeleteWebhook)
381381
m.Get("/:type/new", repo.WebhooksNew)
382-
m.Post("/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
382+
m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
383+
m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
383384
m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
384385
m.Get("/:id", repo.WebHooksEdit)
385-
m.Post("/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
386+
m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
387+
m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksEditPost)
386388
m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
387389
})
388390

@@ -423,11 +425,13 @@ func RegisterRoutes(m *macaron.Macaron) {
423425
m.Get("", repo.Webhooks)
424426
m.Post("/delete", repo.DeleteWebhook)
425427
m.Get("/:type/new", repo.WebhooksNew)
426-
m.Post("/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
428+
m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
429+
m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
427430
m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
428431
m.Get("/:id", repo.WebHooksEdit)
429432
m.Post("/:id/test", repo.TestWebhook)
430-
m.Post("/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
433+
m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
434+
m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
431435
m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
432436

433437
m.Group("/git", func() {

templates/org/settings/hook_new.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
<h4 class="ui top attached header">
1010
{{if .PageIsSettingsHooksNew}}{{.i18n.Tr "repo.settings.add_webhook"}}{{else}}{{.i18n.Tr "repo.settings.update_webhook"}}{{end}}
1111
<div class="ui right">
12-
{{if eq .HookType "gogs"}}
12+
{{if eq .HookType "gitea"}}
1313
<img class="img-13" src="{{AppSubUrl}}/img/favicon.png">
14+
{{else if eq .HookType "gogs"}}
15+
<img class="img-13" src="{{AppSubUrl}}/img/gogs.ico">
1416
{{else if eq .HookType "slack"}}
1517
<img class="img-13" src="{{AppSubUrl}}/img/slack.png">
1618
{{end}}
1719
</div>
1820
</h4>
1921
<div class="ui attached segment">
22+
{{template "repo/settings/hook_gitea" .}}
2023
{{template "repo/settings/hook_gogs" .}}
2124
{{template "repo/settings/hook_slack" .}}
2225
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{{if eq .HookType "gitea"}}
2+
<p>{{.i18n.Tr "repo.settings.add_webhook_desc" "https://docs.gitea.io/features/webhook.html" | Str2html}}</p>
3+
<form class="ui form" action="{{.BaseLink}}/settings/hooks/gitea/{{if .PageIsSettingsHooksNew}}new{{else}}{{.Webhook.ID}}{{end}}" method="post">
4+
{{.CsrfTokenHtml}}
5+
<div class="required field {{if .Err_PayloadURL}}error{{end}}">
6+
<label for="payload_url">{{.i18n.Tr "repo.settings.payload_url"}}</label>
7+
<input id="payload_url" name="payload_url" type="url" value="{{.Webhook.URL}}" autofocus required>
8+
</div>
9+
<div class="field">
10+
<label>{{.i18n.Tr "repo.settings.content_type"}}</label>
11+
<div class="ui selection dropdown">
12+
<input type="hidden" id="content_type" name="content_type" value="{{if .Webhook.ContentType}}{{.Webhook.ContentType}}{{else}}application/json{{end}}">
13+
<div class="default text"></div>
14+
<i class="dropdown icon"></i>
15+
<div class="menu">
16+
<div class="item" data-value="1">application/json</div>
17+
<div class="item" data-value="2">application/x-www-form-urlencoded</div>
18+
</div>
19+
</div>
20+
</div>
21+
<input class="fake" type="password">
22+
<div class="field {{if .Err_Secret}}error{{end}}">
23+
<label for="secret">{{.i18n.Tr "repo.settings.secret"}}</label>
24+
<input id="secret" name="secret" type="password" value="{{.Webhook.Secret}}" autocomplete="off">
25+
</div>
26+
{{template "repo/settings/hook_settings" .}}
27+
</form>
28+
{{end}}

templates/repo/settings/hook_list.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
<div class="ui floating1 jump dropdown">
66
<div class="ui blue tiny button">{{.i18n.Tr "repo.settings.add_webhook"}}</div>
77
<div class="menu">
8-
<a class="item" href="{{.BaseLink}}/settings/hooks/gogs/new">
8+
<a class="item" href="{{.BaseLink}}/settings/hooks/gitea/new">
99
<img class="img-10" src="{{AppSubUrl}}/img/favicon.png">Gitea
1010
</a>
11+
<a class="item" href="{{.BaseLink}}/settings/hooks/gogs/new">
12+
<img class="img-10" src="{{AppSubUrl}}/img/gogs.ico">Gogs
13+
</a>
1114
<a class="item" href="{{.BaseLink}}/settings/hooks/slack/new">
1215
<img class="img-10" src="{{AppSubUrl}}/img/slack.png">Slack
1316
</a>

templates/repo/settings/hook_new.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
<h4 class="ui top attached header">
88
{{if .PageIsSettingsHooksNew}}{{.i18n.Tr "repo.settings.add_webhook"}}{{else}}{{.i18n.Tr "repo.settings.update_webhook"}}{{end}}
99
<div class="ui right">
10-
{{if eq .HookType "gogs"}}
10+
{{if eq .HookType "gitea"}}
1111
<img class="img-13" src="{{AppSubUrl}}/img/favicon.png">
12+
{{else if eq .HookType "gogs"}}
13+
<img class="img-13" src="{{AppSubUrl}}/img/gogs.ico">
1214
{{else if eq .HookType "slack"}}
1315
<img class="img-13" src="{{AppSubUrl}}/img/slack.png">
1416
{{end}}
1517
</div>
1618
</h4>
1719
<div class="ui attached segment">
20+
{{template "repo/settings/hook_gitea" .}}
1821
{{template "repo/settings/hook_gogs" .}}
1922
{{template "repo/settings/hook_slack" .}}
2023
</div>

0 commit comments

Comments
 (0)