Skip to content

Commit ec744f4

Browse files
committed
let sbom_package_license match clearyly defined
1 parent 895a360 commit ec744f4

File tree

3 files changed

+85
-34
lines changed

3 files changed

+85
-34
lines changed

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,61 @@
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

2227
for (purl, license) in curation.iter() {
2328
let license_info = LicenseInfo {
2429
license: license.clone(),
2530
};
2631

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-
// });
32+
sbom_package_license_list.push(sbom_package_license::ActiveModel {
33+
sbom_id: Set(self.sbom.sbom_id),
34+
node_id: Set(CLEARLY_DEFINED_CURATION.to_string()),
35+
license_id: Set(license_info.uuid()),
36+
license_type: Set(sbom_package_license::LicenseCategory::Declared),
37+
});
3338

3439
purls.add(purl);
3540
licenses.add(&license_info);
3641
}
3742
purls.create(db).await?;
3843
licenses.create(db).await?;
3944

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?;
45+
sbom_package_license::Entity::insert_many(sbom_package_license_list)
46+
.on_conflict(
47+
OnConflict::columns([
48+
sbom_package_license::Column::SbomId,
49+
sbom_package_license::Column::NodeId,
50+
sbom_package_license::Column::LicenseId,
51+
sbom_package_license::Column::LicenseType,
52+
])
53+
.do_nothing()
54+
.to_owned(),
55+
)
56+
.do_nothing()
57+
.exec(db)
58+
.await?;
5359

5460
Ok(())
5561
}

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,19 @@ mod test {
9999
use crate::graph::Graph;
100100
use crate::service::{Cache, Error, Format, IngestorService};
101101
use anyhow::anyhow;
102+
use sea_orm::{EntityTrait, FromQueryResult, QuerySelect, RelationTrait};
103+
use sea_query::JoinType;
102104
use test_context::test_context;
103105
use test_log::test;
104106
use trustify_common::purl::Purl;
105-
use trustify_test_context::TrustifyContext;
106-
use trustify_test_context::document_bytes;
107+
use trustify_entity::{license, sbom_package_license};
108+
use trustify_test_context::{TrustifyContext, document_bytes};
109+
110+
#[derive(Debug, FromQueryResult)]
111+
struct PackageLicenseInfo {
112+
pub node_id: String,
113+
pub license_expression: String,
114+
}
107115

108116
fn coordinates_to_purl(coords: &str) -> Result<Purl, Error> {
109117
let parts = coords.split('/').collect::<Vec<_>>();
@@ -179,6 +187,25 @@ mod test {
179187
.await
180188
.expect("must ingest");
181189

190+
let result: Vec<PackageLicenseInfo> = sbom_package_license::Entity::find()
191+
.join(
192+
JoinType::Join,
193+
sbom_package_license::Relation::License.def(),
194+
)
195+
.select_only()
196+
.column_as(sbom_package_license::Column::NodeId, "node_id")
197+
.column_as(license::Column::Text, "license_expression")
198+
.into_model::<PackageLicenseInfo>()
199+
.all(&ctx.db)
200+
.await?;
201+
202+
assert_eq!(1, result.len());
203+
assert_eq!("OTHER", result[0].license_expression);
204+
assert_eq!(
205+
"nuget/nuget/-/microsoft.aspnet.mvc/4.0.40804",
206+
result[0].node_id
207+
);
208+
182209
Ok(())
183210
}
184211
}

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::{Cache, 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)]
@@ -85,6 +87,22 @@ mod test {
8587
.await
8688
.expect("must ingest");
8789

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

0 commit comments

Comments
 (0)