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} }