feat: codeowners multi location (#785)

This commit is contained in:
Abhinav Singh 2023-11-10 01:51:02 +00:00 committed by Harness
parent 281dc6b02a
commit 8c68b0cd51
3 changed files with 32 additions and 14 deletions

View File

@ -67,7 +67,7 @@ func (e *TooLargeError) Is(target error) bool {
} }
type Config struct { type Config struct {
FilePath string FilePaths []string
} }
type Service struct { type Service struct {
@ -130,10 +130,6 @@ func (s *Service) get(
ref string, ref string,
) (*CodeOwners, error) { ) (*CodeOwners, error) {
codeOwnerFile, err := s.getCodeOwnerFile(ctx, repo, ref) codeOwnerFile, err := s.getCodeOwnerFile(ctx, repo, ref)
// no codeowner file
if gitrpc.ErrorStatus(err) == gitrpc.StatusPathNotFound {
return nil, ErrNotFound
}
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to get codeowner file: %w", err) return nil, fmt.Errorf("unable to get codeowner file: %w", err)
} }
@ -193,15 +189,10 @@ func (s *Service) getCodeOwnerFile(
if ref == "" { if ref == "" {
ref = "refs/heads/" + repo.DefaultBranch ref = "refs/heads/" + repo.DefaultBranch
} }
node, err := s.git.GetTreeNode(ctx, &gitrpc.GetTreeNodeParams{ node, err := s.getCodeOwnerFileNode(ctx, params, ref)
ReadParams: params,
GitREF: ref,
Path: s.config.FilePath,
})
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to retrieve codeowner file %w", err) return nil, fmt.Errorf("cannot get codeowner file : %w", err)
} }
if node.Node.Mode != gitrpc.TreeNodeModeFile { if node.Node.Mode != gitrpc.TreeNodeModeFile {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"codeowner file is of format '%s' but expected to be of format '%s'", "codeowner file is of format '%s' but expected to be of format '%s'",
@ -231,6 +222,33 @@ func (s *Service) getCodeOwnerFile(
}, nil }, nil
} }
func (s *Service) getCodeOwnerFileNode(
ctx context.Context,
params gitrpc.ReadParams,
ref string,
) (*gitrpc.GetTreeNodeOutput, error) {
// iterating over multiple possible codeowner file path to get the file
// todo: once we have api to get multi file we can simplify
for _, path := range s.config.FilePaths {
node, err := s.git.GetTreeNode(ctx, &gitrpc.GetTreeNodeParams{
ReadParams: params,
GitREF: ref,
Path: path,
})
if gitrpc.ErrorStatus(err) == gitrpc.StatusPathNotFound {
continue
}
if err != nil {
return nil, fmt.Errorf("error encountered retrieving codeowner : %w", err)
}
log.Ctx(ctx).Debug().Msgf("using codeowner file from path %s", path)
return node, nil
}
// get of codeowner file gives err at all the location then returning one of the error
return nil, fmt.Errorf("no codeowner file found: %w", ErrNotFound)
}
func (s *Service) getApplicableCodeOwnersForPR( func (s *Service) getApplicableCodeOwnersForPR(
ctx context.Context, ctx context.Context,
repo *types.Repository, repo *types.Repository,

View File

@ -339,6 +339,6 @@ func ProvideCleanupConfig(config *types.Config) cleanup.Config {
// ProvideCodeOwnerConfig loads the codeowner config from the main config. // ProvideCodeOwnerConfig loads the codeowner config from the main config.
func ProvideCodeOwnerConfig(config *types.Config) codeowners.Config { func ProvideCodeOwnerConfig(config *types.Config) codeowners.Config {
return codeowners.Config{ return codeowners.Config{
FilePath: config.CodeOwners.FilePath, FilePaths: config.CodeOwners.FilePaths,
} }
} }

View File

@ -283,7 +283,7 @@ type Config struct {
} }
CodeOwners struct { CodeOwners struct {
FilePath string `envconfig:"GITNESS_CODEOWNERS_FILEPATH" default:".harness/CODEOWNERS"` FilePaths []string `envconfig:"GITNESS_CODEOWNERS_FILEPATH" default:"CODEOWNERS,.harness/CODEOWNERS"`
} }
SMTP struct { SMTP struct {