1
1
mirror of https://github.com/go-gitea/gitea synced 2025-01-11 10:14:27 +00:00

Add deleting branch comment

This commit is contained in:
Lunny Xiao 2024-11-09 22:04:09 -08:00
parent 70b06b8fcc
commit ac0a4eae42
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 43 additions and 13 deletions

View File

@ -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

View File

@ -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)
}