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