|
| 1 | +package java |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/jfrog/jfrog-cli-core/v2/utils/config" |
| 9 | + "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" |
| 10 | + "github.com/jfrog/jfrog-cli-core/v2/utils/xray" |
| 11 | + "github.com/jfrog/jfrog-client-go/utils/errorutils" |
| 12 | + xrayUtils "github.com/jfrog/jfrog-client-go/xray/services/utils" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + GavPackageTypeIdentifier = "gav://" |
| 17 | +) |
| 18 | + |
| 19 | +func BuildDependencyTree(depTreeParams DepTreeParams, tech coreutils.Technology) ([]*xrayUtils.GraphNode, map[string][]string, error) { |
| 20 | + if tech == coreutils.Maven { |
| 21 | + return buildMavenDependencyTree(&depTreeParams) |
| 22 | + } |
| 23 | + return buildGradleDependencyTree(&depTreeParams) |
| 24 | +} |
| 25 | + |
| 26 | +type DepTreeParams struct { |
| 27 | + UseWrapper bool |
| 28 | + Server *config.ServerDetails |
| 29 | + DepsRepo string |
| 30 | + IsMavenDepTreeInstalled bool |
| 31 | + IsCurationCmd bool |
| 32 | + CurationCacheFolder string |
| 33 | +} |
| 34 | + |
| 35 | +type DepTreeManager struct { |
| 36 | + server *config.ServerDetails |
| 37 | + depsRepo string |
| 38 | + useWrapper bool |
| 39 | +} |
| 40 | + |
| 41 | +func NewDepTreeManager(params *DepTreeParams) DepTreeManager { |
| 42 | + return DepTreeManager{useWrapper: params.UseWrapper, depsRepo: params.DepsRepo, server: params.Server} |
| 43 | +} |
| 44 | + |
| 45 | +// The structure of a dependency tree of a module in a Gradle/Maven project, as created by the gradle-dep-tree and maven-dep-tree plugins. |
| 46 | +type moduleDepTree struct { |
| 47 | + Root string `json:"root"` |
| 48 | + Nodes map[string]xray.DepTreeNode `json:"nodes"` |
| 49 | +} |
| 50 | + |
| 51 | +// Reads the output files of the gradle-dep-tree and maven-dep-tree plugins and returns them as a slice of GraphNodes. |
| 52 | +// It takes the output of the plugin's run (which is a byte representation of a list of paths of the output files, separated by newlines) as input. |
| 53 | +func getGraphFromDepTree(outputFilePaths string) (depsGraph []*xrayUtils.GraphNode, uniqueDepsMap map[string][]string, err error) { |
| 54 | + modules, err := parseDepTreeFiles(outputFilePaths) |
| 55 | + if err != nil { |
| 56 | + return |
| 57 | + } |
| 58 | + uniqueDepsMap = map[string][]string{} |
| 59 | + for _, module := range modules { |
| 60 | + moduleTree, moduleUniqueDeps := GetModuleTreeAndDependencies(module) |
| 61 | + depsGraph = append(depsGraph, moduleTree) |
| 62 | + for depToAdd, depTypes := range moduleUniqueDeps { |
| 63 | + uniqueDepsMap[depToAdd] = depTypes |
| 64 | + } |
| 65 | + } |
| 66 | + return |
| 67 | +} |
| 68 | + |
| 69 | +// Returns a dependency tree and a flat list of the module's dependencies for the given module |
| 70 | +func GetModuleTreeAndDependencies(module *moduleDepTree) (*xrayUtils.GraphNode, map[string][]string) { |
| 71 | + moduleTreeMap := make(map[string]xray.DepTreeNode) |
| 72 | + moduleDeps := module.Nodes |
| 73 | + for depName, dependency := range moduleDeps { |
| 74 | + dependencyId := GavPackageTypeIdentifier + depName |
| 75 | + var childrenList []string |
| 76 | + for _, childName := range dependency.Children { |
| 77 | + childId := GavPackageTypeIdentifier + childName |
| 78 | + childrenList = append(childrenList, childId) |
| 79 | + } |
| 80 | + moduleTreeMap[dependencyId] = xray.DepTreeNode{ |
| 81 | + Types: dependency.Types, |
| 82 | + Children: childrenList, |
| 83 | + } |
| 84 | + } |
| 85 | + return xray.BuildXrayDependencyTree(moduleTreeMap, GavPackageTypeIdentifier+module.Root) |
| 86 | +} |
| 87 | + |
| 88 | +func parseDepTreeFiles(jsonFilePaths string) ([]*moduleDepTree, error) { |
| 89 | + outputFilePaths := strings.Split(strings.TrimSpace(jsonFilePaths), "\n") |
| 90 | + var modules []*moduleDepTree |
| 91 | + for _, path := range outputFilePaths { |
| 92 | + results, err := parseDepTreeFile(path) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + modules = append(modules, results) |
| 97 | + } |
| 98 | + return modules, nil |
| 99 | +} |
| 100 | + |
| 101 | +func parseDepTreeFile(path string) (results *moduleDepTree, err error) { |
| 102 | + depTreeJson, err := os.ReadFile(strings.TrimSpace(path)) |
| 103 | + if errorutils.CheckError(err) != nil { |
| 104 | + return |
| 105 | + } |
| 106 | + results = &moduleDepTree{} |
| 107 | + err = errorutils.CheckError(json.Unmarshal(depTreeJson, &results)) |
| 108 | + return |
| 109 | +} |
| 110 | + |
| 111 | +func getArtifactoryAuthFromServer(server *config.ServerDetails) (string, string, error) { |
| 112 | + username, password, err := server.GetAuthenticationCredentials() |
| 113 | + if err != nil { |
| 114 | + return "", "", err |
| 115 | + } |
| 116 | + if username == "" { |
| 117 | + return "", "", errorutils.CheckErrorf("a username is required for authenticating with Artifactory") |
| 118 | + } |
| 119 | + return username, password, nil |
| 120 | +} |
| 121 | + |
| 122 | +func (dtm *DepTreeManager) GetDepsRepo() string { |
| 123 | + return dtm.depsRepo |
| 124 | +} |
0 commit comments