diff --git a/services/pull/pull.go b/services/pull/pull.go index 55d7cbe483..bba86ab00f 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -658,35 +658,65 @@ func RetargetBranchPulls(ctx context.Context, doer *user_model.User, repoID int6 return nil } -// CloseBranchPulls close all the pull requests who's head branch is the branch -func CloseBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, branch string) error { +// ClosePullsCausedByBranchDeleted close all the pull requests who's head branch is the branch +// Or who's base branch is the branch if setting.Repository.PullRequest.RetargetChildrenOnMerge is true +func ClosePullsCausedByBranchDeleted(ctx context.Context, doer *user_model.User, repoID int64, branch string) error { + // branch as head branch prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, repoID, branch) if err != nil { return err } - if !setting.Repository.PullRequest.RetargetChildrenOnMerge { - prs2, err := issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repoID, branch) - if err != nil { - return err - } - prs = append(prs, prs2...) - } - if err := issues_model.PullRequestList(prs).LoadAttributes(ctx); err != nil { return err } + if err := issues_model.PullRequestList(prs).LoadRepositories(ctx); err != nil { + return err + } var errs errlist for _, pr := range prs { if err = issue_service.ChangeStatus(ctx, pr.Issue, doer, "", true); err != nil && !issues_model.IsErrPullWasClosed(err) && !issues_model.IsErrDependenciesLeft(err) { errs = append(errs, err) } + if err == nil { + if err := issues_model.AddDeletePRBranchComment(ctx, doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil { + log.Error("AddDeletePRBranchComment: %v", err) + errs = append(errs, err) + } + } } - if len(errs) > 0 { + + if setting.Repository.PullRequest.RetargetChildrenOnMerge { return errs } - return nil + + // branch as base branch + prs, err = issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repoID, branch) + if err != nil { + return err + } + + if err := issues_model.PullRequestList(prs).LoadAttributes(ctx); err != nil { + return err + } + if err := issues_model.PullRequestList(prs).LoadRepositories(ctx); err != nil { + return err + } + + errs = nil + for _, pr := range prs { + if err = issues_model.AddDeletePRBranchComment(ctx, doer, pr.BaseRepo, pr.Issue.ID, pr.BaseBranch); err != nil { + log.Error("AddDeletePRBranchComment: %v", err) + errs = append(errs, err) + } + if err == nil { + if err = issue_service.ChangeStatus(ctx, pr.Issue, doer, "", true); err != nil && !issues_model.IsErrPullWasClosed(err) && !issues_model.IsErrDependenciesLeft(err) { + errs = append(errs, err) + } + } + } + return errs } // CloseRepoBranchesPulls close all pull requests which head branches are in the given repository, but only whose base repo is not in the given repository diff --git a/services/repository/push.go b/services/repository/push.go index 07252cb11b..ef33200d3e 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -282,7 +282,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } } - if err = pull_service.CloseBranchPulls(ctx, pusher, repo.ID, branch); err != nil { + if err = pull_service.ClosePullsCausedByBranchDeleted(ctx, pusher, repo.ID, branch); err != nil { // close all related pulls log.Error("close related pull request failed: %v", err) }