2019-11-02 06:51:22 +08:00
|
|
|
// Copyright 2019 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 webhook
|
|
|
|
|
|
|
|
import (
|
2022-05-20 22:08:52 +08:00
|
|
|
"context"
|
2019-11-02 06:51:22 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-10-22 00:21:56 +08: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"
|
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"
|
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 20:43:08 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
2019-11-02 06:51:22 +08:00
|
|
|
"github.com/gobwas/glob"
|
|
|
|
)
|
|
|
|
|
2020-12-08 18:41:14 +08:00
|
|
|
type webhook struct {
|
2021-11-10 13:13:16 +08:00
|
|
|
name webhook_model.HookType
|
|
|
|
payloadCreator func(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error)
|
2020-12-08 18:41:14 +08:00
|
|
|
}
|
|
|
|
|
2022-01-21 01:46:10 +08:00
|
|
|
var webhooks = map[webhook_model.HookType]*webhook{
|
|
|
|
webhook_model.SLACK: {
|
|
|
|
name: webhook_model.SLACK,
|
|
|
|
payloadCreator: GetSlackPayload,
|
|
|
|
},
|
|
|
|
webhook_model.DISCORD: {
|
|
|
|
name: webhook_model.DISCORD,
|
|
|
|
payloadCreator: GetDiscordPayload,
|
|
|
|
},
|
|
|
|
webhook_model.DINGTALK: {
|
|
|
|
name: webhook_model.DINGTALK,
|
|
|
|
payloadCreator: GetDingtalkPayload,
|
|
|
|
},
|
|
|
|
webhook_model.TELEGRAM: {
|
|
|
|
name: webhook_model.TELEGRAM,
|
|
|
|
payloadCreator: GetTelegramPayload,
|
|
|
|
},
|
|
|
|
webhook_model.MSTEAMS: {
|
|
|
|
name: webhook_model.MSTEAMS,
|
|
|
|
payloadCreator: GetMSTeamsPayload,
|
|
|
|
},
|
|
|
|
webhook_model.FEISHU: {
|
|
|
|
name: webhook_model.FEISHU,
|
|
|
|
payloadCreator: GetFeishuPayload,
|
|
|
|
},
|
|
|
|
webhook_model.MATRIX: {
|
|
|
|
name: webhook_model.MATRIX,
|
|
|
|
payloadCreator: GetMatrixPayload,
|
|
|
|
},
|
|
|
|
webhook_model.WECHATWORK: {
|
|
|
|
name: webhook_model.WECHATWORK,
|
|
|
|
payloadCreator: GetWechatworkPayload,
|
|
|
|
},
|
2022-01-23 21:46:30 +08:00
|
|
|
webhook_model.PACKAGIST: {
|
|
|
|
name: webhook_model.PACKAGIST,
|
|
|
|
payloadCreator: GetPackagistPayload,
|
|
|
|
},
|
2022-01-21 01:46:10 +08:00
|
|
|
}
|
2020-12-08 18:41:14 +08:00
|
|
|
|
|
|
|
// RegisterWebhook registers a webhook
|
|
|
|
func RegisterWebhook(name string, webhook *webhook) {
|
2022-06-20 18:02:49 +08:00
|
|
|
webhooks[name] = webhook
|
2020-12-08 18:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidHookTaskType returns true if a webhook registered
|
|
|
|
func IsValidHookTaskType(name string) bool {
|
2021-11-10 13:13:16 +08:00
|
|
|
if name == webhook_model.GITEA || name == webhook_model.GOGS {
|
2020-12-12 23:33:19 +08:00
|
|
|
return true
|
|
|
|
}
|
2022-06-20 18:02:49 +08:00
|
|
|
_, ok := webhooks[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
|
2022-04-26 02:03:01 +08:00
|
|
|
var hookQueue queue.UniqueQueue
|
2019-11-02 06:51:22 +08:00
|
|
|
|
|
|
|
// getPayloadBranch returns branch for hook event, if applicable.
|
|
|
|
func getPayloadBranch(p api.Payloader) string {
|
|
|
|
switch pp := p.(type) {
|
|
|
|
case *api.CreatePayload:
|
|
|
|
if pp.RefType == "branch" {
|
|
|
|
return pp.Ref
|
|
|
|
}
|
|
|
|
case *api.DeletePayload:
|
|
|
|
if pp.RefType == "branch" {
|
|
|
|
return pp.Ref
|
|
|
|
}
|
|
|
|
case *api.PushPayload:
|
|
|
|
if strings.HasPrefix(pp.Ref, git.BranchPrefix) {
|
|
|
|
return pp.Ref[len(git.BranchPrefix):]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-10-22 00:21:56 +08: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-22 00:21:56 +08:00
|
|
|
// handle delivers hook tasks
|
|
|
|
func handle(data ...queue.Data) []queue.Data {
|
|
|
|
ctx := graceful.GetManager().HammerContext()
|
2022-04-26 02:03:01 +08:00
|
|
|
|
2022-10-22 00:21:56 +08:00
|
|
|
for _, taskID := range data {
|
|
|
|
task, err := webhook_model.GetHookTaskByID(ctx, taskID.(int64))
|
2022-04-26 02:03:01 +08:00
|
|
|
if err != nil {
|
2022-11-23 22:10:04 +08:00
|
|
|
log.Error("GetHookTaskByID[%d] failed: %v", taskID.(int64), err)
|
|
|
|
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-22 00:21:56 +08:00
|
|
|
|
2022-04-26 02:03:01 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 22:10:04 +08: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
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
func checkBranch(w *webhook_model.Webhook, branch string) bool {
|
2019-11-02 06:51:22 +08:00
|
|
|
if w.BranchFilter == "" || w.BranchFilter == "*" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
g, err := glob.Compile(w.BranchFilter)
|
|
|
|
if err != nil {
|
|
|
|
// should not really happen as BranchFilter is validated
|
|
|
|
log.Error("CheckBranch failed: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.Match(branch)
|
|
|
|
}
|
|
|
|
|
2022-10-22 00:21:56 +08:00
|
|
|
// PrepareWebhook creates a hook task and enqueues it for processing
|
|
|
|
func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook_model.HookEventType, p api.Payloader) error {
|
2021-02-12 01:34:34 +08:00
|
|
|
// Skip sending if webhooks are disabled.
|
|
|
|
if setting.DisableWebhooks {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 06:51:22 +08:00
|
|
|
for _, e := range w.EventCheckers() {
|
|
|
|
if event == e.Type {
|
|
|
|
if !e.Has() {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-23 02:54:01 +08:00
|
|
|
|
|
|
|
break
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 10:46:02 +08: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 &&
|
2021-11-10 13:13:16 +08:00
|
|
|
w.Type != webhook_model.GITEA && w.Type != webhook_model.GOGS &&
|
2020-09-03 10:46:02 +08:00
|
|
|
len(pushEvent.Commits) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 06:51:22 +08:00
|
|
|
// If payload has no associated branch (e.g. it's a new tag, issue, etc.),
|
|
|
|
// branch filter has no effect.
|
|
|
|
if branch := getPayloadBranch(p); branch != "" {
|
|
|
|
if !checkBranch(w, branch) {
|
|
|
|
log.Info("Branch %q doesn't match branch filter %q, skipping", branch, w.BranchFilter)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var payloader api.Payloader
|
|
|
|
var err error
|
2021-01-06 23:11:23 +08:00
|
|
|
webhook, ok := webhooks[w.Type]
|
2020-12-08 18:41:14 +08:00
|
|
|
if ok {
|
|
|
|
payloader, err = webhook.payloadCreator(p, event, w.Meta)
|
2020-03-28 21:09:55 +08:00
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("create payload for %s[%s]: %w", w.Type, event, err)
|
2020-03-28 21:09:55 +08:00
|
|
|
}
|
2020-12-08 18:41:14 +08:00
|
|
|
} else {
|
2019-11-02 06:51:22 +08:00
|
|
|
payloader = p
|
|
|
|
}
|
|
|
|
|
2022-10-22 00:21:56 +08:00
|
|
|
task, err := webhook_model.CreateHookTask(ctx, &webhook_model.HookTask{
|
2021-06-28 03:21:09 +08:00
|
|
|
HookID: w.ID,
|
|
|
|
Payloader: payloader,
|
|
|
|
EventType: event,
|
2022-10-22 00:21:56 +08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("CreateHookTask: %w", err)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
2022-10-22 00:21:56 +08:00
|
|
|
|
2022-11-23 22:10:04 +08:00
|
|
|
return enqueueHookTask(task.ID)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareWebhooks adds new webhooks to task queue for given payload.
|
2022-10-22 00:21:56 +08:00
|
|
|
func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_model.HookEventType, p api.Payloader) error {
|
|
|
|
owner := source.Owner
|
2019-11-02 10:35:12 +08:00
|
|
|
|
2022-10-22 00:21:56 +08:00
|
|
|
var ws []*webhook_model.Webhook
|
2019-11-02 06:51:22 +08:00
|
|
|
|
2022-10-22 00:21:56 +08:00
|
|
|
if source.Repository != nil {
|
|
|
|
repoHooks, err := webhook_model.ListWebhooksByOpts(ctx, &webhook_model.ListWebhookOptions{
|
|
|
|
RepoID: source.Repository.ID,
|
|
|
|
IsActive: util.OptionalBoolTrue,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("ListWebhooksByOpts: %w", err)
|
2022-10-22 00:21:56 +08:00
|
|
|
}
|
|
|
|
ws = append(ws, repoHooks...)
|
|
|
|
|
2022-11-19 16:12:33 +08:00
|
|
|
owner = source.Repository.MustOwner(ctx)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
2022-10-22 00:21:56 +08:00
|
|
|
// check if owner is an org and append additional webhooks
|
|
|
|
if owner != nil && owner.IsOrganization() {
|
2022-05-20 22:08:52 +08:00
|
|
|
orgHooks, err := webhook_model.ListWebhooksByOpts(ctx, &webhook_model.ListWebhookOptions{
|
2022-10-22 00:21:56 +08:00
|
|
|
OrgID: owner.ID,
|
2021-08-12 20:43:08 +08:00
|
|
|
IsActive: util.OptionalBoolTrue,
|
|
|
|
})
|
2019-11-02 06:51:22 +08:00
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("ListWebhooksByOpts: %w", err)
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
ws = append(ws, orgHooks...)
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:08:05 +08:00
|
|
|
// Add any admin-defined system webhooks
|
2022-05-20 22:08:52 +08:00
|
|
|
systemHooks, err := webhook_model.GetSystemWebhooks(ctx, util.OptionalBoolTrue)
|
2020-03-09 06:08:05 +08:00
|
|
|
if err != nil {
|
2022-10-25 03:29:17 +08:00
|
|
|
return fmt.Errorf("GetSystemWebhooks: %w", err)
|
2020-03-09 06:08:05 +08: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-22 00:21:56 +08:00
|
|
|
if err := PrepareWebhook(ctx, w, event, p); err != nil {
|
2019-11-02 06:51:22 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-06 05:00:20 +08:00
|
|
|
|
|
|
|
// ReplayHookTask replays a webhook task
|
2022-10-22 00:21:56 +08:00
|
|
|
func ReplayHookTask(ctx context.Context, w *webhook_model.Webhook, uuid string) error {
|
|
|
|
task, err := webhook_model.ReplayHookTask(ctx, w.ID, uuid)
|
2022-01-06 05:00:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-23 22:10:04 +08:00
|
|
|
return enqueueHookTask(task.ID)
|
2022-01-06 05:00:20 +08:00
|
|
|
}
|