diff --git a/models/issues/issue_dev_link.go b/models/issues/issue_dev_link.go index 9e6c7cfc25..bb816ad160 100644 --- a/models/issues/issue_dev_link.go +++ b/models/issues/issue_dev_link.go @@ -64,8 +64,8 @@ func CreateIssueDevLink(ctx context.Context, link *IssueDevLink) error { func DeleteIssueDevLinkByBranchName(ctx context.Context, repoID int64, branchName string) error { _, err := db.GetEngine(ctx). - Where("link_type = ? AND link_index = ? AND linked_repo_id = ?", - IssueDevLinkTypeBranch, branchName, repoID). + Where("linked_repo_id = ? AND link_type = ? AND link_index = ?", + repoID, IssueDevLinkTypeBranch, branchName). Delete(new(IssueDevLink)) return err } diff --git a/models/issues/issue_update.go b/models/issues/issue_update.go index 31d76be5e0..fffca446b8 100644 --- a/models/issues/issue_update.go +++ b/models/issues/issue_update.go @@ -718,6 +718,10 @@ func DeleteIssuesByRepoID(ctx context.Context, repoID int64) (attachmentPaths [] return nil, err } + if _, err = sess.In("issue_id", issueIDs).Delete(&IssueDevLink{}); err != nil { + return nil, err + } + var attachments []*repo_model.Attachment err = sess.In("issue_id", issueIDs).Find(&attachments) if err != nil { diff --git a/routers/private/hook_post_receive.go b/routers/private/hook_post_receive.go index 2d1688523c..d5ddeaf4ec 100644 --- a/routers/private/hook_post_receive.go +++ b/routers/private/hook_post_receive.go @@ -114,6 +114,10 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) { }) return } + + if err := issues_model.DeleteIssueDevLinkByBranchName(ctx, repo.ID, update.RefFullName.BranchName()); err != nil { + log.Error("Failed to DeleteIssueDevLinkByBranchName: %s/%s %s Error: %v", ownerName, repoName, update.RefFullName.BranchName(), err) + } } else { branchesToSync = append(branchesToSync, update) diff --git a/services/issue/issue.go b/services/issue/issue.go index 72ea66c8d9..4180c7264d 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -283,6 +283,12 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) error { return err } + if issue.IsPull { + if err := issues_model.DeleteIssueDevLinkByPullRequestID(ctx, issue.ID); err != nil { + return err + } + } + // find attachments related to this issue and remove them if err := issue.LoadAttributes(ctx); err != nil { return err @@ -311,6 +317,7 @@ func deleteIssue(ctx context.Context, issue *issues_model.Issue) error { &issues_model.Comment{RefIssueID: issue.ID}, &issues_model.IssueDependency{DependencyID: issue.ID}, &issues_model.Comment{DependentIssueID: issue.ID}, + &issues_model.IssueDevLink{IssueID: issue.ID}, ); err != nil { return err } diff --git a/services/repository/branch.go b/services/repository/branch.go index 7fc9993077..a8454471ad 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -501,6 +501,10 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R return err } + if err := issues_model.DeleteIssueDevLinkByBranchName(ctx, repo.ID, branchName); err != nil { + return err + } + return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{ Force: true, })