1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-01-07 21:17:44 +03:00

Merge branch 'lunny/fix_issue_comment_num' into lunny/fix_issue_comment_num2

This commit is contained in:
Lunny Xiao 2024-12-27 21:11:31 -08:00
commit 9c4d2501a5
2 changed files with 26 additions and 7 deletions

View File

@ -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
@ -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
}
}
@ -1300,6 +1300,26 @@ 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.Or(
builder.Eq{"type": CommentTypeComment},
builder.Eq{"type": CommentTypeReview},
builder.Eq{"type": CommentTypeCode},
)).And(builder.Neq{
"invalidated": true,
}))
}
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 {
@ -1332,8 +1352,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
}
}

View File

@ -106,7 +106,7 @@ 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, "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND (type=0 or type=22)) WHERE id=?", id)
}
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
@ -198,7 +198,7 @@ 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("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND (type=0 OR type=22))"),
repoStatsCorrectIssueNumComments,
"issue count 'num_comments'",
},