Skip to content

Commit 6876c9b

Browse files
committed
Support systeml hook API
1 parent 32c4563 commit 6876c9b

File tree

5 files changed

+281
-80
lines changed

5 files changed

+281
-80
lines changed

models/webhook.go

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package models
88
import (
99
"context"
1010
"encoding/json"
11-
"fmt"
1211
"strings"
1312
"time"
1413

@@ -421,44 +420,6 @@ func GetWebhooksByOrgID(orgID int64, listOptions ListOptions) ([]*Webhook, error
421420
return ws, sess.Find(&ws, &Webhook{OrgID: orgID})
422421
}
423422

424-
// GetDefaultWebhooks returns all admin-default webhooks.
425-
func GetDefaultWebhooks() ([]*Webhook, error) {
426-
return getDefaultWebhooks(x)
427-
}
428-
429-
func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
430-
webhooks := make([]*Webhook, 0, 5)
431-
return webhooks, e.
432-
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
433-
Find(&webhooks)
434-
}
435-
436-
// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
437-
func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
438-
webhook := &Webhook{ID: id}
439-
has, err := x.
440-
Where("repo_id=? AND org_id=?", 0, 0).
441-
Get(webhook)
442-
if err != nil {
443-
return nil, err
444-
} else if !has {
445-
return nil, ErrWebhookNotExist{id}
446-
}
447-
return webhook, nil
448-
}
449-
450-
// GetSystemWebhooks returns all admin system webhooks.
451-
func GetSystemWebhooks() ([]*Webhook, error) {
452-
return getSystemWebhooks(x)
453-
}
454-
455-
func getSystemWebhooks(e Engine) ([]*Webhook, error) {
456-
webhooks := make([]*Webhook, 0, 5)
457-
return webhooks, e.
458-
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
459-
Find(&webhooks)
460-
}
461-
462423
// UpdateWebhook updates information of webhook.
463424
func UpdateWebhook(w *Webhook) error {
464425
_, err := x.ID(w.ID).AllCols().Update(w)
@@ -507,47 +468,6 @@ func DeleteWebhookByOrgID(orgID, id int64) error {
507468
})
508469
}
509470

510-
// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
511-
func DeleteDefaultSystemWebhook(id int64) error {
512-
sess := x.NewSession()
513-
defer sess.Close()
514-
if err := sess.Begin(); err != nil {
515-
return err
516-
}
517-
518-
count, err := sess.
519-
Where("repo_id=? AND org_id=?", 0, 0).
520-
Delete(&Webhook{ID: id})
521-
if err != nil {
522-
return err
523-
} else if count == 0 {
524-
return ErrWebhookNotExist{ID: id}
525-
}
526-
527-
if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
528-
return err
529-
}
530-
531-
return sess.Commit()
532-
}
533-
534-
// copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
535-
func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
536-
ws, err := getDefaultWebhooks(e)
537-
if err != nil {
538-
return fmt.Errorf("GetDefaultWebhooks: %v", err)
539-
}
540-
541-
for _, w := range ws {
542-
w.ID = 0
543-
w.RepoID = repoID
544-
if err := createWebhook(e, w); err != nil {
545-
return fmt.Errorf("CreateWebhook: %v", err)
546-
}
547-
}
548-
return nil
549-
}
550-
551471
// ___ ___ __ ___________ __
552472
// / | \ ____ ____ | | _\__ ___/____ _____| | __
553473
// / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /

models/webhook_system.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2021 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 models
6+
7+
import "fmt"
8+
9+
// GetDefaultWebhooks returns all admin-default webhooks.
10+
func GetDefaultWebhooks() ([]*Webhook, error) {
11+
return getDefaultWebhooks(x)
12+
}
13+
14+
func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
15+
webhooks := make([]*Webhook, 0, 5)
16+
return webhooks, e.
17+
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
18+
Find(&webhooks)
19+
}
20+
21+
// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
22+
func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
23+
webhook := &Webhook{ID: id}
24+
has, err := x.
25+
Where("repo_id=? AND org_id=?", 0, 0).
26+
Get(webhook)
27+
if err != nil {
28+
return nil, err
29+
} else if !has {
30+
return nil, ErrWebhookNotExist{id}
31+
}
32+
return webhook, nil
33+
}
34+
35+
// GetSystemWebhooks returns all admin system webhooks.
36+
func GetSystemWebhooks() ([]*Webhook, error) {
37+
return getSystemWebhooks(x)
38+
}
39+
40+
func getSystemWebhooks(e Engine) ([]*Webhook, error) {
41+
webhooks := make([]*Webhook, 0, 5)
42+
return webhooks, e.
43+
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
44+
Find(&webhooks)
45+
}
46+
47+
// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
48+
func DeleteDefaultSystemWebhook(id int64) error {
49+
sess := x.NewSession()
50+
defer sess.Close()
51+
if err := sess.Begin(); err != nil {
52+
return err
53+
}
54+
55+
count, err := sess.
56+
Where("repo_id=? AND org_id=?", 0, 0).
57+
Delete(&Webhook{ID: id})
58+
if err != nil {
59+
return err
60+
} else if count == 0 {
61+
return ErrWebhookNotExist{ID: id}
62+
}
63+
64+
if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
65+
return err
66+
}
67+
68+
return sess.Commit()
69+
}
70+
71+
// copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
72+
func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
73+
ws, err := getDefaultWebhooks(e)
74+
if err != nil {
75+
return fmt.Errorf("GetDefaultWebhooks: %v", err)
76+
}
77+
78+
for _, w := range ws {
79+
w.ID = 0
80+
w.RepoID = repoID
81+
if err := createWebhook(e, w); err != nil {
82+
return fmt.Errorf("CreateWebhook: %v", err)
83+
}
84+
}
85+
return nil
86+
}

routers/api/v1/admin/hooks.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright 2021 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+
"net/http"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/modules/convert"
13+
"code.gitea.io/gitea/modules/setting"
14+
api "code.gitea.io/gitea/modules/structs"
15+
"code.gitea.io/gitea/modules/web"
16+
"code.gitea.io/gitea/routers/api/v1/utils"
17+
)
18+
19+
// ListHooks list system's webhooks
20+
func ListHooks(ctx *context.APIContext) {
21+
// swagger:operation GET /admin/hooks admin adminListHooks
22+
// ---
23+
// summary: List system's webhooks
24+
// produces:
25+
// - application/json
26+
// parameters:
27+
// - name: page
28+
// in: query
29+
// description: page number of results to return (1-based)
30+
// type: integer
31+
// - name: limit
32+
// in: query
33+
// description: page size of results
34+
// type: integer
35+
// responses:
36+
// "200":
37+
// "$ref": "#/responses/HookList"
38+
39+
sysHooks, err := models.GetSystemWebhooks(utils.GetListOptions(ctx))
40+
if err != nil {
41+
ctx.Error(http.StatusInternalServerError, "GetSystemWebhooks", err)
42+
return
43+
}
44+
hooks := make([]*api.Hook, len(sysHooks))
45+
for i, hook := range sysHooks {
46+
hooks[i] = convert.ToHook(setting.AppURL+"/admin", hook)
47+
}
48+
ctx.JSON(http.StatusOK, hooks)
49+
}
50+
51+
// GetHook get an organization's hook by id
52+
func GetHook(ctx *context.APIContext) {
53+
// swagger:operation GET /hooks/{id} admin adminGetHook
54+
// ---
55+
// summary: Get a hook
56+
// produces:
57+
// - application/json
58+
// parameters:
59+
// - name: id
60+
// in: path
61+
// description: id of the hook to get
62+
// type: integer
63+
// format: int64
64+
// required: true
65+
// responses:
66+
// "200":
67+
// "$ref": "#/responses/Hook"
68+
69+
hookID := ctx.ParamsInt64(":id")
70+
hook, err := models.GetSystemOrDefaultWebhook(hookID)
71+
if err != nil {
72+
return
73+
}
74+
ctx.JSON(http.StatusOK, convert.ToHook("/admin/", hook))
75+
}
76+
77+
// CreateHook create a hook for an organization
78+
func CreateHook(ctx *context.APIContext) {
79+
// swagger:operation POST /admin/hooks/ admin adminCreateHook
80+
// ---
81+
// summary: Create a hook
82+
// consumes:
83+
// - application/json
84+
// produces:
85+
// - application/json
86+
// parameters:
87+
// - name: body
88+
// in: body
89+
// required: true
90+
// schema:
91+
// "$ref": "#/definitions/CreateHookOption"
92+
// responses:
93+
// "201":
94+
// "$ref": "#/responses/Hook"
95+
96+
form := web.GetForm(ctx).(*api.CreateHookOption)
97+
//TODO in body params
98+
if !utils.CheckCreateHookOption(ctx, form) {
99+
return
100+
}
101+
utils.AddSystemHook(ctx, form)
102+
}
103+
104+
// EditHook modify a hook of a repository
105+
func EditHook(ctx *context.APIContext) {
106+
// swagger:operation PATCH /admin/hooks/{id} admin adminEditHook
107+
// ---
108+
// summary: Update a hook
109+
// consumes:
110+
// - application/json
111+
// produces:
112+
// - application/json
113+
// parameters:
114+
// - name: id
115+
// in: path
116+
// description: id of the hook to update
117+
// type: integer
118+
// format: int64
119+
// required: true
120+
// - name: body
121+
// in: body
122+
// schema:
123+
// "$ref": "#/definitions/EditHookOption"
124+
// responses:
125+
// "200":
126+
// "$ref": "#/responses/Hook"
127+
128+
form := web.GetForm(ctx).(*api.EditHookOption)
129+
130+
//TODO in body params
131+
hookID := ctx.ParamsInt64(":id")
132+
utils.EditSystemHook(ctx, form, hookID)
133+
}
134+
135+
// DeleteHook delete a system hook
136+
func DeleteHook(ctx *context.APIContext) {
137+
// swagger:operation DELETE /amdin/hooks/{id} admin adminDeleteHook
138+
// ---
139+
// summary: Delete a hook
140+
// produces:
141+
// - application/json
142+
// parameters:
143+
// - name: id
144+
// in: path
145+
// description: id of the hook to delete
146+
// type: integer
147+
// format: int64
148+
// required: true
149+
// responses:
150+
// "204":
151+
// "$ref": "#/responses/empty"
152+
153+
hookID := ctx.ParamsInt64(":id")
154+
if err := models.DeleteDefaultSystemWebhook(hookID); err != nil {
155+
if models.IsErrWebhookNotExist(err) {
156+
ctx.NotFound()
157+
} else {
158+
ctx.Error(http.StatusInternalServerError, "DeleteDefaultSystemWebhook", err)
159+
}
160+
return
161+
}
162+
ctx.Status(http.StatusNoContent)
163+
}

routers/api/v1/api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,13 @@ func Routes() *web.Route {
10281028
m.Post("/{username}/{reponame}", admin.AdoptRepository)
10291029
m.Delete("/{username}/{reponame}", admin.DeleteUnadoptedRepository)
10301030
})
1031+
m.Group("/hooks", func() {
1032+
m.Combo("").Get(admin.ListHooks).
1033+
Post(bind(api.CreateHookOption{}), admin.CreateHook)
1034+
m.Combo("/{id}").Get(admin.GetHook).
1035+
Patch(bind(admin.EditHookOption{}), admin.EditHook).
1036+
Delete(admin.DeleteHook)
1037+
})
10311038
}, reqToken(), reqSiteAdmin())
10321039

10331040
m.Group("/topics", func() {

0 commit comments

Comments
 (0)