Skip to content

Commit 39b46c7

Browse files
authored
Merge branch 'main' into fix-maven-pkg
2 parents 4f8b091 + c09656e commit 39b46c7

File tree

15 files changed

+209
-137
lines changed

15 files changed

+209
-137
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ _test
99

1010
# IntelliJ
1111
.idea
12+
13+
# IntelliJ Gateway
14+
.uuid
15+
1216
# Goland's output filename can not be set manually
1317
/go_build_*
1418
/gitea_*

models/issues/comment.go

Lines changed: 53 additions & 42 deletions
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

@@ -592,26 +606,26 @@ func (c *Comment) LoadAttachments(ctx context.Context) error {
592606
return nil
593607
}
594608

595-
// UpdateAttachments update attachments by UUIDs for the comment
596-
func (c *Comment) UpdateAttachments(ctx context.Context, uuids []string) error {
597-
ctx, committer, err := db.TxContext(ctx)
598-
if err != nil {
599-
return err
600-
}
601-
defer committer.Close()
602-
603-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
604-
if err != nil {
605-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
609+
// UpdateCommentAttachments update attachments by UUIDs for the comment
610+
func UpdateCommentAttachments(ctx context.Context, c *Comment, uuids []string) error {
611+
if len(uuids) == 0 {
612+
return nil
606613
}
607-
for i := 0; i < len(attachments); i++ {
608-
attachments[i].IssueID = c.IssueID
609-
attachments[i].CommentID = c.ID
610-
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
611-
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
614+
return db.WithTx(ctx, func(ctx context.Context) error {
615+
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
616+
if err != nil {
617+
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
612618
}
613-
}
614-
return committer.Commit()
619+
for i := 0; i < len(attachments); i++ {
620+
attachments[i].IssueID = c.IssueID
621+
attachments[i].CommentID = c.ID
622+
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
623+
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
624+
}
625+
}
626+
c.Attachments = attachments
627+
return nil
628+
})
615629
}
616630

617631
// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
@@ -878,7 +892,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
878892
// Check comment type.
879893
switch opts.Type {
880894
case CommentTypeCode:
881-
if err = updateAttachments(ctx, opts, comment); err != nil {
895+
if err = UpdateCommentAttachments(ctx, comment, opts.Attachments); err != nil {
882896
return err
883897
}
884898
if comment.ReviewID != 0 {
@@ -893,12 +907,12 @@ 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
900914
case CommentTypeReview:
901-
if err = updateAttachments(ctx, opts, comment); err != nil {
915+
if err = UpdateCommentAttachments(ctx, comment, opts.Attachments); err != nil {
902916
return err
903917
}
904918
case CommentTypeReopen, CommentTypeClose:
@@ -910,23 +924,6 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
910924
return UpdateIssueCols(ctx, opts.Issue, "updated_unix")
911925
}
912926

913-
func updateAttachments(ctx context.Context, opts *CreateCommentOptions, comment *Comment) error {
914-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, opts.Attachments)
915-
if err != nil {
916-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", opts.Attachments, err)
917-
}
918-
for i := range attachments {
919-
attachments[i].IssueID = opts.Issue.ID
920-
attachments[i].CommentID = comment.ID
921-
// No assign value could be 0, so ignore AllCols().
922-
if _, err = db.GetEngine(ctx).ID(attachments[i].ID).Update(attachments[i]); err != nil {
923-
return fmt.Errorf("update attachment [%d]: %w", attachments[i].ID, err)
924-
}
925-
}
926-
comment.Attachments = attachments
927-
return nil
928-
}
929-
930927
func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) {
931928
var content string
932929
var commentType CommentType
@@ -1182,8 +1179,8 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
11821179
return err
11831180
}
11841181

1185-
if comment.Type == CommentTypeComment {
1186-
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 {
11871184
return err
11881185
}
11891186
}
@@ -1300,6 +1297,21 @@ func (c *Comment) HasOriginalAuthor() bool {
13001297
return c.OriginalAuthor != "" && c.OriginalAuthorID != 0
13011298
}
13021299

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+
13031315
// InsertIssueComments inserts many comments of issues.
13041316
func InsertIssueComments(ctx context.Context, comments []*Comment) error {
13051317
if len(comments) == 0 {
@@ -1332,8 +1344,7 @@ func InsertIssueComments(ctx context.Context, comments []*Comment) error {
13321344
}
13331345

13341346
for _, issueID := range issueIDs {
1335-
if _, err := db.Exec(ctx, "UPDATE issue set num_comments = (SELECT count(*) FROM comment WHERE issue_id = ? AND `type`=?) WHERE id = ?",
1336-
issueID, CommentTypeComment, issueID); err != nil {
1347+
if err := UpdateIssueNumComments(ctx, issueID); err != nil {
13371348
return err
13381349
}
13391350
}

models/issues/comment_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ func TestCreateComment(t *testing.T) {
4545
unittest.AssertInt64InRange(t, now, then, int64(updatedIssue.UpdatedUnix))
4646
}
4747

48+
func Test_UpdateCommentAttachment(t *testing.T) {
49+
assert.NoError(t, unittest.PrepareTestDatabase())
50+
51+
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
52+
attachment := repo_model.Attachment{
53+
Name: "test.txt",
54+
}
55+
assert.NoError(t, db.Insert(db.DefaultContext, &attachment))
56+
57+
err := issues_model.UpdateCommentAttachments(db.DefaultContext, comment, []string{attachment.UUID})
58+
assert.NoError(t, err)
59+
60+
attachment2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: attachment.ID})
61+
assert.EqualValues(t, attachment.Name, attachment2.Name)
62+
assert.EqualValues(t, comment.ID, attachment2.CommentID)
63+
assert.EqualValues(t, comment.IssueID, attachment2.IssueID)
64+
}
65+
4866
func TestFetchCodeComments(t *testing.T) {
4967
assert.NoError(t, unittest.PrepareTestDatabase())
5068

@@ -97,3 +115,12 @@ func TestMigrate_InsertIssueComments(t *testing.T) {
97115

98116
unittest.CheckConsistencyFor(t, &issues_model.Issue{})
99117
}
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/issue_update.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func UpdateIssueCols(ctx context.Context, issue *Issue, cols ...string) error {
3434
return nil
3535
}
3636

37-
func changeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User, isClosed, isMergePull bool) (*Comment, error) {
37+
func ChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User, isClosed, isMergePull bool) (*Comment, error) {
3838
// Reload the issue
3939
currentIssue, err := GetIssueByID(ctx, issue.ID)
4040
if err != nil {
@@ -134,7 +134,7 @@ func CloseIssue(ctx context.Context, issue *Issue, doer *user_model.User) (*Comm
134134
}
135135
defer committer.Close()
136136

137-
comment, err := changeIssueStatus(ctx, issue, doer, true, false)
137+
comment, err := ChangeIssueStatus(ctx, issue, doer, true, false)
138138
if err != nil {
139139
return nil, err
140140
}
@@ -159,7 +159,7 @@ func ReopenIssue(ctx context.Context, issue *Issue, doer *user_model.User) (*Com
159159
}
160160
defer committer.Close()
161161

162-
comment, err := changeIssueStatus(ctx, issue, doer, false, false)
162+
comment, err := ChangeIssueStatus(ctx, issue, doer, false, false)
163163
if err != nil {
164164
return nil, err
165165
}
@@ -405,19 +405,10 @@ func NewIssueWithIndex(ctx context.Context, doer *user_model.User, opts NewIssue
405405
return err
406406
}
407407

408-
if len(opts.Attachments) > 0 {
409-
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, opts.Attachments)
410-
if err != nil {
411-
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", opts.Attachments, err)
412-
}
413-
414-
for i := 0; i < len(attachments); i++ {
415-
attachments[i].IssueID = opts.Issue.ID
416-
if _, err = e.ID(attachments[i].ID).Update(attachments[i]); err != nil {
417-
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
418-
}
419-
}
408+
if err := UpdateIssueAttachments(ctx, opts.Issue.ID, opts.Attachments); err != nil {
409+
return err
420410
}
411+
421412
if err = opts.Issue.LoadAttributes(ctx); err != nil {
422413
return err
423414
}

models/issues/pull.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -499,65 +499,6 @@ func (pr *PullRequest) IsFromFork() bool {
499499
return pr.HeadRepoID != pr.BaseRepoID
500500
}
501501

502-
// SetMerged sets a pull request to merged and closes the corresponding issue
503-
func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) {
504-
if pr.HasMerged {
505-
return false, fmt.Errorf("PullRequest[%d] already merged", pr.Index)
506-
}
507-
if pr.MergedCommitID == "" || pr.MergedUnix == 0 || pr.Merger == nil {
508-
return false, fmt.Errorf("Unable to merge PullRequest[%d], some required fields are empty", pr.Index)
509-
}
510-
511-
pr.HasMerged = true
512-
sess := db.GetEngine(ctx)
513-
514-
if _, err := sess.Exec("UPDATE `issue` SET `repo_id` = `repo_id` WHERE `id` = ?", pr.IssueID); err != nil {
515-
return false, err
516-
}
517-
518-
if _, err := sess.Exec("UPDATE `pull_request` SET `issue_id` = `issue_id` WHERE `id` = ?", pr.ID); err != nil {
519-
return false, err
520-
}
521-
522-
pr.Issue = nil
523-
if err := pr.LoadIssue(ctx); err != nil {
524-
return false, err
525-
}
526-
527-
if tmpPr, err := GetPullRequestByID(ctx, pr.ID); err != nil {
528-
return false, err
529-
} else if tmpPr.HasMerged {
530-
if pr.Issue.IsClosed {
531-
return false, nil
532-
}
533-
return false, fmt.Errorf("PullRequest[%d] already merged but it's associated issue [%d] is not closed", pr.Index, pr.IssueID)
534-
} else if pr.Issue.IsClosed {
535-
return false, fmt.Errorf("PullRequest[%d] already closed", pr.Index)
536-
}
537-
538-
if err := pr.Issue.LoadRepo(ctx); err != nil {
539-
return false, err
540-
}
541-
542-
if err := pr.Issue.Repo.LoadOwner(ctx); err != nil {
543-
return false, err
544-
}
545-
546-
if _, err := changeIssueStatus(ctx, pr.Issue, pr.Merger, true, true); err != nil {
547-
return false, fmt.Errorf("Issue.changeStatus: %w", err)
548-
}
549-
550-
// reset the conflicted files as there cannot be any if we're merged
551-
pr.ConflictedFiles = []string{}
552-
553-
// We need to save all of the data used to compute this merge as it may have already been changed by TestPatch. FIXME: need to set some state to prevent TestPatch from running whilst we are merging.
554-
if _, err := sess.Where("id = ?", pr.ID).Cols("has_merged, status, merge_base, merged_commit_id, merger_id, merged_unix, conflicted_files").Update(pr); err != nil {
555-
return false, fmt.Errorf("Failed to update pr[%d]: %w", pr.ID, err)
556-
}
557-
558-
return true, nil
559-
}
560-
561502
// NewPullRequest creates new pull request with labels for repository.
562503
func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) {
563504
ctx, committer, err := db.TxContext(ctx)

models/issues/review.go

Lines changed: 4 additions & 0 deletions
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()

0 commit comments

Comments
 (0)