Skip to content

Commit e3ed678

Browse files
authored
Move some functions to service layer (#26969)
1 parent b8ad558 commit e3ed678

22 files changed

+733
-748
lines changed

models/issues/comment.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
project_model "code.gitea.io/gitea/models/project"
1818
repo_model "code.gitea.io/gitea/models/repo"
1919
user_model "code.gitea.io/gitea/models/user"
20+
"code.gitea.io/gitea/modules/container"
2021
"code.gitea.io/gitea/modules/git"
2122
"code.gitea.io/gitea/modules/json"
2223
"code.gitea.io/gitea/modules/log"
@@ -1247,3 +1248,44 @@ func FixCommentTypeLabelWithOutsideLabels(ctx context.Context) (int64, error) {
12471248
func (c *Comment) HasOriginalAuthor() bool {
12481249
return c.OriginalAuthor != "" && c.OriginalAuthorID != 0
12491250
}
1251+
1252+
// InsertIssueComments inserts many comments of issues.
1253+
func InsertIssueComments(comments []*Comment) error {
1254+
if len(comments) == 0 {
1255+
return nil
1256+
}
1257+
1258+
issueIDs := make(container.Set[int64])
1259+
for _, comment := range comments {
1260+
issueIDs.Add(comment.IssueID)
1261+
}
1262+
1263+
ctx, committer, err := db.TxContext(db.DefaultContext)
1264+
if err != nil {
1265+
return err
1266+
}
1267+
defer committer.Close()
1268+
for _, comment := range comments {
1269+
if _, err := db.GetEngine(ctx).NoAutoTime().Insert(comment); err != nil {
1270+
return err
1271+
}
1272+
1273+
for _, reaction := range comment.Reactions {
1274+
reaction.IssueID = comment.IssueID
1275+
reaction.CommentID = comment.ID
1276+
}
1277+
if len(comment.Reactions) > 0 {
1278+
if err := db.Insert(ctx, comment.Reactions); err != nil {
1279+
return err
1280+
}
1281+
}
1282+
}
1283+
1284+
for issueID := range issueIDs {
1285+
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
1286+
issueID, CommentTypeComment, issueID); err != nil {
1287+
return err
1288+
}
1289+
}
1290+
return committer.Commit()
1291+
}

models/issues/comment_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,30 @@ func TestAsCommentType(t *testing.T) {
7070
assert.Equal(t, issues_model.CommentTypeComment, issues_model.AsCommentType("comment"))
7171
assert.Equal(t, issues_model.CommentTypePRUnScheduledToAutoMerge, issues_model.AsCommentType("pull_cancel_scheduled_merge"))
7272
}
73+
74+
func TestMigrate_InsertIssueComments(t *testing.T) {
75+
assert.NoError(t, unittest.PrepareTestDatabase())
76+
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
77+
_ = issue.LoadRepo(db.DefaultContext)
78+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: issue.Repo.OwnerID})
79+
reaction := &issues_model.Reaction{
80+
Type: "heart",
81+
UserID: owner.ID,
82+
}
83+
84+
comment := &issues_model.Comment{
85+
PosterID: owner.ID,
86+
Poster: owner,
87+
IssueID: issue.ID,
88+
Issue: issue,
89+
Reactions: []*issues_model.Reaction{reaction},
90+
}
91+
92+
err := issues_model.InsertIssueComments([]*issues_model.Comment{comment})
93+
assert.NoError(t, err)
94+
95+
issueModified := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1})
96+
assert.EqualValues(t, issue.NumComments+1, issueModified.NumComments)
97+
98+
unittest.CheckConsistencyFor(t, &issues_model.Issue{})
99+
}

models/issues/issue.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,50 @@ func IsNewPinAllowed(ctx context.Context, repoID int64, isPull bool) (bool, erro
892892
func IsErrIssueMaxPinReached(err error) bool {
893893
return err == ErrIssueMaxPinReached
894894
}
895+
896+
// InsertIssues insert issues to database
897+
func InsertIssues(issues ...*Issue) error {
898+
ctx, committer, err := db.TxContext(db.DefaultContext)
899+
if err != nil {
900+
return err
901+
}
902+
defer committer.Close()
903+
904+
for _, issue := range issues {
905+
if err := insertIssue(ctx, issue); err != nil {
906+
return err
907+
}
908+
}
909+
return committer.Commit()
910+
}
911+
912+
func insertIssue(ctx context.Context, issue *Issue) error {
913+
sess := db.GetEngine(ctx)
914+
if _, err := sess.NoAutoTime().Insert(issue); err != nil {
915+
return err
916+
}
917+
issueLabels := make([]IssueLabel, 0, len(issue.Labels))
918+
for _, label := range issue.Labels {
919+
issueLabels = append(issueLabels, IssueLabel{
920+
IssueID: issue.ID,
921+
LabelID: label.ID,
922+
})
923+
}
924+
if len(issueLabels) > 0 {
925+
if _, err := sess.Insert(issueLabels); err != nil {
926+
return err
927+
}
928+
}
929+
930+
for _, reaction := range issue.Reactions {
931+
reaction.IssueID = issue.ID
932+
}
933+
934+
if len(issue.Reactions) > 0 {
935+
if _, err := sess.Insert(issue.Reactions); err != nil {
936+
return err
937+
}
938+
}
939+
940+
return nil
941+
}

models/issues/issue_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,3 +573,45 @@ func TestIssueLoadAttributes(t *testing.T) {
573573
}
574574
}
575575
}
576+
577+
func assertCreateIssues(t *testing.T, isPull bool) {
578+
assert.NoError(t, unittest.PrepareTestDatabase())
579+
reponame := "repo1"
580+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame})
581+
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
582+
label := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 1})
583+
milestone := unittest.AssertExistsAndLoadBean(t, &issues_model.Milestone{ID: 1})
584+
assert.EqualValues(t, milestone.ID, 1)
585+
reaction := &issues_model.Reaction{
586+
Type: "heart",
587+
UserID: owner.ID,
588+
}
589+
590+
title := "issuetitle1"
591+
is := &issues_model.Issue{
592+
RepoID: repo.ID,
593+
MilestoneID: milestone.ID,
594+
Repo: repo,
595+
Title: title,
596+
Content: "issuecontent1",
597+
IsPull: isPull,
598+
PosterID: owner.ID,
599+
Poster: owner,
600+
IsClosed: true,
601+
Labels: []*issues_model.Label{label},
602+
Reactions: []*issues_model.Reaction{reaction},
603+
}
604+
err := issues_model.InsertIssues(is)
605+
assert.NoError(t, err)
606+
607+
i := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{Title: title})
608+
unittest.AssertExistsAndLoadBean(t, &issues_model.Reaction{Type: "heart", UserID: owner.ID, IssueID: i.ID})
609+
}
610+
611+
func TestMigrate_CreateIssuesIsPullFalse(t *testing.T) {
612+
assertCreateIssues(t, false)
613+
}
614+
615+
func TestMigrate_CreateIssuesIsPullTrue(t *testing.T) {
616+
assertCreateIssues(t, true)
617+
}

0 commit comments

Comments
 (0)