aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/fixtures/hook_task.yml32
-rw-r--r--models/migrations/migrations.go2
-rw-r--r--models/migrations/v1_22/v290.go17
-rw-r--r--models/webhook/hooktask.go26
-rw-r--r--models/webhook/webhook_test.go61
5 files changed, 97 insertions, 41 deletions
diff --git a/models/fixtures/hook_task.yml b/models/fixtures/hook_task.yml
index 6dbb10151a..d573406b36 100644
--- a/models/fixtures/hook_task.yml
+++ b/models/fixtures/hook_task.yml
@@ -3,3 +3,35 @@
hook_id: 1
uuid: uuid1
is_delivered: true
+ is_succeed: false
+ request_content: >
+ {
+ "url": "/matrix-delivered",
+ "http_method":"PUT",
+ "headers": {
+ "X-Head": "42"
+ },
+ "body": "{}"
+ }
+
+-
+ id: 2
+ hook_id: 1
+ uuid: uuid2
+ is_delivered: false
+
+-
+ id: 3
+ hook_id: 1
+ uuid: uuid3
+ is_delivered: true
+ is_succeed: true
+ payload_content: '{"key":"value"}' # legacy task, payload saved in payload_content (and not in request_content)
+ request_content: >
+ {
+ "url": "/matrix-success",
+ "http_method":"PUT",
+ "headers": {
+ "X-Head": "42"
+ }
+ }
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index d40866f3e9..ce77432db4 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -564,6 +564,8 @@ var migrations = []Migration{
NewMigration("Add user_blocking table", v1_22.AddUserBlockingTable),
// v289 -> v290
NewMigration("Add default_wiki_branch to repository table", v1_22.AddDefaultWikiBranch),
+ // v290 -> v291
+ NewMigration("Add PayloadVersion to HookTask", v1_22.AddPayloadVersionToHookTaskTable),
}
// GetCurrentDBVersion returns the current db version
diff --git a/models/migrations/v1_22/v290.go b/models/migrations/v1_22/v290.go
new file mode 100644
index 0000000000..e9b7f504ba
--- /dev/null
+++ b/models/migrations/v1_22/v290.go
@@ -0,0 +1,17 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_22 //nolint
+
+import (
+ "xorm.io/xorm"
+)
+
+type HookTask struct {
+ PayloadVersion int `xorm:"DEFAULT 1"`
+}
+
+func AddPayloadVersionToHookTaskTable(x *xorm.Engine) error {
+ // create missing column
+ return x.Sync(new(HookTask))
+}
diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go
index 2fb655ebca..ff3fdbadb2 100644
--- a/models/webhook/hooktask.go
+++ b/models/webhook/hooktask.go
@@ -5,13 +5,13 @@ package webhook
import (
"context"
+ "errors"
"time"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
- api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
webhook_module "code.gitea.io/gitea/modules/webhook"
@@ -31,6 +31,7 @@ type HookRequest struct {
URL string `json:"url"`
HTTPMethod string `json:"http_method"`
Headers map[string]string `json:"headers"`
+ Body string `json:"body"`
}
// HookResponse represents hook task response information.
@@ -45,11 +46,15 @@ type HookTask struct {
ID int64 `xorm:"pk autoincr"`
HookID int64 `xorm:"index"`
UUID string `xorm:"unique"`
- api.Payloader `xorm:"-"`
PayloadContent string `xorm:"LONGTEXT"`
- EventType webhook_module.HookEventType
- IsDelivered bool
- Delivered timeutil.TimeStampNano
+ // PayloadVersion number to allow for smooth version upgrades:
+ // - PayloadVersion 1: PayloadContent contains the JSON as sent to the URL
+ // - PayloadVersion 2: PayloadContent contains the original event
+ PayloadVersion int `xorm:"DEFAULT 1"`
+
+ EventType webhook_module.HookEventType
+ IsDelivered bool
+ Delivered timeutil.TimeStampNano
// History info.
IsSucceed bool
@@ -115,16 +120,12 @@ func HookTasks(ctx context.Context, hookID int64, page int) ([]*HookTask, error)
// it handles conversion from Payload to PayloadContent.
func CreateHookTask(ctx context.Context, t *HookTask) (*HookTask, error) {
t.UUID = gouuid.New().String()
- if t.Payloader != nil {
- data, err := t.Payloader.JSONPayload()
- if err != nil {
- return nil, err
- }
- t.PayloadContent = string(data)
- }
if t.Delivered == 0 {
t.Delivered = timeutil.TimeStampNanoNow()
}
+ if t.PayloadVersion == 0 {
+ return nil, errors.New("missing HookTask.PayloadVersion")
+ }
return t, db.Insert(ctx, t)
}
@@ -165,6 +166,7 @@ func ReplayHookTask(ctx context.Context, hookID int64, uuid string) (*HookTask,
HookID: task.HookID,
PayloadContent: task.PayloadContent,
EventType: task.EventType,
+ PayloadVersion: task.PayloadVersion,
})
}
diff --git a/models/webhook/webhook_test.go b/models/webhook/webhook_test.go
index c70c8e99fc..f4403776ce 100644
--- a/models/webhook/webhook_test.go
+++ b/models/webhook/webhook_test.go
@@ -12,7 +12,6 @@ import (
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/optional"
- api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
webhook_module "code.gitea.io/gitea/modules/webhook"
@@ -35,8 +34,10 @@ func TestWebhook_History(t *testing.T) {
webhook := unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 1})
tasks, err := webhook.History(db.DefaultContext, 0)
assert.NoError(t, err)
- if assert.Len(t, tasks, 1) {
- assert.Equal(t, int64(1), tasks[0].ID)
+ if assert.Len(t, tasks, 3) {
+ assert.Equal(t, int64(3), tasks[0].ID)
+ assert.Equal(t, int64(2), tasks[1].ID)
+ assert.Equal(t, int64(1), tasks[2].ID)
}
webhook = unittest.AssertExistsAndLoadBean(t, &Webhook{ID: 2})
@@ -197,8 +198,10 @@ func TestHookTasks(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTasks, err := HookTasks(db.DefaultContext, 1, 1)
assert.NoError(t, err)
- if assert.Len(t, hookTasks, 1) {
- assert.Equal(t, int64(1), hookTasks[0].ID)
+ if assert.Len(t, hookTasks, 3) {
+ assert.Equal(t, int64(3), hookTasks[0].ID)
+ assert.Equal(t, int64(2), hookTasks[1].ID)
+ assert.Equal(t, int64(1), hookTasks[2].ID)
}
hookTasks, err = HookTasks(db.DefaultContext, unittest.NonexistentID, 1)
@@ -209,8 +212,8 @@ func TestHookTasks(t *testing.T) {
func TestCreateHookTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
- HookID: 3,
- Payloader: &api.PushPayload{},
+ HookID: 3,
+ PayloadVersion: 2,
}
unittest.AssertNotExistsBean(t, hookTask)
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -232,10 +235,10 @@ func TestUpdateHookTask(t *testing.T) {
func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
- HookID: 3,
- Payloader: &api.PushPayload{},
- IsDelivered: true,
- Delivered: timeutil.TimeStampNanoNow(),
+ HookID: 3,
+ IsDelivered: true,
+ Delivered: timeutil.TimeStampNanoNow(),
+ PayloadVersion: 2,
}
unittest.AssertNotExistsBean(t, hookTask)
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -249,9 +252,9 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
- HookID: 4,
- Payloader: &api.PushPayload{},
- IsDelivered: false,
+ HookID: 4,
+ IsDelivered: false,
+ PayloadVersion: 2,
}
unittest.AssertNotExistsBean(t, hookTask)
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -265,10 +268,10 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
- HookID: 4,
- Payloader: &api.PushPayload{},
- IsDelivered: true,
- Delivered: timeutil.TimeStampNanoNow(),
+ HookID: 4,
+ IsDelivered: true,
+ Delivered: timeutil.TimeStampNanoNow(),
+ PayloadVersion: 2,
}
unittest.AssertNotExistsBean(t, hookTask)
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -282,10 +285,10 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
- HookID: 3,
- Payloader: &api.PushPayload{},
- IsDelivered: true,
- Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -8).UnixNano()),
+ HookID: 3,
+ IsDelivered: true,
+ Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -8).UnixNano()),
+ PayloadVersion: 2,
}
unittest.AssertNotExistsBean(t, hookTask)
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -299,9 +302,9 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
- HookID: 4,
- Payloader: &api.PushPayload{},
- IsDelivered: false,
+ HookID: 4,
+ IsDelivered: false,
+ PayloadVersion: 2,
}
unittest.AssertNotExistsBean(t, hookTask)
_, err := CreateHookTask(db.DefaultContext, hookTask)
@@ -315,10 +318,10 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
- HookID: 4,
- Payloader: &api.PushPayload{},
- IsDelivered: true,
- Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -6).UnixNano()),
+ HookID: 4,
+ IsDelivered: true,
+ Delivered: timeutil.TimeStampNano(time.Now().AddDate(0, 0, -6).UnixNano()),
+ PayloadVersion: 2,
}
unittest.AssertNotExistsBean(t, hookTask)
_, err := CreateHookTask(db.DefaultContext, hookTask)