Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retrieve_all_sbom_roots_by_name() func #1041

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions modules/analysis/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,40 @@ impl AnalysisService {
Ok(paginated.paginate_array(&components))
}

pub async fn retrieve_all_sbom_roots_by_name<TX: AsRef<Transactional>>(
&self,
sbom_id: Uuid,
component_name: String,
tx: TX,
) -> Result<Vec<AncNode>, Error> {
// This function searches for a component(s) by name in a specific sbom, then returns that components
// root components.

let connection = self.db.connection(&tx);
let distinct_sbom_ids = vec![sbom_id.to_string()];
load_graphs(&connection, &distinct_sbom_ids).await;

let components = AnalysisService::query_ancestor_graph::<TX>(
Option::from(component_name),
None,
None,
distinct_sbom_ids,
)
.await;

let mut root_components = Vec::new();
for component in components {
if let Some(last_ancestor) = component.ancestors.last() {
if !root_components.contains(last_ancestor) {
// we want distinct list
root_components.push(last_ancestor.clone());
}
}
}

Ok(root_components)
}

#[instrument(skip(self, tx), err)]
pub async fn retrieve_root_components_by_name<TX: AsRef<Transactional>>(
&self,
Expand Down Expand Up @@ -1288,4 +1322,35 @@ mod test {

Ok(assert_eq!(analysis_graph.total, 1))
}

#[test_context(TrustifyContext)]
#[test(tokio::test)]
async fn test_retrieve_all_sbom_roots_by_name1(
ctx: &TrustifyContext,
) -> Result<(), anyhow::Error> {
ctx.ingest_documents(["spdx/quarkus-bom-3.2.11.Final-redhat-00001.json"])
.await?;

let service = AnalysisService::new(ctx.db.clone());
let component_name = "quarkus-vertx-http".to_string();

let analysis_graph = service
.retrieve_root_components(Query::q(&component_name), Paginated::default(), ())
.await?;

let sbom_id = analysis_graph
.items
.last()
.unwrap()
.sbom_id
.parse::<Uuid>()?;

let roots = service
.retrieve_all_sbom_roots_by_name(sbom_id, component_name, ())
.await?;

assert_eq!(roots.last().unwrap().name, "quarkus-bom");

Ok(())
}
}