Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add GitHub formatter #14

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ composer diff --help # Display detailed usage instructions
- `--no-prod` - ignore prod dependencies (`require`)
- `--with-platform` (`-p`) - include platform dependencies (PHP, extensions, etc.)
- `--with-links` (`-l`) - include compare/release URLs
- `--format` (`-f`) - output format (mdtable, mdlist, json) - default: `mdtable`
- `--format` (`-f`) - output format (mdtable, mdlist, json, github) - default: `mdtable`
- `--gitlab-domains` - custom gitlab domains for compare/release URLs - default: use composer config

## Advanced usage
Expand Down
5 changes: 4 additions & 1 deletion src/Command/DiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use IonBazan\ComposerDiff\Diff\DiffEntries;
use IonBazan\ComposerDiff\Diff\DiffEntry;
use IonBazan\ComposerDiff\Formatter\Formatter;
use IonBazan\ComposerDiff\Formatter\GitHubFormatter;
use IonBazan\ComposerDiff\Formatter\JsonFormatter;
use IonBazan\ComposerDiff\Formatter\MarkdownListFormatter;
use IonBazan\ComposerDiff\Formatter\MarkdownTableFormatter;
Expand Down Expand Up @@ -58,7 +59,7 @@ protected function configure()
->addOption('no-prod', null, InputOption::VALUE_NONE, 'Ignore prod dependencies')
->addOption('with-platform', 'p', InputOption::VALUE_NONE, 'Include platform dependencies (PHP version, extensions, etc.)')
->addOption('with-links', 'l', InputOption::VALUE_NONE, 'Include compare/release URLs')
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format (mdtable, mdlist, json)', 'mdtable')
->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format (mdtable, mdlist, json, github)', 'mdtable')
->addOption('gitlab-domains', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Extra Gitlab domains (inherited from Composer config by default)', array())
->addOption('strict', 's', InputOption::VALUE_NONE, 'Return non-zero exit code if there are any changes')
->setHelp(<<<'EOF'
Expand Down Expand Up @@ -202,6 +203,8 @@ private function getFormatter(InputInterface $input, OutputInterface $output)
return new JsonFormatter($output, $urlGenerators);
case 'mdlist':
return new MarkdownListFormatter($output, $urlGenerators);
case 'github':
return new GitHubFormatter($output, $urlGenerators);
// case 'mdtable':
default:
return new MarkdownTableFormatter($output, $urlGenerators);
Expand Down
93 changes: 93 additions & 0 deletions src/Formatter/GitHubFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace IonBazan\ComposerDiff\Formatter;

use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use IonBazan\ComposerDiff\Diff\DiffEntries;
use IonBazan\ComposerDiff\Diff\DiffEntry;

class GitHubFormatter extends AbstractFormatter
{
/**
* {@inheritdoc}
*/
public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls)
{
$this->renderSingle($prodEntries, 'Prod Packages', $withUrls);
$this->renderSingle($devEntries, 'Dev Packages', $withUrls);
}

/**
* {@inheritdoc}
*/
public function renderSingle(DiffEntries $entries, $title, $withUrls)
{
if (!\count($entries)) {
return;
}

$message = str_replace("\n", '%0A', implode("\n", $this->transformEntries($entries, $withUrls)));
$this->output->writeln(sprintf('::notice title=%s::%s', $title, $message));
}

/**
* @param bool $withUrls
*
* @return string[]
*/
private function transformEntries(DiffEntries $entries, $withUrls)
{
$rows = array();

foreach ($entries as $entry) {
$rows[] = $this->transformEntry($entry, $withUrls);
}

return $rows;
}

/**
* @param bool $withUrls
*
* @return string
*/
private function transformEntry(DiffEntry $entry, $withUrls)
{
$operation = $entry->getOperation();
$url = $withUrls ? $this->getUrl($entry) : null;
$url = (null !== $url) ? ' '.$url : '';

if ($operation instanceof InstallOperation) {
return sprintf(
' - Install %s (%s)%s',
$operation->getPackage()->getName(),
$operation->getPackage()->getFullPrettyVersion(),
$url
);
}

if ($operation instanceof UpdateOperation) {
return sprintf(
' - %s %s (%s => %s)%s',
ucfirst($entry->getType()),
$operation->getInitialPackage()->getName(),
$operation->getInitialPackage()->getFullPrettyVersion(),
$operation->getTargetPackage()->getFullPrettyVersion(),
$url
);
}

if ($operation instanceof UninstallOperation) {
return sprintf(
' - Uninstall %s (%s)%s',
$operation->getPackage()->getName(),
$operation->getPackage()->getFullPrettyVersion(),
$url
);
}

throw new \InvalidArgumentException('Invalid operation');
}
}
11 changes: 11 additions & 0 deletions tests/Command/DiffCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ public function outputDataProvider()
'-f' => 'json',
),
),
'GitHub' => array(
<<<OUTPUT
::notice title=Prod Packages:: - Install a/package-1 (1.0.0)%0A - Upgrade a/package-2 (1.0.0 => 1.2.0)%0A - Uninstall a/package-3 (0.1.1)%0A - Uninstall a/package-4 (0.1.1)%0A - Uninstall a/package-5 (0.1.1)%0A - Uninstall a/package-6 (0.1.1)%0A - Downgrade a/package-7 (1.2.0 => 1.0.0)

OUTPUT
,
array(
'--no-dev' => null,
'-f' => 'github',
),
),
);
}
}
40 changes: 40 additions & 0 deletions tests/Formatter/GitHubFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace IonBazan\ComposerDiff\Tests\Formatter;

use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use IonBazan\ComposerDiff\Formatter\GitHubFormatter;
use IonBazan\ComposerDiff\Formatter\JsonFormatter;
use IonBazan\ComposerDiff\Url\GeneratorContainer;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;

class GitHubFormatterTest extends FormatterTest
{
protected function getSampleOutput($withUrls)
{
if ($withUrls) {
return <<<OUTPUT
::notice title=Prod Packages:: - Install a/package-1 (1.0.0) https://example.com/r/1.0.0%0A - Install a/no-link-1 (1.0.0)%0A - Upgrade a/package-2 (1.0.0 => 1.2.0) https://example.com/c/1.0.0..1.2.0%0A - Downgrade a/package-3 (2.0.0 => 1.1.1) https://example.com/c/2.0.0..1.1.1%0A - Downgrade a/no-link-2 (2.0.0 => 1.1.1)%0A - Change php (>=7.4.6 => ^8.0)
::notice title=Dev Packages:: - Change a/package-5 (dev-master 1234567 => 1.1.1) https://example.com/c/dev-master..1.1.1%0A - Uninstall a/package-4 (0.1.1) https://example.com/r/0.1.1%0A - Uninstall a/no-link-2 (0.1.1)

OUTPUT;
}

return <<<OUTPUT
::notice title=Prod Packages:: - Install a/package-1 (1.0.0)%0A - Install a/no-link-1 (1.0.0)%0A - Upgrade a/package-2 (1.0.0 => 1.2.0)%0A - Downgrade a/package-3 (2.0.0 => 1.1.1)%0A - Downgrade a/no-link-2 (2.0.0 => 1.1.1)%0A - Change php (>=7.4.6 => ^8.0)
::notice title=Dev Packages:: - Change a/package-5 (dev-master 1234567 => 1.1.1)%0A - Uninstall a/package-4 (0.1.1)%0A - Uninstall a/no-link-2 (0.1.1)

OUTPUT;
}

/**
* {@inheritdoc}
*/
protected function getFormatter(OutputInterface $output, GeneratorContainer $generators)
{
return new GitHubFormatter($output, $generators);
}
}