@@ -589,26 +589,27 @@ func (c *Comment) LoadAttachments(ctx context.Context) error {
589589 return nil
590590}
591591
592- // UpdateAttachments update attachments by UUIDs for the comment
593- func (c * Comment ) UpdateAttachments (ctx context.Context , uuids []string ) error {
594- ctx , committer , err := db .TxContext (ctx )
595- if err != nil {
596- return err
592+ // UpdateCommentAttachments update attachments by UUIDs for the comment
593+ func UpdateCommentAttachments (ctx context.Context , c * Comment , uuids []string ) error {
594+ if len (uuids ) == 0 {
595+ return nil
597596 }
598- defer committer .Close ()
599597
600- attachments , err := repo_model .GetAttachmentsByUUIDs (ctx , uuids )
601- if err != nil {
602- return fmt .Errorf ("getAttachmentsByUUIDs [uuids: %v]: %w" , uuids , err )
603- }
604- for i := 0 ; i < len (attachments ); i ++ {
605- attachments [i ].IssueID = c .IssueID
606- attachments [i ].CommentID = c .ID
607- if err := repo_model .UpdateAttachment (ctx , attachments [i ]); err != nil {
608- return fmt .Errorf ("update attachment [id: %d]: %w" , attachments [i ].ID , err )
598+ return db .WithTx (ctx , func (ctx context.Context ) error {
599+ attachments , err := repo_model .GetAttachmentsByUUIDs (ctx , uuids )
600+ if err != nil {
601+ return fmt .Errorf ("getAttachmentsByUUIDs [uuids: %v]: %w" , uuids , err )
609602 }
610- }
611- return committer .Commit ()
603+ for i := 0 ; i < len (attachments ); i ++ {
604+ attachments [i ].IssueID = c .IssueID
605+ attachments [i ].CommentID = c .ID
606+ if err := repo_model .UpdateAttachment (ctx , attachments [i ]); err != nil {
607+ return fmt .Errorf ("update attachment [id: %d]: %w" , attachments [i ].ID , err )
608+ }
609+ }
610+ c .Attachments = attachments
611+ return nil
612+ })
612613}
613614
614615// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
@@ -875,7 +876,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
875876 // Check comment type.
876877 switch opts .Type {
877878 case CommentTypeCode :
878- if err = updateAttachments (ctx , opts , comment ); err != nil {
879+ if err = UpdateCommentAttachments (ctx , comment , opts . Attachments ); err != nil {
879880 return err
880881 }
881882 if comment .ReviewID != 0 {
@@ -895,7 +896,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
895896 }
896897 fallthrough
897898 case CommentTypeReview :
898- if err = updateAttachments (ctx , opts , comment ); err != nil {
899+ if err = UpdateCommentAttachments (ctx , comment , opts . Attachments ); err != nil {
899900 return err
900901 }
901902 case CommentTypeReopen , CommentTypeClose :
@@ -907,23 +908,6 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
907908 return UpdateIssueCols (ctx , opts .Issue , "updated_unix" )
908909}
909910
910- func updateAttachments (ctx context.Context , opts * CreateCommentOptions , comment * Comment ) error {
911- attachments , err := repo_model .GetAttachmentsByUUIDs (ctx , opts .Attachments )
912- if err != nil {
913- return fmt .Errorf ("getAttachmentsByUUIDs [uuids: %v]: %w" , opts .Attachments , err )
914- }
915- for i := range attachments {
916- attachments [i ].IssueID = opts .Issue .ID
917- attachments [i ].CommentID = comment .ID
918- // No assign value could be 0, so ignore AllCols().
919- if _ , err = db .GetEngine (ctx ).ID (attachments [i ].ID ).Update (attachments [i ]); err != nil {
920- return fmt .Errorf ("update attachment [%d]: %w" , attachments [i ].ID , err )
921- }
922- }
923- comment .Attachments = attachments
924- return nil
925- }
926-
927911func createDeadlineComment (ctx context.Context , doer * user_model.User , issue * Issue , newDeadlineUnix timeutil.TimeStamp ) (* Comment , error ) {
928912 var content string
929913 var commentType CommentType
0 commit comments