loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[BUG] Don't fire notification for comment of pending review' (#4487) from gusted/webhook-issue into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4487
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>

+173 -4
+2
models/fixtures/comment.yml
··· 14 14 content: "good work!" 15 15 created_unix: 946684811 16 16 updated_unix: 946684811 17 + content_version: 1 17 18 - 18 19 id: 3 19 20 type: 0 # comment ··· 33 34 tree_path: "README.md" 34 35 created_unix: 946684812 35 36 invalidated: false 37 + content_version: 1 36 38 - 37 39 id: 5 38 40 type: 21 # code comment
+1
release-notes/4487.md
··· 1 + Do not fire webhook notifications for updates and deletions of comments that are part of an ongoing review (a review that is still in draft). Also, content history will not be saved for such comments, to avoid exposing fixing embarrassing typos you've have made while the review was still pending.
+15 -3
services/issue/comments.go
··· 75 75 76 76 // UpdateComment updates information of comment. 77 77 func UpdateComment(ctx context.Context, c *issues_model.Comment, contentVersion int, doer *user_model.User, oldContent string) error { 78 - needsContentHistory := c.Content != oldContent && c.Type.HasContentSupport() 78 + if err := c.LoadReview(ctx); err != nil { 79 + return err 80 + } 81 + isPartOfPendingReview := c.Review != nil && c.Review.Type == issues_model.ReviewTypePending 82 + 83 + needsContentHistory := c.Content != oldContent && c.Type.HasContentSupport() && !isPartOfPendingReview 79 84 if needsContentHistory { 80 85 hasContentHistory, err := issues_model.HasIssueContentHistory(ctx, c.IssueID, c.ID) 81 86 if err != nil { ··· 104 109 } 105 110 } 106 111 107 - notify_service.UpdateComment(ctx, doer, c, oldContent) 112 + if !isPartOfPendingReview { 113 + notify_service.UpdateComment(ctx, doer, c, oldContent) 114 + } 108 115 109 116 return nil 110 117 } ··· 118 125 return err 119 126 } 120 127 121 - notify_service.DeleteComment(ctx, doer, comment) 128 + if err := comment.LoadReview(ctx); err != nil { 129 + return err 130 + } 131 + if comment.Review == nil || comment.Review.Type != issues_model.ReviewTypePending { 132 + notify_service.DeleteComment(ctx, doer, comment) 133 + } 122 134 123 135 return nil 124 136 }
+147
services/issue/comments_test.go
··· 1 + // Copyright 2024 The Forgejo Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package issue_test 5 + 6 + import ( 7 + "testing" 8 + 9 + "code.gitea.io/gitea/models/db" 10 + issues_model "code.gitea.io/gitea/models/issues" 11 + "code.gitea.io/gitea/models/unittest" 12 + user_model "code.gitea.io/gitea/models/user" 13 + webhook_model "code.gitea.io/gitea/models/webhook" 14 + "code.gitea.io/gitea/modules/setting" 15 + "code.gitea.io/gitea/modules/test" 16 + issue_service "code.gitea.io/gitea/services/issue" 17 + "code.gitea.io/gitea/tests" 18 + 19 + _ "code.gitea.io/gitea/services/webhook" 20 + 21 + "github.com/stretchr/testify/assert" 22 + "github.com/stretchr/testify/require" 23 + ) 24 + 25 + func TestDeleteComment(t *testing.T) { 26 + // Use the webhook notification to check if a notification is fired for an action. 27 + defer test.MockVariableValue(&setting.DisableWebhooks, false)() 28 + require.NoError(t, unittest.PrepareTestDatabase()) 29 + 30 + t.Run("Normal comment", func(t *testing.T) { 31 + defer tests.PrintCurrentTest(t)() 32 + 33 + comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2}) 34 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}) 35 + unittest.AssertCount(t, &issues_model.Reaction{CommentID: comment.ID}, 2) 36 + 37 + require.NoError(t, webhook_model.CreateWebhook(db.DefaultContext, &webhook_model.Webhook{ 38 + RepoID: issue.RepoID, 39 + IsActive: true, 40 + Events: `{"choose_events":true,"events":{"issue_comment": true}}`, 41 + })) 42 + hookTaskCount := unittest.GetCount(t, &webhook_model.HookTask{}) 43 + 44 + require.NoError(t, issue_service.DeleteComment(db.DefaultContext, nil, comment)) 45 + 46 + // The comment doesn't exist anymore. 47 + unittest.AssertNotExistsBean(t, &issues_model.Comment{ID: comment.ID}) 48 + // Reactions don't exist anymore for this comment. 49 + unittest.AssertNotExistsBean(t, &issues_model.Reaction{CommentID: comment.ID}) 50 + // Number of comments was decreased. 51 + assert.EqualValues(t, issue.NumComments-1, unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}).NumComments) 52 + // A notification was fired for the deletion of this comment. 53 + assert.EqualValues(t, hookTaskCount+1, unittest.GetCount(t, &webhook_model.HookTask{})) 54 + }) 55 + 56 + t.Run("Comment of pending review", func(t *testing.T) { 57 + defer tests.PrintCurrentTest(t)() 58 + 59 + // We have to ensure that this comment's linked review is pending. 60 + comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 4}, "review_id != 0") 61 + review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: comment.ReviewID}) 62 + assert.EqualValues(t, issues_model.ReviewTypePending, review.Type) 63 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}) 64 + 65 + require.NoError(t, webhook_model.CreateWebhook(db.DefaultContext, &webhook_model.Webhook{ 66 + RepoID: issue.RepoID, 67 + IsActive: true, 68 + Events: `{"choose_events":true,"events":{"issue_comment": true}}`, 69 + })) 70 + hookTaskCount := unittest.GetCount(t, &webhook_model.HookTask{}) 71 + 72 + require.NoError(t, issue_service.DeleteComment(db.DefaultContext, nil, comment)) 73 + 74 + // The comment doesn't exist anymore. 75 + unittest.AssertNotExistsBean(t, &issues_model.Comment{ID: comment.ID}) 76 + // Ensure that the number of comments wasn't decreased. 77 + assert.EqualValues(t, issue.NumComments, unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}).NumComments) 78 + // No notification was fired for the deletion of this comment. 79 + assert.EqualValues(t, hookTaskCount, unittest.GetCount(t, &webhook_model.HookTask{})) 80 + }) 81 + } 82 + 83 + func TestUpdateComment(t *testing.T) { 84 + // Use the webhook notification to check if a notification is fired for an action. 85 + defer test.MockVariableValue(&setting.DisableWebhooks, false)() 86 + require.NoError(t, unittest.PrepareTestDatabase()) 87 + 88 + admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{IsAdmin: true}) 89 + t.Run("Normal comment", func(t *testing.T) { 90 + defer tests.PrintCurrentTest(t)() 91 + 92 + comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2}) 93 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}) 94 + unittest.AssertNotExistsBean(t, &issues_model.ContentHistory{CommentID: comment.ID}) 95 + require.NoError(t, webhook_model.CreateWebhook(db.DefaultContext, &webhook_model.Webhook{ 96 + RepoID: issue.RepoID, 97 + IsActive: true, 98 + Events: `{"choose_events":true,"events":{"issue_comment": true}}`, 99 + })) 100 + hookTaskCount := unittest.GetCount(t, &webhook_model.HookTask{}) 101 + oldContent := comment.Content 102 + comment.Content = "Hello!" 103 + 104 + require.NoError(t, issue_service.UpdateComment(db.DefaultContext, comment, 1, admin, oldContent)) 105 + 106 + newComment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2}) 107 + // Content was updated. 108 + assert.EqualValues(t, comment.Content, newComment.Content) 109 + // Content version was updated. 110 + assert.EqualValues(t, 2, newComment.ContentVersion) 111 + // A notification was fired for the update of this comment. 112 + assert.EqualValues(t, hookTaskCount+1, unittest.GetCount(t, &webhook_model.HookTask{})) 113 + // Issue history was saved for this comment. 114 + unittest.AssertExistsAndLoadBean(t, &issues_model.ContentHistory{CommentID: comment.ID, IsFirstCreated: true, ContentText: oldContent}) 115 + unittest.AssertExistsAndLoadBean(t, &issues_model.ContentHistory{CommentID: comment.ID, ContentText: comment.Content}, "is_first_created = false") 116 + }) 117 + 118 + t.Run("Comment of pending review", func(t *testing.T) { 119 + defer tests.PrintCurrentTest(t)() 120 + 121 + comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 4}, "review_id != 0") 122 + review := unittest.AssertExistsAndLoadBean(t, &issues_model.Review{ID: comment.ReviewID}) 123 + assert.EqualValues(t, issues_model.ReviewTypePending, review.Type) 124 + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}) 125 + unittest.AssertNotExistsBean(t, &issues_model.ContentHistory{CommentID: comment.ID}) 126 + require.NoError(t, webhook_model.CreateWebhook(db.DefaultContext, &webhook_model.Webhook{ 127 + RepoID: issue.RepoID, 128 + IsActive: true, 129 + Events: `{"choose_events":true,"events":{"issue_comment": true}}`, 130 + })) 131 + hookTaskCount := unittest.GetCount(t, &webhook_model.HookTask{}) 132 + oldContent := comment.Content 133 + comment.Content = "Hello!" 134 + 135 + require.NoError(t, issue_service.UpdateComment(db.DefaultContext, comment, 1, admin, oldContent)) 136 + 137 + newComment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2}) 138 + // Content was updated. 139 + assert.EqualValues(t, comment.Content, newComment.Content) 140 + // Content version was updated. 141 + assert.EqualValues(t, 2, newComment.ContentVersion) 142 + // No notification was fired for the update of this comment. 143 + assert.EqualValues(t, hookTaskCount, unittest.GetCount(t, &webhook_model.HookTask{})) 144 + // Issue history was not saved for this comment. 145 + unittest.AssertNotExistsBean(t, &issues_model.ContentHistory{CommentID: comment.ID}) 146 + }) 147 + }
+8 -1
services/issue/main_test.go
··· 7 7 "testing" 8 8 9 9 "code.gitea.io/gitea/models/unittest" 10 + "code.gitea.io/gitea/modules/setting" 11 + "code.gitea.io/gitea/services/webhook" 10 12 11 13 _ "code.gitea.io/gitea/models/actions" 12 14 ) 13 15 14 16 func TestMain(m *testing.M) { 15 - unittest.MainTest(m) 17 + unittest.MainTest(m, &unittest.TestOptions{ 18 + SetUp: func() error { 19 + setting.LoadQueueSettings() 20 + return webhook.Init() 21 + }, 22 + }) 16 23 }