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

Branch Protection: Error out if an empty branch is given #3433

Merged
merged 1 commit into from
May 27, 2024
Merged
Changes from all commits
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
8 changes: 8 additions & 0 deletions internal/providers/github/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ var (
// ErroNoCheckPermissions is a fixed error returned when the credentialed
// identity has not been authorized to use the checks API
ErroNoCheckPermissions = errors.New("missing permissions: check")
// ErrBranchNameEmpty is a fixed error returned when the branch name is empty
ErrBranchNameEmpty = errors.New("branch name cannot be empty")
)

// GitHub is the struct that contains the shared GitHub client operations
Expand Down Expand Up @@ -491,6 +493,9 @@ func (c *GitHub) GetRepository(ctx context.Context, owner string, name string) (
func (c *GitHub) GetBranchProtection(ctx context.Context, owner string,
repo_name string, branch_name string) (*github.Protection, error) {
var respErr *github.ErrorResponse
if branch_name == "" {
return nil, ErrBranchNameEmpty
}

protection, _, err := c.client.Repositories.GetBranchProtection(ctx, owner, repo_name, branch_name)
if errors.As(err, &respErr) {
Expand All @@ -507,6 +512,9 @@ func (c *GitHub) GetBranchProtection(ctx context.Context, owner string,
func (c *GitHub) UpdateBranchProtection(
ctx context.Context, owner, repo, branch string, preq *github.ProtectionRequest,
) error {
if branch == "" {
return ErrBranchNameEmpty
}
_, _, err := c.client.Repositories.UpdateBranchProtection(ctx, owner, repo, branch, preq)
return err
}
Expand Down