Skip to content

Commit

Permalink
Skip branch protection remediations for repos with no branches
Browse files Browse the repository at this point in the history
Comparing the error message for a know string might seem hacky but
go-github does that internally too, so...

Fixes: #1992
  • Loading branch information
jhrozek committed Apr 10, 2024
1 parent 68f0b35 commit 5e0bbc8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit 5e0bbc8

Please sign in to comment.