Skip to content

Commit fe32ffe

Browse files
authored
Merge updatecommentattachment functions (#33044)
Extract from #32178
1 parent d45456b commit fe32ffe

File tree

4 files changed

+42
-50
lines changed

4 files changed

+42
-50
lines changed

models/issues/comment.go

+20-37
Original file line numberDiff line numberDiff line change
@@ -592,26 +592,26 @@ func (c *Comment) LoadAttachments(ctx context.Context) error {
592592
return nil
593593
}
594594

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)
595+
// UpdateCommentAttachments update attachments by UUIDs for the comment
596+
func UpdateCommentAttachments(ctx context.Context, c *Comment, uuids []string) error {
597+
if len(uuids) == 0 {
598+
return nil
606599
}
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)
600+
return db.WithTx(ctx, func(ctx context.Context) error {
601+
attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, uuids)
602+
if err != nil {
603+
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
612604
}
613-
}
614-
return committer.Commit()
605+
for i := 0; i < len(attachments); i++ {
606+
attachments[i].IssueID = c.IssueID
607+
attachments[i].CommentID = c.ID
608+
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {
609+
return fmt.Errorf("update attachment [id: %d]: %w", attachments[i].ID, err)
610+
}
611+
}
612+
c.Attachments = attachments
613+
return nil
614+
})
615615
}
616616

617617
// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
@@ -878,7 +878,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
878878
// Check comment type.
879879
switch opts.Type {
880880
case CommentTypeCode:
881-
if err = updateAttachments(ctx, opts, comment); err != nil {
881+
if err = UpdateCommentAttachments(ctx, comment, opts.Attachments); err != nil {
882882
return err
883883
}
884884
if comment.ReviewID != 0 {
@@ -898,7 +898,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
898898
}
899899
fallthrough
900900
case CommentTypeReview:
901-
if err = updateAttachments(ctx, opts, comment); err != nil {
901+
if err = UpdateCommentAttachments(ctx, comment, opts.Attachments); err != nil {
902902
return err
903903
}
904904
case CommentTypeReopen, CommentTypeClose:
@@ -910,23 +910,6 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
910910
return UpdateIssueCols(ctx, opts.Issue, "updated_unix")
911911
}
912912

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-
930913
func createDeadlineComment(ctx context.Context, doer *user_model.User, issue *Issue, newDeadlineUnix timeutil.TimeStamp) (*Comment, error) {
931914
var content string
932915
var commentType CommentType

models/issues/comment_test.go

+18
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

models/issues/issue_update.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -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
}

routers/web/repo/issue.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func updateAttachments(ctx *context.Context, item any, files []string) error {
602602
case *issues_model.Issue:
603603
err = issues_model.UpdateIssueAttachments(ctx, content.ID, files)
604604
case *issues_model.Comment:
605-
err = content.UpdateAttachments(ctx, files)
605+
err = issues_model.UpdateCommentAttachments(ctx, content, files)
606606
default:
607607
return fmt.Errorf("unknown Type: %T", content)
608608
}

0 commit comments

Comments
 (0)