Skip to content

Commit 0f64a3e

Browse files
committed
Do not allow deleting protected branch
1 parent 77617da commit 0f64a3e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

routers/repo/issue.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,13 @@ func ViewIssue(ctx *context.Context) {
651651
if err := pull.GetHeadRepo(); err != nil {
652652
log.Error(4, "GetHeadRepo: %v", err)
653653
} else if pull.HeadBranch != pull.HeadRepo.DefaultBranch && ctx.User.IsWriterOfRepo(pull.HeadRepo) {
654-
canDelete = true
655-
ctx.Data["DeleteBranchLink"] = ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index) + "/cleanup"
654+
// Check if branch is not protected
655+
if protected, err := pull.HeadRepo.IsProtectedBranch(pull.HeadBranch); err != nil {
656+
log.Error(4, "IsProtectedBranch: %v", err)
657+
} else if !protected {
658+
canDelete = true
659+
ctx.Data["DeleteBranchLink"] = ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index) + "/cleanup"
660+
}
656661
}
657662
}
658663

routers/repo/pull.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ func CleanUpPullRequest(ctx *context.Context) {
788788
ctx.Handle(500, "GetBaseRepo", err)
789789
return
790790
} else if pr.HeadRepo.GetOwner(); err != nil {
791-
ctx.Handle(500, "GetOwner", err)
791+
ctx.Handle(500, "HeadRepo.GetOwner", err)
792792
return
793793
}
794794

@@ -822,6 +822,15 @@ func CleanUpPullRequest(ctx *context.Context) {
822822
return
823823
}
824824

825+
// Check if branch is not protected
826+
if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch); err != nil || protected {
827+
if err != nil {
828+
log.Error(4, "HeadRepo.IsProtectedBranch: %v", err)
829+
}
830+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
831+
return
832+
}
833+
825834
// Check if branch has no new commits
826835
if len(pr.MergedCommitID) > 0 {
827836
branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)

0 commit comments

Comments
 (0)