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

Add branch delete to API #11112

Merged
merged 7 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
make sure default branch can not be deleted
  • Loading branch information
6543 committed Apr 17, 2020
commit 4dca94b34a9b7912cca51183e44aa0a14aba1a43
14 changes: 14 additions & 0 deletions integrations/api_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func testAPIDeleteBranchProtection(t *testing.T, branchName string, expectedHTTP
session.MakeRequest(t, req, expectedHTTPStatus)
}

func testAPIDeleteBranch(t *testing.T, branchName string, expectedHTTPStatus int) {
session := loginUser(t, "user2")
token := getTokenForLoggedInUser(t, session)
req := NewRequestf(t, "DELETE", "/api/v1/repos/user2/repo1/branches/%s?token=%s", branchName, token)
session.MakeRequest(t, req, expectedHTTPStatus)
}

func TestAPIGetBranch(t *testing.T) {
for _, test := range []struct {
BranchName string
Expand All @@ -106,10 +113,17 @@ func TestAPIBranchProtection(t *testing.T) {
// Can only create once
testAPICreateBranchProtection(t, "master", http.StatusForbidden)

// Can't delete a protected branch
testAPIDeleteBranch(t, "master", http.StatusForbidden)

testAPIGetBranchProtection(t, "master", http.StatusOK)
testAPIEditBranchProtection(t, "master", &api.BranchProtection{
EnablePush: true,
}, http.StatusOK)

testAPIDeleteBranchProtection(t, "master", http.StatusNoContent)

// Test branch deletion
testAPIDeleteBranch(t, "master", http.StatusForbidden)
testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
}
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,7 @@ branch.deleted_by = Deleted by %s
branch.restore_success = Branch '%s' has been restored.
branch.restore_failed = Failed to restore branch '%s'.
branch.protected_deletion_failed = Branch '%s' is protected. It cannot be deleted.
branch.dfault_deletion_failed = Branch '%s' is default branch. It cannot be deleted.
branch.restore = Restore Branch '%s'
branch.download = Download Branch '%s'
branch.included_desc = This branch is part of the default branch
Expand Down
5 changes: 5 additions & 0 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ func DeleteBranch(ctx *context.APIContext) {
return
}

if ctx.Repo.Repository.DefaultBranch == ctx.Repo.BranchName {
ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch"))
return
}

isProtected, err := ctx.Repo.Repository.IsProtectedBranch(ctx.Repo.BranchName, ctx.User)
if err != nil {
ctx.InternalServerError(err)
Expand Down
7 changes: 6 additions & 1 deletion routers/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ func Branches(ctx *context.Context) {
// DeleteBranchPost responses for delete merged branch
func DeleteBranchPost(ctx *context.Context) {
defer redirect(ctx)

branchName := ctx.Query("name")

if ctx.Repo.Repository.DefaultBranch == ctx.Repo.BranchName {
ctx.Flash.Error(ctx.Tr("repo.branch.dfault_deletion_failed", branchName))
return
}

isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
if err != nil {
log.Error("DeleteBranch: %v", err)
Expand Down