@@ -631,9 +631,9 @@ func (errs errlist) Error() string {
631631
632632var _ error = & errlist {}
633633
634- // RetargetBranchPulls change target branch for all pull requests whose base branch is the branch
634+ // retargetBranchPulls change target branch for all pull requests whose base branch is the branch
635635// Both branch and targetBranch must be in the same repo (for security reasons)
636- func RetargetBranchPulls (ctx context.Context , doer * user_model.User , repoID int64 , branch , targetBranch string ) error {
636+ func retargetBranchPulls (ctx context.Context , doer * user_model.User , repoID int64 , branch , targetBranch string ) error {
637637 prs , err := issues_model .GetUnmergedPullRequestsByBaseInfo (ctx , repoID , branch )
638638 if err != nil {
639639 return err
@@ -660,18 +660,20 @@ func RetargetBranchPulls(ctx context.Context, doer *user_model.User, repoID int6
660660 return nil
661661}
662662
663- // ClosePullsCausedByBranchDeleted close all the pull requests who's head branch is the branch
664- // Or who's base branch is the branch if setting.Repository.PullRequest.RetargetChildrenOnMerge is true
665- func ClosePullsCausedByBranchDeleted (ctx context.Context , doer * user_model.User , repoID int64 , branch string ) error {
663+ // AdjustPullsCausedByBranchDeleted close all the pull requests who's head branch is the branch
664+ // Or Close all the plls who's base branch is the branch if setting.Repository.PullRequest.RetargetChildrenOnMerge is false.
665+ // If it's true, Retarget all these pulls to the default branch.
666+ func AdjustPullsCausedByBranchDeleted (ctx context.Context , doer * user_model.User , repo * repo_model.Repository , branch string ) error {
666667 // branch as head branch
667- prs , err := issues_model .GetUnmergedPullRequestsByHeadInfo (ctx , repoID , branch )
668+ prs , err := issues_model .GetUnmergedPullRequestsByHeadInfo (ctx , repo . ID , branch )
668669 if err != nil {
669670 return err
670671 }
671672
672673 if err := issues_model .PullRequestList (prs ).LoadAttributes (ctx ); err != nil {
673674 return err
674675 }
676+ issues_model .PullRequestList (prs ).SetHeadRepo (repo )
675677 if err := issues_model .PullRequestList (prs ).LoadRepositories (ctx ); err != nil {
676678 return err
677679 }
@@ -690,18 +692,23 @@ func ClosePullsCausedByBranchDeleted(ctx context.Context, doer *user_model.User,
690692 }
691693
692694 if setting .Repository .PullRequest .RetargetChildrenOnMerge {
695+ if err := retargetBranchPulls (ctx , doer , repo .ID , branch , repo .DefaultBranch ); err != nil {
696+ log .Error ("retargetBranchPulls failed: %v" , err )
697+ errs = append (errs , err )
698+ }
693699 return errs
694700 }
695701
696702 // branch as base branch
697- prs , err = issues_model .GetUnmergedPullRequestsByBaseInfo (ctx , repoID , branch )
703+ prs , err = issues_model .GetUnmergedPullRequestsByBaseInfo (ctx , repo . ID , branch )
698704 if err != nil {
699705 return err
700706 }
701707
702708 if err := issues_model .PullRequestList (prs ).LoadAttributes (ctx ); err != nil {
703709 return err
704710 }
711+ issues_model .PullRequestList (prs ).SetBaseRepo (repo )
705712 if err := issues_model .PullRequestList (prs ).LoadRepositories (ctx ); err != nil {
706713 return err
707714 }
0 commit comments