2019-11-02 06:51:22 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-02 06:51:22 +08:00
|
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-20 22:08:52 +08:00
|
|
|
"context"
|
2023-07-28 23:16:48 +05:30
|
|
|
"errors"
|
2019-11-02 06:51:22 +08:00
|
|
|
"fmt"
|
2024-03-07 23:18:38 +01:00
|
|
|
"net/http"
|
2019-11-02 06:51:22 +08:00
|
|
|
|
2023-11-24 11:49:41 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-10-21 18:21:56 +02:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-10 13:13:16 +08:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2019-11-02 06:51:22 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2025-09-14 02:01:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/glob"
|
2022-04-26 02:03:01 +08:00
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-11-02 06:51:22 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-03-02 16:42:31 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2022-04-26 02:03:01 +08:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-11-02 06:51:22 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-08-12 14:43:08 +02:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-01-01 16:23:15 +01:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2019-11-02 06:51:22 +08:00
|
|
|
)
|
|
|
|
|
|
2025-01-29 19:09:48 -08:00
|
|
|
type Requester func(context.Context, *webhook_model.Webhook, *webhook_model.HookTask) (req *http.Request, body []byte, err error)
|
|
|
|
|
|
|
|
|
|
var webhookRequesters = map[webhook_module.HookType]Requester{}
|
|
|
|
|
|
|
|
|
|
func RegisterWebhookRequester(hookType webhook_module.HookType, requester Requester) {
|
|
|
|
|
webhookRequesters[hookType] = requester
|
2022-01-20 18:46:10 +01:00
|
|
|
}
|
2020-12-08 18:41:14 +08:00
|
|
|
|
|
|
|
|
// IsValidHookTaskType returns true if a webhook registered
|
|
|
|
|
func IsValidHookTaskType(name string) bool {
|
2023-01-01 16:23:15 +01:00
|
|
|
if name == webhook_module.GITEA || name == webhook_module.GOGS {
|
2020-12-12 23:33:19 +08:00
|
|
|
return true
|
|
|
|
|
}
|
2024-03-07 23:18:38 +01:00
|
|
|
_, ok := webhookRequesters[name]
|
2020-12-08 18:41:14 +08:00
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-02 10:35:12 +08:00
|
|
|
// hookQueue is a global queue of web hooks
|
2023-05-08 19:49:59 +08:00
|
|
|
var hookQueue *queue.WorkerPoolQueue[int64]
|
2019-11-02 06:51:22 +08:00
|
|
|
|
2025-10-03 12:21:57 +05:30
|
|
|
// getPayloadRef returns the full ref name for hook event, if applicable.
|
|
|
|
|
func getPayloadRef(p api.Payloader) git.RefName {
|
2019-11-02 06:51:22 +08:00
|
|
|
switch pp := p.(type) {
|
|
|
|
|
case *api.CreatePayload:
|
2025-10-03 12:21:57 +05:30
|
|
|
switch pp.RefType {
|
|
|
|
|
case "branch":
|
|
|
|
|
return git.RefNameFromBranch(pp.Ref)
|
|
|
|
|
case "tag":
|
|
|
|
|
return git.RefNameFromTag(pp.Ref)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
case *api.DeletePayload:
|
2025-10-03 12:21:57 +05:30
|
|
|
switch pp.RefType {
|
|
|
|
|
case "branch":
|
|
|
|
|
return git.RefNameFromBranch(pp.Ref)
|
|
|
|
|
case "tag":
|
|
|
|
|
return git.RefNameFromTag(pp.Ref)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
case *api.PushPayload:
|
2025-10-03 12:21:57 +05:30
|
|
|
return git.RefName(pp.Ref)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 18:21:56 +02:00
|
|
|
// EventSource represents the source of a webhook action. Repository and/or Owner must be set.
|
|
|
|
|
type EventSource struct {
|
|
|
|
|
Repository *repo_model.Repository
|
|
|
|
|
Owner *user_model.User
|
|
|
|
|
}
|
2022-04-26 02:03:01 +08:00
|
|
|
|
2022-10-21 18:21:56 +02:00
|
|
|
// handle delivers hook tasks
|
2023-05-08 19:49:59 +08:00
|
|
|
func handler(items ...int64) []int64 {
|
2022-10-21 18:21:56 +02:00
|
|
|
ctx := graceful.GetManager().HammerContext()
|
2022-04-26 02:03:01 +08:00
|
|
|
|
2023-05-08 19:49:59 +08:00
|
|
|
for _, taskID := range items {
|
|
|
|
|
task, err := webhook_model.GetHookTaskByID(ctx, taskID)
|
2022-04-26 02:03:01 +08:00
|
|
|
if err != nil {
|
2023-07-28 23:16:48 +05:30
|
|
|
if errors.Is(err, util.ErrNotExist) {
|
|
|
|
|
log.Warn("GetHookTaskByID[%d] warn: %v", taskID, err)
|
|
|
|
|
} else {
|
|
|
|
|
log.Error("GetHookTaskByID[%d] failed: %v", taskID, err)
|
|
|
|
|
}
|
2022-11-23 14:10:04 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if task.IsDelivered {
|
|
|
|
|
// Already delivered in the meantime
|
|
|
|
|
log.Trace("Task[%d] has already been delivered", task.ID)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := Deliver(ctx, task); err != nil {
|
|
|
|
|
log.Error("Unable to deliver webhook task[%d]: %v", task.ID, err)
|
2022-04-26 02:03:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-21 18:21:56 +02:00
|
|
|
|
2022-04-26 02:03:01 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 14:10:04 +00:00
|
|
|
func enqueueHookTask(taskID int64) error {
|
|
|
|
|
err := hookQueue.Push(taskID)
|
2022-04-26 02:03:01 +08:00
|
|
|
if err != nil && err != queue.ErrAlreadyInQueue {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 12:21:57 +05:30
|
|
|
func checkBranchFilter(branchFilter string, ref git.RefName) bool {
|
|
|
|
|
if branchFilter == "" || branchFilter == "*" || branchFilter == "**" {
|
2019-11-02 06:51:22 +08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 12:21:57 +05:30
|
|
|
g, err := glob.Compile(branchFilter)
|
2019-11-02 06:51:22 +08:00
|
|
|
if err != nil {
|
|
|
|
|
// should not really happen as BranchFilter is validated
|
2025-10-03 12:21:57 +05:30
|
|
|
log.Debug("checkBranchFilter failed to compile filer %q, err: %s", branchFilter, err)
|
2019-11-02 06:51:22 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 12:21:57 +05:30
|
|
|
if ref.IsBranch() && g.Match(ref.BranchName()) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return g.Match(ref.String())
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
// PrepareWebhook creates a hook task and enqueues it for processing.
|
|
|
|
|
// The payload is saved as-is. The adjustments depending on the webhook type happen
|
|
|
|
|
// right before delivery, in the [Deliver] method.
|
2023-01-01 16:23:15 +01:00
|
|
|
func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook_module.HookEventType, p api.Payloader) error {
|
2021-02-11 18:34:34 +01:00
|
|
|
// Skip sending if webhooks are disabled.
|
|
|
|
|
if setting.DisableWebhooks {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 10:53:06 -08:00
|
|
|
if !w.HasEvent(event) {
|
|
|
|
|
return nil
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
2020-09-03 04:46:02 +02:00
|
|
|
// Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.).
|
|
|
|
|
// Integration webhooks (e.g. drone) still receive the required data.
|
|
|
|
|
if pushEvent, ok := p.(*api.PushPayload); ok &&
|
2023-01-01 16:23:15 +01:00
|
|
|
w.Type != webhook_module.GITEA && w.Type != webhook_module.GOGS &&
|
2020-09-03 04:46:02 +02:00
|
|
|
len(pushEvent.Commits) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-03 12:21:57 +05:30
|
|
|
// If payload has no associated branch (e.g. it's a new tag, issue, etc.), branch filter has no effect.
|
|
|
|
|
if ref := getPayloadRef(p); ref != "" {
|
|
|
|
|
// Check the payload's git ref against the webhook's branch filter.
|
|
|
|
|
if !checkBranchFilter(w.BranchFilter, ref) {
|
2019-11-02 06:51:22 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
payload, err := p.JSONPayload()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("JSONPayload for %s: %w", event, err)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-21 18:21:56 +02:00
|
|
|
task, err := webhook_model.CreateHookTask(ctx, &webhook_model.HookTask{
|
2024-03-07 23:18:38 +01:00
|
|
|
HookID: w.ID,
|
|
|
|
|
PayloadContent: string(payload),
|
|
|
|
|
EventType: event,
|
|
|
|
|
PayloadVersion: 2,
|
2022-10-21 18:21:56 +02:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-03-07 23:18:38 +01:00
|
|
|
return fmt.Errorf("CreateHookTask for %s: %w", event, err)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
2022-10-21 18:21:56 +02:00
|
|
|
|
2022-11-23 14:10:04 +00:00
|
|
|
return enqueueHookTask(task.ID)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PrepareWebhooks adds new webhooks to task queue for given payload.
|
2023-01-01 16:23:15 +01:00
|
|
|
func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_module.HookEventType, p api.Payloader) error {
|
2022-10-21 18:21:56 +02:00
|
|
|
owner := source.Owner
|
2019-11-02 10:35:12 +08:00
|
|
|
|
2022-10-21 18:21:56 +02:00
|
|
|
var ws []*webhook_model.Webhook
|
2019-11-02 06:51:22 +08:00
|
|
|
|
2022-10-21 18:21:56 +02:00
|
|
|
if source.Repository != nil {
|
2023-11-24 11:49:41 +08:00
|
|
|
repoHooks, err := db.Find[webhook_model.Webhook](ctx, webhook_model.ListWebhookOptions{
|
2022-10-21 18:21:56 +02:00
|
|
|
RepoID: source.Repository.ID,
|
2024-03-02 16:42:31 +01:00
|
|
|
IsActive: optional.Some(true),
|
2022-10-21 18:21:56 +02:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("ListWebhooksByOpts: %w", err)
|
2022-10-21 18:21:56 +02:00
|
|
|
}
|
|
|
|
|
ws = append(ws, repoHooks...)
|
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
owner = source.Repository.MustOwner(ctx)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-10 15:28:32 +01:00
|
|
|
// append additional webhooks of a user or organization
|
|
|
|
|
if owner != nil {
|
2023-11-24 11:49:41 +08:00
|
|
|
ownerHooks, err := db.Find[webhook_model.Webhook](ctx, webhook_model.ListWebhookOptions{
|
2023-03-10 15:28:32 +01:00
|
|
|
OwnerID: owner.ID,
|
2024-03-02 16:42:31 +01:00
|
|
|
IsActive: optional.Some(true),
|
2021-08-12 14:43:08 +02:00
|
|
|
})
|
2019-11-02 06:51:22 +08:00
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("ListWebhooksByOpts: %w", err)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
2023-03-10 15:28:32 +01:00
|
|
|
ws = append(ws, ownerHooks...)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
2020-03-08 22:08:05 +00:00
|
|
|
// Add any admin-defined system webhooks
|
2024-03-02 16:42:31 +01:00
|
|
|
systemHooks, err := webhook_model.GetSystemWebhooks(ctx, optional.Some(true))
|
2020-03-08 22:08:05 +00:00
|
|
|
if err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("GetSystemWebhooks: %w", err)
|
2020-03-08 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
ws = append(ws, systemHooks...)
|
|
|
|
|
|
2019-11-02 06:51:22 +08:00
|
|
|
if len(ws) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, w := range ws {
|
2022-10-21 18:21:56 +02:00
|
|
|
if err := PrepareWebhook(ctx, w, event, p); err != nil {
|
2019-11-02 06:51:22 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-01-05 22:00:20 +01:00
|
|
|
|
|
|
|
|
// ReplayHookTask replays a webhook task
|
2022-10-21 18:21:56 +02:00
|
|
|
func ReplayHookTask(ctx context.Context, w *webhook_model.Webhook, uuid string) error {
|
|
|
|
|
task, err := webhook_model.ReplayHookTask(ctx, w.ID, uuid)
|
2022-01-05 22:00:20 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 14:10:04 +00:00
|
|
|
return enqueueHookTask(task.ID)
|
2022-01-05 22:00:20 +01:00
|
|
|
}
|