From df70ce90e2778e4756440ce529163eeccdcdcf0c Mon Sep 17 00:00:00 2001 From: louib Date: Sun, 3 Sep 2023 11:31:03 -0400 Subject: [PATCH] feat: use the generic PURL scope --- src/cyclone_dx.rs | 2 +- src/nix.rs | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/cyclone_dx.rs b/src/cyclone_dx.rs index 86216bf..c3e8e87 100644 --- a/src/cyclone_dx.rs +++ b/src/cyclone_dx.rs @@ -116,7 +116,7 @@ pub fn dump_derivation(derivation_path: &str, package_node: &crate::nix::Package component_builder.type_("application".to_string()); // I'm assuming here that if a package has been installed by Nix, it was required. component_builder.scope("required".to_string()); - component_builder.purl(package_node.package.get_purl()); + component_builder.purl(package_node.get_purl().unwrap()); component_builder.version(package_node.package.version.to_string()); if let Some(description) = &package_node.package.meta.description { diff --git a/src/nix.rs b/src/nix.rs index 322804f..67c9306 100644 --- a/src/nix.rs +++ b/src/nix.rs @@ -320,20 +320,8 @@ pub struct Package { pub meta: PackageMeta, } impl Package { - pub fn get_purl(&self) -> String { - // FIXME this should not be using the nix scope, which does not actually exist. - // See https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst - // for the accepted scopes. - format!("pkg:nix/{}@{}", self.name, self.version) - } - pub fn pretty_print(&self, base_indent: usize, display_options: &DisplayOptions) -> Vec { let mut response: Vec = vec![]; - response.push(PrettyPrintLine::new(&self.pname, base_indent)); - response.push(PrettyPrintLine::new( - format!("purl: {}", &self.get_purl()), - base_indent + 1, - )); if self.meta.broken.unwrap_or(false) { response.push(PrettyPrintLine::new("broken: true", base_indent + 1)); } @@ -504,6 +492,27 @@ pub struct PackageNode { } impl PackageNode { + pub fn get_purl(&self) -> Option { + if self.main_derivation.get_urls().len() != 0 { + let urls = self.main_derivation.get_urls(); + let url = urls.get(0).unwrap(); + if url.starts_with("https://github.com/") { + // let namespace = ""; + // return Some(format!( + // "pkg:github/{}/{}@{}", + // namespace, self.package.name, self.package.version + // )); + } + return Some(format!( + "pkg:generic/{}@{}", + self.package.name, self.package.version + )); + } + // FIXME this should not be using the nix scope, which does not actually exist. + // See https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst + // for the accepted scopes. + Some(format!("pkg:nix/{}@{}", self.package.name, self.package.version)) + } pub fn pretty_print( &self, graph: &PackageGraph, @@ -512,6 +521,7 @@ impl PackageNode { ) -> Vec { let mut lines: Vec = vec![]; + lines.push(PrettyPrintLine::new(self.get_purl().unwrap(), base_indent)); for line in self.package.pretty_print(base_indent, display_options) { lines.push(line); }