-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Protect default branch against deletion #11115
Protect default branch against deletion #11115
Conversation
…e but now go-code take care of it too)
4e1a99d
to
3e4411f
Compare
Also need to add to pre-receive hook |
--- a/routers/private/hook.go
+++ b/routers/private/hook.go
@@ -206,6 +206,14 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) {
refFullName := opts.RefFullNames[i]
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
+ if branchName == repo.DefaultBranch && newCommitID == git.EmptySHA {
+ log.Warn("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted", branchName, repo)
+ ctx.JSON(http.StatusForbidden, map[string]interface{}{
+ "err": fmt.Sprintf("branch %s is the default branch and cannot be deleted", branchName),
+ })
+ return
+ }
+
protectBranch, err := models.GetProtectedBranchBy(repo.ID, branchName)
if err != nil {
log.Error("Unable to get protected branch: %s in %-v Error: %v", branchName, repo, err)
|
Codecov Report
@@ Coverage Diff @@
## master #11115 +/- ##
==========================================
- Coverage 43.86% 43.84% -0.02%
==========================================
Files 607 607
Lines 86909 86913 +4
==========================================
- Hits 38121 38110 -11
- Misses 44090 44100 +10
- Partials 4698 4703 +5
Continue to review full report at Codecov.
|
Signed-off-by: Andrew Thornton <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from small nit, looks good
Co-authored-by: Lauris BH <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
Although default branch is not offered for deletion in the templates, we need to prevent it both at the router level and in the pre-receive hook. Co-authored-by: Andrew Thornton <[email protected]> Co-authored-by: Lauris BH <[email protected]>
Although default branch is not offered for deletion in the templates, we need to prevent it both at the router level and in the pre-receive hook.