Files
gitea/services/repository/branch_test.go
T
b015cd67e2 feat(repo): allow deleting protected branches (#39243)
This adds a Branch Deletion setting with three modes: disabled by
default, anyone with push access, or a restricted user/team/deploy-key
allowlist. The policy applies to Git pushes, API/UI deletion, and
post-merge cleanup; the default branch and the pull request target
branch remain undeletable.

Closes https://github.com/go-gitea/gitea/issues/33305

---------

Co-authored-by: Enoque Duarte <12515551+enoquefcd@users.noreply.github.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-10-14 07:19:49 +00:00

45 lines
1.4 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
import (
"testing"
"gitea.dev/models/db"
git_model "gitea.dev/models/git"
repo_model "gitea.dev/models/repo"
"gitea.dev/models/unit"
"gitea.dev/models/unittest"
user_model "gitea.dev/models/user"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCanDeleteBranchDefaultTargets(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
ctx := t.Context()
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
rule := &git_model.ProtectedBranch{
RepoID: repo.ID, RuleName: "*",
ProtectedBranchConfig: git_model.ProtectedBranchConfig{CanPush: true, CanDelete: true},
}
require.NoError(t, db.Insert(ctx, rule))
assert.NoError(t, CanDeleteBranch(ctx, repo, "branch2", owner))
assert.ErrorIs(t, CanDeleteBranch(ctx, repo, repo.DefaultBranch, owner), ErrBranchIsDefault)
pullRequestUnit, err := repo.GetUnit(ctx, unit.TypePullRequests)
assert.NoError(t, err)
pullRequestConfig := pullRequestUnit.PullRequestsConfig()
pullRequestConfig.DefaultTargetBranch = "branch2"
pullRequestUnit.Config = pullRequestConfig
assert.NoError(t, repo_model.UpdateRepoUnitConfig(ctx, pullRequestUnit))
repo.Units = nil
assert.ErrorIs(t, CanDeleteBranch(ctx, repo, "branch2", owner), ErrBranchIsDefault)
}