Skip to content

Commit

Permalink
Support systeml hook API
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Feb 5, 2021
1 parent 32c4563 commit 6876c9b
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 80 deletions.
80 changes: 0 additions & 80 deletions models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package models
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

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

// GetDefaultWebhooks returns all admin-default webhooks.
func GetDefaultWebhooks() ([]*Webhook, error) {
return getDefaultWebhooks(x)
}

func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
webhooks := make([]*Webhook, 0, 5)
return webhooks, e.
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
Find(&webhooks)
}

// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
webhook := &Webhook{ID: id}
has, err := x.
Where("repo_id=? AND org_id=?", 0, 0).
Get(webhook)
if err != nil {
return nil, err
} else if !has {
return nil, ErrWebhookNotExist{id}
}
return webhook, nil
}

// GetSystemWebhooks returns all admin system webhooks.
func GetSystemWebhooks() ([]*Webhook, error) {
return getSystemWebhooks(x)
}

func getSystemWebhooks(e Engine) ([]*Webhook, error) {
webhooks := make([]*Webhook, 0, 5)
return webhooks, e.
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
Find(&webhooks)
}

// UpdateWebhook updates information of webhook.
func UpdateWebhook(w *Webhook) error {
_, err := x.ID(w.ID).AllCols().Update(w)
Expand Down Expand Up @@ -507,47 +468,6 @@ func DeleteWebhookByOrgID(orgID, id int64) error {
})
}

// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
func DeleteDefaultSystemWebhook(id int64) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

count, err := sess.
Where("repo_id=? AND org_id=?", 0, 0).
Delete(&Webhook{ID: id})
if err != nil {
return err
} else if count == 0 {
return ErrWebhookNotExist{ID: id}
}

if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
return err
}

return sess.Commit()
}

// copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
ws, err := getDefaultWebhooks(e)
if err != nil {
return fmt.Errorf("GetDefaultWebhooks: %v", err)
}

for _, w := range ws {
w.ID = 0
w.RepoID = repoID
if err := createWebhook(e, w); err != nil {
return fmt.Errorf("CreateWebhook: %v", err)
}
}
return nil
}

// ___ ___ __ ___________ __
// / | \ ____ ____ | | _\__ ___/____ _____| | __
// / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
Expand Down
86 changes: 86 additions & 0 deletions models/webhook_system.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2021 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.

package models

import "fmt"

// GetDefaultWebhooks returns all admin-default webhooks.
func GetDefaultWebhooks() ([]*Webhook, error) {
return getDefaultWebhooks(x)
}

func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
webhooks := make([]*Webhook, 0, 5)
return webhooks, e.
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
Find(&webhooks)
}

// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
webhook := &Webhook{ID: id}
has, err := x.
Where("repo_id=? AND org_id=?", 0, 0).
Get(webhook)
if err != nil {
return nil, err
} else if !has {
return nil, ErrWebhookNotExist{id}
}
return webhook, nil
}

// GetSystemWebhooks returns all admin system webhooks.
func GetSystemWebhooks() ([]*Webhook, error) {
return getSystemWebhooks(x)
}

func getSystemWebhooks(e Engine) ([]*Webhook, error) {
webhooks := make([]*Webhook, 0, 5)
return webhooks, e.
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
Find(&webhooks)
}

// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
func DeleteDefaultSystemWebhook(id int64) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

count, err := sess.
Where("repo_id=? AND org_id=?", 0, 0).
Delete(&Webhook{ID: id})
if err != nil {
return err
} else if count == 0 {
return ErrWebhookNotExist{ID: id}
}

if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
return err
}

return sess.Commit()
}

// copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
ws, err := getDefaultWebhooks(e)
if err != nil {
return fmt.Errorf("GetDefaultWebhooks: %v", err)
}

for _, w := range ws {
w.ID = 0
w.RepoID = repoID
if err := createWebhook(e, w); err != nil {
return fmt.Errorf("CreateWebhook: %v", err)
}
}
return nil
}
163 changes: 163 additions & 0 deletions routers/api/v1/admin/hooks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright 2021 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.

package org

import (
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/utils"
)

// ListHooks list system's webhooks
func ListHooks(ctx *context.APIContext) {
// swagger:operation GET /admin/hooks admin adminListHooks
// ---
// summary: List system's webhooks
// produces:
// - application/json
// parameters:
// - name: page
// in: query
// description: page number of results to return (1-based)
// type: integer
// - name: limit
// in: query
// description: page size of results
// type: integer
// responses:
// "200":
// "$ref": "#/responses/HookList"

sysHooks, err := models.GetSystemWebhooks(utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetSystemWebhooks", err)
return
}
hooks := make([]*api.Hook, len(sysHooks))
for i, hook := range sysHooks {
hooks[i] = convert.ToHook(setting.AppURL+"/admin", hook)
}
ctx.JSON(http.StatusOK, hooks)
}

// GetHook get an organization's hook by id
func GetHook(ctx *context.APIContext) {
// swagger:operation GET /hooks/{id} admin adminGetHook
// ---
// summary: Get a hook
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: id of the hook to get
// type: integer
// format: int64
// required: true
// responses:
// "200":
// "$ref": "#/responses/Hook"

hookID := ctx.ParamsInt64(":id")
hook, err := models.GetSystemOrDefaultWebhook(hookID)
if err != nil {
return
}
ctx.JSON(http.StatusOK, convert.ToHook("/admin/", hook))
}

// CreateHook create a hook for an organization
func CreateHook(ctx *context.APIContext) {
// swagger:operation POST /admin/hooks/ admin adminCreateHook
// ---
// summary: Create a hook
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: body
// in: body
// required: true
// schema:
// "$ref": "#/definitions/CreateHookOption"
// responses:
// "201":
// "$ref": "#/responses/Hook"

form := web.GetForm(ctx).(*api.CreateHookOption)
//TODO in body params
if !utils.CheckCreateHookOption(ctx, form) {
return
}
utils.AddSystemHook(ctx, form)
}

// EditHook modify a hook of a repository
func EditHook(ctx *context.APIContext) {
// swagger:operation PATCH /admin/hooks/{id} admin adminEditHook
// ---
// summary: Update a hook
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: id of the hook to update
// type: integer
// format: int64
// required: true
// - name: body
// in: body
// schema:
// "$ref": "#/definitions/EditHookOption"
// responses:
// "200":
// "$ref": "#/responses/Hook"

form := web.GetForm(ctx).(*api.EditHookOption)

//TODO in body params
hookID := ctx.ParamsInt64(":id")
utils.EditSystemHook(ctx, form, hookID)
}

// DeleteHook delete a system hook
func DeleteHook(ctx *context.APIContext) {
// swagger:operation DELETE /amdin/hooks/{id} admin adminDeleteHook
// ---
// summary: Delete a hook
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: id of the hook to delete
// type: integer
// format: int64
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"

hookID := ctx.ParamsInt64(":id")
if err := models.DeleteDefaultSystemWebhook(hookID); err != nil {
if models.IsErrWebhookNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "DeleteDefaultSystemWebhook", err)
}
return
}
ctx.Status(http.StatusNoContent)
}
7 changes: 7 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,13 @@ func Routes() *web.Route {
m.Post("/{username}/{reponame}", admin.AdoptRepository)
m.Delete("/{username}/{reponame}", admin.DeleteUnadoptedRepository)
})
m.Group("/hooks", func() {
m.Combo("").Get(admin.ListHooks).
Post(bind(api.CreateHookOption{}), admin.CreateHook)
m.Combo("/{id}").Get(admin.GetHook).
Patch(bind(admin.EditHookOption{}), admin.EditHook).
Delete(admin.DeleteHook)
})
}, reqToken(), reqSiteAdmin())

m.Group("/topics", func() {
Expand Down
Loading

0 comments on commit 6876c9b

Please sign in to comment.