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

Parse and propagate all licenses #10

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) struct ModelComponent {
pub(crate) version: String,
pub(crate) description: String,
pub(crate) external_references: Vec<ModelExternalReference>,
pub(crate) licenses: Option<Vec<ModelLicense>>,
}

#[derive(Debug)]
Expand All @@ -38,6 +39,15 @@ pub(crate) enum ModelExternalReferenceType {
Website,
}

// TODO: Consider if it is worth splitting this struct up into 2 different
// structs like the cyclone spec. For now, just make id and name both Options
#[derive(Debug)]
pub(crate) struct ModelLicense {
// SPDX id
pub(crate) id: Option<String>,
pub(crate) name: Option<String>,
}

#[derive(Debug)]
pub(crate) struct ModelDependency {
pub(crate) r#ref: String,
Expand Down Expand Up @@ -80,6 +90,11 @@ impl From<ModelComponent> for cyclonedx::Component {
.map(Into::into)
.collect();

if let Some(model_licenses) = model_component.licenses {
let licenses = model_licenses.into_iter().map(Into::into).collect();
builder.licenses(cyclonedx::LicenseChoiceUrl::Variant0(licenses));
}

builder.external_references(external_references);

builder.build().unwrap()
Expand All @@ -96,6 +111,22 @@ impl From<ModelExternalReference> for cyclonedx::ExternalReference {
}
}

impl From<ModelLicense> for cyclonedx::LicenseChoiceUrlVariant0ItemUrl {
fn from(model_license: ModelLicense) -> Self {
let mut builder = cyclonedx::LicenseBuilder::default();

if let Some(id) = model_license.id {
builder.id(id);
}
if let Some(name) = model_license.name {
builder.name(name);
}
cyclonedx::LicenseChoiceUrlVariant0ItemUrl {
license: builder.build().unwrap(),
}
}
}

impl From<ModelDependency> for cyclonedx::Dependency {
fn from(model_dependency: ModelDependency) -> Self {
let depends_on: Vec<serde_json::Value> = model_dependency
Expand Down
27 changes: 25 additions & 2 deletions src/nixtract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::Deserialize;

use crate::model::{
Model, ModelComponent, ModelDependency, ModelExternalReference, ModelExternalReferenceType,
ModelType,
ModelLicense, ModelType,
};

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -45,8 +45,15 @@ pub(crate) struct NixtractNixpkgsMetadata {
pub(crate) pname: String,
pub(crate) version: String,
pub(crate) broken: bool,
pub(crate) license: String,
pub(crate) homepage: String,
pub(crate) licenses: Option<Vec<NixtractLicense>>,
}

#[derive(Deserialize, Debug)]
pub(crate) struct NixtractLicense {
// Not all licenses in nixpkgs have an associated spdx id
pub(crate) spdx_id: Option<String>,
pub(crate) full_name: String,
}

#[derive(Deserialize, Debug)]
Expand All @@ -72,13 +79,20 @@ impl From<Nixtract> for Model {
}
acc
};
let licenses = entry
.nixpkgs_metadata
.licenses
.as_ref()
.map(|v| v.into_iter().map(Into::into).collect());

ModelComponent {
r#type: ModelType::Application,
name: entry.parsed_name.name.clone(),
r#ref: entry.output_path.clone(),
version: entry.nixpkgs_metadata.version.clone(),
description: entry.nixpkgs_metadata.description.clone(),
external_references,
licenses,
}
})
.collect();
Expand All @@ -102,3 +116,12 @@ impl From<Nixtract> for Model {
}
}
}

impl From<&NixtractLicense> for ModelLicense {
fn from(nixtract_license: &NixtractLicense) -> Self {
ModelLicense {
id: nixtract_license.spdx_id.clone(),
name: Some(nixtract_license.full_name.clone()),
}
}
}
Loading
Loading