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 (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/modules/sync"
|
|
|
|
"github.com/gobwas/glob"
|
|
|
|
)
|
|
|
|
|
2020-12-08 18:41:14 +08:00
|
|
|
type webhook struct {
|
|
|
|
name models.HookTaskType
|
|
|
|
payloadCreator func(p api.Payloader, event models.HookEventType, meta string) (api.Payloader, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
webhooks = map[models.HookTaskType]*webhook{
|
|
|
|
models.SLACK: {
|
|
|
|
name: models.SLACK,
|
|
|
|
payloadCreator: GetSlackPayload,
|
|
|
|
},
|
|
|
|
models.DISCORD: {
|
|
|
|
name: models.DISCORD,
|
|
|
|
payloadCreator: GetDiscordPayload,
|
|
|
|
},
|
|
|
|
models.DINGTALK: {
|
|
|
|
name: models.DINGTALK,
|
|
|
|
payloadCreator: GetDingtalkPayload,
|
|
|
|
},
|
|
|
|
models.TELEGRAM: {
|
|
|
|
name: models.TELEGRAM,
|
|
|
|
payloadCreator: GetTelegramPayload,
|
|
|
|
},
|
|
|
|
models.MSTEAMS: {
|
|
|
|
name: models.MSTEAMS,
|
|
|
|
payloadCreator: GetMSTeamsPayload,
|
|
|
|
},
|
|
|
|
models.FEISHU: {
|
|
|
|
name: models.FEISHU,
|
|
|
|
payloadCreator: GetFeishuPayload,
|
|
|
|
},
|
|
|
|
models.MATRIX: {
|
|
|
|
name: models.MATRIX,
|
|
|
|
payloadCreator: GetMatrixPayload,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// RegisterWebhook registers a webhook
|
|
|
|
func RegisterWebhook(name string, webhook *webhook) {
|
2021-01-06 23:11:23 +08:00
|
|
|
webhooks[models.HookTaskType(name)] = webhook
|
2020-12-08 18:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidHookTaskType returns true if a webhook registered
|
|
|
|
func IsValidHookTaskType(name string) bool {
|
2020-12-12 23:33:19 +08:00
|
|
|
if name == models.GITEA || name == models.GOGS {
|
|
|
|
return true
|
|
|
|
}
|
2021-01-06 23:11:23 +08:00
|
|
|
_, ok := webhooks[models.HookTaskType(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
|
|
|
|
var hookQueue = sync.NewUniqueQueue(setting.Webhook.QueueLength)
|
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 ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareWebhook adds special webhook to task queue for given payload.
|
|
|
|
func PrepareWebhook(w *models.Webhook, repo *models.Repository, event models.HookEventType, p api.Payloader) error {
|
2019-11-02 10:35:12 +08:00
|
|
|
if err := prepareWebhook(w, repo, event, p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go hookQueue.Add(repo.ID)
|
|
|
|
return nil
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkBranch(w *models.Webhook, branch string) bool {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 &&
|
2020-12-10 01:20:13 +08:00
|
|
|
w.Type != models.GITEA && w.Type != models.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 {
|
2020-12-10 01:20:13 +08:00
|
|
|
return fmt.Errorf("create payload for %s[%s]: %v", 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
|
|
|
p.SetSecret(w.Secret)
|
|
|
|
payloader = p
|
|
|
|
}
|
|
|
|
|
|
|
|
var signature string
|
|
|
|
if len(w.Secret) > 0 {
|
|
|
|
data, err := payloader.JSONPayload()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("prepareWebhooks.JSONPayload: %v", err)
|
|
|
|
}
|
|
|
|
sig := hmac.New(sha256.New, []byte(w.Secret))
|
|
|
|
_, err = sig.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("prepareWebhooks.sigWrite: %v", err)
|
|
|
|
}
|
|
|
|
signature = hex.EncodeToString(sig.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = models.CreateHookTask(&models.HookTask{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
HookID: w.ID,
|
2020-12-10 01:20:13 +08:00
|
|
|
Typ: w.Type,
|
2019-11-02 06:51:22 +08:00
|
|
|
URL: w.URL,
|
|
|
|
Signature: signature,
|
|
|
|
Payloader: payloader,
|
|
|
|
HTTPMethod: w.HTTPMethod,
|
|
|
|
ContentType: w.ContentType,
|
|
|
|
EventType: event,
|
|
|
|
IsSSL: w.IsSSL,
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("CreateHookTask: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareWebhooks adds new webhooks to task queue for given payload.
|
|
|
|
func PrepareWebhooks(repo *models.Repository, event models.HookEventType, p api.Payloader) error {
|
2019-11-02 10:35:12 +08:00
|
|
|
if err := prepareWebhooks(repo, event, p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go hookQueue.Add(repo.ID)
|
|
|
|
return nil
|
2019-11-02 06:51:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func prepareWebhooks(repo *models.Repository, event models.HookEventType, p api.Payloader) error {
|
|
|
|
ws, err := models.GetActiveWebhooksByRepoID(repo.ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetActiveWebhooksByRepoID: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if repo belongs to org and append additional webhooks
|
|
|
|
if repo.MustOwner().IsOrganization() {
|
|
|
|
// get hooks for org
|
|
|
|
orgHooks, err := models.GetActiveWebhooksByOrgID(repo.OwnerID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetActiveWebhooksByOrgID: %v", err)
|
|
|
|
}
|
|
|
|
ws = append(ws, orgHooks...)
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:08:05 +08:00
|
|
|
// Add any admin-defined system webhooks
|
|
|
|
systemHooks, err := models.GetSystemWebhooks()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetSystemWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
ws = append(ws, systemHooks...)
|
|
|
|
|
2019-11-02 06:51:22 +08:00
|
|
|
if len(ws) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range ws {
|
|
|
|
if err = prepareWebhook(w, repo, event, p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|