11package io .kipp .mill .github .dependency .graph
22
3+ import com .github .packageurl .PackageURLBuilder
34import coursier .graph .DependencyTree
45import io .kipp .github .dependency .graph .domain ._
56import mill .scalalib .JavaModule
67
78import scala .collection .mutable
9+ import scala .util .Try
810
911/** Represents a project modules an the dependency trees that belong to it.
1012 *
@@ -25,7 +27,9 @@ final case class ModuleTrees(
2527 * @return Mapping of the name of the dependency and the DependencyNode that
2628 * corresponds to it. The format of the name is org:module:version.
2729 */
28- def toFlattenedNodes (): Map [String , DependencyNode ] = {
30+ def toFlattenedNodes ()(implicit
31+ ctx : mill.api.Ctx
32+ ): Map [String , DependencyNode ] = {
2933
3034 val allDependencies = mutable.Map [String , DependencyNode ]()
3135
@@ -37,16 +41,34 @@ final case class ModuleTrees(
3741
3842 def putTogether : DependencyNode = {
3943 // TODO consider classifiers
40- val packageUrl =
41- s " pkg:maven/ ${dep.module.organization.value}/ ${dep.module.name.value}@ ${reconciledVersion}"
44+
45+ val purl = Try (
46+ PackageURLBuilder
47+ .aPackageURL()
48+ .withType(" maven" )
49+ .withNamespace(dep.module.organization.value)
50+ .withName(dep.module.name.value)
51+ .withVersion(reconciledVersion)
52+ .build()
53+ ).fold(
54+ e => {
55+ ctx.log.error(
56+ s " PURL can't be created from: ${dep.module.orgName}: ${reconciledVersion}"
57+ )
58+ ctx.log.error(e.getMessage())
59+ None
60+ },
61+ validPurl => Some (validPurl.toString())
62+ )
63+
4264 val relationShip : DependencyRelationship =
4365 if (root) DependencyRelationship .direct
4466 else DependencyRelationship .indirect
4567 val dependencies = tree.children.map { child =>
4668 s " ${child.dependency.module.orgName}: ${child.reconciledVersion}"
4769 }
4870 DependencyNode (
49- Some (packageUrl) ,
71+ purl ,
5072 // TODO we can check if original == reconciled here and add metadata that it is a reconciled version
5173 Map .empty,
5274 Some (relationShip),
@@ -60,15 +82,36 @@ final case class ModuleTrees(
6082
6183 allDependencies.get(name) match {
6284 // If the node is found and the relationship is correct just do nothing
63- case Some (node) if verifyRelationship(node) => ()
85+ case Some (node) if verifyRelationship(node) =>
86+ ctx.log.debug(
87+ s " Already seen ${name} with this relationship in this manifest, so skipping... "
88+ )
6489 // If the node is found and the relationship is incorrect, but it's a
6590 // root node, then make sure to mark it as direct
6691 case Some (node) if root =>
92+ ctx.log.debug(
93+ s " Already seen ${name} but we're at the root level so marking as direct... "
94+ )
6795 val updated =
6896 node.copy(relationship = Some (DependencyRelationship .direct))
6997 allDependencies += ((name, updated))
70- // Should never really happen, but it it does do nothing
71- case Some (_) => ()
98+ case Some (_) =>
99+ ctx.log.debug(
100+ s " Found ${name}, but it's already marked as direct so skipping... "
101+ )
102+ // Not a very elegant check, but we don't want to include a range in
103+ // here. These shouldn't still be a range at this point, but it is for
104+ // whatever reason. For now ignore it. This should be incredibly rare
105+ // and I believe a bug in coursier.
106+ case None if reconciledVersion.contains(" ," ) =>
107+ ctx.log.error(
108+ s """ Found what I think is a range version that shouldn't be here...
109+ |
110+ | ${dep.module.organization.value}: ${dep.module.name.value}: ${reconciledVersion}
111+ |
112+ |If you see this, report it. Skipping...
113+ | """ .stripMargin
114+ )
72115 // Unseen dependency, create a node for it
73116 case None =>
74117 val node = putTogether
@@ -82,7 +125,7 @@ final case class ModuleTrees(
82125 allDependencies.toMap
83126 }
84127
85- def toManifest () = {
128+ def toManifest ()( implicit ctx : mill.api. Ctx ) = {
86129 // NOTE: That this may seem odd when reading the spec that we have a
87130 // manifest per module basically, but we did check with the GitHub team and
88131 // they verified the manifests that we showed them.
0 commit comments