Skip to content

Commit

Permalink
feat: support for non-PyPi Components - PackageURL type is now defina…
Browse files Browse the repository at this point in the history
…ble when creating a Component

Signed-off-by: Paul Horton <phorton@sonatype.com>
  • Loading branch information
madpah committed Oct 11, 2021
1 parent a655d29 commit fde79e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cyclonedx/model/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

from .vulnerability import Vulnerability

PURL_TYPE_PREFIX = 'pypi'


class ComponentType(Enum):
"""
Expand All @@ -51,6 +49,7 @@ class Component:
See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.3/#type_component
"""
_type: ComponentType
_package_url_type: str
_name: str
_version: str
_qualifiers: str
Expand All @@ -62,12 +61,13 @@ class Component:
_vulnerabilites: List[Vulnerability] = []

def __init__(self, name: str, version: str, qualifiers: str = None,
component_type: ComponentType = ComponentType.LIBRARY):
component_type: ComponentType = ComponentType.LIBRARY, package_url_type: str = 'pypi'):
self._name = name
self._version = version
self._type = component_type
self._qualifiers = qualifiers
self._vulnerabilites = []
self._package_url_type = package_url_type

def add_vulnerability(self, vulnerability: Vulnerability):
"""
Expand Down Expand Up @@ -125,7 +125,7 @@ def get_purl(self) -> str:
Returns:
PackageURL that reflects this Component as `str`.
"""
base_purl = 'pkg:{}/{}@{}'.format(PURL_TYPE_PREFIX, self._name, self._version)
base_purl = 'pkg:{}/{}@{}'.format(self._package_url_type, self._name, self._version)
if self._qualifiers:
base_purl = '{}?{}'.format(base_purl, self._qualifiers)
return base_purl
Expand Down Expand Up @@ -213,7 +213,7 @@ def to_package_url(self) -> PackageURL:
`packageurl.PackageURL` instance which represents this Component.
"""""
return PackageURL(
type=PURL_TYPE_PREFIX,
type=self._package_url_type,
name=self._name,
version=self._version,
qualifiers=self._qualifiers
Expand Down
9 changes: 9 additions & 0 deletions tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def setUpClass(cls) -> None:
cls._component: Component = Component(name='setuptools', version='50.3.2')
cls._component_with_qualifiers: Component = Component(name='setuptools', version='50.3.2',
qualifiers='extension=tar.gz')
cls._component_generic_file: Component = Component(
name='/test.py', version='UNKNOWN', package_url_type='generic'
)

def test_purl_correct(self):
self.assertEqual(
Expand Down Expand Up @@ -95,3 +98,9 @@ def test_as_package_url_3(self):
type='pypi', name='setuptools', version='50.3.2', qualifiers='extension=tar.gz'
)
self.assertEqual(TestComponent._component_with_qualifiers.to_package_url(), purl)

def test_custom_package_url_type(self):
purl = PackageURL(
type='generic', name='/test.py', version='UNKNOWN'
)
self.assertEqual(TestComponent._component_generic_file.to_package_url(), purl)

0 comments on commit fde79e0

Please sign in to comment.