-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
281 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.