@@ -658,35 +658,65 @@ func RetargetBranchPulls(ctx context.Context, doer *user_model.User, repoID int6
658658	return  nil 
659659}
660660
661- // CloseBranchPulls close all the pull requests who's head branch is the branch 
662- func  CloseBranchPulls (ctx  context.Context , doer  * user_model.User , repoID  int64 , branch  string ) error  {
661+ // ClosePullsCausedByBranchDeleted close all the pull requests who's head branch is the branch 
662+ // Or who's base branch is the branch if setting.Repository.PullRequest.RetargetChildrenOnMerge is true 
663+ func  ClosePullsCausedByBranchDeleted (ctx  context.Context , doer  * user_model.User , repoID  int64 , branch  string ) error  {
664+ 	// branch as head branch 
663665	prs , err  :=  issues_model .GetUnmergedPullRequestsByHeadInfo (ctx , repoID , branch )
664666	if  err  !=  nil  {
665667		return  err 
666668	}
667669
668- 	if  ! setting .Repository .PullRequest .RetargetChildrenOnMerge  {
669- 		prs2 , err  :=  issues_model .GetUnmergedPullRequestsByBaseInfo (ctx , repoID , branch )
670- 		if  err  !=  nil  {
671- 			return  err 
672- 		}
673- 		prs  =  append (prs , prs2 ... )
674- 	}
675- 
676670	if  err  :=  issues_model .PullRequestList (prs ).LoadAttributes (ctx ); err  !=  nil  {
677671		return  err 
678672	}
673+ 	if  err  :=  issues_model .PullRequestList (prs ).LoadRepositories (ctx ); err  !=  nil  {
674+ 		return  err 
675+ 	}
679676
680677	var  errs  errlist 
681678	for  _ , pr  :=  range  prs  {
682679		if  err  =  issue_service .ChangeStatus (ctx , pr .Issue , doer , "" , true ); err  !=  nil  &&  ! issues_model .IsErrPullWasClosed (err ) &&  ! issues_model .IsErrDependenciesLeft (err ) {
683680			errs  =  append (errs , err )
684681		}
682+ 		if  err  ==  nil  {
683+ 			if  err  :=  issues_model .AddDeletePRBranchComment (ctx , doer , pr .BaseRepo , pr .Issue .ID , pr .HeadBranch ); err  !=  nil  {
684+ 				log .Error ("AddDeletePRBranchComment: %v" , err )
685+ 				errs  =  append (errs , err )
686+ 			}
687+ 		}
685688	}
686- 	if  len (errs ) >  0  {
689+ 
690+ 	if  setting .Repository .PullRequest .RetargetChildrenOnMerge  {
687691		return  errs 
688692	}
689- 	return  nil 
693+ 
694+ 	// branch as base branch 
695+ 	prs , err  =  issues_model .GetUnmergedPullRequestsByBaseInfo (ctx , repoID , branch )
696+ 	if  err  !=  nil  {
697+ 		return  err 
698+ 	}
699+ 
700+ 	if  err  :=  issues_model .PullRequestList (prs ).LoadAttributes (ctx ); err  !=  nil  {
701+ 		return  err 
702+ 	}
703+ 	if  err  :=  issues_model .PullRequestList (prs ).LoadRepositories (ctx ); err  !=  nil  {
704+ 		return  err 
705+ 	}
706+ 
707+ 	errs  =  nil 
708+ 	for  _ , pr  :=  range  prs  {
709+ 		if  err  =  issues_model .AddDeletePRBranchComment (ctx , doer , pr .BaseRepo , pr .Issue .ID , pr .BaseBranch ); err  !=  nil  {
710+ 			log .Error ("AddDeletePRBranchComment: %v" , err )
711+ 			errs  =  append (errs , err )
712+ 		}
713+ 		if  err  ==  nil  {
714+ 			if  err  =  issue_service .ChangeStatus (ctx , pr .Issue , doer , "" , true ); err  !=  nil  &&  ! issues_model .IsErrPullWasClosed (err ) &&  ! issues_model .IsErrDependenciesLeft (err ) {
715+ 				errs  =  append (errs , err )
716+ 			}
717+ 		}
718+ 	}
719+ 	return  errs 
690720}
691721
692722// CloseRepoBranchesPulls close all pull requests which head branches are in the given repository, but only whose base repo is not in the given repository 
0 commit comments