Skip to content

Commit 3926de2

Browse files
authored
Merge pull request #31 from darthsteven/support-drupalorg-packages
Support drupalorg packages
2 parents 57c9f41 + e2a7c06 commit 3926de2

File tree

6 files changed

+203
-1
lines changed

6 files changed

+203
-1
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ jobs:
6464
run: |
6565
composer config --no-plugins allow-plugins.infection/extension-installer true
6666
composer req infection/infection -W
67-
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4
67+
vendor/bin/infection --ignore-msi-with-no-mutations --min-covered-msi=100 --min-msi=100 -s -j4 --only-covered
6868
- name: Run phpstan
6969
if: ${{ matrix.php-versions == 8.3 && matrix.operating-system == 'ubuntu-latest' }}
7070
run: |

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ parameters:
22
level: 6
33
paths:
44
- src
5+
excludePaths:
6+
- src/Command/BaseNotTypedCommand.php
57
checkGenericClassInNonGenericObjectType: true
68
checkMissingIterableValueType: true
79
bootstrapFiles:

src/Url/DrupalGenerator.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Url;
4+
5+
use Composer\Package\PackageInterface;
6+
7+
class DrupalGenerator extends GitlabGenerator
8+
{
9+
const DRUPAL_CORE = 'drupal/core';
10+
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
public function supportsPackage(PackageInterface $package)
15+
{
16+
return self::DRUPAL_CORE === $package->getName() || parent::supportsPackage($package);
17+
}
18+
19+
/**
20+
* @return string
21+
*/
22+
protected function getCompareRef(PackageInterface $package)
23+
{
24+
if (!$package->isDev()) {
25+
return $package->getDistReference();
26+
}
27+
28+
return parent::getCompareRef($package);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getReleaseUrl(PackageInterface $package)
35+
{
36+
// Not sure we can support dev releases right now. Can we distinguish major version dev releases from regular branches?
37+
if ($package->isDev()) {
38+
return null;
39+
}
40+
41+
return sprintf('%s/releases/%s', $this->getProjectUrl($package), $this->getVersionReference($package));
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function getProjectUrl(PackageInterface $package)
48+
{
49+
return sprintf('https://www.drupal.org/project/%s', $this->getDrupalProjectName($package));
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected function getDomain()
56+
{
57+
return 'git.drupalcode.org';
58+
}
59+
60+
/**
61+
* @return string|null
62+
*/
63+
private function getVersionReference(PackageInterface $package)
64+
{
65+
if ($package->getDistReference()) {
66+
return $package->getDistReference();
67+
}
68+
69+
return $package->getSourceReference();
70+
}
71+
72+
/**
73+
* @return string
74+
*/
75+
private function getDrupalProjectName(PackageInterface $package)
76+
{
77+
if (self::DRUPAL_CORE === $package->getName()) {
78+
return 'drupal';
79+
}
80+
81+
return preg_replace('/^drupal\//', '', $package->getName());
82+
}
83+
}

src/Url/GeneratorContainer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class GeneratorContainer implements UrlGenerator
1717
public function __construct(array $gitlabDomains)
1818
{
1919
$generators = array(
20+
new DrupalGenerator(),
2021
new GithubGenerator(),
2122
new BitBucketGenerator(),
2223
new GitlabGenerator(),

tests/Url/DrupalGeneratorTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace IonBazan\ComposerDiff\Tests\Url;
4+
5+
use Composer\Package\PackageInterface;
6+
use IonBazan\ComposerDiff\Url\DrupalGenerator;
7+
8+
class DrupalGeneratorTest extends GeneratorTest
9+
{
10+
public function releaseUrlProvider()
11+
{
12+
return array(
13+
'contrib-legacy-version' => array(
14+
$this->getPackageWithSource('drupal/token', '1.0.0', 'https://git.drupalcode.org/project/token.git', '8.x-1.0'),
15+
'https://www.drupal.org/project/token/releases/8.x-1.0',
16+
),
17+
'contrib-semver-version' => array(
18+
$this->getPackageWithSource('drupal/webform', '6.0.0', 'https://git.drupalcode.org/project/webform.git', '6.0.0'),
19+
'https://www.drupal.org/project/webform/releases/6.0.0',
20+
),
21+
'semver-semver-dist' => array(
22+
$this->getPackageWithSourceAndDist('drupal/webform', '6.0.0', '6.0.0', 'https://git.drupalcode.org/project/webform.git'),
23+
'https://www.drupal.org/project/webform/releases/6.0.0',
24+
),
25+
'core' => array(
26+
$this->getPackageWithSource('drupal/core', '9.0.0', 'https://github.com/drupal/core.git', '9.0.0'),
27+
'https://www.drupal.org/project/drupal/releases/9.0.0',
28+
),
29+
'core-dev' => array(
30+
$this->getPackageWithSource('drupal/core', 'dev-9.0.x', 'https://github.com/drupal/core.git'),
31+
null,
32+
),
33+
'core-dev-alternate' => array(
34+
$this->getPackageWithSource('drupal/core', '9.0.x-dev', 'https://github.com/drupal/core.git'),
35+
null,
36+
),
37+
'contrib-dev' => array(
38+
$this->getPackageWithSource('drupal/webform', 'dev-9.0.x', 'https://github.com/drupal/core.git'),
39+
null,
40+
),
41+
'contrib-dev-alternate' => array(
42+
$this->getPackageWithSource('drupal/webform', 'dev-9.0.x', 'https://github.com/drupal/core.git'),
43+
null,
44+
),
45+
);
46+
}
47+
48+
public function projectUrlProvider()
49+
{
50+
return array(
51+
'contrib-legacy-version' => array(
52+
$this->getPackageWithSource('drupal/token', '8.x-1.0', 'https://git.drupalcode.org/project/token.git'),
53+
'https://www.drupal.org/project/token',
54+
),
55+
'contrib-semver-version' => array(
56+
$this->getPackageWithSource('drupal/webform', '6.0.0', 'https://git.drupalcode.org/project/webform.git'),
57+
'https://www.drupal.org/project/webform',
58+
),
59+
'core' => array(
60+
$this->getPackageWithSource('drupal/core', '9.0.0', 'https://github.com/drupal/core.git'),
61+
'https://www.drupal.org/project/drupal',
62+
),
63+
);
64+
}
65+
66+
public function compareUrlProvider()
67+
{
68+
return array(
69+
'semver' => array(
70+
$this->getPackageWithSourceAndDist('drupal/webform', '6.0.0', '6.0.0', 'https://git.drupalcode.org/project/webform.git'),
71+
$this->getPackageWithSourceAndDist('drupal/webform', '6.0.1', '6.0.1', 'https://git.drupalcode.org/project/webform.git'),
72+
'https://git.drupalcode.org/project/webform/compare/6.0.0...6.0.1',
73+
),
74+
'legacy-version' => array(
75+
$this->getPackageWithSourceAndDist('drupal/color_field', '2.4.0', '8.x-2.4', 'https://git.drupalcode.org/project/color_field.git'),
76+
$this->getPackageWithSourceAndDist('drupal/color_field', '2.5.0', '8.x-2.5', 'https://git.drupalcode.org/project/color_field.git'),
77+
'https://git.drupalcode.org/project/color_field/compare/8.x-2.4...8.x-2.5',
78+
),
79+
'dev-version' => array(
80+
$this->getPackageWithSourceAndDist('drupal/color_field', '2.4.0', '8.x-2.4', 'https://git.drupalcode.org/project/color_field.git'),
81+
$this->getPackageWithSourceAndDist('drupal/color_field', 'dev-2.5.0', '8.x-2.5', 'https://git.drupalcode.org/project/color_field.git', 'd46283075d76ed244f7825b378eeb1cee246af73'),
82+
'https://git.drupalcode.org/project/color_field/compare/8.x-2.4...d462830',
83+
),
84+
);
85+
}
86+
87+
/**
88+
* @param string $name
89+
* @param string $version
90+
* @param string|null $sourceUrl
91+
* @param string|null $sourceReference
92+
*
93+
* @return PackageInterface
94+
*/
95+
protected function getPackageWithSourceAndDist($name, $version, $distVersion, $sourceUrl, $sourceReference = null)
96+
{
97+
$package = $this->getPackage($name, $version, $sourceReference);
98+
$package->method('getSourceUrl')->willReturn($sourceUrl);
99+
$package->method('getDistReference')->willReturn($distVersion);
100+
$package->method('getSourceReference')->willReturn($sourceReference);
101+
$package->method('isDev')->willReturn(0 === strpos($version, 'dev-') || '-dev' === substr($version, -4));
102+
103+
return $package;
104+
}
105+
106+
/**
107+
* {@inheritdoc}
108+
*/
109+
protected function getGenerator()
110+
{
111+
return new DrupalGenerator();
112+
}
113+
}

tests/Url/GeneratorContainerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public function testGetsProperGenerator()
1919
$this->assertNotSame($gitlabGenerator, $gitlab2Generator);
2020
$this->assertNull($container->get($this->getPackageWithSource('', '', 'https://gitlab3.org')));
2121
$this->assertNull($container->get($this->getPackageWithSource('', '', null)));
22+
$drupalGenerator = $container->get($this->getPackageWithSource('', '', 'https://git.drupalcode.org'));
23+
$this->assertInstanceOf('IonBazan\ComposerDiff\Url\DrupalGenerator', $drupalGenerator);
24+
$this->assertNotSame($gitlabGenerator, $drupalGenerator);
2225
}
2326

2427
public function testItSupportsPackageSupportedByOneOfTheGenerators()

0 commit comments

Comments
 (0)