Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple HookTask from Repository #17940

Merged
merged 24 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
94c442a
Decouple HookTask from Repository.
KN4CK3R Dec 9, 2021
09a677c
Remove hook tasks when deleting repo.
KN4CK3R Dec 10, 2021
398827b
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Dec 10, 2021
252d1b2
Revert removed column and mark as unused.
KN4CK3R Dec 12, 2021
42f0bbb
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Jan 5, 2022
33e6b58
Removed RepoID from ReplayHookTask.
KN4CK3R Jan 6, 2022
e607055
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Feb 17, 2022
98e0aa5
Merge branch 'main' into feature-user-org-webhook
lunny Feb 22, 2022
5874c7d
Merge branch 'main' into feature-user-org-webhook
lunny Mar 21, 2022
1a26dcb
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Mar 31, 2022
b05d84f
Renamed struct.
KN4CK3R Mar 31, 2022
f3123b5
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R May 9, 2022
c2d6e11
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R May 9, 2022
4f06a8c
Merge branch 'feature-user-org-webhook' of https://github.com/KN4CK3R…
KN4CK3R May 9, 2022
dcd036d
Merge branch 'main' into feature-user-org-webhook
6543 May 12, 2022
3f8c549
use builder and add to beens too
6543 May 12, 2022
8d5198b
jup
6543 May 12, 2022
cd71a9d
Update models/webhook/hooktask.go
6543 May 12, 2022
4011cbc
fix
6543 May 12, 2022
fb453ec
fix lint
6543 May 12, 2022
c576eb9
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Oct 19, 2022
f454853
Put individual hook tasks in queue.
KN4CK3R Oct 20, 2022
aec14a5
Merge branch 'main' of https://github.com/go-gitea/gitea into feature…
KN4CK3R Oct 20, 2022
9f79d44
Merge branch 'main' into feature-user-org-webhook
6543 Oct 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion models/fixtures/hook_task.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-
id: 1
repo_id: 1
hook_id: 1
uuid: uuid1
is_delivered: true
11 changes: 10 additions & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,14 +765,23 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
return err
}

hooks, err := webhook.ListWebhooksByOptsCtx(ctx, &webhook.ListWebhookOptions{RepoID: repoID})
if err != nil {
return err
}
for _, hook := range hooks {
if err := db.DeleteBeans(ctx, &webhook.HookTask{HookID: hook.ID}); err != nil {
return err
}
}

6543 marked this conversation as resolved.
Show resolved Hide resolved
if err := db.DeleteBeans(ctx,
&Access{RepoID: repo.ID},
&Action{RepoID: repo.ID},
&Collaboration{RepoID: repoID},
&Comment{RefRepoID: repoID},
&CommitStatus{RepoID: repoID},
&DeletedBranch{RepoID: repoID},
&webhook.HookTask{RepoID: repoID},
6543 marked this conversation as resolved.
Show resolved Hide resolved
&LFSLock{RepoID: repoID},
&repo_model.LanguageStat{RepoID: repoID},
&Milestone{RepoID: repoID},
Expand Down
22 changes: 4 additions & 18 deletions models/webhook/hooktask.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type HookResponse struct {
// HookTask represents a hook task.
type HookTask struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"INDEX"`
lunny marked this conversation as resolved.
Show resolved Hide resolved
RepoID int64 `xorm:"INDEX"` // unused
6543 marked this conversation as resolved.
Show resolved Hide resolved
6543 marked this conversation as resolved.
Show resolved Hide resolved
HookID int64
UUID string
api.Payloader `xorm:"-"`
Expand Down Expand Up @@ -191,10 +191,8 @@ func UpdateHookTask(t *HookTask) error {
}

// 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 {
func ReplayHookTask(hookID int64, uuid string) error {
return db.WithTx(func(ctx context.Context) error {
task := &HookTask{
HookID: hookID,
UUID: uuid,
Expand All @@ -209,17 +207,14 @@ func ReplayHookTask(hookID int64, uuid string) (*HookTask, error) {
}
}

newTask = &HookTask{
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
Expand All @@ -231,15 +226,6 @@ func FindUndeliveredHookTasks() ([]*HookTask, error) {
return tasks, nil
}

// FindRepoUndeliveredHookTasks represents find the undelivered hook tasks of one repository
func FindRepoUndeliveredHookTasks(repoID int64) ([]*HookTask, error) {
tasks := make([]*HookTask, 0, 5)
if err := db.GetEngine(db.DefaultContext).Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
return nil, err
}
return tasks, nil
}

// CleanupHookTaskTable deletes rows from hook_task as needed.
func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType, olderThan time.Duration, numberToKeep int) error {
log.Trace("Doing: CleanupHookTaskTable")
Expand Down
8 changes: 5 additions & 3 deletions models/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,10 @@ func (opts *ListWebhookOptions) toCond() builder.Cond {
return cond
}

func listWebhooksByOpts(e db.Engine, opts *ListWebhookOptions) ([]*Webhook, error) {
sess := e.Where(opts.toCond())
// ListWebhooksByOptsCtx return webhooks based on options
func ListWebhooksByOptsCtx(ctx context.Context, opts *ListWebhookOptions) ([]*Webhook, error) {
sess := db.GetEngine(ctx).
Where(opts.toCond())

if opts.Page != 0 {
sess = db.SetSessionPagination(sess, opts)
Expand All @@ -463,7 +465,7 @@ func listWebhooksByOpts(e db.Engine, opts *ListWebhookOptions) ([]*Webhook, erro

// ListWebhooksByOpts return webhooks based on options
func ListWebhooksByOpts(opts *ListWebhookOptions) ([]*Webhook, error) {
return listWebhooksByOpts(db.GetEngine(db.DefaultContext), opts)
return ListWebhooksByOptsCtx(db.DefaultContext, opts)
}

// CountWebhooksByOpts count webhooks based on options and ignore pagination
Expand Down
7 changes: 0 additions & 7 deletions models/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ func TestHookTasks(t *testing.T) {
func TestCreateHookTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
RepoID: 3,
HookID: 3,
Payloader: &api.PushPayload{},
}
Expand All @@ -231,7 +230,6 @@ func TestUpdateHookTask(t *testing.T) {
func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
RepoID: 3,
HookID: 3,
Payloader: &api.PushPayload{},
IsDelivered: true,
Expand All @@ -248,7 +246,6 @@ func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
RepoID: 2,
HookID: 4,
Payloader: &api.PushPayload{},
IsDelivered: false,
Expand All @@ -264,7 +261,6 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
RepoID: 2,
HookID: 4,
Payloader: &api.PushPayload{},
IsDelivered: true,
Expand All @@ -281,7 +277,6 @@ func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
RepoID: 3,
HookID: 3,
Payloader: &api.PushPayload{},
IsDelivered: true,
Expand All @@ -298,7 +293,6 @@ func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
RepoID: 2,
HookID: 4,
Payloader: &api.PushPayload{},
IsDelivered: false,
Expand All @@ -314,7 +308,6 @@ func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
hookTask := &HookTask{
RepoID: 2,
HookID: 4,
Payloader: &api.PushPayload{},
IsDelivered: true,
Expand Down