From 74c81e22708cfeff316eaaa595a6a3fd063a06e5 Mon Sep 17 00:00:00 2001 From: shm12 Date: Thu, 9 Jun 2022 16:42:03 +0300 Subject: [PATCH] Fixed relative path in git repo scan --- core/cautils/localgitrepository.go | 9 +++++++++ core/pkg/resourcehandler/filesloader.go | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/core/cautils/localgitrepository.go b/core/cautils/localgitrepository.go index 66215aef..1f6463d9 100644 --- a/core/cautils/localgitrepository.go +++ b/core/cautils/localgitrepository.go @@ -116,3 +116,12 @@ func (g *LocalGitRepository) GetFileLastCommit(filePath string) (*apis.Commit, e Files: []apis.Files{}, }, nil } + +func (g *LocalGitRepository) GetRootDir() (string, error) { + wt, err := g.repo.Worktree() + if err != nil { + return "", fmt.Errorf("failed to get repo root") + } + + return wt.Filesystem.Root(), nil +} diff --git a/core/pkg/resourcehandler/filesloader.go b/core/pkg/resourcehandler/filesloader.go index f674930d..e2901b18 100644 --- a/core/pkg/resourcehandler/filesloader.go +++ b/core/pkg/resourcehandler/filesloader.go @@ -65,8 +65,21 @@ func (fileHandler *FileResourceHandler) GetResources(sessionObj *cautils.OPASess if err != nil { return nil, allResources, nil, err } + + // Get repo root + repoRoot := "" + giRepo, err := cautils.NewLocalGitRepository(path) + if err == nil { + repoRoot, _ = giRepo.GetRootDir() + } + for source, ws := range sourceToWorkloads { workloads = append(workloads, ws...) + + relSource, err := filepath.Rel(repoRoot, source) + if err == nil { + source = relSource + } for i := range ws { workloadIDToSource[ws[i].GetID()] = reporthandling.Source{RelativePath: source} }