From a7901c35b717e520aef96e6bb09d2a3a9e14d1a0 Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Mon, 6 Dec 2021 16:19:58 +0800 Subject: [PATCH] add GitHub formatter --- README.md | 2 +- src/Command/DiffCommand.php | 5 +- src/Formatter/GitHubFormatter.php | 93 +++++++++++++++++++++++++ tests/Command/DiffCommandTest.php | 11 +++ tests/Formatter/GitHubFormatterTest.php | 40 +++++++++++ 5 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 src/Formatter/GitHubFormatter.php create mode 100644 tests/Formatter/GitHubFormatterTest.php diff --git a/README.md b/README.md index f512408..3ea816e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Command/DiffCommand.php b/src/Command/DiffCommand.php index 238a77b..d3daa79 100644 --- a/src/Command/DiffCommand.php +++ b/src/Command/DiffCommand.php @@ -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; @@ -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' @@ -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); diff --git a/src/Formatter/GitHubFormatter.php b/src/Formatter/GitHubFormatter.php new file mode 100644 index 0000000..18654d3 --- /dev/null +++ b/src/Formatter/GitHubFormatter.php @@ -0,0 +1,93 @@ +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'); + } +} diff --git a/tests/Command/DiffCommandTest.php b/tests/Command/DiffCommandTest.php index 7d8c742..b509d02 100644 --- a/tests/Command/DiffCommandTest.php +++ b/tests/Command/DiffCommandTest.php @@ -252,6 +252,17 @@ public function outputDataProvider() '-f' => 'json', ), ), + 'GitHub' => array( + << 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', + ), + ), ); } } diff --git a/tests/Formatter/GitHubFormatterTest.php b/tests/Formatter/GitHubFormatterTest.php new file mode 100644 index 0000000..99c1d6e --- /dev/null +++ b/tests/Formatter/GitHubFormatterTest.php @@ -0,0 +1,40 @@ + 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 << 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); + } +}