Skip to content

Commit 0099fb3

Browse files
committed
fix: /sbom/by-package api can now sort results by name
fixes: #1476
1 parent 004af9b commit 0099fb3

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

modules/fundamental/src/sbom/endpoints/test.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use trustify_common::{id::Id, model::PaginatedResults};
1313
use trustify_entity::labels::Labels;
1414
use trustify_module_ingestor::model::IngestResult;
1515
use trustify_test_context::{TrustifyContext, call::CallService, document_bytes};
16+
use urlencoding::encode;
1617
use uuid::Uuid;
18+
1719
#[test_context(TrustifyContext)]
1820
#[test(actix_web::test)]
1921
async fn license_export(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
@@ -303,3 +305,39 @@ async fn query_sboms_by_ingested_time(ctx: &TrustifyContext) -> Result<(), anyho
303305

304306
Ok(())
305307
}
308+
309+
#[test_context(TrustifyContext)]
310+
#[test(actix_web::test)]
311+
async fn query_sboms_by_package(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
312+
let query = async |purl, sort| {
313+
let app = caller(ctx).await.unwrap();
314+
let uri = format!(
315+
"/api/v2/sbom/by-package?purl={}&sort={}",
316+
encode(purl),
317+
encode(sort)
318+
);
319+
let request = TestRequest::get().uri(&uri).to_request();
320+
let response: Value = app.call_and_read_body_json(request).await;
321+
tracing::debug!(test = "", "{response:#?}");
322+
response
323+
};
324+
325+
// Ingest 2 SBOM's that depend on the same purl
326+
ctx.ingest_documents(["spdx/simple-ext-a.json", "spdx/simple-ext-b.json"])
327+
.await?;
328+
329+
assert_eq!(
330+
2,
331+
query("pkg:rpm/redhat/[email protected]?arch=src", "").await["total"]
332+
);
333+
assert_eq!(
334+
"simple-a",
335+
query("pkg:rpm/redhat/[email protected]?arch=src", "name:asc").await["items"][0]["name"]
336+
);
337+
assert_eq!(
338+
"simple-b",
339+
query("pkg:rpm/redhat/[email protected]?arch=src", "name:desc").await["items"][0]["name"]
340+
);
341+
342+
Ok(())
343+
}

modules/fundamental/src/sbom/service/sbom.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,12 @@ impl SbomService {
321321
.filter(sbom_package_cpe_ref::Column::CpeId.eq(cpe.uuid())),
322322
};
323323

324-
let query = select.filtering(query)?.find_also_linked(SbomNodeLink);
324+
let query = select.find_also_linked(SbomNodeLink).filtering_with(
325+
query,
326+
Columns::from_entity::<sbom::Entity>()
327+
.add_columns(sbom_node::Entity)
328+
.alias("sbom_node", "r0"),
329+
)?;
325330

326331
// limit and execute
327332

0 commit comments

Comments
 (0)