Skip to content

Commit 739ae40

Browse files
committed
let sbom_package_license match clearyly defined
1 parent 0099fb3 commit 739ae40

File tree

3 files changed

+86
-34
lines changed

3 files changed

+86
-34
lines changed

modules/ingestor/src/graph/sbom/clearly_defined.rs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
11
use crate::graph::purl::creator::PurlCreator;
22
use crate::graph::sbom::{LicenseCreator, LicenseInfo, SbomContext, SbomInformation};
3-
use sea_orm::ConnectionTrait;
3+
use sea_orm::{ConnectionTrait, EntityTrait, Set};
4+
use sea_query::OnConflict;
45
use serde::{Deserialize, Serialize};
56
use std::collections::HashMap;
67
use tracing::instrument;
78
use trustify_common::purl::Purl;
9+
use trustify_entity::sbom_package_license;
10+
use uuid::Uuid;
11+
12+
const CLEARLY_DEFINED_CURATION: &str = "ClearlyDefinedCuration";
813

914
impl SbomContext {
1015
#[instrument(skip(db, curation), err)]
1116
pub async fn ingest_clearly_defined_curation<C: ConnectionTrait>(
1217
&self,
1318
curation: Curation,
1419
db: &C,
20+
sbom_id: Uuid,
1521
) -> Result<(), anyhow::Error> {
1622
let mut purls = PurlCreator::new();
1723
let mut licenses = LicenseCreator::new();
1824

19-
// TODO: Since the node id cannot be obtained here, it’s not possible to replace purl_license_assertion with sbom_package_license.
20-
// let mut assertions = Vec::new();
25+
let mut sbom_package_license_list = Vec::new();
2126

27+
let _a: SbomInformation = (&curation).into();
2228
for (purl, license) in curation.iter() {
2329
let license_info = LicenseInfo {
2430
license: license.clone(),
2531
};
2632

27-
// assertions.push(purl_license_assertion::ActiveModel {
28-
// id: Default::default(),
29-
// license_id: Set(license_info.uuid()),
30-
// versioned_purl_id: Set(purl.version_uuid()),
31-
// sbom_id: Set(self.sbom.sbom_id),
32-
// });
33+
sbom_package_license_list.push(sbom_package_license::ActiveModel {
34+
sbom_id: Set(self.sbom.sbom_id),
35+
node_id: Set(CLEARLY_DEFINED_CURATION.to_string()),
36+
license_id: Set(license_info.uuid()),
37+
license_type: Set(sbom_package_license::LicenseCategory::Declared),
38+
});
3339

3440
purls.add(purl);
3541
licenses.add(&license_info);
3642
}
3743
purls.create(db).await?;
3844
licenses.create(db).await?;
3945

40-
// purl_license_assertion::Entity::insert_many(assertions)
41-
// .on_conflict(
42-
// OnConflict::columns([
43-
// purl_license_assertion::Column::SbomId,
44-
// purl_license_assertion::Column::LicenseId,
45-
// purl_license_assertion::Column::VersionedPurlId,
46-
// ])
47-
// .do_nothing()
48-
// .to_owned(),
49-
// )
50-
// .do_nothing()
51-
// .exec(db)
52-
// .await?;
46+
sbom_package_license::Entity::insert_many(sbom_package_license_list)
47+
.on_conflict(
48+
OnConflict::columns([
49+
sbom_package_license::Column::SbomId,
50+
sbom_package_license::Column::NodeId,
51+
sbom_package_license::Column::LicenseId,
52+
sbom_package_license::Column::LicenseType,
53+
])
54+
.do_nothing()
55+
.to_owned(),
56+
)
57+
.do_nothing()
58+
.exec(db)
59+
.await?;
5360

5461
Ok(())
5562
}

modules/ingestor/src/service/sbom/clearly_defined.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,19 @@ mod test {
115115
use crate::graph::Graph;
116116
use crate::service::{Error, Format, IngestorService};
117117
use anyhow::anyhow;
118+
use sea_orm::{EntityTrait, FromQueryResult, QuerySelect, RelationTrait};
119+
use sea_query::JoinType;
118120
use test_context::test_context;
119121
use test_log::test;
120122
use trustify_common::purl::Purl;
121-
use trustify_test_context::TrustifyContext;
122-
use trustify_test_context::document_bytes;
123+
use trustify_entity::{license, sbom_package_license};
124+
use trustify_test_context::{TrustifyContext, document_bytes};
125+
126+
#[derive(Debug, FromQueryResult)]
127+
struct PackageLicenseInfo {
128+
pub node_id: String,
129+
pub license_expression: String,
130+
}
123131

124132
fn coordinates_to_purl(coords: &str) -> Result<Purl, Error> {
125133
let parts = coords.split('/').collect::<Vec<_>>();
@@ -189,6 +197,25 @@ mod test {
189197
.await
190198
.expect("must ingest");
191199

200+
let result: Vec<PackageLicenseInfo> = sbom_package_license::Entity::find()
201+
.join(
202+
JoinType::Join,
203+
sbom_package_license::Relation::License.def(),
204+
)
205+
.select_only()
206+
.column_as(sbom_package_license::Column::NodeId, "node_id")
207+
.column_as(license::Column::Text, "license_expression")
208+
.into_model::<PackageLicenseInfo>()
209+
.all(&ctx.db)
210+
.await?;
211+
212+
assert_eq!(1, result.len());
213+
assert_eq!("OTHER", result[0].license_expression);
214+
assert_eq!(
215+
"nuget/nuget/-/microsoft.aspnet.mvc/4.0.40804",
216+
result[0].node_id
217+
);
218+
192219
Ok(())
193220
}
194221
}

modules/ingestor/src/service/sbom/clearly_defined_curation.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,15 @@ impl<'g> ClearlyDefinedCurationLoader<'g> {
2626
) -> Result<IngestResult, Error> {
2727
let tx = self.graph.db.begin().await?;
2828

29+
let document_id = curation.document_id().clone();
2930
let sbom = match self
3031
.graph
31-
.ingest_sbom(
32-
labels,
33-
digests,
34-
Some(curation.document_id()),
35-
&curation,
36-
&tx,
37-
)
32+
.ingest_sbom(labels, digests, Some(document_id.clone()), &curation, &tx)
3833
.await?
3934
{
4035
Outcome::Existed(sbom) => sbom,
4136
Outcome::Added(sbom) => {
42-
sbom.ingest_clearly_defined_curation(curation, &tx)
37+
sbom.ingest_clearly_defined_curation(curation, &tx, sbom.sbom.sbom_id)
4338
.await
4439
.map_err(Error::Generic)?;
4540

@@ -61,10 +56,17 @@ impl<'g> ClearlyDefinedCurationLoader<'g> {
6156
mod test {
6257
use crate::graph::Graph;
6358
use crate::service::{Format, IngestorService};
59+
use sea_orm::{EntityTrait, FromQueryResult, QuerySelect, RelationTrait};
60+
use sea_query::JoinType;
6461
use test_context::test_context;
6562
use test_log::test;
66-
use trustify_test_context::TrustifyContext;
67-
use trustify_test_context::document_bytes;
63+
use trustify_entity::{license, sbom_package_license};
64+
use trustify_test_context::{TrustifyContext, document_bytes};
65+
#[derive(Debug, FromQueryResult)]
66+
struct PackageLicenseInfo {
67+
pub node_id: String,
68+
pub license_expression: String,
69+
}
6870

6971
#[test_context(TrustifyContext)]
7072
#[test(tokio::test)]
@@ -84,6 +86,22 @@ mod test {
8486
.await
8587
.expect("must ingest");
8688

89+
let result: Vec<PackageLicenseInfo> = sbom_package_license::Entity::find()
90+
.join(
91+
JoinType::Join,
92+
sbom_package_license::Relation::License.def(),
93+
)
94+
.select_only()
95+
.column_as(sbom_package_license::Column::NodeId, "node_id")
96+
.column_as(license::Column::Text, "license_expression")
97+
.into_model::<PackageLicenseInfo>()
98+
.all(&ctx.db)
99+
.await?;
100+
101+
assert_eq!(1, result.len());
102+
assert_eq!("Apache-2.0 OR MIT", result[0].license_expression);
103+
assert_eq!("ClearlyDefinedCuration", result[0].node_id);
104+
87105
Ok(())
88106
}
89107
}

0 commit comments

Comments
 (0)