2023-01-29 02:12:10 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2024-03-02 16:42:31 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2023-01-29 02:12:10 +08:00
|
|
|
)
|
|
|
|
|
Let API create and edit system webhooks, attempt 2 (#33180)
This PR fixes inconsistencies between system and default webhooks in the
Gitea API. (See also #26418)
- A system webhook is a webhook that captures events for all
repositories.
- A default webhook is copied to a new repository when it is created.
Before this PR `POST /api/v1/admin/hooks/` creates default webhooks (if
not configured otherwise) and `GET /api/v1/admin/hooks/` returns system
webhooks.
The PR introduces an optional query parameter to `GET
/api/v1/admin/hooks/` to enable selecting if either default, system or
both kind of webhooks should be retrieved. By default the flag is set to
return system webhooks keep current behaviour.
## Examples
### System Webhooks
#### Create
```
POST /api/v1/admin/hooks/
{
"type": "gitea",
"active": false,
"branch_filter": "*",
"events": [ "create", "..." ],
"config": {
"url": "http://...",
"content_type": "json",
"secret": "secret",
"is_system_webhook": true // <-- controls hook type
}
}
```
#### List
```
GET/api/v1/admin/hooks?type=system //type argument is optional here since it's the default
```
#### Others
The other relevant endpoints work as expected by referencing the hook by
id
```
GET /api/v1/admin/hooks/:id
PATCH /api/v1/admin/hooks/:id
DELETE /api/v1/admin/hooks/:id
```
### Default Webhooks
#### Create
```
POST /api/v1/admin/hooks/
{
"type": "gitea",
"active": false,
"branch_filter": "*",
"events": [ "create", "..." ],
"config": {
"url": "http://...",
"content_type": "json",
"secret": "secret",
"is_system_webhook": false // optional, as false is the default value
}
}
```
#### List
```
GET/api/v1/admin/hooks?type=default
```
#### Others
The other relevant endpoints work as expected by referencing the hook by
id
```
GET /api/v1/admin/hooks/:id
PATCH /api/v1/admin/hooks/:id
DELETE /api/v1/admin/hooks/:id
```
2025-01-13 18:17:39 +01:00
|
|
|
// GetSystemOrDefaultWebhooks returns webhooks by given argument or all if argument is missing.
|
|
|
|
func GetSystemOrDefaultWebhooks(ctx context.Context, isSystemWebhook optional.Option[bool]) ([]*Webhook, error) {
|
|
|
|
webhooks := make([]*Webhook, 0, 5)
|
|
|
|
if !isSystemWebhook.Has() {
|
|
|
|
return webhooks, db.GetEngine(ctx).Where("repo_id=? AND owner_id=?", 0, 0).
|
|
|
|
Find(&webhooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
return webhooks, db.GetEngine(ctx).
|
|
|
|
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, isSystemWebhook.Value()).
|
|
|
|
Find(&webhooks)
|
|
|
|
}
|
|
|
|
|
2023-01-29 02:12:10 +08:00
|
|
|
// GetDefaultWebhooks returns all admin-default webhooks.
|
|
|
|
func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
|
|
|
|
webhooks := make([]*Webhook, 0, 5)
|
|
|
|
return webhooks, db.GetEngine(ctx).
|
2023-03-10 15:28:32 +01:00
|
|
|
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, false).
|
2023-01-29 02:12:10 +08:00
|
|
|
Find(&webhooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
|
|
|
|
func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error) {
|
|
|
|
webhook := &Webhook{ID: id}
|
|
|
|
has, err := db.GetEngine(ctx).
|
2023-03-10 15:28:32 +01:00
|
|
|
Where("repo_id=? AND owner_id=?", 0, 0).
|
2023-01-29 02:12:10 +08:00
|
|
|
Get(webhook)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrWebhookNotExist{ID: id}
|
|
|
|
}
|
|
|
|
return webhook, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSystemWebhooks returns all admin system webhooks.
|
2024-03-02 16:42:31 +01:00
|
|
|
func GetSystemWebhooks(ctx context.Context, isActive optional.Option[bool]) ([]*Webhook, error) {
|
2023-01-29 02:12:10 +08:00
|
|
|
webhooks := make([]*Webhook, 0, 5)
|
2024-03-02 16:42:31 +01:00
|
|
|
if !isActive.Has() {
|
2023-01-29 02:12:10 +08:00
|
|
|
return webhooks, db.GetEngine(ctx).
|
2023-03-10 15:28:32 +01:00
|
|
|
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, true).
|
2023-01-29 02:12:10 +08:00
|
|
|
Find(&webhooks)
|
|
|
|
}
|
|
|
|
return webhooks, db.GetEngine(ctx).
|
2024-03-02 16:42:31 +01:00
|
|
|
Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, true, isActive.Value()).
|
2023-01-29 02:12:10 +08:00
|
|
|
Find(&webhooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
|
|
|
|
func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error {
|
|
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
count, err := db.GetEngine(ctx).
|
2023-03-10 15:28:32 +01:00
|
|
|
Where("repo_id=? AND owner_id=?", 0, 0).
|
2023-01-29 02:12:10 +08:00
|
|
|
Delete(&Webhook{ID: id})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if count == 0 {
|
|
|
|
return ErrWebhookNotExist{ID: id}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.DeleteByBean(ctx, &HookTask{HookID: id})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
|
|
|
|
func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error {
|
|
|
|
ws, err := GetDefaultWebhooks(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetDefaultWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range ws {
|
|
|
|
w.ID = 0
|
|
|
|
w.RepoID = repoID
|
|
|
|
if err := CreateWebhook(ctx, w); err != nil {
|
|
|
|
return fmt.Errorf("CreateWebhook: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|