Skip to content

Commit c7cf99b

Browse files
committed
Organization webhook API endpoints
1 parent fa3abc2 commit c7cf99b

File tree

4 files changed

+316
-134
lines changed

4 files changed

+316
-134
lines changed

routers/api/v1/api.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,8 @@ func RegisterRoutes(m *macaron.Macaron) {
266266
m.Group("/hooks", func() {
267267
m.Combo("").Get(repo.ListHooks).
268268
Post(bind(api.CreateHookOption{}), repo.CreateHook)
269-
m.Combo("/:id").Patch(bind(api.EditHookOption{}), repo.EditHook).
269+
m.Combo("/:id").Get(repo.GetHook).
270+
Patch(bind(api.EditHookOption{}), repo.EditHook).
270271
Delete(repo.DeleteHook)
271272
})
272273
m.Put("/collaborators/:collaborator", bind(api.AddCollaboratorOption{}), repo.AddCollaborator)
@@ -343,6 +344,13 @@ func RegisterRoutes(m *macaron.Macaron) {
343344
m.Group("/orgs/:orgname", func() {
344345
m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
345346
m.Combo("/teams").Get(org.ListTeams)
347+
m.Group("/hooks", func() {
348+
m.Combo("").Get(org.ListHooks).
349+
Post(bind(api.CreateHookOption{}), org.CreateHook)
350+
m.Combo("/:id").Get(org.GetHook).
351+
Patch(bind(api.EditHookOption{}), org.EditHook).
352+
Delete(org.DeleteHook)
353+
})
346354
}, orgAssignment(true))
347355

348356
m.Any("/*", func(ctx *context.Context) {

routers/api/v1/org/hook.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2016 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package org
6+
7+
import (
8+
api "code.gitea.io/sdk/gitea"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/routers/api/v1/convert"
13+
"code.gitea.io/gitea/routers/api/v1/utils"
14+
)
15+
16+
// ListHooks list an organziation's webhooks
17+
func ListHooks(ctx *context.APIContext) {
18+
org := ctx.Org.Organization
19+
orgHooks, err := models.GetWebhooksByOrgID(org.ID)
20+
if err != nil {
21+
ctx.Error(500, "GetWebhooksByOrgID", err)
22+
return
23+
}
24+
hooks := make([]*api.Hook, len(orgHooks))
25+
for i, hook := range orgHooks {
26+
hooks[i] = convert.ToHook(org.HomeLink(), hook)
27+
}
28+
ctx.JSON(200, hooks)
29+
}
30+
31+
// GetHook get an organization's hook by id
32+
func GetHook(ctx *context.APIContext) {
33+
org := ctx.Org.Organization
34+
hookID := ctx.ParamsInt64(":id")
35+
hook, err := utils.GetOrgHook(ctx, org.ID, hookID)
36+
if err != nil {
37+
return
38+
}
39+
ctx.JSON(200, convert.ToHook(org.HomeLink(), hook))
40+
}
41+
42+
// CreateHook create a hook for an organization
43+
func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
44+
if !utils.CheckCreateHookOption(ctx, &form) {
45+
return
46+
}
47+
utils.AddOrgHook(ctx, &form)
48+
}
49+
50+
// EditHook modify a hook of a repository
51+
func EditHook(ctx *context.APIContext, form api.EditHookOption) {
52+
hookID := ctx.ParamsInt64(":id")
53+
utils.EditOrgHook(ctx, &form, hookID)
54+
}
55+
56+
// DeleteHook delete a hook of an organization
57+
func DeleteHook(ctx *context.APIContext) {
58+
org := ctx.Org.Organization
59+
hookID := ctx.ParamsInt64(":id")
60+
if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
61+
ctx.Error(500, "DeleteWebhookByOrgID", err)
62+
return
63+
}
64+
ctx.Status(204)
65+
}

routers/api/v1/repo/hook.go

Lines changed: 15 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
package repo
66

77
import (
8-
"encoding/json"
9-
10-
"github.com/Unknwon/com"
11-
128
api "code.gitea.io/sdk/gitea"
139

1410
"code.gitea.io/gitea/models"
1511
"code.gitea.io/gitea/modules/context"
1612
"code.gitea.io/gitea/routers/api/v1/convert"
13+
"code.gitea.io/gitea/routers/api/v1/utils"
1714
)
1815

1916
// ListHooks list all hooks of a repository
@@ -32,146 +29,31 @@ func ListHooks(ctx *context.APIContext) {
3229
ctx.JSON(200, &apiHooks)
3330
}
3431

35-
// CreateHook create a hook for a repository
36-
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
37-
func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
38-
if !models.IsValidHookTaskType(form.Type) {
39-
ctx.Error(422, "", "Invalid hook type")
40-
return
41-
}
42-
for _, name := range []string{"url", "content_type"} {
43-
if _, ok := form.Config[name]; !ok {
44-
ctx.Error(422, "", "Missing config option: "+name)
45-
return
46-
}
47-
}
48-
if !models.IsValidHookContentType(form.Config["content_type"]) {
49-
ctx.Error(422, "", "Invalid content type")
32+
// GetHook get a repo's hook by id
33+
func GetHook(ctx *context.APIContext) {
34+
repo := ctx.Repo
35+
hookID := ctx.ParamsInt64(":id")
36+
hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
37+
if err != nil {
5038
return
5139
}
40+
ctx.JSON(200, convert.ToHook(repo.RepoLink, hook))
41+
}
5242

53-
if len(form.Events) == 0 {
54-
form.Events = []string{"push"}
55-
}
56-
w := &models.Webhook{
57-
RepoID: ctx.Repo.Repository.ID,
58-
URL: form.Config["url"],
59-
ContentType: models.ToHookContentType(form.Config["content_type"]),
60-
Secret: form.Config["secret"],
61-
HookEvent: &models.HookEvent{
62-
ChooseEvents: true,
63-
HookEvents: models.HookEvents{
64-
Create: com.IsSliceContainsStr(form.Events, string(models.HookEventCreate)),
65-
Push: com.IsSliceContainsStr(form.Events, string(models.HookEventPush)),
66-
PullRequest: com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest)),
67-
},
68-
},
69-
IsActive: form.Active,
70-
HookTaskType: models.ToHookTaskType(form.Type),
71-
}
72-
if w.HookTaskType == models.SLACK {
73-
channel, ok := form.Config["channel"]
74-
if !ok {
75-
ctx.Error(422, "", "Missing config option: channel")
76-
return
77-
}
78-
meta, err := json.Marshal(&models.SlackMeta{
79-
Channel: channel,
80-
Username: form.Config["username"],
81-
IconURL: form.Config["icon_url"],
82-
Color: form.Config["color"],
83-
})
84-
if err != nil {
85-
ctx.Error(500, "slack: JSON marshal failed", err)
86-
return
87-
}
88-
w.Meta = string(meta)
89-
}
90-
91-
if err := w.UpdateEvent(); err != nil {
92-
ctx.Error(500, "UpdateEvent", err)
93-
return
94-
} else if err := models.CreateWebhook(w); err != nil {
95-
ctx.Error(500, "CreateWebhook", err)
43+
// CreateHook create a hook for a repository
44+
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
45+
func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
46+
if !utils.CheckCreateHookOption(ctx, &form) {
9647
return
9748
}
98-
99-
ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w))
49+
utils.AddRepoHook(ctx, &form)
10050
}
10151

10252
// EditHook modify a hook of a repository
10353
// see https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
10454
func EditHook(ctx *context.APIContext, form api.EditHookOption) {
10555
hookID := ctx.ParamsInt64(":id")
106-
w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
107-
if err != nil {
108-
if models.IsErrWebhookNotExist(err) {
109-
ctx.Status(404)
110-
} else {
111-
ctx.Error(500, "GetWebhookByID", err)
112-
}
113-
return
114-
}
115-
116-
if form.Config != nil {
117-
if url, ok := form.Config["url"]; ok {
118-
w.URL = url
119-
}
120-
if ct, ok := form.Config["content_type"]; ok {
121-
if !models.IsValidHookContentType(ct) {
122-
ctx.Error(422, "", "Invalid content type")
123-
return
124-
}
125-
w.ContentType = models.ToHookContentType(ct)
126-
}
127-
128-
if w.HookTaskType == models.SLACK {
129-
if channel, ok := form.Config["channel"]; ok {
130-
meta, err := json.Marshal(&models.SlackMeta{
131-
Channel: channel,
132-
Username: form.Config["username"],
133-
IconURL: form.Config["icon_url"],
134-
Color: form.Config["color"],
135-
})
136-
if err != nil {
137-
ctx.Error(500, "slack: JSON marshal failed", err)
138-
return
139-
}
140-
w.Meta = string(meta)
141-
}
142-
}
143-
}
144-
145-
// Update events
146-
if len(form.Events) == 0 {
147-
form.Events = []string{"push"}
148-
}
149-
w.PushOnly = false
150-
w.SendEverything = false
151-
w.ChooseEvents = true
152-
w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
153-
w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
154-
w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
155-
if err = w.UpdateEvent(); err != nil {
156-
ctx.Error(500, "UpdateEvent", err)
157-
return
158-
}
159-
160-
if form.Active != nil {
161-
w.IsActive = *form.Active
162-
}
163-
164-
if err := models.UpdateWebhook(w); err != nil {
165-
ctx.Error(500, "UpdateWebhook", err)
166-
return
167-
}
168-
169-
updated, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
170-
if err != nil {
171-
ctx.Error(500, "GetWebhookByRepoID", err)
172-
return
173-
}
174-
ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, updated))
56+
utils.EditRepoHook(ctx, &form, hookID)
17557
}
17658

17759
// DeleteHook delete a hook of a repository

0 commit comments

Comments
 (0)