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

Fix issue comment number #30556

Merged
merged 22 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions models/issues/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
}
fallthrough
case CommentTypeComment:
if _, err = db.Exec(ctx, "UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
if err := UpdateIssueNumComments(ctx, opts.Issue.ID); err != nil {
return err
}
fallthrough
Expand Down Expand Up @@ -1182,8 +1182,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
return err
}

if comment.Type == CommentTypeComment {
if _, err := e.ID(comment.IssueID).Decr("num_comments").Update(new(Issue)); err != nil {
if comment.Type == CommentTypeComment || comment.Type == CommentTypeReview {
if err := UpdateIssueNumComments(ctx, comment.IssueID); err != nil {
return err
}
}
Expand Down Expand Up @@ -1300,6 +1300,23 @@ func (c *Comment) HasOriginalAuthor() bool {
return c.OriginalAuthor != "" && c.OriginalAuthorID != 0
}

func CountCommentsBuilder(issueID int64) *builder.Builder {
return builder.Select("count(*)").From("comment").Where(builder.Eq{
"issue_id": issueID,
}.And(builder.In("type",
CommentTypeComment,
CommentTypeReview,
)))
}

func UpdateIssueNumComments(ctx context.Context, issueID int64) error {
_, err := db.GetEngine(ctx).
SetExpr("num_comments", CountCommentsBuilder(issueID)).
ID(issueID).
Update(new(Issue))
return err
}

// InsertIssueComments inserts many comments of issues.
func InsertIssueComments(ctx context.Context, comments []*Comment) error {
if len(comments) == 0 {
Expand Down Expand Up @@ -1332,8 +1349,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
}

for _, issueID := range issueIDs {
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
issueID, CommentTypeComment, issueID); err != nil {
if err := UpdateIssueNumComments(ctx, issueID); err != nil {
return err
}
}
Expand Down
9 changes: 9 additions & 0 deletions models/issues/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,12 @@ func TestMigrate_InsertIssueComments(t *testing.T) {

unittest.CheckConsistencyFor(t, &issues_model.Issue{})
}

func Test_UpdateIssueNumComments(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
issue2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})

assert.NoError(t, issues_model.UpdateIssueNumComments(db.DefaultContext, issue2.ID))
issue2 = unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
assert.EqualValues(t, 1, issue2.NumComments)
}
7 changes: 5 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package models

import (
"context"
"fmt"
"strconv"

_ "image/jpeg" // Needed for jpeg support
Expand Down Expand Up @@ -106,7 +107,8 @@ func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
}

func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
return StatsCorrectSQL(ctx, "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND type=0) WHERE id=?", id)
return StatsCorrectSQL(ctx, fmt.Sprintf("UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND (type=%d or type=%d)) WHERE id=?",
issues_model.CommentTypeComment, issues_model.CommentTypeReview), id)
}

func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
Expand Down Expand Up @@ -198,7 +200,8 @@ func CheckRepoStats(ctx context.Context) error {
},
// Issue.NumComments
{
statsQuery("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)"),
statsQuery(fmt.Sprintf("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND (type=%d OR type=%d))",
issues_model.CommentTypeComment, issues_model.CommentTypeReview)),
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
repoStatsCorrectIssueNumComments,
"issue count 'num_comments'",
},
Expand Down
12 changes: 12 additions & 0 deletions models/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"

"github.com/stretchr/testify/assert"
Expand All @@ -22,3 +23,14 @@ func TestDoctorUserStarNum(t *testing.T) {

assert.NoError(t, DoctorUserStarNum(db.DefaultContext))
}

func Test_repoStatsCorrectIssueNumComments(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

issue2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
assert.NotNil(t, issue2)
assert.EqualValues(t, 0, issue2.NumComments) // the fixture data is wrong, but we don't fix it here

assert.NoError(t, repoStatsCorrectIssueNumComments(db.DefaultContext, 2))
assert.EqualValues(t, 1, issue2.NumComments)
}
Loading