1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-01-07 21:17:44 +03:00
This commit is contained in:
Lunny Xiao 2024-12-27 21:35:58 -08:00
parent 9c4d2501a5
commit c9c3e76446
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
4 changed files with 29 additions and 6 deletions

View File

@ -1303,10 +1303,9 @@ func (c *Comment) HasOriginalAuthor() bool {
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.In("type",
CommentTypeComment,
CommentTypeReview,
)).And(builder.Neq{
"invalidated": true,
}))

View File

@ -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)
}

View File

@ -6,6 +6,7 @@ package models
import (
"context"
"fmt"
"strconv"
_ "image/jpeg" // Needed for jpeg support
@ -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 or type=22)) 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 {
@ -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 OR type=22))"),
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)),
repoStatsCorrectIssueNumComments,
"issue count 'num_comments'",
},

View File

@ -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"
@ -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)
}