From dce92b5dc23567a13afe4af1f51c3a3458908d78 Mon Sep 17 00:00:00 2001 From: Andrea Vibelli Date: Thu, 19 Dec 2024 13:31:34 +0100 Subject: [PATCH] feat: allow searching of release manifests by advisory id or name --- .../sbom/service/RequestEventRepository.java | 35 ++++++++++++++++++- .../rest/api/v1beta1/RequestsV1Beta1.java | 8 ++++- .../AlternativeRequestEventRepository.java | 21 +++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/service/src/main/java/org/jboss/sbomer/service/feature/sbom/service/RequestEventRepository.java b/service/src/main/java/org/jboss/sbomer/service/feature/sbom/service/RequestEventRepository.java index 50107e89d..e74218410 100644 --- a/service/src/main/java/org/jboss/sbomer/service/feature/sbom/service/RequestEventRepository.java +++ b/service/src/main/java/org/jboss/sbomer/service/feature/sbom/service/RequestEventRepository.java @@ -249,6 +249,24 @@ protected StringBuilder addConfigCondition( return query; } + protected StringBuilder addReleaseMetadataCondition( + StringBuilder query, + String condition, + String sbomAlias, + String property, + String operator) { + query.append(" ") + .append(condition) + .append(sbomAlias != null && sbomAlias.length() > 0 ? " " + sbomAlias + "." : " ") + .append("release_metadata ->> '") + .append(property) + .append("' ") + .append(operator) + .append(" :") + .append(property); + return query; + } + protected long executeCountQuery(String query, Map params) { Query q = getEntityManager().createNativeQuery(query); for (Map.Entry entry : params.entrySet()) { @@ -291,13 +309,18 @@ public List searchRequestRecords(QueryParameters param }); } + private static final String RELEASE_METADATA_ERRATA_ID = "release.errata_id"; + private static final String RELEASE_METADATA_ERRATA_FULLNAME = "release.errata_fullname"; + private static final Set ALLOWED_TYPE_KEYS = Set.of( "id", ErrataAdvisoryRequestConfig.TYPE_NAME, ImageRequestConfig.TYPE_NAME, PncAnalysisRequestConfig.TYPE_NAME, PncBuildRequestConfig.TYPE_NAME, - PncOperationRequestConfig.TYPE_NAME); + PncOperationRequestConfig.TYPE_NAME, + RELEASE_METADATA_ERRATA_ID, + RELEASE_METADATA_ERRATA_FULLNAME); private static final Map> TYPE_TO_CONFIG_CLASS = Map.of( ImageRequestConfig.TYPE_NAME, @@ -368,6 +391,16 @@ private Map filterAndBuildQueryParams(StringBuilder sb, String t sb.append("WHERE re.id = :id"); return Map.of("id", typeValue); } + if (RELEASE_METADATA_ERRATA_ID.equals(typeKey)) { + sb.append("WHERE s.release_metadata is not null"); + addReleaseMetadataCondition(sb, "AND", "s", "errata_id", "="); + return Map.of("errata_id", typeValue); + } + if (RELEASE_METADATA_ERRATA_FULLNAME.equals(typeKey)) { + sb.append("WHERE s.release_metadata is not null"); + addReleaseMetadataCondition(sb, "AND", "s", "errata_fullname", "="); + return Map.of("errata_fullname", typeValue); + } String identifierKey = getValueOfField(TYPE_TO_CONFIG_CLASS.get(typeKey), "IDENTIFIER_KEY"); addConfigCondition(sb, "WHERE", REQUEST_CONFIG_TYPE, "="); diff --git a/service/src/main/java/org/jboss/sbomer/service/rest/api/v1beta1/RequestsV1Beta1.java b/service/src/main/java/org/jboss/sbomer/service/rest/api/v1beta1/RequestsV1Beta1.java index dd7685dd3..b55aad61d 100644 --- a/service/src/main/java/org/jboss/sbomer/service/rest/api/v1beta1/RequestsV1Beta1.java +++ b/service/src/main/java/org/jboss/sbomer/service/rest/api/v1beta1/RequestsV1Beta1.java @@ -83,7 +83,13 @@ public class RequestsV1Beta1 { name = "Filter requests of events generated for the PNC analysis on the for the specified milestone"), @ExampleObject( value = "pnc-operation=BDQXCNRZJYYAA", - name = "Filter requests of events generated for the specified PNC operation") }) + name = "Filter requests of events generated for the specified PNC operation"), + @ExampleObject( + value = "release.errata_id=1234", + name = "Filter requests of events generated for the specified Errata release by advisory id "), + @ExampleObject( + value = "release.errata_fullname=ERRATA", + name = "Filter requests of events generated for the specified Errata release by advisory name") }) @APIResponse( responseCode = "200", description = "The request event with all the manifest generated", diff --git a/service/src/test/java/org/jboss/sbomer/service/test/utils/AlternativeRequestEventRepository.java b/service/src/test/java/org/jboss/sbomer/service/test/utils/AlternativeRequestEventRepository.java index 2c4268cb4..13775dd61 100644 --- a/service/src/test/java/org/jboss/sbomer/service/test/utils/AlternativeRequestEventRepository.java +++ b/service/src/test/java/org/jboss/sbomer/service/test/utils/AlternativeRequestEventRepository.java @@ -78,6 +78,27 @@ protected StringBuilder addConfigCondition( return query; } + @Override + protected StringBuilder addReleaseMetadataCondition( + StringBuilder query, + String condition, + String sbomAlias, + String property, + String operator) { + query.append(" ") + .append(condition) + .append( + sbomAlias != null && sbomAlias.length() > 0 ? " JSON_EXTRACT(" + sbomAlias + "." + : " JSON_EXTRACT(") + .append("release_metadata, '$.") + .append(property) + .append("') ") + .append(operator) + .append(" :") + .append(property); + return query; + } + @Override protected Instant convertFromTimestamp(Object rawTimeObject) { OffsetDateTime offsetDateTime = (OffsetDateTime) rawTimeObject;