@@ -301,12 +301,14 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) ([]string, erro
301301 attachmentPaths = append (attachmentPaths , issue .Attachments [i ].RelativePath ())
302302 }
303303
304- // TODO: deference all review comments
304+ // deference all review comments
305+ if err := issue .LoadComments (ctx ); err != nil {
306+ return nil , err
307+ }
305308
306309 // delete all database data still assigned to this issue
307310 if err := db .DeleteBeans (ctx ,
308311 & issues_model.ContentHistory {IssueID : issue .ID },
309- & issues_model.Comment {IssueID : issue .ID },
310312 & issues_model.IssueLabel {IssueID : issue .ID },
311313 & issues_model.IssueDependency {IssueID : issue .ID },
312314 & issues_model.IssueAssignees {IssueID : issue .ID },
@@ -327,6 +329,41 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) ([]string, erro
327329 return nil , err
328330 }
329331
332+ for _ , comment := range issue .Comments {
333+ if err := issues_model .DeleteComment (ctx , comment ); err != nil {
334+ return nil , err
335+ }
336+
337+ if comment .ReviewID > 0 {
338+ if err := comment .LoadIssue (ctx ); err != nil {
339+ return nil , err
340+ }
341+ if err := comment .Issue .LoadRepo (ctx ); err != nil {
342+ return nil , err
343+ }
344+ if err := comment .Issue .LoadPullRequest (ctx ); err != nil {
345+ return nil , err
346+ }
347+ if err := git .RemoveRef (ctx , comment .Issue .Repo .RepoPath (), issues_model .GetCodeCommentRef (comment .Issue .PullRequest .Index , comment .ID )); err != nil {
348+ log .Error ("Unable to remove ref in base repository for PR[%d] Error: %v" , comment .Issue .PullRequest .ID , err )
349+ // We should not return error here, because the comment has been removed from database.
350+ // users have to delete this ref manually or we should have a synchronize between
351+ // database comment table and git refs.
352+ }
353+ }
354+
355+ // delete all attachments related to this comment
356+ for _ , attachment := range comment .Attachments {
357+ if err := storage .Attachments .Delete (repo_model .AttachmentRelativePath (attachment .UUID )); err != nil {
358+ // Even delete files failed, but the attachments has been removed from database, so we
359+ // should not return error but only record the error on logs.
360+ // users have to delete this attachments manually or we should have a
361+ // synchronize between database attachment table and attachment storage
362+ log .Error ("delete attachment[uuid: %s] failed: %v" , attachment .UUID , err )
363+ }
364+ }
365+ }
366+
330367 if err := committer .Commit (); err != nil {
331368 return nil , err
332369 }
0 commit comments