Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automerge supports deleting branch automatically after merging #32343

Merged
merged 33 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e22862e
Automerge supports deleting branch automatically after merging
lunny Oct 26, 2024
499eb51
Fix lint
lunny Oct 26, 2024
47c9581
Fix bug
lunny Nov 5, 2024
ef98449
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Nov 5, 2024
b68ed81
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Nov 7, 2024
c879891
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Nov 8, 2024
6d79d8a
Add permission check when deleting branch after automerge succeed
lunny Nov 8, 2024
7562393
refactor delete head branch of pull request
lunny Nov 9, 2024
e70e375
Move all checks into DeleteBranch and PushUpdate
lunny Nov 10, 2024
04fa8f2
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Nov 10, 2024
70b06b8
Fix lint
lunny Nov 10, 2024
ac0a4ea
Add deleting branch comment
lunny Nov 10, 2024
a154697
Fix lint
lunny Nov 10, 2024
6c08cc5
don't return error even retargetbranch of pull request failed
lunny Nov 10, 2024
c124150
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Nov 10, 2024
7cddaeb
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Nov 22, 2024
2f74844
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Dec 5, 2024
2dbf222
Fix bug
lunny Dec 6, 2024
808e11d
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Dec 6, 2024
16bd651
Remove unnecessary variable
lunny Dec 6, 2024
fd7c953
We don't need to duplicated operation when delete branch
lunny Dec 6, 2024
93e8dfd
Fix test
lunny Dec 6, 2024
80110be
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Dec 17, 2024
aee8ef3
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Dec 25, 2024
8ed0375
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Dec 30, 2024
be5a5ea
Fix merge bug
lunny Dec 30, 2024
fa12a24
Remove unnecessary errlist
lunny Dec 31, 2024
a00773c
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Dec 31, 2024
f549185
Move migration to v1.24
lunny Dec 31, 2024
774152b
Merge branch 'main' into lunny/automerge_support_delete_branch
lunny Dec 31, 2024
101063a
Merge branch 'main' into lunny/automerge_support_delete_branch
GiteaBot Jan 9, 2025
50c2316
Fix merge bug
lunny Jan 9, 2025
1317040
Merge branch 'main' into lunny/automerge_support_delete_branch
GiteaBot Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions models/issues/pull_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ func (prs PullRequestList) getRepositoryIDs() []int64 {
return repoIDs.Values()
}

func (prs PullRequestList) SetBaseRepo(baseRepo *repo_model.Repository) {
for _, pr := range prs {
if pr.BaseRepo == nil {
pr.BaseRepo = baseRepo
}
}
}

func (prs PullRequestList) SetHeadRepo(headRepo *repo_model.Repository) {
for _, pr := range prs {
if pr.HeadRepo == nil {
pr.HeadRepo = headRepo
pr.isHeadRepoLoaded = true
}
}
}

func (prs PullRequestList) LoadRepositories(ctx context.Context) error {
repoIDs := prs.getRepositoryIDs()
reposMap := make(map[int64]*repo_model.Repository, len(repoIDs))
Expand Down
1 change: 1 addition & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func prepareMigrationTasks() []*migration {
newMigration(309, "Improve Notification table indices", v1_23.ImproveNotificationTableIndices),
newMigration(310, "Add Priority to ProtectedBranch", v1_23.AddPriorityToProtectedBranch),
newMigration(311, "Add TimeEstimate to Issue table", v1_23.AddTimeEstimateColumnToIssueTable),
newMigration(312, "Add DeleteBranchAfterMerge to AutoMerge", v1_23.AddDeleteBranchAfterMergeForAutoMerge),
}
return preparedMigrations
}
Expand Down
21 changes: 21 additions & 0 deletions models/migrations/v1_23/v312.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_23 //nolint

import (
"xorm.io/xorm"
)

type pullAutoMerge struct {
DeleteBranchAfterMerge bool
}

// TableName return database table name for xorm
func (pullAutoMerge) TableName() string {
return "pull_auto_merge"
}

func AddDeleteBranchAfterMergeForAutoMerge(x *xorm.Engine) error {
return x.Sync(new(pullAutoMerge))
}
26 changes: 14 additions & 12 deletions models/pull/automerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (

// AutoMerge represents a pull request scheduled for merging when checks succeed
type AutoMerge struct {
ID int64 `xorm:"pk autoincr"`
PullID int64 `xorm:"UNIQUE"`
DoerID int64 `xorm:"INDEX NOT NULL"`
Doer *user_model.User `xorm:"-"`
MergeStyle repo_model.MergeStyle `xorm:"varchar(30)"`
Message string `xorm:"LONGTEXT"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
ID int64 `xorm:"pk autoincr"`
PullID int64 `xorm:"UNIQUE"`
DoerID int64 `xorm:"INDEX NOT NULL"`
Doer *user_model.User `xorm:"-"`
MergeStyle repo_model.MergeStyle `xorm:"varchar(30)"`
Message string `xorm:"LONGTEXT"`
DeleteBranchAfterMerge bool
CreatedUnix timeutil.TimeStamp `xorm:"created"`
}

// TableName return database table name for xorm
Expand Down Expand Up @@ -49,7 +50,7 @@ func IsErrAlreadyScheduledToAutoMerge(err error) bool {
}

// ScheduleAutoMerge schedules a pull request to be merged when all checks succeed
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pullID int64, style repo_model.MergeStyle, message string) error {
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pullID int64, style repo_model.MergeStyle, message string, deleteBranchAfterMerge bool) error {
// Check if we already have a merge scheduled for that pull request
if exists, _, err := GetScheduledMergeByPullID(ctx, pullID); err != nil {
return err
Expand All @@ -58,10 +59,11 @@ func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pullID int64,
}

_, err := db.GetEngine(ctx).Insert(&AutoMerge{
DoerID: doer.ID,
PullID: pullID,
MergeStyle: style,
Message: message,
DoerID: doer.ID,
PullID: pullID,
MergeStyle: style,
Message: message,
DeleteBranchAfterMerge: deleteBranchAfterMerge,
})
return err
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/convert"
pull_service "code.gitea.io/gitea/services/pull"

Check failure on line 25 in routers/api/v1/repo/branch.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

could not import code.gitea.io/gitea/services/pull (-: # code.gitea.io/gitea/services/pull

Check failure on line 25 in routers/api/v1/repo/branch.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

could not import code.gitea.io/gitea/services/pull (-: # code.gitea.io/gitea/services/pull

Check failure on line 25 in routers/api/v1/repo/branch.go

View workflow job for this annotation

GitHub Actions / lint-backend

could not import code.gitea.io/gitea/services/pull (-: # code.gitea.io/gitea/services/pull
release_service "code.gitea.io/gitea/services/release"
repo_service "code.gitea.io/gitea/services/repository"
)
Expand Down Expand Up @@ -150,7 +150,7 @@
}
}

if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName, nil); err != nil {
switch {
case git.IsErrBranchNotExist(err):
ctx.NotFound(err)
Expand Down
13 changes: 3 additions & 10 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ func MergePullRequest(ctx *context.APIContext) {
}

if form.MergeWhenChecksSucceed {
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message)
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message, form.DeleteBranchAfterMerge)
if err != nil {
if pull_model.IsErrAlreadyScheduledToAutoMerge(err) {
ctx.Error(http.StatusConflict, "ScheduleAutoMerge", err)
Expand Down Expand Up @@ -1043,11 +1043,8 @@ func MergePullRequest(ctx *context.APIContext) {
}
defer headRepo.Close()
}
if err := pull_service.RetargetChildrenOnMerge(ctx, ctx.Doer, pr); err != nil {
ctx.Error(http.StatusInternalServerError, "RetargetChildrenOnMerge", err)
return
}
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch); err != nil {

if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch, pr); err != nil {
switch {
case git.IsErrBranchNotExist(err):
ctx.NotFound(err)
Expand All @@ -1060,10 +1057,6 @@ func MergePullRequest(ctx *context.APIContext) {
}
return
}
if err := issues_model.AddDeletePRBranchComment(ctx, ctx.Doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
// Do not fail here as branch has already been deleted
log.Error("DeleteBranch: %v", err)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion routers/private/hook_post_receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestHandlePullRequestMerging(t *testing.T) {

user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})

err = pull_model.ScheduleAutoMerge(db.DefaultContext, user1, pr.ID, repo_model.MergeStyleSquash, "squash merge a pr")
err = pull_model.ScheduleAutoMerge(db.DefaultContext, user1, pr.ID, repo_model.MergeStyleSquash, "squash merge a pr", false)
assert.NoError(t, err)

autoMerge := unittest.AssertExistsAndLoadBean(t, &pull_model.AutoMerge{PullID: pr.ID})
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"code.gitea.io/gitea/routers/utils"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/forms"
pull_service "code.gitea.io/gitea/services/pull"

Check failure on line 28 in routers/web/repo/branch.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

could not import code.gitea.io/gitea/services/pull (-: # code.gitea.io/gitea/services/pull

Check failure on line 28 in routers/web/repo/branch.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

could not import code.gitea.io/gitea/services/pull (-: # code.gitea.io/gitea/services/pull

Check failure on line 28 in routers/web/repo/branch.go

View workflow job for this annotation

GitHub Actions / lint-backend

could not import code.gitea.io/gitea/services/pull (-: # code.gitea.io/gitea/services/pull
release_service "code.gitea.io/gitea/services/release"
repo_service "code.gitea.io/gitea/services/repository"
)
Expand Down Expand Up @@ -97,7 +97,7 @@
defer redirect(ctx)
branchName := ctx.FormString("name")

if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName, nil); err != nil {
switch {
case git.IsErrBranchNotExist(err):
log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName)
Expand Down
14 changes: 2 additions & 12 deletions routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ func MergePullRequest(ctx *context.Context) {
// delete all scheduled auto merges
_ = pull_model.DeleteScheduledAutoMerge(ctx, pr.ID)
// schedule auto merge
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message)
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message, form.DeleteBranchAfterMerge)
if err != nil {
ctx.ServerError("ScheduleAutoMerge", err)
return
Expand Down Expand Up @@ -1504,12 +1504,7 @@ func CleanUpPullRequest(ctx *context.Context) {
func deleteBranch(ctx *context.Context, pr *issues_model.PullRequest, gitRepo *git.Repository) {
fullBranchName := pr.HeadRepo.FullName() + ":" + pr.HeadBranch

if err := pull_service.RetargetChildrenOnMerge(ctx, ctx.Doer, pr); err != nil {
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
return
}

if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, gitRepo, pr.HeadBranch); err != nil {
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, gitRepo, pr.HeadBranch, pr); err != nil {
switch {
case git.IsErrBranchNotExist(err):
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
Expand All @@ -1524,11 +1519,6 @@ func deleteBranch(ctx *context.Context, pr *issues_model.PullRequest, gitRepo *g
return
}

if err := issues_model.AddDeletePRBranchComment(ctx, ctx.Doer, pr.BaseRepo, pr.IssueID, pr.HeadBranch); err != nil {
// Do not fail here as branch has already been deleted
log.Error("DeleteBranch: %v", err)
}

ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
}

Expand Down
11 changes: 9 additions & 2 deletions services/automerge/automerge.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/queue"
notify_service "code.gitea.io/gitea/services/notify"
pull_service "code.gitea.io/gitea/services/pull"
repo_service "code.gitea.io/gitea/services/repository"
)

// prAutoMergeQueue represents a queue to handle update pull request tests
Expand Down Expand Up @@ -63,9 +64,9 @@ func addToQueue(pr *issues_model.PullRequest, sha string) {
}

// ScheduleAutoMerge if schedule is false and no error, pull can be merged directly
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string) (scheduled bool, err error) {
func ScheduleAutoMerge(ctx context.Context, doer *user_model.User, pull *issues_model.PullRequest, style repo_model.MergeStyle, message string, deleteBranchAfterMerge bool) (scheduled bool, err error) {
err = db.WithTx(ctx, func(ctx context.Context) error {
if err := pull_model.ScheduleAutoMerge(ctx, doer, pull.ID, style, message); err != nil {
if err := pull_model.ScheduleAutoMerge(ctx, doer, pull.ID, style, message, deleteBranchAfterMerge); err != nil {
return err
}
scheduled = true
Expand Down Expand Up @@ -303,4 +304,10 @@ func handlePullRequestAutoMerge(pullID int64, sha string) {
// on the pull request page. But this should not be finished in a bug fix PR which will be backport to release branch.
return
}

if pr.Flow == issues_model.PullRequestFlowGithub && scheduledPRM.DeleteBranchAfterMerge {
if err := repo_service.DeleteBranch(ctx, doer, pr.HeadRepo, headGitRepo, pr.HeadBranch, pr); err != nil {
log.Error("DeletePullRequestHeadBranch: %v", err)
}
}
}
68 changes: 52 additions & 16 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,17 +651,11 @@
return ""
}

// RetargetChildrenOnMerge retarget children pull requests on merge if possible
func RetargetChildrenOnMerge(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) error {
if setting.Repository.PullRequest.RetargetChildrenOnMerge && pr.BaseRepoID == pr.HeadRepoID {
return RetargetBranchPulls(ctx, doer, pr.HeadRepoID, pr.HeadBranch, pr.BaseBranch)
}
return nil
}
var _ error = &errlist{}

// RetargetBranchPulls change target branch for all pull requests whose base branch is the branch
// retargetBranchPulls change target branch for all pull requests whose base branch is the branch
// Both branch and targetBranch must be in the same repo (for security reasons)
func RetargetBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, branch, targetBranch string) error {
func retargetBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, branch, targetBranch string) error {
prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repoID, branch)
if err != nil {
return err
Expand All @@ -688,28 +682,70 @@
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 {
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, repoID, branch)
// AdjustPullsCausedByBranchDeleted close all the pull requests who's head branch is the branch
// Or Close all the plls who's base branch is the branch if setting.Repository.PullRequest.RetargetChildrenOnMerge is false.
// If it's true, Retarget all these pulls to the default branch.
func AdjustPullsCausedByBranchDeleted(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, branch string) error {
// branch as head branch
prs, err := issues_model.GetUnmergedPullRequestsByHeadInfo(ctx, repo.ID, branch)
if err != nil {
return err
}

prs2, err := issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repoID, branch)
if err := issues_model.PullRequestList(prs).LoadAttributes(ctx); err != nil {
return err
}
issues_model.PullRequestList(prs).SetHeadRepo(repo)
if err := issues_model.PullRequestList(prs).LoadRepositories(ctx); err != nil {
return err
}

var errs errlist
for _, pr := range prs {
if err = issue_service.CloseIssue(ctx, pr.Issue, doer, ""); 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 setting.Repository.PullRequest.RetargetChildrenOnMerge {
if err := retargetBranchPulls(ctx, doer, repo.ID, branch, repo.DefaultBranch); err != nil {
log.Error("retargetBranchPulls failed: %v", err)
errs = append(errs, err)
}
return errs
}

// branch as base branch
prs, err = issues_model.GetUnmergedPullRequestsByBaseInfo(ctx, repo.ID, branch)
if err != nil {
return err
}

prs = append(prs, prs2...)
if err := issues_model.PullRequestList(prs).LoadAttributes(ctx); err != nil {
return err
}
issues_model.PullRequestList(prs).SetBaseRepo(repo)
if err := issues_model.PullRequestList(prs).LoadRepositories(ctx); err != nil {
return err
}

var errs errlist
errs = nil
for _, pr := range prs {
if err = issue_service.CloseIssue(ctx, pr.Issue, doer, ""); err != nil && !issues_model.IsErrPullWasClosed(err) && !issues_model.IsErrDependenciesLeft(err) {
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) {

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / backend

undefined: issue_service.ChangeStatus

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / test-sqlite

undefined: issue_service.ChangeStatus

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / test-mysql

undefined: issue_service.ChangeStatus

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: issue_service.ChangeStatus

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / test-unit

undefined: issue_service.ChangeStatus

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / test-mssql

undefined: issue_service.ChangeStatus

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / checks-backend

undefined: issue_service.ChangeStatus

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / test-e2e

undefined: issue_service.ChangeStatus

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: issue_service.ChangeStatus) (typecheck)

Check failure on line 745 in services/pull/pull.go

View workflow job for this annotation

GitHub Actions / lint-backend

undefined: issue_service.ChangeStatus) (typecheck)
errs = append(errs, err)
}
}
}
if len(errs) > 0 {
return errs
Expand Down
8 changes: 7 additions & 1 deletion services/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func CanDeleteBranch(ctx context.Context, repo *repo_model.Repository, branchNam
}

// DeleteBranch delete branch
func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string, pr *issues_model.PullRequest) error {
err := repo.MustNotBeArchived()
if err != nil {
return err
Expand Down Expand Up @@ -519,6 +519,12 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
}
}

if pr != nil {
if err := issues_model.AddDeletePRBranchComment(ctx, doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
return fmt.Errorf("DeleteBranch: %v", err)
}
}

return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
Force: true,
})
Expand Down
3 changes: 2 additions & 1 deletion services/repository/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
}
} else {
notify_service.DeleteRef(ctx, pusher, repo, opts.RefFullName)
if err = pull_service.CloseBranchPulls(ctx, pusher, repo.ID, branch); err != nil {

if err := pull_service.AdjustPullsCausedByBranchDeleted(ctx, pusher, repo, branch); err != nil {
// close all related pulls
log.Error("close related pull request failed: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func TestCreateDeleteRefEvent(t *testing.T) {
assert.NotNil(t, run)

// delete the branch
err = repo_service.DeleteBranch(db.DefaultContext, user2, repo, gitRepo, "test-create-branch")
err = repo_service.DeleteBranch(db.DefaultContext, user2, repo, gitRepo, "test-create-branch", nil)
assert.NoError(t, err)
run = unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{
Title: "add workflow",
Expand Down
Loading
Loading