Skip to content

Commit

Permalink
Merge pull request #16 from giggsey/project-links
Browse files Browse the repository at this point in the history
Add Project Links
  • Loading branch information
IonBazan authored Jan 6, 2022
2 parents 52b0f49 + 347c7ee commit 319f10b
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 45 deletions.
27 changes: 27 additions & 0 deletions src/Formatter/AbstractFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IonBazan\ComposerDiff\Formatter;

use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Package\PackageInterface;
Expand Down Expand Up @@ -46,6 +47,32 @@ public function getUrl(DiffEntry $entry)
return null;
}

/**
* @return string|null
*/
public function getProjectUrl(OperationInterface $operation)
{
if ($operation instanceof UpdateOperation) {
$package = $operation->getInitialPackage();
}

if ($operation instanceof InstallOperation || $operation instanceof UninstallOperation) {
$package = $operation->getPackage();
}

if (!isset($package)) {
return null;
}

$generator = $this->generators->get($package);

if (!$generator) {
return null;
}

return $generator->getProjectUrl($package);
}

/**
* @return string|null
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IonBazan\ComposerDiff\Formatter;

use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use IonBazan\ComposerDiff\Diff\DiffEntries;
Expand Down Expand Up @@ -53,6 +54,7 @@ private function transformEntries(DiffEntries $entries, $withUrls)

if ($withUrls) {
$row['compare'] = $this->getUrl($entry);
$row['link'] = $this->getProjectUrl($entry->getOperation());
}

$rows[$row['name']] = $row;
Expand Down
12 changes: 9 additions & 3 deletions src/Formatter/MarkdownListFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,35 @@ private function getRow(DiffEntry $entry, $withUrls)
$operation = $entry->getOperation();

if ($operation instanceof InstallOperation) {
$packageName = $operation->getPackage()->getName();
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
return sprintf(
' - Install <fg=green>%s</> (<fg=yellow>%s</>)%s',
$operation->getPackage()->getName(),
$packageUrl ?: $packageName,
$operation->getPackage()->getFullPrettyVersion(),
$url
);
}

if ($operation instanceof UpdateOperation) {
$packageName = $operation->getInitialPackage()->getName();
$projectUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
return sprintf(
' - %s <fg=green>%s</> (<fg=yellow>%s</> => <fg=yellow>%s</>)%s',
ucfirst($entry->getType()),
$operation->getInitialPackage()->getName(),
$projectUrl ?: $packageName,
$operation->getInitialPackage()->getFullPrettyVersion(),
$operation->getTargetPackage()->getFullPrettyVersion(),
$url
);
}

if ($operation instanceof UninstallOperation) {
$packageName = $operation->getPackage()->getName();
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
return sprintf(
' - Uninstall <fg=green>%s</> (<fg=yellow>%s</>)%s',
$operation->getPackage()->getName(),
$packageUrl ?: $packageName,
$operation->getPackage()->getFullPrettyVersion(),
$url
);
Expand Down
17 changes: 12 additions & 5 deletions src/Formatter/MarkdownTableFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function renderSingle(DiffEntries $entries, $title, $withUrls)
$rows = array();

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

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

/**
* @param bool $withUrls
* @return string[]
*/
private function getTableRow(DiffEntry $entry)
private function getTableRow(DiffEntry $entry, $withUrls)
{
$operation = $entry->getOperation();
if ($operation instanceof InstallOperation) {
$packageName = $operation->getPackage()->getName();
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
return array(
$operation->getPackage()->getName(),
$packageUrl ?: $packageName,
'<fg=green>New</>',
'-',
$operation->getPackage()->getFullPrettyVersion(),
);
}

if ($operation instanceof UpdateOperation) {
$packageName = $operation->getInitialPackage()->getName();
$projectUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
return array(
$operation->getInitialPackage()->getName(),
$projectUrl ?: $packageName,
$entry->isChange() ? '<fg=magenta>Changed</>' : ($entry->isUpgrade() ? '<fg=cyan>Upgraded</>' : '<fg=yellow>Downgraded</>'),
$operation->getInitialPackage()->getFullPrettyVersion(),
$operation->getTargetPackage()->getFullPrettyVersion(),
);
}

if ($operation instanceof UninstallOperation) {
$packageName = $operation->getPackage()->getName();
$packageUrl = $withUrls ? $this->formatUrl($this->getProjectUrl($operation), $packageName) : $packageName;
return array(
$operation->getPackage()->getName(),
$packageUrl ?: $packageName,
'<fg=red>Removed</>',
$operation->getPackage()->getFullPrettyVersion(),
'-',
Expand Down
8 changes: 8 additions & 0 deletions src/Url/BitBucketGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public function getReleaseUrl(PackageInterface $package)
{
return sprintf('%s/src/%s', $this->getRepositoryUrl($package), $package->isDev() ? $package->getSourceReference() : $package->getPrettyVersion());
}

/**
* {@inheritdoc}
*/
public function getProjectUrl(PackageInterface $package)
{
return $this->getRepositoryUrl($package);
}
}
8 changes: 8 additions & 0 deletions src/Url/GithubGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function getReleaseUrl(PackageInterface $package)
return sprintf('%s/releases/tag/%s', $this->getRepositoryUrl($package), $package->getPrettyVersion());
}

/**
* {@inheritdoc}
*/
public function getProjectUrl(PackageInterface $package)
{
return $this->getRepositoryUrl($package);
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Url/GitlabGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public function getReleaseUrl(PackageInterface $package)

return sprintf('%s/tags/%s', $this->getRepositoryUrl($package), $package->getPrettyVersion());
}

/**
* {@inheritdoc}
*/
public function getProjectUrl(PackageInterface $package)
{
return $this->getRepositoryUrl($package);
}
}
5 changes: 5 additions & 0 deletions src/Url/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function getCompareUrl(PackageInterface $initialPackage, PackageInterface
* @return string|null
*/
public function getReleaseUrl(PackageInterface $package);

/**
* @return string|null
*/
public function getProjectUrl(PackageInterface $package);
}
36 changes: 18 additions & 18 deletions tests/Command/DiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ public function outputDataProvider()
),
'Markdown with URLs' => array(
<<<OUTPUT
| Prod Packages | Operation | Base | Target | Link |
|---------------|------------|-------|--------|--------------------------------------------|
| a/package-1 | New | - | 1.0.0 | [Compare](github.com/releases/tag/1.0.0) |
| a/package-2 | Upgraded | 1.0.0 | 1.2.0 | [Compare](github.com/compare/1.0.0..1.2.0) |
| a/package-3 | Removed | 0.1.1 | - | [Compare](github.com/releases/tag/0.1.1) |
| a/package-4 | Removed | 0.1.1 | - | [Compare](gitlab.org/tags/0.1.1) |
| a/package-5 | Removed | 0.1.1 | - | [Compare](gitlab2.org/tags/0.1.1) |
| a/package-6 | Removed | 0.1.1 | - | |
| a/package-7 | Downgraded | 1.2.0 | 1.0.0 | [Compare](github.com/compare/1.2.0..1.0.0) |
| Prod Packages | Operation | Base | Target | Link |
|----------------------------|------------|-------|--------|--------------------------------------------|
| [a/package-1](github.com) | New | - | 1.0.0 | [Compare](github.com/releases/tag/1.0.0) |
| [a/package-2](github.com) | Upgraded | 1.0.0 | 1.2.0 | [Compare](github.com/compare/1.0.0..1.2.0) |
| [a/package-3](github.com) | Removed | 0.1.1 | - | [Compare](github.com/releases/tag/0.1.1) |
| [a/package-4](gitlab.org) | Removed | 0.1.1 | - | [Compare](gitlab.org/tags/0.1.1) |
| [a/package-5](gitlab2.org) | Removed | 0.1.1 | - | [Compare](gitlab2.org/tags/0.1.1) |
| a/package-6 | Removed | 0.1.1 | - | |
| [a/package-7](github.com) | Downgraded | 1.2.0 | 1.0.0 | [Compare](github.com/compare/1.2.0..1.0.0) |
OUTPUT
Expand All @@ -155,15 +155,15 @@ public function outputDataProvider()
),
'Markdown with URLs and custom gitlab domains' => array(
<<<OUTPUT
| Prod Packages | Operation | Base | Target | Link |
|---------------|------------|-------|--------|--------------------------------------------|
| a/package-1 | New | - | 1.0.0 | [Compare](github.com/releases/tag/1.0.0) |
| a/package-2 | Upgraded | 1.0.0 | 1.2.0 | [Compare](github.com/compare/1.0.0..1.2.0) |
| a/package-3 | Removed | 0.1.1 | - | [Compare](github.com/releases/tag/0.1.1) |
| a/package-4 | Removed | 0.1.1 | - | [Compare](gitlab.org/tags/0.1.1) |
| a/package-5 | Removed | 0.1.1 | - | [Compare](gitlab2.org/tags/0.1.1) |
| a/package-6 | Removed | 0.1.1 | - | [Compare](gitlab3.org/tags/0.1.1) |
| a/package-7 | Downgraded | 1.2.0 | 1.0.0 | [Compare](github.com/compare/1.2.0..1.0.0) |
| Prod Packages | Operation | Base | Target | Link |
|----------------------------|------------|-------|--------|--------------------------------------------|
| [a/package-1](github.com) | New | - | 1.0.0 | [Compare](github.com/releases/tag/1.0.0) |
| [a/package-2](github.com) | Upgraded | 1.0.0 | 1.2.0 | [Compare](github.com/compare/1.0.0..1.2.0) |
| [a/package-3](github.com) | Removed | 0.1.1 | - | [Compare](github.com/releases/tag/0.1.1) |
| [a/package-4](gitlab.org) | Removed | 0.1.1 | - | [Compare](gitlab.org/tags/0.1.1) |
| [a/package-5](gitlab2.org) | Removed | 0.1.1 | - | [Compare](gitlab2.org/tags/0.1.1) |
| [a/package-6](gitlab3.org) | Removed | 0.1.1 | - | [Compare](gitlab3.org/tags/0.1.1) |
| [a/package-7](github.com) | Downgraded | 1.2.0 | 1.0.0 | [Compare](github.com/compare/1.2.0..1.0.0) |
OUTPUT
Expand Down
11 changes: 11 additions & 0 deletions tests/Formatter/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public function testGetUrlReturnsNullForInvalidOperation()
$this->assertNull($formatter->getUrl(new DiffEntry($operation)));
}

public function testGetProjectUrlReturnsNullForInvalidOperation()
{
$output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock();
$operation = $this->getMockBuilder('Composer\DependencyResolver\Operation\OperationInterface')->getMock();
$formatter = $this->getFormatter($output, $this->getGenerators());
$this->assertNull($formatter->getProjectUrl($operation));
}

/**
* @param bool $withUrls
*
Expand Down Expand Up @@ -111,6 +119,9 @@ protected function getGenerators()
$generator->method('getReleaseUrl')->willReturnCallback(function (PackageInterface $package) {
return sprintf('https://example.com/r/%s', $package->getVersion());
});
$generator->method('getProjectUrl')->willReturnCallback(function (PackageInterface $package) {
return sprintf('https://example.com/r/%s', $package->getName());
});

$generators = $this->getMockBuilder('IonBazan\ComposerDiff\Url\GeneratorContainer')
->disableOriginalConstructor()
Expand Down
Loading

0 comments on commit 319f10b

Please sign in to comment.