Skip to content

Commit 319f10b

Browse files
authored
Merge pull request #16 from giggsey/project-links
Add Project Links
2 parents 52b0f49 + 347c7ee commit 319f10b

17 files changed

+241
-45
lines changed

src/Formatter/AbstractFormatter.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace IonBazan\ComposerDiff\Formatter;
44

55
use Composer\DependencyResolver\Operation\InstallOperation;
6+
use Composer\DependencyResolver\Operation\OperationInterface;
67
use Composer\DependencyResolver\Operation\UninstallOperation;
78
use Composer\DependencyResolver\Operation\UpdateOperation;
89
use Composer\Package\PackageInterface;
@@ -46,6 +47,32 @@ public function getUrl(DiffEntry $entry)
4647
return null;
4748
}
4849

50+
/**
51+
* @return string|null
52+
*/
53+
public function getProjectUrl(OperationInterface $operation)
54+
{
55+
if ($operation instanceof UpdateOperation) {
56+
$package = $operation->getInitialPackage();
57+
}
58+
59+
if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
60+
$package = $operation->getPackage();
61+
}
62+
63+
if (!isset($package)) {
64+
return null;
65+
}
66+
67+
$generator = $this->generators->get($package);
68+
69+
if (!$generator) {
70+
return null;
71+
}
72+
73+
return $generator->getProjectUrl($package);
74+
}
75+
4976
/**
5077
* @return string|null
5178
*/

src/Formatter/JsonFormatter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace IonBazan\ComposerDiff\Formatter;
44

55
use Composer\DependencyResolver\Operation\InstallOperation;
6+
use Composer\DependencyResolver\Operation\OperationInterface;
67
use Composer\DependencyResolver\Operation\UninstallOperation;
78
use Composer\DependencyResolver\Operation\UpdateOperation;
89
use IonBazan\ComposerDiff\Diff\DiffEntries;
@@ -53,6 +54,7 @@ private function transformEntries(DiffEntries $entries, $withUrls)
5354

5455
if ($withUrls) {
5556
$row['compare'] = $this->getUrl($entry);
57+
$row['link'] = $this->getProjectUrl($entry->getOperation());
5658
}
5759

5860
$rows[$row['name']] = $row;

src/Formatter/MarkdownListFormatter.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,35 @@ private function getRow(DiffEntry $entry, $withUrls)
5151
$operation = $entry->getOperation();
5252

5353
if ($operation instanceof InstallOperation) {
54+
$packageName = $operation->getPackage()->getName();
55+
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
5456
return sprintf(
5557
' - Install <fg=green>%s</> (<fg=yellow>%s</>)%s',
56-
$operation->getPackage()->getName(),
58+
$packageUrl ?: $packageName,
5759
$operation->getPackage()->getFullPrettyVersion(),
5860
$url
5961
);
6062
}
6163

6264
if ($operation instanceof UpdateOperation) {
65+
$packageName = $operation->getInitialPackage()->getName();
66+
$projectUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
6367
return sprintf(
6468
' - %s <fg=green>%s</> (<fg=yellow>%s</> => <fg=yellow>%s</>)%s',
6569
ucfirst($entry->getType()),
66-
$operation->getInitialPackage()->getName(),
70+
$projectUrl ?: $packageName,
6771
$operation->getInitialPackage()->getFullPrettyVersion(),
6872
$operation->getTargetPackage()->getFullPrettyVersion(),
6973
$url
7074
);
7175
}
7276

7377
if ($operation instanceof UninstallOperation) {
78+
$packageName = $operation->getPackage()->getName();
79+
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
7480
return sprintf(
7581
' - Uninstall <fg=green>%s</> (<fg=yellow>%s</>)%s',
76-
$operation->getPackage()->getName(),
82+
$packageUrl ?: $packageName,
7783
$operation->getPackage()->getFullPrettyVersion(),
7884
$url
7985
);

src/Formatter/MarkdownTableFormatter.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls)
3232
$rows = array();
3333

3434
foreach ($entries as $entry) {
35-
$row = $this->getTableRow($entry);
35+
$row = $this->getTableRow($entry, $withUrls);
3636

3737
if ($withUrls) {
3838
$row[] = $this->formatUrl($this->getUrl($entry), 'Compare');
@@ -53,32 +53,39 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls)
5353
}
5454

5555
/**
56+
* @param bool $withUrls
5657
* @return string[]
5758
*/
58-
private function getTableRow(DiffEntry $entry)
59+
private function getTableRow(DiffEntry $entry, $withUrls)
5960
{
6061
$operation = $entry->getOperation();
6162
if ($operation instanceof InstallOperation) {
63+
$packageName = $operation->getPackage()->getName();
64+
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
6265
return array(
63-
$operation->getPackage()->getName(),
66+
$packageUrl ?: $packageName,
6467
'<fg=green>New</>',
6568
'-',
6669
$operation->getPackage()->getFullPrettyVersion(),
6770
);
6871
}
6972

7073
if ($operation instanceof UpdateOperation) {
74+
$packageName = $operation->getInitialPackage()->getName();
75+
$projectUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
7176
return array(
72-
$operation->getInitialPackage()->getName(),
77+
$projectUrl ?: $packageName,
7378
$entry->isChange() ? '<fg=magenta>Changed</>' : ($entry->isUpgrade() ? '<fg=cyan>Upgraded</>' : '<fg=yellow>Downgraded</>'),
7479
$operation->getInitialPackage()->getFullPrettyVersion(),
7580
$operation->getTargetPackage()->getFullPrettyVersion(),
7681
);
7782
}
7883

7984
if ($operation instanceof UninstallOperation) {
85+
$packageName = $operation->getPackage()->getName();
86+
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
8087
return array(
81-
$operation->getPackage()->getName(),
88+
$packageUrl ?: $packageName,
8289
'<fg=red>Removed</>',
8390
$operation->getPackage()->getFullPrettyVersion(),
8491
'-',

src/Url/BitBucketGenerator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,12 @@ public function getReleaseUrl(PackageInterface $package)
5555
{
5656
return sprintf('%s/src/%s', $this->getRepositoryUrl($package), $package->isDev() ? $package->getSourceReference() : $package->getPrettyVersion());
5757
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function getProjectUrl(PackageInterface $package)
63+
{
64+
return $this->getRepositoryUrl($package);
65+
}
5866
}

src/Url/GithubGenerator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public function getReleaseUrl(PackageInterface $package)
3535
return sprintf('%s/releases/tag/%s', $this->getRepositoryUrl($package), $package->getPrettyVersion());
3636
}
3737

38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function getProjectUrl(PackageInterface $package)
42+
{
43+
return $this->getRepositoryUrl($package);
44+
}
45+
3846
/**
3947
* {@inheritdoc}
4048
*/

src/Url/GitlabGenerator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,12 @@ public function getReleaseUrl(PackageInterface $package)
5858

5959
return sprintf('%s/tags/%s', $this->getRepositoryUrl($package), $package->getPrettyVersion());
6060
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function getProjectUrl(PackageInterface $package)
66+
{
67+
return $this->getRepositoryUrl($package);
68+
}
6169
}

src/Url/UrlGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public function getCompareUrl(PackageInterface $initialPackage, PackageInterface
2020
* @return string|null
2121
*/
2222
public function getReleaseUrl(PackageInterface $package);
23+
24+
/**
25+
* @return string|null
26+
*/
27+
public function getProjectUrl(PackageInterface $package);
2328
}

tests/Command/DiffCommandTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ public function outputDataProvider()
134134
),
135135
'Markdown with URLs' => array(
136136
<<<OUTPUT
137-
| Prod Packages | Operation | Base | Target | Link |
138-
|---------------|------------|-------|--------|--------------------------------------------|
139-
| a/package-1 | New | - | 1.0.0 | [Compare](github.com/releases/tag/1.0.0) |
140-
| a/package-2 | Upgraded | 1.0.0 | 1.2.0 | [Compare](github.com/compare/1.0.0..1.2.0) |
141-
| a/package-3 | Removed | 0.1.1 | - | [Compare](github.com/releases/tag/0.1.1) |
142-
| a/package-4 | Removed | 0.1.1 | - | [Compare](gitlab.org/tags/0.1.1) |
143-
| a/package-5 | Removed | 0.1.1 | - | [Compare](gitlab2.org/tags/0.1.1) |
144-
| a/package-6 | Removed | 0.1.1 | - | |
145-
| a/package-7 | Downgraded | 1.2.0 | 1.0.0 | [Compare](github.com/compare/1.2.0..1.0.0) |
137+
| Prod Packages | Operation | Base | Target | Link |
138+
|----------------------------|------------|-------|--------|--------------------------------------------|
139+
| [a/package-1](github.com) | New | - | 1.0.0 | [Compare](github.com/releases/tag/1.0.0) |
140+
| [a/package-2](github.com) | Upgraded | 1.0.0 | 1.2.0 | [Compare](github.com/compare/1.0.0..1.2.0) |
141+
| [a/package-3](github.com) | Removed | 0.1.1 | - | [Compare](github.com/releases/tag/0.1.1) |
142+
| [a/package-4](gitlab.org) | Removed | 0.1.1 | - | [Compare](gitlab.org/tags/0.1.1) |
143+
| [a/package-5](gitlab2.org) | Removed | 0.1.1 | - | [Compare](gitlab2.org/tags/0.1.1) |
144+
| a/package-6 | Removed | 0.1.1 | - | |
145+
| [a/package-7](github.com) | Downgraded | 1.2.0 | 1.0.0 | [Compare](github.com/compare/1.2.0..1.0.0) |
146146
147147
148148
OUTPUT
@@ -155,15 +155,15 @@ public function outputDataProvider()
155155
),
156156
'Markdown with URLs and custom gitlab domains' => array(
157157
<<<OUTPUT
158-
| Prod Packages | Operation | Base | Target | Link |
159-
|---------------|------------|-------|--------|--------------------------------------------|
160-
| a/package-1 | New | - | 1.0.0 | [Compare](github.com/releases/tag/1.0.0) |
161-
| a/package-2 | Upgraded | 1.0.0 | 1.2.0 | [Compare](github.com/compare/1.0.0..1.2.0) |
162-
| a/package-3 | Removed | 0.1.1 | - | [Compare](github.com/releases/tag/0.1.1) |
163-
| a/package-4 | Removed | 0.1.1 | - | [Compare](gitlab.org/tags/0.1.1) |
164-
| a/package-5 | Removed | 0.1.1 | - | [Compare](gitlab2.org/tags/0.1.1) |
165-
| a/package-6 | Removed | 0.1.1 | - | [Compare](gitlab3.org/tags/0.1.1) |
166-
| a/package-7 | Downgraded | 1.2.0 | 1.0.0 | [Compare](github.com/compare/1.2.0..1.0.0) |
158+
| Prod Packages | Operation | Base | Target | Link |
159+
|----------------------------|------------|-------|--------|--------------------------------------------|
160+
| [a/package-1](github.com) | New | - | 1.0.0 | [Compare](github.com/releases/tag/1.0.0) |
161+
| [a/package-2](github.com) | Upgraded | 1.0.0 | 1.2.0 | [Compare](github.com/compare/1.0.0..1.2.0) |
162+
| [a/package-3](github.com) | Removed | 0.1.1 | - | [Compare](github.com/releases/tag/0.1.1) |
163+
| [a/package-4](gitlab.org) | Removed | 0.1.1 | - | [Compare](gitlab.org/tags/0.1.1) |
164+
| [a/package-5](gitlab2.org) | Removed | 0.1.1 | - | [Compare](gitlab2.org/tags/0.1.1) |
165+
| [a/package-6](gitlab3.org) | Removed | 0.1.1 | - | [Compare](gitlab3.org/tags/0.1.1) |
166+
| [a/package-7](github.com) | Downgraded | 1.2.0 | 1.0.0 | [Compare](github.com/compare/1.2.0..1.0.0) |
167167
168168
169169
OUTPUT

tests/Formatter/FormatterTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public function testGetUrlReturnsNullForInvalidOperation()
3333
$this->assertNull($formatter->getUrl(new DiffEntry($operation)));
3434
}
3535

36+
public function testGetProjectUrlReturnsNullForInvalidOperation()
37+
{
38+
$output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
39+
$operation = $this->getMockBuilder('Composer\DependencyResolver\Operation\OperationInterface')->getMock();
40+
$formatter = $this->getFormatter($output, $this->getGenerators());
41+
$this->assertNull($formatter->getProjectUrl($operation));
42+
}
43+
3644
/**
3745
* @param bool $withUrls
3846
*
@@ -111,6 +119,9 @@ protected function getGenerators()
111119
$generator->method('getReleaseUrl')->willReturnCallback(function (PackageInterface $package) {
112120
return sprintf('https://example.com/r/%s', $package->getVersion());
113121
});
122+
$generator->method('getProjectUrl')->willReturnCallback(function (PackageInterface $package) {
123+
return sprintf('https://example.com/r/%s', $package->getName());
124+
});
114125

115126
$generators = $this->getMockBuilder('IonBazan\ComposerDiff\Url\GeneratorContainer')
116127
->disableOriginalConstructor()

0 commit comments

Comments
 (0)