Skip to content

Commit

Permalink
fix: codeowner file parse error as first class error (#2003)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav-harness authored and Harness committed Apr 24, 2024
1 parent e2689a3 commit e528ce2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
5 changes: 1 addition & 4 deletions app/api/controller/pullreq/codeowner.go
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"fmt"

"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/services/codeowners"
"github.com/harness/gitness/types"
Expand Down Expand Up @@ -46,9 +45,7 @@ func (c *Controller) CodeOwners(
}

ownerEvaluation, err := c.codeOwners.Evaluate(ctx, repo, pr, reviewers)
if codeowners.IsTooLargeError(err) {
return types.CodeOwnerEvaluation{}, usererror.UnprocessableEntityf(err.Error())
}

if err != nil {
return types.CodeOwnerEvaluation{}, err
}
Expand Down
16 changes: 9 additions & 7 deletions app/api/usererror/translate.go
Expand Up @@ -33,12 +33,13 @@ import (

func Translate(ctx context.Context, err error) *Error {
var (
rError *Error
checkError *check.ValidationError
appError *errors.Error
maxBytesErr *http.MaxBytesError
codeOwnersTooLargeError *codeowners.TooLargeError
lockError *lock.Error
rError *Error
checkError *check.ValidationError
appError *errors.Error
maxBytesErr *http.MaxBytesError
codeOwnersTooLargeError *codeowners.TooLargeError
codeOwnersFileParseError *codeowners.FileParseError
lockError *lock.Error
)

// print original error for debugging purposes
Expand Down Expand Up @@ -106,7 +107,8 @@ func Translate(ctx context.Context, err error) *Error {
return ErrCodeOwnersNotFound
case errors.As(err, &codeOwnersTooLargeError):
return UnprocessableEntityf(codeOwnersTooLargeError.Error())

case errors.As(err, &codeOwnersFileParseError):
return UnprocessableEntityf(codeOwnersFileParseError.Error())
// lock errors
case errors.As(err, &lockError):
return errorFromLockError(lockError)
Expand Down
22 changes: 17 additions & 5 deletions app/services/codeowners/service.go
Expand Up @@ -51,10 +51,6 @@ type TooLargeError struct {
FileSize int64
}

func IsTooLargeError(err error) bool {
return errors.Is(err, &TooLargeError{})
}

func (e *TooLargeError) Error() string {
return fmt.Sprintf(
"The repository's CODEOWNERS file size %.2fMB exceeds the maximum supported size of %dMB",
Expand All @@ -69,6 +65,22 @@ func (e *TooLargeError) Is(target error) bool {
return ok
}

// FileParseError represents an error if codeowners file is not parsable.
type FileParseError struct {
line string
}

func (e *FileParseError) Error() string {
return fmt.Sprintf(
"The repository's CODEOWNERS file has an invalid line: %s", e.line,
)
}

func (e *FileParseError) Is(target error) bool {
_, ok := target.(*FileParseError)
return ok
}

type Config struct {
FilePaths []string
}
Expand Down Expand Up @@ -173,7 +185,7 @@ func (s *Service) parseCodeOwner(codeOwnersContent string) ([]Entry, error) {

parts := strings.Split(line, " ")
if len(parts) < 2 {
return nil, fmt.Errorf("line has invalid format: '%s'", line)
return nil, &FileParseError{line}
}

pattern := parts[0]
Expand Down

0 comments on commit e528ce2

Please sign in to comment.