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

Skip branch protection remediations for repos with no branches #3035

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
engerrors "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/providers"
mindergh "github.com/stacklok/minder/internal/providers/github"
"github.com/stacklok/minder/internal/util"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provifv1 "github.com/stacklok/minder/pkg/providers/v1"
Expand Down Expand Up @@ -138,6 +139,8 @@ func (r *GhBranchProtectRemediator) Do(
// this will create a new branch protection using github's defaults
// which appear quite sensible
res = &github.Protection{}
} else if errors.Is(err, mindergh.ErrBranchNotFound) {
return nil, fmt.Errorf("branch %s not found: %w", branch, engerrors.ErrActionSkipped)
} else if err != nil {
return nil, fmt.Errorf("error getting branch protection: %w", err)
}
Expand Down
14 changes: 12 additions & 2 deletions internal/providers/github/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ const (
MaxRateLimitRetries = 1
// DefaultRateLimitWaitTime is the default time to wait for a rate limit to reset
DefaultRateLimitWaitTime = 1 * time.Minute

githubBranchNotFoundMsg = "Branch not found"
)

var (
// ErrNotFound Denotes if the call returned a 404
// ErrNotFound denotes if the call returned a 404
ErrNotFound = errors.New("not found")
// ErrBranchNotFound denotes if the branch was not found
ErrBranchNotFound = errors.New("branch not found")
)

// GitHub is the struct that contains the shared GitHub client operations
Expand Down Expand Up @@ -486,8 +490,14 @@ func (c *GitHub) GetRepository(ctx context.Context, owner string, name string) (
// GetBranchProtection returns the branch protection for a given branch
func (c *GitHub) GetBranchProtection(ctx context.Context, owner string,
repo_name string, branch_name string) (*github.Protection, error) {
var respErr *github.ErrorResponse

protection, _, err := c.client.Repositories.GetBranchProtection(ctx, owner, repo_name, branch_name)
if err != nil {
if errors.As(err, &respErr) {
if respErr.Message == githubBranchNotFoundMsg {
return nil, ErrBranchNotFound
}
} else if err != nil {
return nil, err
}
return protection, nil
Expand Down