-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added CLI switch
--no-version-normalization
is related to #102 Signed-off-by: Jan Kowalleck <[email protected]>
- Loading branch information
1 parent
e32e105
commit 93ccb8c
Showing
6 changed files
with
101 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,12 +48,17 @@ class ComponentBuilder | |
/** @var PackageUrlFactory */ | ||
private $packageUrlFactory; | ||
|
||
/** @var bool */ | ||
private $enableVersionNormalization; | ||
|
||
public function __construct( | ||
LicenseFactory $licenseFactory, | ||
PackageUrlFactory $packageUrlFactory | ||
PackageUrlFactory $packageUrlFactory, | ||
bool $enableVersionNormalization = true | ||
) { | ||
$this->licenseFactory = $licenseFactory; | ||
$this->packageUrlFactory = $packageUrlFactory; | ||
$this->enableVersionNormalization = $enableVersionNormalization; | ||
} | ||
|
||
public function getLicenseFactory(): LicenseFactory | ||
|
@@ -66,6 +71,12 @@ public function getPackageUrlFactory(): PackageUrlFactory | |
return $this->packageUrlFactory; | ||
} | ||
|
||
public function setVersionNormalization(bool $enableVersionNormalization): self | ||
{ | ||
$this->enableVersionNormalization = $enableVersionNormalization; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @throws UnexpectedValueException if the given package does not provide a name or version | ||
*/ | ||
|
@@ -144,17 +155,20 @@ private function getPackageVersion(PackageInterface $package): string | |
return $version; | ||
} | ||
|
||
// Versions of Composer packages may be prefixed with "v". | ||
// * This prefix appears to be problematic for CPE and PURL matching and thus is removed here. | ||
// * | ||
// * See for example {@link https://ossindex.sonatype.org/component/pkg:composer/phpmailer/[email protected]} | ||
// * vs {@link https://ossindex.sonatype.org/component/pkg:composer/phpmailer/[email protected]}. | ||
// | ||
// A _numeric_ version can be prefixed with 'v'. | ||
// Strip leading 'v' must not be applied if the "version" is actually a branch name, | ||
// which is totally fine in the composer ecosystem. | ||
if (1 === preg_match('/^v\\d/', $version)) { | ||
return substr($version, 1); | ||
if ($this->enableVersionNormalization) | ||
{ | ||
// Versions of Composer packages may be prefixed with "v". | ||
// * This prefix appears to be problematic for CPE and PURL matching and thus is removed here. | ||
// * | ||
// * See for example {@link https://ossindex.sonatype.org/component/pkg:composer/phpmailer/[email protected]} | ||
// * vs {@link https://ossindex.sonatype.org/component/pkg:composer/phpmailer/[email protected]}. | ||
// | ||
// A _numeric_ version can be prefixed with 'v'. | ||
// Strip leading 'v' must not be applied if the "version" is actually a branch name, | ||
// which is totally fine in the composer ecosystem. | ||
if (1 === preg_match('/^v\\d/', $version)) { | ||
return substr($version, 1); | ||
} | ||
} | ||
|
||
return $version; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,12 +130,14 @@ public function testMakeFromPackageEmptPurlOnThrow(): void | |
public function testMakeFromPackage( | ||
PackageInterface $package, | ||
Component $expected, | ||
bool $enableVersionNormalization = true, | ||
?LicenseFactory $licenseFactory = null | ||
): void { | ||
$packageUrlFactory = $this->createMock(PackageUrlFactory::class); | ||
$builder = new ComponentBuilder( | ||
$licenseFactory ?? $this->createStub(LicenseFactory::class), | ||
$packageUrlFactory | ||
$packageUrlFactory, | ||
$enableVersionNormalization | ||
); | ||
|
||
$purlMadeFromComponent = null; | ||
|
@@ -172,6 +174,7 @@ public function dpMakeFromPackage(): \Generator | |
(new Component('library', 'some-library', '1.2.3')) | ||
->setPackageUrl((new PackageUrl('composer', 'some-library'))->setVersion('1.2.3')) | ||
->setBomRefValue('pkg:composer/[email protected]'), | ||
true, | ||
null, | ||
]; | ||
|
||
|
@@ -187,6 +190,7 @@ public function dpMakeFromPackage(): \Generator | |
(new Component('application', 'some-project', '1.2.3')) | ||
->setPackageUrl((new PackageUrl('composer', 'some-project'))->setVersion('1.2.3')) | ||
->setBomRefValue('pkg:composer/[email protected]'), | ||
true, | ||
null, | ||
]; | ||
|
||
|
@@ -202,6 +206,7 @@ public function dpMakeFromPackage(): \Generator | |
(new Component('application', 'some-composer-plugin', '1.2.3')) | ||
->setPackageUrl((new PackageUrl('composer', 'some-composer-plugin'))->setVersion('1.2.3')) | ||
->setBomRefValue('pkg:composer/[email protected]'), | ||
true, | ||
null, | ||
]; | ||
|
||
|
@@ -218,6 +223,7 @@ public function dpMakeFromPackage(): \Generator | |
(new Component('library', 'some-inDev', 'dev-master')) | ||
->setPackageUrl((new PackageUrl('composer', 'some-inDev'))->setVersion('dev-master')) | ||
->setBomRefValue('pkg:composer/some-inDev@dev-master'), | ||
true, | ||
null, | ||
]; | ||
|
||
|
@@ -233,6 +239,7 @@ public function dpMakeFromPackage(): \Generator | |
(new Component('library', 'some-noVersion', RootPackage::DEFAULT_PRETTY_VERSION)) | ||
->setPackageUrl((new PackageUrl('composer', 'some-noVersion'))->setVersion(null)) | ||
->setBomRefValue('pkg:composer/some-noVersion'), | ||
true, | ||
null, | ||
]; | ||
|
||
|
@@ -266,7 +273,28 @@ public function dpMakeFromPackage(): \Generator | |
->setLicense($license) | ||
->setHashRepository(new HashRepository([HashAlgorithm::SHA_1 => '12345678901234567890123456789012'])) | ||
->setBomRefValue('pkg:composer/my/[email protected]?checksum=sha1:12345678901234567890123456789012'), | ||
true, | ||
$licenseFactory, | ||
]; | ||
|
||
yield 'library with non-normalized version' => [ | ||
$this->createConfiguredMock( | ||
CompletePackageInterface::class, | ||
[ | ||
'getPrettyName' => 'my/package', | ||
'getPrettyVersion' => 'v1.2.3', | ||
] | ||
), | ||
(new Component('library', 'package', 'v1.2.3')) | ||
->setGroup('my') | ||
->setPackageUrl( | ||
(new PackageUrl('composer', 'package')) | ||
->setNamespace('my') | ||
->setVersion('v1.2.3') | ||
) | ||
->setBomRefValue('pkg:composer/my/[email protected]'), | ||
false, | ||
null, | ||
]; | ||
} | ||
} |