diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2022-01-05 22:00:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-05 15:00:20 -0600 |
commit | bf7b083cfe47cc922090ce7922b89f7a5030a44d (patch) | |
tree | 62291bc31a1dd28252a8802555d09085d30416ee /models/webhook/hooktask.go | |
parent | a38ba634a4da15fbb2d1b6ac6742cf01c1503ea4 (diff) | |
download | gitea-bf7b083cfe47cc922090ce7922b89f7a5030a44d.tar.gz gitea-bf7b083cfe47cc922090ce7922b89f7a5030a44d.zip |
Add replay of webhooks. (#18191)
Diffstat (limited to 'models/webhook/hooktask.go')
-rw-r--r-- | models/webhook/hooktask.go | 39 |
1 files changed, 33 insertions, 6 deletions
diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go index 1967ded298..1d19ebd24e 100644 --- a/models/webhook/hooktask.go +++ b/models/webhook/hooktask.go @@ -175,18 +175,13 @@ func HookTasks(hookID int64, page int) ([]*HookTask, error) { // CreateHookTask creates a new hook task, // it handles conversion from Payload to PayloadContent. func CreateHookTask(t *HookTask) error { - return createHookTask(db.GetEngine(db.DefaultContext), t) -} - -func createHookTask(e db.Engine, t *HookTask) error { data, err := t.Payloader.JSONPayload() if err != nil { return err } t.UUID = gouuid.New().String() t.PayloadContent = string(data) - _, err = e.Insert(t) - return err + return db.Insert(db.DefaultContext, t) } // UpdateHookTask updates information of hook task. @@ -195,6 +190,38 @@ func UpdateHookTask(t *HookTask) error { return err } +// ReplayHookTask copies a hook task to get re-delivered +func ReplayHookTask(hookID int64, uuid string) (*HookTask, error) { + var newTask *HookTask + + err := db.WithTx(func(ctx context.Context) error { + task := &HookTask{ + HookID: hookID, + UUID: uuid, + } + has, err := db.GetByBean(ctx, task) + if err != nil { + return err + } else if !has { + return ErrHookTaskNotExist{ + HookID: hookID, + UUID: uuid, + } + } + + newTask = &HookTask{ + UUID: gouuid.New().String(), + RepoID: task.RepoID, + HookID: task.HookID, + PayloadContent: task.PayloadContent, + EventType: task.EventType, + } + return db.Insert(ctx, newTask) + }) + + return newTask, err +} + // FindUndeliveredHookTasks represents find the undelivered hook tasks func FindUndeliveredHookTasks() ([]*HookTask, error) { tasks := make([]*HookTask, 0, 10) |