@@ -68,14 +68,17 @@ export default class ComponentDetection {
6868
6969 public static async getManifestsFromResults ( ) : Promise < Manifest [ ] | undefined > {
7070 core . info ( "Getting manifests from results" ) ;
71+ const results = await fs . readFileSync ( this . outputPath , 'utf8' ) ;
72+ var json : any = JSON . parse ( results ) ;
73+ return this . processComponentsToManifests ( json . componentsFound ) ;
74+ }
75+
76+ public static processComponentsToManifests ( componentsFound : any [ ] ) : Manifest [ ] {
7177 // Parse the result file and add the packages to the package cache
7278 const packageCache = new PackageCache ( ) ;
7379 const packages : Array < ComponentDetectionPackage > = [ ] ;
7480
75- const results = await fs . readFileSync ( this . outputPath , 'utf8' ) ;
76-
77- var json : any = JSON . parse ( results ) ;
78- json . componentsFound . forEach ( async ( component : any ) => {
81+ componentsFound . forEach ( async ( component : any ) => {
7982 // Skip components without packageUrl
8083 if ( ! component . component . packageUrl ) {
8184 core . debug ( `Skipping component detected without packageUrl: ${ JSON . stringify ( {
@@ -113,6 +116,7 @@ export default class ComponentDetection {
113116 }
114117
115118 const referrerUrl = ComponentDetection . makePackageUrl ( referrer . packageUrl ) ;
119+ referrer . packageUrlString = referrerUrl
116120
117121 // Skip if the generated packageUrl is empty
118122 if ( ! referrerUrl ) {
@@ -135,20 +139,32 @@ export default class ComponentDetection {
135139 const manifests : Array < Manifest > = [ ] ;
136140
137141 // Check the locationsFoundAt for every package and add each as a manifest
138- packages . forEach ( async ( pkg : ComponentDetectionPackage ) => {
139- pkg . locationsFoundAt . forEach ( async ( location : any ) => {
142+ this . addPackagesToManifests ( packages , manifests ) ;
143+
144+ return manifests ;
145+ }
146+
147+ private static addPackagesToManifests ( packages : Array < ComponentDetectionPackage > , manifests : Array < Manifest > ) : void {
148+ packages . forEach ( ( pkg : ComponentDetectionPackage ) => {
149+ pkg . locationsFoundAt . forEach ( ( location : any ) => {
140150 if ( ! manifests . find ( ( manifest : Manifest ) => manifest . name == location ) ) {
141151 const manifest = new Manifest ( location , location ) ;
142152 manifests . push ( manifest ) ;
143153 }
144- if ( pkg . topLevelReferrers . length == 0 ) {
154+
155+ // Filter out self-references from topLevelReferrers
156+ const nonSelfReferrers = pkg . topLevelReferrers . filter ( ( referrer : any ) => {
157+ if ( ! referrer . packageUrlString ) return false ;
158+ return referrer . packageUrlString !== pkg . packageUrlString ;
159+ } ) ;
160+
161+ if ( nonSelfReferrers . length == 0 ) {
145162 manifests . find ( ( manifest : Manifest ) => manifest . name == location ) ?. addDirectDependency ( pkg , ComponentDetection . getDependencyScope ( pkg ) ) ;
146163 } else {
147164 manifests . find ( ( manifest : Manifest ) => manifest . name == location ) ?. addIndirectDependency ( pkg , ComponentDetection . getDependencyScope ( pkg ) ) ;
148165 }
149166 } ) ;
150167 } ) ;
151- return manifests ;
152168 }
153169
154170 private static getDependencyScope ( pkg : ComponentDetectionPackage ) {
@@ -236,10 +252,12 @@ export default class ComponentDetection {
236252}
237253
238254class ComponentDetectionPackage extends Package {
255+ public packageUrlString : string ;
239256
240257 constructor ( packageUrl : string , public id : string , public isDevelopmentDependency : boolean , public topLevelReferrers : [ ] ,
241258 public locationsFoundAt : [ ] , public containerDetailIds : [ ] , public containerLayerIds : [ ] ) {
242259 super ( packageUrl ) ;
260+ this . packageUrlString = packageUrl ;
243261 }
244262}
245263
0 commit comments