@@ -592,26 +592,26 @@ func (c *Comment) LoadAttachments(ctx context.Context) error {
592
592
return nil
593
593
}
594
594
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
606
599
}
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 )
612
604
}
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
+ })
615
615
}
616
616
617
617
// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
@@ -878,7 +878,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
878
878
// Check comment type.
879
879
switch opts .Type {
880
880
case CommentTypeCode :
881
- if err = updateAttachments (ctx , opts , comment ); err != nil {
881
+ if err = UpdateCommentAttachments (ctx , comment , opts . Attachments ); err != nil {
882
882
return err
883
883
}
884
884
if comment .ReviewID != 0 {
@@ -898,7 +898,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
898
898
}
899
899
fallthrough
900
900
case CommentTypeReview :
901
- if err = updateAttachments (ctx , opts , comment ); err != nil {
901
+ if err = UpdateCommentAttachments (ctx , comment , opts . Attachments ); err != nil {
902
902
return err
903
903
}
904
904
case CommentTypeReopen , CommentTypeClose :
@@ -910,23 +910,6 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
910
910
return UpdateIssueCols (ctx , opts .Issue , "updated_unix" )
911
911
}
912
912
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
-
930
913
func createDeadlineComment (ctx context.Context , doer * user_model.User , issue * Issue , newDeadlineUnix timeutil.TimeStamp ) (* Comment , error ) {
931
914
var content string
932
915
var commentType CommentType
0 commit comments