Skip to content

Commit

Permalink
Merge pull request #31 from darthsteven/support-drupalorg-packages
Browse files Browse the repository at this point in the history
Support drupalorg packages
  • Loading branch information
IonBazan authored Apr 26, 2024
2 parents 57c9f41 + e2a7c06 commit 3926de2
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
run: |
composer config --no-plugins allow-plugins.infection/extension-installer true
composer req infection/infection -W
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4 --only-covered
- name: Run phpstan
if: ${{ matrix.php-versions == 8.3 && matrix.operating-system == 'ubuntu-latest' }}
run: |
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ parameters:
level: 6
paths:
- src
excludePaths:
- src/Command/BaseNotTypedCommand.php
checkGenericClassInNonGenericObjectType: true
checkMissingIterableValueType: true
bootstrapFiles:
Expand Down
83 changes: 83 additions & 0 deletions src/Url/DrupalGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace IonBazan\ComposerDiff\Url;

use Composer\Package\PackageInterface;

class DrupalGenerator extends GitlabGenerator
{
const DRUPAL_CORE = 'drupal/core';

/**
* {@inheritdoc}
*/
public function supportsPackage(PackageInterface $package)
{
return self::DRUPAL_CORE === $package->getName() || parent::supportsPackage($package);
}

/**
* @return string
*/
protected function getCompareRef(PackageInterface $package)
{
if (!$package->isDev()) {
return $package->getDistReference();
}

return parent::getCompareRef($package);
}

/**
* {@inheritdoc}
*/
public function getReleaseUrl(PackageInterface $package)
{
// Not sure we can support dev releases right now. Can we distinguish major version dev releases from regular branches?
if ($package->isDev()) {
return null;
}

return sprintf('%s/releases/%s', $this->getProjectUrl($package), $this->getVersionReference($package));
}

/**
* {@inheritdoc}
*/
public function getProjectUrl(PackageInterface $package)
{
return sprintf('https://www.drupal.org/project/%s', $this->getDrupalProjectName($package));
}

/**
* {@inheritdoc}
*/
protected function getDomain()
{
return 'git.drupalcode.org';
}

/**
* @return string|null
*/
private function getVersionReference(PackageInterface $package)
{
if ($package->getDistReference()) {
return $package->getDistReference();
}

return $package->getSourceReference();
}

/**
* @return string
*/
private function getDrupalProjectName(PackageInterface $package)
{
if (self::DRUPAL_CORE === $package->getName()) {
return 'drupal';
}

return preg_replace('/^drupal\//', '', $package->getName());
}
}
1 change: 1 addition & 0 deletions src/Url/GeneratorContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class GeneratorContainer implements UrlGenerator
public function __construct(array $gitlabDomains)
{
$generators = array(
new DrupalGenerator(),
new GithubGenerator(),
new BitBucketGenerator(),
new GitlabGenerator(),
Expand Down
113 changes: 113 additions & 0 deletions tests/Url/DrupalGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace IonBazan\ComposerDiff\Tests\Url;

use Composer\Package\PackageInterface;
use IonBazan\ComposerDiff\Url\DrupalGenerator;

class DrupalGeneratorTest extends GeneratorTest
{
public function releaseUrlProvider()
{
return array(
'contrib-legacy-version' => array(
$this->getPackageWithSource('drupal/token', '1.0.0', 'https://git.drupalcode.org/project/token.git', '8.x-1.0'),
'https://www.drupal.org/project/token/releases/8.x-1.0',
),
'contrib-semver-version' => array(
$this->getPackageWithSource('drupal/webform', '6.0.0', 'https://git.drupalcode.org/project/webform.git', '6.0.0'),
'https://www.drupal.org/project/webform/releases/6.0.0',
),
'semver-semver-dist' => array(
$this->getPackageWithSourceAndDist('drupal/webform', '6.0.0', '6.0.0', 'https://git.drupalcode.org/project/webform.git'),
'https://www.drupal.org/project/webform/releases/6.0.0',
),
'core' => array(
$this->getPackageWithSource('drupal/core', '9.0.0', 'https://github.com/drupal/core.git', '9.0.0'),
'https://www.drupal.org/project/drupal/releases/9.0.0',
),
'core-dev' => array(
$this->getPackageWithSource('drupal/core', 'dev-9.0.x', 'https://github.com/drupal/core.git'),
null,
),
'core-dev-alternate' => array(
$this->getPackageWithSource('drupal/core', '9.0.x-dev', 'https://github.com/drupal/core.git'),
null,
),
'contrib-dev' => array(
$this->getPackageWithSource('drupal/webform', 'dev-9.0.x', 'https://github.com/drupal/core.git'),
null,
),
'contrib-dev-alternate' => array(
$this->getPackageWithSource('drupal/webform', 'dev-9.0.x', 'https://github.com/drupal/core.git'),
null,
),
);
}

public function projectUrlProvider()
{
return array(
'contrib-legacy-version' => array(
$this->getPackageWithSource('drupal/token', '8.x-1.0', 'https://git.drupalcode.org/project/token.git'),
'https://www.drupal.org/project/token',
),
'contrib-semver-version' => array(
$this->getPackageWithSource('drupal/webform', '6.0.0', 'https://git.drupalcode.org/project/webform.git'),
'https://www.drupal.org/project/webform',
),
'core' => array(
$this->getPackageWithSource('drupal/core', '9.0.0', 'https://github.com/drupal/core.git'),
'https://www.drupal.org/project/drupal',
),
);
}

public function compareUrlProvider()
{
return array(
'semver' => array(
$this->getPackageWithSourceAndDist('drupal/webform', '6.0.0', '6.0.0', 'https://git.drupalcode.org/project/webform.git'),
$this->getPackageWithSourceAndDist('drupal/webform', '6.0.1', '6.0.1', 'https://git.drupalcode.org/project/webform.git'),
'https://git.drupalcode.org/project/webform/compare/6.0.0...6.0.1',
),
'legacy-version' => array(
$this->getPackageWithSourceAndDist('drupal/color_field', '2.4.0', '8.x-2.4', 'https://git.drupalcode.org/project/color_field.git'),
$this->getPackageWithSourceAndDist('drupal/color_field', '2.5.0', '8.x-2.5', 'https://git.drupalcode.org/project/color_field.git'),
'https://git.drupalcode.org/project/color_field/compare/8.x-2.4...8.x-2.5',
),
'dev-version' => array(
$this->getPackageWithSourceAndDist('drupal/color_field', '2.4.0', '8.x-2.4', 'https://git.drupalcode.org/project/color_field.git'),
$this->getPackageWithSourceAndDist('drupal/color_field', 'dev-2.5.0', '8.x-2.5', 'https://git.drupalcode.org/project/color_field.git', 'd46283075d76ed244f7825b378eeb1cee246af73'),
'https://git.drupalcode.org/project/color_field/compare/8.x-2.4...d462830',
),
);
}

/**
* @param string $name
* @param string $version
* @param string|null $sourceUrl
* @param string|null $sourceReference
*
* @return PackageInterface
*/
protected function getPackageWithSourceAndDist($name, $version, $distVersion, $sourceUrl, $sourceReference = null)
{
$package = $this->getPackage($name, $version, $sourceReference);
$package->method('getSourceUrl')->willReturn($sourceUrl);
$package->method('getDistReference')->willReturn($distVersion);
$package->method('getSourceReference')->willReturn($sourceReference);
$package->method('isDev')->willReturn(0 === strpos($version, 'dev-') || '-dev' === substr($version, -4));

return $package;
}

/**
* {@inheritdoc}
*/
protected function getGenerator()
{
return new DrupalGenerator();
}
}
3 changes: 3 additions & 0 deletions tests/Url/GeneratorContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function testGetsProperGenerator()
$this->assertNotSame($gitlabGenerator, $gitlab2Generator);
$this->assertNull($container->get($this->getPackageWithSource('', '', 'https://gitlab3.org')));
$this->assertNull($container->get($this->getPackageWithSource('', '', null)));
$drupalGenerator = $container->get($this->getPackageWithSource('', '', 'https://git.drupalcode.org'));
$this->assertInstanceOf('IonBazan\ComposerDiff\Url\DrupalGenerator', $drupalGenerator);
$this->assertNotSame($gitlabGenerator, $drupalGenerator);
}

public function testItSupportsPackageSupportedByOneOfTheGenerators()
Expand Down

0 comments on commit 3926de2

Please sign in to comment.