Skip to content

Commit

Permalink
style: Fix some more sonar issues with service
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Feb 11, 2025
1 parent c09e4a1 commit a8e7714
Show file tree
Hide file tree
Showing 63 changed files with 521 additions and 466 deletions.
1 change: 0 additions & 1 deletion service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.jboss.sbomer</groupId>
<version>1.0.0-SNAPSHOT</version>
<artifactId>sbomer-service</artifactId>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ public boolean standardErrataImageManifestGenerationEnabled() {
}

/**
* Returns {@code true} if the manifest generation of text only Errata is enabled.
* Returns {@code true} if the manifest generation of text-only Errata is enabled.
*
* @return {@code true} if the manifest generation of text only Errata is enabled, {@code false} otherwise
* @return {@code true} if the manifest generation of text-only Errata is enabled, {@code false} otherwise
*/
public boolean textOnlyErrataManifestGenerationEnabled() {
return unleash.isEnabled(TOGGLE_TEXTONLY_ERRATA_MANIFEST_GENERATION, textonlyErrataGeneration);
Expand Down Expand Up @@ -214,9 +214,9 @@ public boolean standardErrataImageReleaseManifestGenerationEnabled() {
}

/**
* Returns {@code true} if the release manifest generation of text only Errata is enabled.
* Returns {@code true} if the release manifest generation of text-only Errata is enabled.
*
* @return {@code true} if the release manifest generation of text only Errata is enabled, {@code false} otherwise
* @return {@code true} if the release manifest generation of text-only Errata is enabled, {@code false} otherwise
*/
public boolean textOnlyErrataReleaseManifestGenerationEnabled() {
return unleash.isEnabled(TOGGLE_TEXTONLY_ERRATA_RELEASE_MANIFEST_GENERATION, textonlyErrataReleaseGeneration);
Expand Down Expand Up @@ -257,18 +257,11 @@ private void updateToggles(final FeatureToggleResponse toggleResponse) {
TOGGLE_STANDARD_ERRATA_IMAGE_RELEASE_MANIFEST_GENERATION,
TOGGLE_TEXTONLY_ERRATA_RELEASE_MANIFEST_GENERATION)) {
FeatureToggle toggle = toggleResponse.getToggleCollection().getToggle(toggleName);
Boolean previousValue = toggleValues.put(toggleName, toggle.isEnabled());

// FIXME: toggle != null is always 'true'
if (toggle != null) {
Boolean previousValue = toggleValues.put(toggleName, toggle.isEnabled());

if (previousValue == null || previousValue != toggle.isEnabled()) {
log.info("Feature toggle {} was just {}", toggleName, toggle.isEnabled() ? "enabled" : "disabled");
bus.publish(EVENT_NAME, Map.of(toggleName, toggle.isEnabled()));
}
} else {
log.debug("Feature toggle {} was disabled", toggleName);
toggleValues.remove(toggleName);
if (previousValue == null || previousValue != toggle.isEnabled()) {
log.info("Feature toggle {} was just {}", toggleName, toggle.isEnabled() ? "enabled" : "disabled");
bus.publish(EVENT_NAME, Map.of(toggleName, toggle.isEnabled()));
}
}

Expand All @@ -280,7 +273,7 @@ private void updateToggles(final FeatureToggleResponse toggleResponse) {
}

/**
* A callback which will be called when feature flags will be retreieved.
* A callback which will be called when feature flags are retrieved.
*/
@Override
public void togglesFetched(FeatureToggleResponse toggleResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ void init(@Observes StartupEvent ev) {

/**
* Event listener for the {@link FeatureFlags#TOGGLE_S3_STORAGE} toggle. In case it is enabled, the S3 client is
* instantiated. When the toggle is disabled, client is closed.
* instantiated. When the toggle is disabled, the client is closed.
*
* @param flag
* @param flag the flags
*/
@ConsumeEvent(FeatureFlags.EVENT_NAME)
void featureFlag(Map<String, Boolean> flag) {
Expand All @@ -93,7 +93,7 @@ void featureFlag(Map<String, Boolean> flag) {

ensureClient();
} else {
// And it was disabled, close the client, if there is one.
// And it was disabled, close the client if there is one.
if (client != null) {
log.debug("Disabling S3 storage handler");
client.close();
Expand All @@ -108,7 +108,7 @@ void featureFlag(Map<String, Boolean> flag) {
*/
public void ensureClient() {
if (client != null) {
// In case the client it available, will assume it's valid.
// In case the client is available, will assume it's valid.
return;
}

Expand Down Expand Up @@ -188,8 +188,8 @@ public void upload(String path, String key) {
/**
* Returns list of paths within the S3 bucket to log files for a given {@link GenerationRequest} identifier.
*
* @param generationRequestId
* @return
* @param generationRequestId the generation request identifier
* @return the list of paths
*/
public List<String> logFileNames(String generationRequestId) {
ListObjectsV2Request req = ListObjectsV2Request.builder()
Expand All @@ -215,8 +215,8 @@ public List<String> logFileNames(String generationRequestId) {
/**
* Returns list of paths within the S3 bucket to log files for a given {@link GenerationRequest} identifier.
*
* @param generationRequestId
* @return
* @param generationRequestId the generation request identifier
* @return the path
*/
public String log(String generationRequestId, String path) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class S3StorageHandler {
/**
* Returns all paths to files found under a given {@code rootDirectory}.
*
* @param rootDirectory
* @param rootDirectory the root directory to scan for files
* @return List of paths to all files.
*/
private List<String> getFilePaths(File rootDirectory) {
Expand All @@ -75,8 +75,8 @@ private List<String> getFilePaths(File rootDirectory) {
/**
* Populates the {@code filePaths} with absolute paths to files located under the {@code directory}.
*
* @param directory
* @param filePaths
* @param directory the directory
* @param filePaths the list of file paths
*/
private static void getAllFileNamesRecursive(File directory, List<String> filePaths) {
log.debug("Handling '{}' directory...", directory.getAbsolutePath());
Expand Down Expand Up @@ -146,8 +146,8 @@ public void storeFiles(GenerationRequest generationRequest) {
/**
* Returns list of paths within the S3 bucket to log files for a given {@link GenerationRequest} identifier.
*
* @param generationRequestId
* @return
* @param generationRequestId the generation request identifier
* @return the list of log file names
*/
public List<String> listLogFilesInBucket(String generationRequestId) {
SbomGenerationRequest generationRequest = SbomGenerationRequest.findById(generationRequestId); // NOSONAR
Expand All @@ -166,10 +166,10 @@ public List<String> listLogFilesInBucket(String generationRequestId) {
}

/**
* Get log file for a given {@link GenerationRequest} and the requested path.
* Gets the log file for a given {@link GenerationRequest} and the requested path.
*
* @param generationRequestId
* @return
* @param generationRequestId the generation request identifier
* @return the log file content
*/
public String getLog(String generationRequestId, String path) {
SbomGenerationRequest generationRequest = SbomGenerationRequest.findById(generationRequestId); // NOSONAR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

public class ErrataMessageHelper {
private ErrataMessageHelper() {
throw new IllegalStateException("This is a utility class that should not be instantiated");
}

static final ObjectMapper jsonObjectMapper = new ObjectMapper().registerModule(new JavaTimeModule())
private static final ObjectMapper JSON_OBJECT_MAPPER = new ObjectMapper().registerModule(new JavaTimeModule())
.registerModule(new SimpleModule().addDeserializer(Instant.class, new CustomInstantDeserializer()))
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE)
Expand All @@ -50,11 +53,11 @@ public static String decode(byte[] encodedJson) {
}

public static ErrataStatusChangeMessageBody fromStatusChangeMessage(String json) throws JsonProcessingException {
return jsonObjectMapper.readValue(json, ErrataStatusChangeMessageBody.class);
return JSON_OBJECT_MAPPER.readValue(json, ErrataStatusChangeMessageBody.class);
}

public static ErrataStatusChangeMessageBody fromStatusChangeMessage(JsonNode jsonNode) throws IOException {
return jsonObjectMapper.readValue(
return JSON_OBJECT_MAPPER.readValue(
jsonNode.isTextual() ? jsonNode.textValue().getBytes() : jsonNode.toString().getBytes(),
ErrataStatusChangeMessageBody.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,11 @@ public static class Content {
private WrappedContent content;

public Optional<Details> getDetails() {
switch (originalType) {
case RHSA:
return Optional.of(errata.rhsa);
case RHBA:
return Optional.of(errata.rhba);
case RHEA:
return Optional.of(errata.rhea);
default:
return Optional.empty();
}
return switch (originalType) {
case RHSA -> Optional.of(errata.rhsa);
case RHBA -> Optional.of(errata.rhba);
case RHEA -> Optional.of(errata.rhea);
};
}

public Optional<JsonNode> getNotesMapping() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import lombok.Getter;

@Getter
public enum ErrataCDNContentType {

BINARY("Binary"), DEBUGINFO("Debuginfo"), SOURCE("Source");

@Getter
final String type;

ErrataCDNContentType(String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import lombok.Getter;

@Getter
public enum ErrataCDNReleaseType {

PRIMARY("Primary"), EUS("EUS"), FASTTRACK("FastTrack"), LONGLIFE("LongLife");

@Getter
final String type;

ErrataCDNReleaseType(String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -47,17 +48,20 @@

@Slf4j
public class AdvisoryEventUtils {
private AdvisoryEventUtils() {
throw new IllegalStateException("This is a utility class that should not be instantiated");
}

/*
* In case of mainline RHEL releases if the ProductVersion contains a major.minor, augment the CPE. e.g. in case of
* ProductVersion 'RHEL-9.4.0.Z.EUS', change
* In the case of mainline RHEL releases, if the ProductVersion contains a major.minor, augment the CPE, e.g., in
* the case of ProductVersion 'RHEL-9.4.0.Z.EUS', change
*
* cpe:/a:redhat:enterprise_linux:9::highavailability -> cpe:/a:redhat:enterprise_linux:9.4::highavailability
* cpe:/a:redhat:enterprise_linux:9::server -> cpe:/a:redhat:enterprise_linux:9.4::server
*/
/**
* In case of mainline RHEL releases if the ProductVersion contains a {major}.{minor} pattern, augment the original
* CPEs with the {minor} value, if not present already.
* In the case of mainline RHEL releases, if the ProductVersion contains a {major}.{minor} pattern, augment the
* original CPEs with the {minor} value, if not present already.
*
* @param productVersion {@link ProductVersionEntry} to analyze
* @param originalCPEs the set of original CPEs to augment with more granular {minor} value
Expand Down Expand Up @@ -130,7 +134,7 @@ public static List<String> extractPurlUrisFromManifestNode(JsonNode manifestNode
* Creates a set of purls from the given list of {@link RepositoryCoordinates} and with a specified version
*
* @param repositories the list of {@link RepositoryCoordinates} which contain registry, repository and tag values
* @param version the version to setup on the purls
* @param version the version to set up on the purls
* @param includeRepositoryQualifiers the flag which specifies whether the purls should contain the repository
* coordinates as qualifiers
* @return The list of purls as strings.
Expand All @@ -139,19 +143,22 @@ public static Set<String> createPurls(
List<RepositoryCoordinates> repositories,
String version,
boolean includeRepositoryQualifiers) {

return repositories.stream()
Set<String> set = repositories.stream()
.map(repository -> createPurl(repository, version, includeRepositoryQualifiers))
.filter(Objects::nonNull)
.sorted(Comparator.comparingInt(String::length).reversed()) // longest first
.collect(Collectors.toSet());
.sorted(
Comparator.comparingInt(String::length)
.reversed() // longest first
.thenComparing(Comparator.naturalOrder()))
.collect(Collectors.toCollection(LinkedHashSet::new));
return Collections.unmodifiableSet(set);
}

/**
* Creates a purl from the given {@link RepositoryCoordinates} and with a specified version
*
* @param repositories the {@link RepositoryCoordinates} which contains registry, repository and tag values
* @param version the version to setup on the purl
* @param repository the {@link RepositoryCoordinates} which contains registry, repository and tag values
* @param version the version to set up on the purl
* @param includeRepositoryQualifiers the flag which specifies whether the purl should contain the repository
* coordinates as qualifiers
* @return The purl as string.
Expand Down Expand Up @@ -180,7 +187,7 @@ public static String createPurl(

/**
* Creates a set of purls from the starting purl and the list of {@link ErrataCDNRepoNormalized} this build is
* publised to
* published to
*
* @param purl the purl to modify
* @param cdns the list of {@link ErrataCDNRepoNormalized} which contain the CDN information
Expand Down Expand Up @@ -209,7 +216,7 @@ public static Set<String> createPurls(
.map(cdn -> rebuildPurl(purl, cdn))
.collect(Collectors.toSet());
} else if (componentArch.equals("noarch")) {
// Select the "-rpms" CDN repositories (excluding -source-rpms and -debug-rpms) and include all the
// Select the "-rpms" CDN repositories (excluding "-source-rpms" and "-debug-rpms") and include all the
// archs provided
return cdns.stream()
.filter(
Expand Down Expand Up @@ -252,7 +259,7 @@ public static Set<String> createPurls(
/**
* Given an input purl, creates a new purl with the same name, namespace, subpath, type, version and qualifiers.
* Additionally, add the new qualifier "repository_id" with the values provided in the
* {@link ErrataCDNRepoNormalized}. Finally rebuilds the purl to make sure it is valid and qualifiers are properly
* {@link ErrataCDNRepoNormalized}. Finally, rebuilds the purl to make sure it is valid and qualifiers are properly
* sorted.
*
* @param originalPurl the input purl string
Expand Down Expand Up @@ -288,7 +295,7 @@ public static String rebuildPurl(String originalPurl, ErrataCDNRepoNormalized cd
/**
* Given an input purl, creates a new purl with the same name, namespace, subpath, type, version and qualifiers.
* Additionally, add new qualifiers "tag" and "repository_url" with the values provided in the
* {@link RepositoryCoordinates}. Finally rebuilds the purl to make sure it is valid and qualifiers are properly
* {@link RepositoryCoordinates}. Finally, rebuilds the purl to make sure it is valid and qualifiers are properly
* sorted.
*
* @param originalPurl the input purl string
Expand Down Expand Up @@ -325,11 +332,11 @@ public static String rebuildPurl(String originalPurl, RepositoryCoordinates repo
/**
* Given an input purl, creates a set of new purls with the same name, namespace, subpath, type, version and
* qualifiers. Additionally, add new qualifiers "tag" and "repository_url" with the values provided in the
* {@link RepositoryCoordinates}. Finally rebuilds the purls to make sure they are valid and qualifiers are properly
* sorted.
* {@link RepositoryCoordinates}. Finally, rebuilds the purls to make sure they are valid and qualifiers are
* properly sorted.
*
* @param originalPurl the input purl string
* @param repository the list of {@link RepositoryCoordinates} which contain registry, repository and tag values
* @param repositories the list of {@link RepositoryCoordinates} which contain registry, repository and tag values
* @return The new validated set of purls as string.
*/
public static Set<String> rebuildPurls(String originalPurl, List<RepositoryCoordinates> repositories) {
Expand All @@ -338,9 +345,9 @@ public static Set<String> rebuildPurls(String originalPurl, List<RepositoryCoord

/**
* Given a list of {@link RepositoryCoordinates} find the preferred one (based on a score which gives the highest
* value to (repository fragment + tag) highest length).
* value to (repository fragment + tag) the highest length).
*
* @param repository the list of {@link RepositoryCoordinates} which contain registry, repository and tag values
* @param repositories the list of {@link RepositoryCoordinates} which contain registry, repository and tag values
* @return The preferred {@link RepositoryCoordinates}.
*/
public static RepositoryCoordinates findPreferredRepo(List<RepositoryCoordinates> repositories) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

@Slf4j
public class EventNotificationFiringUtil {
private EventNotificationFiringUtil() {
throw new IllegalStateException("This is a utility class that should not be instantiated");
}

public static void notifyRequestEventStatusUpdate(Object requestEventNotification) {
RequestEventStatusUpdateEvent requestEvent = (RequestEventStatusUpdateEvent) requestEventNotification;
Expand Down
Loading

0 comments on commit a8e7714

Please sign in to comment.