Skip to content

Commit fd7c953

Browse files
committed
We don't need to duplicated operation when delete branch
1 parent 16bd651 commit fd7c953

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

models/issues/pull_list.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,23 @@ func (prs PullRequestList) getRepositoryIDs() []int64 {
165165
return repoIDs.Values()
166166
}
167167

168+
func (prs PullRequestList) SetBaseRepo(baseRepo *repo_model.Repository) {
169+
for _, pr := range prs {
170+
if pr.BaseRepo == nil {
171+
pr.BaseRepo = baseRepo
172+
}
173+
}
174+
}
175+
176+
func (prs PullRequestList) SetHeadRepo(headRepo *repo_model.Repository) {
177+
for _, pr := range prs {
178+
if pr.HeadRepo == nil {
179+
pr.HeadRepo = headRepo
180+
pr.isHeadRepoLoaded = true
181+
}
182+
}
183+
}
184+
168185
func (prs PullRequestList) LoadRepositories(ctx context.Context) error {
169186
repoIDs := prs.getRepositoryIDs()
170187
reposMap := make(map[int64]*repo_model.Repository, len(repoIDs))

routers/api/v1/repo/pull.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,13 +1085,6 @@ func MergePullRequest(ctx *context.APIContext) {
10851085
defer headRepo.Close()
10861086
}
10871087

1088-
// TODO: why only retarget same repository pull requests?
1089-
if setting.Repository.PullRequest.RetargetChildrenOnMerge && pr.BaseRepoID == pr.HeadRepoID {
1090-
if err := pull_service.RetargetBranchPulls(ctx, ctx.Doer, pr.HeadRepoID, pr.HeadBranch, pr.BaseBranch); err != nil {
1091-
ctx.Error(http.StatusInternalServerError, "RetargetBranchPulls", err)
1092-
return
1093-
}
1094-
}
10951088
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch, pr); err != nil {
10961089
switch {
10971090
case git.IsErrBranchNotExist(err):
@@ -1105,10 +1098,6 @@ func MergePullRequest(ctx *context.APIContext) {
11051098
}
11061099
return
11071100
}
1108-
if err := issues_model.AddDeletePRBranchComment(ctx, ctx.Doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
1109-
// Do not fail here as branch has already been deleted
1110-
log.Error("DeleteBranch: %v", err)
1111-
}
11121101
}
11131102
}
11141103

services/pull/pull.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,9 @@ func (errs errlist) Error() string {
631631

632632
var _ 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
}

services/repository/push.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
276276
} else {
277277
notify_service.DeleteRef(ctx, pusher, repo, opts.RefFullName)
278278

279-
if setting.Repository.PullRequest.RetargetChildrenOnMerge {
280-
if err := pull_service.RetargetBranchPulls(ctx, pusher, repo.ID, branch, repo.DefaultBranch); err != nil {
281-
log.Error("retargetBranchPulls failed: %v", err)
282-
}
283-
}
284-
285-
if err := pull_service.ClosePullsCausedByBranchDeleted(ctx, pusher, repo.ID, branch); err != nil {
279+
if err := pull_service.AdjustPullsCausedByBranchDeleted(ctx, pusher, repo, branch); err != nil {
286280
// close all related pulls
287281
log.Error("close related pull request failed: %v", err)
288282
}

0 commit comments

Comments
 (0)