Add replay of webhooks. (#18191)
This commit is contained in:
parent
a38ba634a4
commit
bf7b083cfe
8 changed files with 108 additions and 13 deletions
|
@ -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)
|
||||
|
|
|
@ -41,6 +41,22 @@ func (err ErrWebhookNotExist) Error() string {
|
|||
return fmt.Sprintf("webhook does not exist [id: %d]", err.ID)
|
||||
}
|
||||
|
||||
// ErrHookTaskNotExist represents a "HookTaskNotExist" kind of error.
|
||||
type ErrHookTaskNotExist struct {
|
||||
HookID int64
|
||||
UUID string
|
||||
}
|
||||
|
||||
// IsErrWebhookNotExist checks if an error is a ErrWebhookNotExist.
|
||||
func IsErrHookTaskNotExist(err error) bool {
|
||||
_, ok := err.(ErrHookTaskNotExist)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrHookTaskNotExist) Error() string {
|
||||
return fmt.Sprintf("hook task does not exist [hook: %d, uuid: %s]", err.HookID, err.UUID)
|
||||
}
|
||||
|
||||
// HookContentType is the content type of a web hook
|
||||
type HookContentType int
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue