diff --git a/internal/engine/actions/remediate/gh_branch_protect/gh_branch_protect.go b/internal/engine/actions/remediate/gh_branch_protect/gh_branch_protect.go index e256006978..c2196a982e 100644 --- a/internal/engine/actions/remediate/gh_branch_protect/gh_branch_protect.go +++ b/internal/engine/actions/remediate/gh_branch_protect/gh_branch_protect.go @@ -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" @@ -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) } diff --git a/internal/providers/github/common.go b/internal/providers/github/common.go index b8c63310d7..a0c8fa8e99 100644 --- a/internal/providers/github/common.go +++ b/internal/providers/github/common.go @@ -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 @@ -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