@@ -607,26 +607,26 @@ func (c *Comment) LoadAttachments(ctx context.Context) error {
607607 return nil
608608}
609609
610- // UpdateAttachments update attachments by UUIDs for the comment
611- func (c * Comment ) UpdateAttachments (ctx context.Context , uuids []string ) error {
612- ctx , committer , err := db .TxContext (ctx )
613- if err != nil {
614- return err
615- }
616- defer committer .Close ()
617-
618- attachments , err := repo_model .GetAttachmentsByUUIDs (ctx , uuids )
619- if err != nil {
620- return fmt .Errorf ("getAttachmentsByUUIDs [uuids: %v]: %w" , uuids , err )
610+ // UpdateCommentAttachments update attachments by UUIDs for the comment
611+ func UpdateCommentAttachments (ctx context.Context , c * Comment , uuids []string ) error {
612+ if len (uuids ) == 0 {
613+ return nil
621614 }
622- for i := 0 ; i < len (attachments ); i ++ {
623- attachments [i ].IssueID = c .IssueID
624- attachments [i ].CommentID = c .ID
625- if err := repo_model .UpdateAttachment (ctx , attachments [i ]); err != nil {
626- return fmt .Errorf ("update attachment [id: %d]: %w" , attachments [i ].ID , err )
615+ return db .WithTx (ctx , func (ctx context.Context ) error {
616+ attachments , err := repo_model .GetAttachmentsByUUIDs (ctx , uuids )
617+ if err != nil {
618+ return fmt .Errorf ("getAttachmentsByUUIDs [uuids: %v]: %w" , uuids , err )
627619 }
628- }
629- return committer .Commit ()
620+ for i := 0 ; i < len (attachments ); i ++ {
621+ attachments [i ].IssueID = c .IssueID
622+ attachments [i ].CommentID = c .ID
623+ if err := repo_model .UpdateAttachment (ctx , attachments [i ]); err != nil {
624+ return fmt .Errorf ("update attachment [id: %d]: %w" , attachments [i ].ID , err )
625+ }
626+ }
627+ c .Attachments = attachments
628+ return nil
629+ })
630630}
631631
632632// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
@@ -893,7 +893,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
893893 // Check comment type.
894894 switch opts .Type {
895895 case CommentTypeCode :
896- if err = updateAttachments (ctx , opts , comment ); err != nil {
896+ if err = UpdateCommentAttachments (ctx , comment , opts . Attachments ); err != nil {
897897 return err
898898 }
899899 if comment .ReviewID != 0 {
@@ -913,7 +913,7 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
913913 }
914914 fallthrough
915915 case CommentTypeReview :
916- if err = updateAttachments (ctx , opts , comment ); err != nil {
916+ if err = UpdateCommentAttachments (ctx , comment , opts . Attachments ); err != nil {
917917 return err
918918 }
919919 case CommentTypeReopen , CommentTypeClose :
@@ -925,23 +925,6 @@ func updateCommentInfos(ctx context.Context, opts *CreateCommentOptions, comment
925925 return UpdateIssueCols (ctx , opts .Issue , "updated_unix" )
926926}
927927
928- func updateAttachments (ctx context.Context , opts * CreateCommentOptions , comment * Comment ) error {
929- attachments , err := repo_model .GetAttachmentsByUUIDs (ctx , opts .Attachments )
930- if err != nil {
931- return fmt .Errorf ("getAttachmentsByUUIDs [uuids: %v]: %w" , opts .Attachments , err )
932- }
933- for i := range attachments {
934- attachments [i ].IssueID = opts .Issue .ID
935- attachments [i ].CommentID = comment .ID
936- // No assign value could be 0, so ignore AllCols().
937- if _ , err = db .GetEngine (ctx ).ID (attachments [i ].ID ).Update (attachments [i ]); err != nil {
938- return fmt .Errorf ("update attachment [%d]: %w" , attachments [i ].ID , err )
939- }
940- }
941- comment .Attachments = attachments
942- return nil
943- }
944-
945928func createDeadlineComment (ctx context.Context , doer * user_model.User , issue * Issue , newDeadlineUnix timeutil.TimeStamp ) (* Comment , error ) {
946929 var content string
947930 var commentType CommentType
0 commit comments