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,47 +156,45 @@ 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 <>() ;
197+ List <String > result ;
196198 // collect all deps that starts with require keyword
197199 result =
198200 Arrays .stream (lines )
@@ -245,15 +247,15 @@ public void determineMainModuleVersion(Path directory) {
245247 VersionControlSystem vcs = new GitVersionControlSystemImpl ();
246248 if (vcs .isDirectoryRepo (directory )) {
247249 TagInfo latestTagInfo = vcs .getLatestTag (directory );
248- if (!latestTagInfo .getTagName ().trim ().equals ( "" )) {
250+ if (!latestTagInfo .getTagName ().trim ().isEmpty ( )) {
249251 if (!latestTagInfo .isCurrentCommitPointedByTag ()) {
250252 String nextTagVersion = vcs .getNextTagVersion (latestTagInfo );
251253 this .mainModuleVersion = vcs .getPseudoVersion (latestTagInfo , nextTagVersion );
252254 } else {
253255 this .mainModuleVersion = latestTagInfo .getTagName ();
254256 }
255257 } else {
256- if (!latestTagInfo .getCurrentCommitDigest ().trim ().equals ( "" )) {
258+ if (!latestTagInfo .getCurrentCommitDigest ().trim ().isEmpty ( )) {
257259 this .mainModuleVersion =
258260 vcs .getPseudoVersion (latestTagInfo , getDefaultMainModuleVersion ());
259261 }
@@ -262,7 +264,7 @@ public void determineMainModuleVersion(Path directory) {
262264 }
263265
264266 private Sbom buildSbomFromGraph (
265- String goModulesResult , List <PackageURL > ignoredDeps , Path manifestPath ) throws IOException {
267+ String goModulesResult , List <PackageURL > ignoredDeps , Path manifestPath ) {
266268 // Each entry contains a key of the module, and the list represents the module direct
267269 // dependencies , so
268270 // pairing of the key with each of the dependencies in a list is basically an edge in the graph.
@@ -272,7 +274,7 @@ private Sbom buildSbomFromGraph(
272274 // value of list of that module' dependencies.
273275 List <String > linesList = Arrays .asList (goModulesResult .split (System .lineSeparator ()));
274276
275- Integer startingIndex = 0 ;
277+ int startingIndex = 0 ;
276278 for (String line : linesList ) {
277279 if (!edges .containsKey (getParentVertex (line ))) {
278280 // Collect all direct dependencies of the current module into a list.
@@ -298,8 +300,7 @@ private Sbom buildSbomFromGraph(
298300 PackageURL source = toPurl (key , "@" , this .goEnvironmentVariableForPurl );
299301 value .forEach (
300302 dep -> {
301- PackageURL targetPurl =
302- toPurl ((String ) dep , "@" , this .goEnvironmentVariableForPurl );
303+ PackageURL targetPurl = toPurl (dep , "@" , this .goEnvironmentVariableForPurl );
303304 sbom .addDependency (source , targetPurl , null );
304305 });
305306 });
@@ -348,12 +349,11 @@ private Map<String, List<String>> getFinalPackagesVersionsForModule(
348349
349350 private List <String > getListOfPackagesWithFinalVersions (
350351 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 ());
352+ return entry .getValue ().stream ()
353+ .map (
354+ (packageWithVersion ) ->
355+ getPackageWithFinalVersion (finalModulesVersions , packageWithVersion ))
356+ .collect (Collectors .toList ());
357357 }
358358
359359 public static String getPackageWithFinalVersion (
@@ -387,11 +387,10 @@ private TreeMap<String, String> getQualifiers(boolean includeOsAndArch) {
387387 getEnvironmentVariable (goEnvironmentVariables , GO_HOST_ARCHITECTURE_ENV_NAME );
388388 String hostOS =
389389 getEnvironmentVariable (goEnvironmentVariables , GO_HOST_OPERATION_SYSTEM_ENV_NAME );
390- return new TreeMap <String , String >(
391- Map .of ("type" , "module" , "goos" , hostOS , "goarch" , hostArch ));
390+ return new TreeMap <>(Map .of ("type" , "module" , "goos" , hostOS , "goarch" , hostArch ));
392391 }
393392
394- return new TreeMap <String , String >(Map .of ("type" , "module" ));
393+ return new TreeMap <>(Map .of ("type" , "module" ));
395394 }
396395
397396 private static String getEnvironmentVariable (String goEnvironmentVariables , String envName ) {
@@ -403,8 +402,7 @@ private static String getEnvironmentVariable(String goEnvironmentVariables, Stri
403402 return envValue .replaceAll ("\" " , "" );
404403 }
405404
406- private String buildGoModulesDependencies (Path manifestPath )
407- throws JsonMappingException , JsonProcessingException {
405+ private String buildGoModulesDependencies (Path manifestPath ) {
408406 String [] goModulesDeps ;
409407 goModulesDeps = new String [] {goExecutable , "mod" , "graph" };
410408
0 commit comments