1717
1818import static com .redhat .exhort .impl .ExhortApi .debugLoggingIsNeeded ;
1919
20- import com .fasterxml .jackson .core .JsonProcessingException ;
21- import com .fasterxml .jackson .databind .JsonMappingException ;
2220import com .github .packageurl .MalformedPackageURLException ;
2321import com .github .packageurl .PackageURL ;
2422import com .redhat .exhort .Api ;
3634import java .nio .charset .StandardCharsets ;
3735import java .nio .file .Files ;
3836import java .nio .file .Path ;
39- import java .util .*;
37+ import java .util .ArrayList ;
38+ import java .util .Arrays ;
39+ import java .util .HashMap ;
40+ import java .util .List ;
41+ import java .util .Map ;
42+ import java .util .Objects ;
43+ import java .util .TreeMap ;
4044import java .util .logging .Logger ;
4145import java .util .regex .Pattern ;
4246import java .util .stream .Collectors ;
@@ -152,56 +156,52 @@ private void performManifestVersionsCheck(String[] goModGraphLines, Path manifes
152156 List <String > comparisonLines =
153157 Arrays .stream (goModGraphLines )
154158 .filter ((line ) -> line .startsWith (root ))
155- .map (( line ) -> getChildVertex ( line ) )
159+ .map (GoModulesProvider :: getChildVertex )
156160 .collect (Collectors .toList ());
157161 List <String > goModDependencies = collectAllDepsFromManifest (lines , goModLines );
158- comparisonLines .stream ()
159- .forEach (
160- (dependency ) -> {
161- String [] parts = dependency .split ("@" );
162- String version = parts [1 ];
163- String depName = parts [0 ];
164- goModDependencies .stream ()
165- .forEach (
166- (dep ) -> {
167- String [] artifactParts = dep .trim ().split (" " );
168- String currentDepName = artifactParts [0 ];
169- String currentVersion = artifactParts [1 ];
170- if (currentDepName .trim ().equals (depName .trim ())) {
171- if (!currentVersion .trim ().equals (version .trim ())) {
172- throw new RuntimeException (
173- String .format (
174- "Can't continue with analysis - versions mismatch for"
175- + " dependency name=%s, manifest version=%s, installed"
176- + " Version=%s, if you want to allow version mismatch for"
177- + " analysis between installed and requested packages,"
178- + " set environment variable/setting -"
179- + " %s=false" ,
180- depName ,
181- currentVersion ,
182- version ,
183- Provider .PROP_MATCH_MANIFEST_VERSIONS ));
184- }
185- }
186- });
187- });
162+ comparisonLines .forEach (
163+ (dependency ) -> {
164+ String [] parts = dependency .split ("@" );
165+ String version = parts [1 ];
166+ String depName = parts [0 ];
167+ goModDependencies .forEach (
168+ (dep ) -> {
169+ String [] artifactParts = dep .trim ().split (" " );
170+ String currentDepName = artifactParts [0 ];
171+ String currentVersion = artifactParts [1 ];
172+ if (currentDepName .trim ().equals (depName .trim ())) {
173+ if (!currentVersion .trim ().equals (version .trim ())) {
174+ throw new RuntimeException (
175+ String .format (
176+ "Can't continue with analysis - versions mismatch for"
177+ + " dependency name=%s, manifest version=%s, installed"
178+ + " Version=%s, if you want to allow version mismatch for"
179+ + " analysis between installed and requested packages,"
180+ + " set environment variable/setting -"
181+ + " %s=false" ,
182+ depName ,
183+ currentVersion ,
184+ version ,
185+ Provider .PROP_MATCH_MANIFEST_VERSIONS ));
186+ }
187+ }
188+ });
189+ });
188190 } catch (IOException e ) {
189191 throw new RuntimeException (
190192 "Failed to open go.mod file for manifest versions check validation!" );
191193 }
192194 }
193195
194196 private List <String > collectAllDepsFromManifest (String [] lines , String goModLines ) {
195- List <String > result = new ArrayList <>();
196197 // collect all deps that starts with require keyword
197- result =
198+ List < String > result =
198199 Arrays .stream (lines )
199200 .filter ((line ) -> line .trim ().startsWith ("require" ) && !line .contains ("(" ))
200201 .map ((dep ) -> dep .substring ("require" .length ()).trim ())
201202 .collect (Collectors .toList ());
202203
203204 // collect all deps that are inside `require` blocks
204-
205205 String currentSegmentOfGoMod = goModLines ;
206206 Map <String , Integer > requirePosObject = decideRequireBlockIndex (currentSegmentOfGoMod );
207207 while (requirePosObject .get ("index" ) > -1 ) {
@@ -245,15 +245,15 @@ public void determineMainModuleVersion(Path directory) {
245245 VersionControlSystem vcs = new GitVersionControlSystemImpl ();
246246 if (vcs .isDirectoryRepo (directory )) {
247247 TagInfo latestTagInfo = vcs .getLatestTag (directory );
248- if (!latestTagInfo .getTagName ().trim ().equals ( "" )) {
248+ if (!latestTagInfo .getTagName ().trim ().isEmpty ( )) {
249249 if (!latestTagInfo .isCurrentCommitPointedByTag ()) {
250250 String nextTagVersion = vcs .getNextTagVersion (latestTagInfo );
251251 this .mainModuleVersion = vcs .getPseudoVersion (latestTagInfo , nextTagVersion );
252252 } else {
253253 this .mainModuleVersion = latestTagInfo .getTagName ();
254254 }
255255 } else {
256- if (!latestTagInfo .getCurrentCommitDigest ().trim ().equals ( "" )) {
256+ if (!latestTagInfo .getCurrentCommitDigest ().trim ().isEmpty ( )) {
257257 this .mainModuleVersion =
258258 vcs .getPseudoVersion (latestTagInfo , getDefaultMainModuleVersion ());
259259 }
@@ -262,7 +262,7 @@ public void determineMainModuleVersion(Path directory) {
262262 }
263263
264264 private Sbom buildSbomFromGraph (
265- String goModulesResult , List <PackageURL > ignoredDeps , Path manifestPath ) throws IOException {
265+ String goModulesResult , List <PackageURL > ignoredDeps , Path manifestPath ) {
266266 // Each entry contains a key of the module, and the list represents the module direct
267267 // dependencies , so
268268 // pairing of the key with each of the dependencies in a list is basically an edge in the graph.
@@ -272,7 +272,7 @@ private Sbom buildSbomFromGraph(
272272 // value of list of that module' dependencies.
273273 List <String > linesList = Arrays .asList (goModulesResult .split (System .lineSeparator ()));
274274
275- Integer startingIndex = 0 ;
275+ int startingIndex = 0 ;
276276 for (String line : linesList ) {
277277 if (!edges .containsKey (getParentVertex (line ))) {
278278 // Collect all direct dependencies of the current module into a list.
@@ -298,8 +298,7 @@ private Sbom buildSbomFromGraph(
298298 PackageURL source = toPurl (key , "@" , this .goEnvironmentVariableForPurl );
299299 value .forEach (
300300 dep -> {
301- PackageURL targetPurl =
302- toPurl ((String ) dep , "@" , this .goEnvironmentVariableForPurl );
301+ PackageURL targetPurl = toPurl (dep , "@" , this .goEnvironmentVariableForPurl );
303302 sbom .addDependency (source , targetPurl , null );
304303 });
305304 });
@@ -348,12 +347,11 @@ private Map<String, List<String>> getFinalPackagesVersionsForModule(
348347
349348 private List <String > getListOfPackagesWithFinalVersions (
350349 Map <String , String > finalModulesVersions , Map .Entry <String , List <String >> entry ) {
351- return (List <String >)
352- entry .getValue ().stream ()
353- .map (
354- (packageWithVersion ) ->
355- getPackageWithFinalVersion (finalModulesVersions , (String ) packageWithVersion ))
356- .collect (Collectors .toList ());
350+ return entry .getValue ().stream ()
351+ .map (
352+ (packageWithVersion ) ->
353+ getPackageWithFinalVersion (finalModulesVersions , packageWithVersion ))
354+ .collect (Collectors .toList ());
357355 }
358356
359357 public static String getPackageWithFinalVersion (
@@ -387,11 +385,10 @@ private TreeMap<String, String> getQualifiers(boolean includeOsAndArch) {
387385 getEnvironmentVariable (goEnvironmentVariables , GO_HOST_ARCHITECTURE_ENV_NAME );
388386 String hostOS =
389387 getEnvironmentVariable (goEnvironmentVariables , GO_HOST_OPERATION_SYSTEM_ENV_NAME );
390- return new TreeMap <String , String >(
391- Map .of ("type" , "module" , "goos" , hostOS , "goarch" , hostArch ));
388+ return new TreeMap <>(Map .of ("type" , "module" , "goos" , hostOS , "goarch" , hostArch ));
392389 }
393390
394- return new TreeMap <String , String >(Map .of ("type" , "module" ));
391+ return new TreeMap <>(Map .of ("type" , "module" ));
395392 }
396393
397394 private static String getEnvironmentVariable (String goEnvironmentVariables , String envName ) {
@@ -403,8 +400,7 @@ private static String getEnvironmentVariable(String goEnvironmentVariables, Stri
403400 return envValue .replaceAll ("\" " , "" );
404401 }
405402
406- private String buildGoModulesDependencies (Path manifestPath )
407- throws JsonMappingException , JsonProcessingException {
403+ private String buildGoModulesDependencies (Path manifestPath ) {
408404 String [] goModulesDeps ;
409405 goModulesDeps = new String [] {goExecutable , "mod" , "graph" };
410406
0 commit comments