Skip to content

Commit

Permalink
Curation - Generate a cache directory dedicated to each project (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
asafambar authored Mar 14, 2024
1 parent d82ecc0 commit 2299b78
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
48 changes: 41 additions & 7 deletions commands/audit/scarunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"os"
"time"

Expand Down Expand Up @@ -174,12 +175,26 @@ func getDirectDependenciesFromTree(dependencyTrees []*xrayCmdUtils.GraphNode) []
return directDependencies.ToSlice()
}

func getCurationCacheByTech(tech coreutils.Technology) (string, error) {
if tech == coreutils.Maven {
return xrayutils.GetCurationMavenCacheFolder()
}
return "", nil
}

func GetTechDependencyTree(params xrayutils.AuditParams, tech coreutils.Technology) (flatTree *xrayCmdUtils.GraphNode, fullDependencyTrees []*xrayCmdUtils.GraphNode, err error) {
logMessage := fmt.Sprintf("Calculating %s dependencies", tech.ToFormal())
curationLogMsg, curationCacheFolder, err := getCurationCacheFolderAndLogMsg(params, tech)
if err != nil {
return
}
// In case it's not curation command these 'curationLogMsg' be empty
logMessage += curationLogMsg
log.Info(logMessage + "...")
if params.Progress() != nil {
params.Progress().SetHeadlineMsg(logMessage)
}

err = SetResolutionRepoIfExists(params, tech)
if err != nil {
return
Expand All @@ -191,15 +206,9 @@ func GetTechDependencyTree(params xrayutils.AuditParams, tech coreutils.Technolo
var uniqueDeps []string
var uniqDepsWithTypes map[string][]string
startTime := time.Now()

switch tech {
case coreutils.Maven, coreutils.Gradle:
curationCacheFolder := ""
if params.IsCurationCmd() {
curationCacheFolder, err = xrayutils.GetCurationMavenCacheFolder()
if err != nil {
return
}
}
fullDependencyTrees, uniqDepsWithTypes, err = java.BuildDependencyTree(java.DepTreeParams{
Server: serverDetails,
DepsRepo: params.DepsRepo(),
Expand Down Expand Up @@ -238,6 +247,31 @@ func GetTechDependencyTree(params xrayutils.AuditParams, tech coreutils.Technolo
return
}

func getCurationCacheFolderAndLogMsg(params xrayutils.AuditParams, tech coreutils.Technology) (logMessage string, curationCacheFolder string, err error) {
if !params.IsCurationCmd() {
return
}
if curationCacheFolder, err = getCurationCacheByTech(tech); err != nil {
return
}

dirExist, err := fileutils.IsDirExists(curationCacheFolder, false)
if err != nil {
return
}

if dirExist {
if dirIsEmpty, scopErr := fileutils.IsDirEmpty(curationCacheFolder); scopErr != nil || !dirIsEmpty {
err = scopErr
return
}
}

logMessage = ". Project's cache is currently empty, so this run may take longer to complete"

return logMessage, curationCacheFolder, err
}

// Associates a technology with another of a different type in the structure.
// Docker is not present, as there is no docker-config command and, consequently, no docker.yaml file we need to operate on.
var TechType = map[coreutils.Technology]project.ProjectType{
Expand Down
5 changes: 5 additions & 0 deletions commands/curation/curationaudit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,12 @@ func getTestCasesForDoCurationAudit() []testCase {
pathToPreTest: filepath.Join(TestDataDir, "projects", "package-managers", "maven", "maven-curation", "pretest"),
preTestExec: "mvn",
funcToGetGoals: func(t *testing.T) []string {
rootDir, err := os.Getwd()
assert.NoError(t, err)
// set the cache to test project dir, in order to fill its cache with dependencies
callbackPreTest := clienttestutils.ChangeDirWithCallback(t, rootDir, filepath.Join("..", "test"))
curationCache, err := utils.GetCurationMavenCacheFolder()
callbackPreTest()
require.NoError(t, err)
return []string{"com.jfrog:maven-dep-tree:tree", "-DdepsTreeOutputFile=output", "-Dmaven.repo.local=" + curationCache}
},
Expand Down
18 changes: 16 additions & 2 deletions utils/paths.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import (
// #nosec G505 -- Not in use for secrets.
"crypto/sha1"
"encoding/hex"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"os"
"path/filepath"
Expand Down Expand Up @@ -35,10 +38,21 @@ func GetCurationCacheFolder() (string, error) {
return filepath.Join(curationFolder, "cache"), nil
}

func GetCurationMavenCacheFolder() (string, error) {
func GetCurationMavenCacheFolder() (projectDir string, err error) {
curationFolder, err := GetCurationCacheFolder()
if err != nil {
return "", err
}
return filepath.Join(curationFolder, "maven"), nil
workingDir, err := os.Getwd()
if err != nil {
return "", err
}
// #nosec G401 -- Not a secret hash.
hasher := sha1.New()
_, err = hasher.Write([]byte(workingDir))
if err != nil {
return "", err
}
projectDir = filepath.Join(curationFolder, "maven", hex.EncodeToString(hasher.Sum(nil)))
return
}

0 comments on commit 2299b78

Please sign in to comment.