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 21, 2025
1 parent 7f7b482 commit ee0c7bc
Show file tree
Hide file tree
Showing 68 changed files with 609 additions and 553 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import com.github.packageurl.PackageURLBuilder;

public class SbomUtils {
public static final String PROTOCOL = "https://";

private SbomUtils() {
// This is a utility class
Expand Down Expand Up @@ -205,7 +206,7 @@ public static Component setPncBuildMetadata(Component component, Build pncBuild,
addExternalReference(
component,
ExternalReference.Type.BUILD_SYSTEM,
"https://" + pncApiUrl + "/pnc-rest/v2/builds/" + pncBuild.getId(),
PROTOCOL + pncApiUrl + "/pnc-rest/v2/builds/" + pncBuild.getId(),
SBOM_RED_HAT_PNC_BUILD_ID);

addExternalReference(
Expand Down Expand Up @@ -249,7 +250,7 @@ public static Component setArtifactMetadata(Component component, Artifact artifa
addExternalReference(
component,
ExternalReference.Type.BUILD_SYSTEM,
"https://" + pncApiUrl + "/pnc-rest/v2/artifacts/" + artifact.getId(),
PROTOCOL + pncApiUrl + "/pnc-rest/v2/artifacts/" + artifact.getId(),
SBOM_RED_HAT_PNC_ARTIFACT_ID);
return component;
}
Expand All @@ -263,7 +264,7 @@ public static Component setPncOperationMetadata(
addExternalReference(
component,
ExternalReference.Type.BUILD_SYSTEM,
"https://" + pncApiUrl + "/pnc-rest/v2/operations/deliverable-analyzer/" + operation.getId(),
PROTOCOL + pncApiUrl + "/pnc-rest/v2/operations/deliverable-analyzer/" + operation.getId(),
SBOM_RED_HAT_PNC_OPERATION_ID);
}

Expand Down Expand Up @@ -705,7 +706,7 @@ public static void addPedigreeCommit(Component c, String url, String uid) {
"Found URL to be added as pedigree commit with the 'git@' protocol: '{}', trying to convert it into 'https://'",
url);

url = "https://" + matcher.group(1) + "/" + matcher.group(2);
url = PROTOCOL + matcher.group(1) + "/" + matcher.group(2);

log.debug("Converted into: '{}'", url);

Expand Down
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,14 +108,15 @@ 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;
}

validateClientConfiguration();

log.debug("Instantiating new S3 client");

// TODO: Set the credentials explicitly on this builder
client = S3Client.builder().region(Region.of(bucketRegion())).build();

log.info("S3 client instantiated");
Expand Down Expand Up @@ -188,8 +189,8 @@ public void upload(Path 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 +216,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 @@ -58,7 +58,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<Path> getFilePaths(Path rootDirectory) {
Expand Down Expand Up @@ -147,8 +147,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 @@ -167,10 +167,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 @@ -140,7 +140,7 @@ default Collection<ErrataVariant.VariantData> getVariantOfProductAndProductVersi
.filter(
variant -> productVersionId
.equals(variant.getAttributes().getRelationships().getProductVersion().getId()))
.collect(Collectors.toList());
.toList();
}

default Map<String, Collection<ErrataCDNRepoNormalized>> getCDNReposOfVariant(
Expand All @@ -152,7 +152,6 @@ default Map<String, Collection<ErrataCDNRepoNormalized>> getCDNReposOfVariant(
}

default Collection<ErrataCDNRepoNormalized> getCDNReposOfVariant(String variantName, String shortProductName) {

Collection<ErrataCDNRepo> allCDNRepos = getAllEntities(
Map.of("filter[variant_name]", variantName),
this::getAllCDNRepos);
Expand All @@ -163,14 +162,13 @@ default Collection<ErrataCDNRepoNormalized> getCDNReposOfVariant(String variantN
&& !cdn.getAttributes().getContentType().equalsIgnoreCase("docker"))
.map(cdn -> new ErrataCDNRepoNormalized(cdn, variantName, !"rhel".equalsIgnoreCase(shortProductName)))
.distinct()
.collect(Collectors.toList());
.toList();
}

// Default method for handling pagination logic with a generic type `T` and a function `getPageFunction`
default <T> Collection<T> getAllEntities(
Map<String, String> filters,
Function<ErrataQueryParameters, ErrataPage<T>> getPageFunction) {

ErrataQueryParameters parameters = ErrataQueryParameters.builder().withFilters(filters).build();

Collection<T> entities = new ArrayList<>();
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
Loading

0 comments on commit ee0c7bc

Please sign in to comment.