Skip to content

Commit 87ab9ea

Browse files
committed
Fix issue comment number
1 parent 2da1dcf commit 87ab9ea

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

models/db/engine.go

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type Engine interface {
5757
SumInt(bean any, columnName string) (res int64, err error)
5858
Sync(...any) error
5959
Select(string) *xorm.Session
60+
SetExpr(column string, expression any) *xorm.Session
6061
NotIn(string, ...any) *xorm.Session
6162
OrderBy(any, ...any) *xorm.Session
6263
Exist(...any) (bool, error)

models/issues/comment.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
868868
}
869869
fallthrough
870870
case CommentTypeComment:
871-
if _, err = db.Exec(ctx, "UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
871+
if err := UpdateIssueNumComments(ctx, opts.Issue.ID); err != nil {
872872
return err
873873
}
874874
fallthrough
@@ -1148,8 +1148,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
11481148
return err
11491149
}
11501150

1151-
if comment.Type == CommentTypeComment {
1152-
if _, err := e.ID(comment.IssueID).Decr("num_comments").Update(new(Issue)); err != nil {
1151+
if comment.Type == CommentTypeComment || comment.Type == CommentTypeReview {
1152+
if err := UpdateIssueNumComments(ctx, comment.IssueID); err != nil {
11531153
return err
11541154
}
11551155
}
@@ -1266,6 +1266,26 @@ func (c *Comment) HasOriginalAuthor() bool {
12661266
return c.OriginalAuthor != "" && c.OriginalAuthorID != 0
12671267
}
12681268

1269+
func CountCommentsBuilder(issueID int64) *builder.Builder {
1270+
return builder.Select("count(*)").From("comment").Where(builder.Eq{
1271+
"issue_id": issueID,
1272+
}.And(builder.Or(
1273+
builder.Eq{"type": CommentTypeComment},
1274+
builder.Eq{"type": CommentTypeReview},
1275+
builder.Eq{"type": CommentTypeCode},
1276+
)).And(builder.Neq{
1277+
"invalidated": true,
1278+
}))
1279+
}
1280+
1281+
func UpdateIssueNumComments(ctx context.Context, issueID int64) error {
1282+
_, err := db.GetEngine(ctx).
1283+
SetExpr("num_comments", CountCommentsBuilder(issueID)).
1284+
ID(issueID).
1285+
Update(new(Issue))
1286+
return err
1287+
}
1288+
12691289
// InsertIssueComments inserts many comments of issues.
12701290
func InsertIssueComments(ctx context.Context, comments []*Comment) error {
12711291
if len(comments) == 0 {
@@ -1298,8 +1318,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
12981318
}
12991319

13001320
for _, issueID := range issueIDs {
1301-
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
1302-
issueID, CommentTypeComment, issueID); err != nil {
1321+
if err := UpdateIssueNumComments(ctx, issueID); err != nil {
13031322
return err
13041323
}
13051324
}

models/repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
109109
}
110110

111111
func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
112-
return StatsCorrectSQL(ctx, "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND type=0) WHERE id=?", id)
112+
return StatsCorrectSQL(ctx, "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND (type=0 or type=22)) WHERE id=?", id)
113113
}
114114

115115
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
@@ -201,7 +201,7 @@ func CheckRepoStats(ctx context.Context) error {
201201
},
202202
// Issue.NumComments
203203
{
204-
statsQuery("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)"),
204+
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))"),
205205
repoStatsCorrectIssueNumComments,
206206
"issue count 'num_comments'",
207207
},

0 commit comments

Comments
 (0)