Skip to content

Commit a54cc05

Browse files
authored
Fix issue comment number (#30556)
1 parent a871688 commit a54cc05

File tree

5 files changed

+89
-19
lines changed

5 files changed

+89
-19
lines changed

models/issues/comment.go

+33-5
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ func (t CommentType) HasMailReplySupport() bool {
197197
return false
198198
}
199199

200+
func (t CommentType) CountedAsConversation() bool {
201+
for _, ct := range ConversationCountedCommentType() {
202+
if t == ct {
203+
return true
204+
}
205+
}
206+
return false
207+
}
208+
209+
// ConversationCountedCommentType returns the comment types that are counted as a conversation
210+
func ConversationCountedCommentType() []CommentType {
211+
return []CommentType{CommentTypeComment, CommentTypeReview}
212+
}
213+
200214
// RoleInRepo presents the user's participation in the repo
201215
type RoleInRepo string
202216

@@ -893,7 +907,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
893907
}
894908
fallthrough
895909
case CommentTypeComment:
896-
if _, err = db.Exec(ctx, "UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?", opts.Issue.ID); err != nil {
910+
if err := UpdateIssueNumComments(ctx, opts.Issue.ID); err != nil {
897911
return err
898912
}
899913
fallthrough
@@ -1165,8 +1179,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
11651179
return err
11661180
}
11671181

1168-
if comment.Type == CommentTypeComment {
1169-
if _, err := e.ID(comment.IssueID).Decr("num_comments").Update(new(Issue)); err != nil {
1182+
if comment.Type.CountedAsConversation() {
1183+
if err := UpdateIssueNumComments(ctx, comment.IssueID); err != nil {
11701184
return err
11711185
}
11721186
}
@@ -1283,6 +1297,21 @@ func (c *Comment) HasOriginalAuthor() bool {
12831297
return c.OriginalAuthor != "" && c.OriginalAuthorID != 0
12841298
}
12851299

1300+
func UpdateIssueNumCommentsBuilder(issueID int64) *builder.Builder {
1301+
subQuery := builder.Select("COUNT(*)").From("`comment`").Where(
1302+
builder.Eq{"issue_id": issueID}.And(
1303+
builder.In("`type`", ConversationCountedCommentType()),
1304+
))
1305+
1306+
return builder.Update(builder.Eq{"num_comments": subQuery}).
1307+
From("`issue`").Where(builder.Eq{"id": issueID})
1308+
}
1309+
1310+
func UpdateIssueNumComments(ctx context.Context, issueID int64) error {
1311+
_, err := db.GetEngine(ctx).Exec(UpdateIssueNumCommentsBuilder(issueID))
1312+
return err
1313+
}
1314+
12861315
// InsertIssueComments inserts many comments of issues.
12871316
func InsertIssueComments(ctx context.Context, comments []*Comment) error {
12881317
if len(comments) == 0 {
@@ -1315,8 +1344,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
13151344
}
13161345

13171346
for _, issueID := range issueIDs {
1318-
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
1319-
issueID, CommentTypeComment, issueID); err != nil {
1347+
if err := UpdateIssueNumComments(ctx, issueID); err != nil {
13201348
return err
13211349
}
13221350
}

models/issues/comment_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,12 @@ func TestMigrate_InsertIssueComments(t *testing.T) {
115115

116116
unittest.CheckConsistencyFor(t, &issues_model.Issue{})
117117
}
118+
119+
func Test_UpdateIssueNumComments(t *testing.T) {
120+
assert.NoError(t, unittest.PrepareTestDatabase())
121+
issue2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
122+
123+
assert.NoError(t, issues_model.UpdateIssueNumComments(db.DefaultContext, issue2.ID))
124+
issue2 = unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
125+
assert.EqualValues(t, 1, issue2.NumComments)
126+
}

models/issues/review.go

+4
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ func InsertReviews(ctx context.Context, reviews []*Review) error {
639639
return err
640640
}
641641
}
642+
643+
if err := UpdateIssueNumComments(ctx, review.IssueID); err != nil {
644+
return err
645+
}
642646
}
643647

644648
return committer.Commit()

models/repo.go

+29-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"code.gitea.io/gitea/models/unit"
1717
user_model "code.gitea.io/gitea/models/user"
1818
"code.gitea.io/gitea/modules/log"
19+
20+
"xorm.io/builder"
1921
)
2022

2123
// Init initialize model
@@ -24,7 +26,7 @@ func Init(ctx context.Context) error {
2426
}
2527

2628
type repoChecker struct {
27-
querySQL func(ctx context.Context) ([]map[string][]byte, error)
29+
querySQL func(ctx context.Context) ([]int64, error)
2830
correctSQL func(ctx context.Context, id int64) error
2931
desc string
3032
}
@@ -35,8 +37,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
3537
log.Error("Select %s: %v", checker.desc, err)
3638
return
3739
}
38-
for _, result := range results {
39-
id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
40+
for _, id := range results {
4041
select {
4142
case <-ctx.Done():
4243
log.Warn("CheckRepoStats: Cancelled before checking %s for with id=%d", checker.desc, id)
@@ -51,21 +52,23 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
5152
}
5253
}
5354

54-
func StatsCorrectSQL(ctx context.Context, sql string, id int64) error {
55-
_, err := db.GetEngine(ctx).Exec(sql, id, id)
55+
func StatsCorrectSQL(ctx context.Context, sql any, ids ...any) error {
56+
args := []any{sql}
57+
args = append(args, ids...)
58+
_, err := db.GetEngine(ctx).Exec(args...)
5659
return err
5760
}
5861

5962
func repoStatsCorrectNumWatches(ctx context.Context, id int64) error {
60-
return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?", id)
63+
return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?", id, id)
6164
}
6265

6366
func repoStatsCorrectNumStars(ctx context.Context, id int64) error {
64-
return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?", id)
67+
return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?", id, id)
6568
}
6669

6770
func labelStatsCorrectNumIssues(ctx context.Context, id int64) error {
68-
return StatsCorrectSQL(ctx, "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?", id)
71+
return StatsCorrectSQL(ctx, "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?", id, id)
6972
}
7073

7174
func labelStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
@@ -102,11 +105,11 @@ func milestoneStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
102105
}
103106

104107
func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
105-
return StatsCorrectSQL(ctx, "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?", id)
108+
return StatsCorrectSQL(ctx, "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?", id, id)
106109
}
107110

108111
func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
109-
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, issues_model.UpdateIssueNumCommentsBuilder(id))
110113
}
111114

112115
func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
@@ -125,9 +128,12 @@ func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
125128
return repo_model.UpdateRepoIssueNumbers(ctx, id, true, true)
126129
}
127130

128-
func statsQuery(args ...any) func(context.Context) ([]map[string][]byte, error) {
129-
return func(ctx context.Context) ([]map[string][]byte, error) {
130-
return db.GetEngine(ctx).Query(args...)
131+
// statsQuery returns a function that queries the database for a list of IDs
132+
// sql could be a string or a *builder.Builder
133+
func statsQuery(sql any, args ...any) func(context.Context) ([]int64, error) {
134+
return func(ctx context.Context) ([]int64, error) {
135+
var ids []int64
136+
return ids, db.GetEngine(ctx).SQL(sql, args...).Find(&ids)
131137
}
132138
}
133139

@@ -198,7 +204,16 @@ func CheckRepoStats(ctx context.Context) error {
198204
},
199205
// Issue.NumComments
200206
{
201-
statsQuery("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)"),
207+
statsQuery(builder.Select("`issue`.id").From("`issue`").Where(
208+
builder.Neq{
209+
"`issue`.num_comments": builder.Select("COUNT(*)").From("`comment`").Where(
210+
builder.Expr("issue_id = `issue`.id").And(
211+
builder.In("type", issues_model.ConversationCountedCommentType()),
212+
),
213+
),
214+
},
215+
),
216+
),
202217
repoStatsCorrectIssueNumComments,
203218
"issue count 'num_comments'",
204219
},

models/repo_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"code.gitea.io/gitea/models/db"
10+
issues_model "code.gitea.io/gitea/models/issues"
1011
"code.gitea.io/gitea/models/unittest"
1112

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

2324
assert.NoError(t, DoctorUserStarNum(db.DefaultContext))
2425
}
26+
27+
func Test_repoStatsCorrectIssueNumComments(t *testing.T) {
28+
assert.NoError(t, unittest.PrepareTestDatabase())
29+
30+
issue2 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
31+
assert.NotNil(t, issue2)
32+
assert.EqualValues(t, 0, issue2.NumComments) // the fixture data is wrong, but we don't fix it here
33+
34+
assert.NoError(t, repoStatsCorrectIssueNumComments(db.DefaultContext, 2))
35+
// reload the issue
36+
issue2 = unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2})
37+
assert.EqualValues(t, 1, issue2.NumComments)
38+
}

0 commit comments

Comments
 (0)