2019-11-02 04:21:22 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-02 04:21:22 +05:30
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2022-05-20 19:38:52 +05:30
|
|
|
"context"
|
2023-07-28 23:16:48 +05:30
|
|
|
"errors"
|
2019-11-02 04:21:22 +05:30
|
|
|
"fmt"
|
2024-03-22 20:32:48 +05:30
|
|
|
"html/template"
|
2024-03-08 03:48:38 +05:30
|
|
|
"net/http"
|
2019-11-02 04:21:22 +05:30
|
|
|
"strings"
|
|
|
|
|
2023-11-24 09:19:41 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-10-21 21:51:56 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-10 10:43:16 +05:30
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2019-11-02 04:21:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2022-04-25 23:33:01 +05:30
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
2019-11-02 04:21:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-03-02 21:12:31 +05:30
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2022-04-25 23:33:01 +05:30
|
|
|
"code.gitea.io/gitea/modules/queue"
|
2019-11-02 04:21:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-08-12 18:13:08 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2023-01-01 20:53:15 +05:30
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2024-03-21 17:53:27 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2024-03-13 21:19:48 +05:30
|
|
|
"code.gitea.io/gitea/services/webhook/sourcehut"
|
2021-08-12 18:13:08 +05:30
|
|
|
|
2019-11-02 04:21:22 +05:30
|
|
|
"github.com/gobwas/glob"
|
|
|
|
)
|
|
|
|
|
2024-03-20 20:14:01 +05:30
|
|
|
type Handler interface {
|
|
|
|
Type() webhook_module.HookType
|
|
|
|
Metadata(*webhook_model.Webhook) any
|
2024-04-03 17:52:36 +05:30
|
|
|
// UnmarshalForm provides a function to bind the request to the form.
|
2024-03-21 17:53:27 +05:30
|
|
|
// If form implements the [binding.Validator] interface, the Validate method will be called
|
2024-04-03 17:52:36 +05:30
|
|
|
UnmarshalForm(bind func(form any)) forms.WebhookForm
|
2024-03-21 17:53:27 +05:30
|
|
|
NewRequest(context.Context, *webhook_model.Webhook, *webhook_model.HookTask) (req *http.Request, body []byte, err error)
|
2024-03-22 20:32:48 +05:30
|
|
|
Icon(size int) template.HTML
|
2024-03-21 17:53:27 +05:30
|
|
|
}
|
|
|
|
|
2024-03-20 20:14:01 +05:30
|
|
|
var webhookHandlers = []Handler{
|
|
|
|
defaultHandler{true},
|
|
|
|
defaultHandler{false},
|
|
|
|
gogsHandler{},
|
|
|
|
|
|
|
|
slackHandler{},
|
|
|
|
discordHandler{},
|
|
|
|
dingtalkHandler{},
|
|
|
|
telegramHandler{},
|
|
|
|
msteamsHandler{},
|
|
|
|
feishuHandler{},
|
|
|
|
matrixHandler{},
|
|
|
|
wechatworkHandler{},
|
|
|
|
packagistHandler{},
|
2024-03-13 21:19:48 +05:30
|
|
|
sourcehut.BuildsHandler{},
|
2024-03-20 20:14:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetWebhookHandler return the handler for a given webhook type (nil if not found)
|
|
|
|
func GetWebhookHandler(name webhook_module.HookType) Handler {
|
|
|
|
for _, h := range webhookHandlers {
|
|
|
|
if h.Type() == name {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List provides a list of the supported webhooks
|
|
|
|
func List() []Handler {
|
|
|
|
return webhookHandlers
|
2022-01-20 23:16:10 +05:30
|
|
|
}
|
2020-12-08 16:11:14 +05:30
|
|
|
|
|
|
|
// IsValidHookTaskType returns true if a webhook registered
|
|
|
|
func IsValidHookTaskType(name string) bool {
|
2024-03-20 20:14:01 +05:30
|
|
|
return GetWebhookHandler(name) != nil
|
2020-12-08 16:11:14 +05:30
|
|
|
}
|
|
|
|
|
2019-11-02 08:05:12 +05:30
|
|
|
// hookQueue is a global queue of web hooks
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 17:19:59 +05:30
|
|
|
var hookQueue *queue.WorkerPoolQueue[int64]
|
2019-11-02 04:21:22 +05:30
|
|
|
|
|
|
|
// 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-21 21:51:56 +05:30
|
|
|
// 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-25 23:33:01 +05:30
|
|
|
|
2022-10-21 21:51:56 +05:30
|
|
|
// handle delivers hook tasks
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 17:19:59 +05:30
|
|
|
func handler(items ...int64) []int64 {
|
2022-10-21 21:51:56 +05:30
|
|
|
ctx := graceful.GetManager().HammerContext()
|
2022-04-25 23:33:01 +05:30
|
|
|
|
Rewrite queue (#24505)
# ⚠️ Breaking
Many deprecated queue config options are removed (actually, they should
have been removed in 1.18/1.19).
If you see the fatal message when starting Gitea: "Please update your
app.ini to remove deprecated config options", please follow the error
messages to remove these options from your app.ini.
Example:
```
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].ISSUE_INDEXER_QUEUE_TYPE`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [E] Removed queue option: `[indexer].UPDATE_BUFFER_LEN`. Use new options in `[queue.issue_indexer]`
2023/05/06 19:39:22 [F] Please update your app.ini to remove deprecated config options
```
Many options in `[queue]` are are dropped, including:
`WRAP_IF_NECESSARY`, `MAX_ATTEMPTS`, `TIMEOUT`, `WORKERS`,
`BLOCK_TIMEOUT`, `BOOST_TIMEOUT`, `BOOST_WORKERS`, they can be removed
from app.ini.
# The problem
The old queue package has some legacy problems:
* complexity: I doubt few people could tell how it works.
* maintainability: Too many channels and mutex/cond are mixed together,
too many different structs/interfaces depends each other.
* stability: due to the complexity & maintainability, sometimes there
are strange bugs and difficult to debug, and some code doesn't have test
(indeed some code is difficult to test because a lot of things are mixed
together).
* general applicability: although it is called "queue", its behavior is
not a well-known queue.
* scalability: it doesn't seem easy to make it work with a cluster
without breaking its behaviors.
It came from some very old code to "avoid breaking", however, its
technical debt is too heavy now. It's a good time to introduce a better
"queue" package.
# The new queue package
It keeps using old config and concept as much as possible.
* It only contains two major kinds of concepts:
* The "base queue": channel, levelqueue, redis
* They have the same abstraction, the same interface, and they are
tested by the same testing code.
* The "WokerPoolQueue", it uses the "base queue" to provide "worker
pool" function, calls the "handler" to process the data in the base
queue.
* The new code doesn't do "PushBack"
* Think about a queue with many workers, the "PushBack" can't guarantee
the order for re-queued unhandled items, so in new code it just does
"normal push"
* The new code doesn't do "pause/resume"
* The "pause/resume" was designed to handle some handler's failure: eg:
document indexer (elasticsearch) is down
* If a queue is paused for long time, either the producers blocks or the
new items are dropped.
* The new code doesn't do such "pause/resume" trick, it's not a common
queue's behavior and it doesn't help much.
* If there are unhandled items, the "push" function just blocks for a
few seconds and then re-queue them and retry.
* The new code doesn't do "worker booster"
* Gitea's queue's handlers are light functions, the cost is only the
go-routine, so it doesn't make sense to "boost" them.
* The new code only use "max worker number" to limit the concurrent
workers.
* The new "Push" never blocks forever
* Instead of creating more and more blocking goroutines, return an error
is more friendly to the server and to the end user.
There are more details in code comments: eg: the "Flush" problem, the
strange "code.index" hanging problem, the "immediate" queue problem.
Almost ready for review.
TODO:
* [x] add some necessary comments during review
* [x] add some more tests if necessary
* [x] update documents and config options
* [x] test max worker / active worker
* [x] re-run the CI tasks to see whether any test is flaky
* [x] improve the `handleOldLengthConfiguration` to provide more
friendly messages
* [x] fine tune default config values (eg: length?)
## Code coverage:
![image](https://user-images.githubusercontent.com/2114189/236620635-55576955-f95d-4810-b12f-879026a3afdf.png)
2023-05-08 17:19:59 +05:30
|
|
|
for _, taskID := range items {
|
|
|
|
task, err := webhook_model.GetHookTaskByID(ctx, taskID)
|
2022-04-25 23:33:01 +05:30
|
|
|
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 19:40:04 +05:30
|
|
|
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-25 23:33:01 +05:30
|
|
|
}
|
|
|
|
}
|
2022-10-21 21:51:56 +05:30
|
|
|
|
2022-04-25 23:33:01 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:40:04 +05:30
|
|
|
func enqueueHookTask(taskID int64) error {
|
|
|
|
err := hookQueue.Push(taskID)
|
2022-04-25 23:33:01 +05:30
|
|
|
if err != nil && err != queue.ErrAlreadyInQueue {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
func checkBranch(w *webhook_model.Webhook, branch string) bool {
|
2019-11-02 04:21:22 +05:30
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
// 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 20:53:15 +05:30
|
|
|
func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook_module.HookEventType, p api.Payloader) error {
|
2021-02-11 23:04:34 +05:30
|
|
|
// Skip sending if webhooks are disabled.
|
|
|
|
if setting.DisableWebhooks {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 04:21:22 +05:30
|
|
|
for _, e := range w.EventCheckers() {
|
|
|
|
if event == e.Type {
|
|
|
|
if !e.Has() {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-23 00:24:01 +05:30
|
|
|
|
|
|
|
break
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 08:16:02 +05:30
|
|
|
// 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-14 14:37:01 +05:30
|
|
|
w.Type != webhook_module.FORGEJO && w.Type != webhook_module.GITEA && w.Type != webhook_module.GOGS &&
|
2020-09-03 08:16:02 +05:30
|
|
|
len(pushEvent.Commits) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-02 04:21:22 +05:30
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
payload, err := p.JSONPayload()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("JSONPayload for %s: %w", event, err)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
|
2022-10-21 21:51:56 +05:30
|
|
|
task, err := webhook_model.CreateHookTask(ctx, &webhook_model.HookTask{
|
2024-03-08 03:48:38 +05:30
|
|
|
HookID: w.ID,
|
|
|
|
PayloadContent: string(payload),
|
|
|
|
EventType: event,
|
|
|
|
PayloadVersion: 2,
|
2022-10-21 21:51:56 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
2024-03-08 03:48:38 +05:30
|
|
|
return fmt.Errorf("CreateHookTask for %s: %w", event, err)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
2022-10-21 21:51:56 +05:30
|
|
|
|
2022-11-23 19:40:04 +05:30
|
|
|
return enqueueHookTask(task.ID)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareWebhooks adds new webhooks to task queue for given payload.
|
2023-01-01 20:53:15 +05:30
|
|
|
func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_module.HookEventType, p api.Payloader) error {
|
2022-10-21 21:51:56 +05:30
|
|
|
owner := source.Owner
|
2019-11-02 08:05:12 +05:30
|
|
|
|
2022-10-21 21:51:56 +05:30
|
|
|
var ws []*webhook_model.Webhook
|
2019-11-02 04:21:22 +05:30
|
|
|
|
2022-10-21 21:51:56 +05:30
|
|
|
if source.Repository != nil {
|
2023-11-24 09:19:41 +05:30
|
|
|
repoHooks, err := db.Find[webhook_model.Webhook](ctx, webhook_model.ListWebhookOptions{
|
2022-10-21 21:51:56 +05:30
|
|
|
RepoID: source.Repository.ID,
|
2024-03-02 21:12:31 +05:30
|
|
|
IsActive: optional.Some(true),
|
2022-10-21 21:51:56 +05:30
|
|
|
})
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("ListWebhooksByOpts: %w", err)
|
2022-10-21 21:51:56 +05:30
|
|
|
}
|
|
|
|
ws = append(ws, repoHooks...)
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
owner = source.Repository.MustOwner(ctx)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
|
2023-03-10 19:58:32 +05:30
|
|
|
// append additional webhooks of a user or organization
|
|
|
|
if owner != nil {
|
2023-11-24 09:19:41 +05:30
|
|
|
ownerHooks, err := db.Find[webhook_model.Webhook](ctx, webhook_model.ListWebhookOptions{
|
2023-03-10 19:58:32 +05:30
|
|
|
OwnerID: owner.ID,
|
2024-03-02 21:12:31 +05:30
|
|
|
IsActive: optional.Some(true),
|
2021-08-12 18:13:08 +05:30
|
|
|
})
|
2019-11-02 04:21:22 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("ListWebhooksByOpts: %w", err)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
2023-03-10 19:58:32 +05:30
|
|
|
ws = append(ws, ownerHooks...)
|
2019-11-02 04:21:22 +05:30
|
|
|
}
|
|
|
|
|
2020-03-09 03:38:05 +05:30
|
|
|
// Add any admin-defined system webhooks
|
2024-04-09 15:29:10 +05:30
|
|
|
systemHooks, err := webhook_model.GetSystemWebhooks(ctx, true)
|
2020-03-09 03:38:05 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("GetSystemWebhooks: %w", err)
|
2020-03-09 03:38:05 +05:30
|
|
|
}
|
|
|
|
ws = append(ws, systemHooks...)
|
|
|
|
|
2019-11-02 04:21:22 +05:30
|
|
|
if len(ws) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range ws {
|
2022-10-21 21:51:56 +05:30
|
|
|
if err := PrepareWebhook(ctx, w, event, p); err != nil {
|
2019-11-02 04:21:22 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-06 02:30:20 +05:30
|
|
|
|
|
|
|
// ReplayHookTask replays a webhook task
|
2022-10-21 21:51:56 +05:30
|
|
|
func ReplayHookTask(ctx context.Context, w *webhook_model.Webhook, uuid string) error {
|
|
|
|
task, err := webhook_model.ReplayHookTask(ctx, w.ID, uuid)
|
2022-01-06 02:30:20 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:40:04 +05:30
|
|
|
return enqueueHookTask(task.ID)
|
2022-01-06 02:30:20 +05:30
|
|
|
}
|