|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace IonBazan\ComposerDiff\Formatter; |
| 4 | + |
| 5 | +use Composer\DependencyResolver\Operation\InstallOperation; |
| 6 | +use Composer\DependencyResolver\Operation\UninstallOperation; |
| 7 | +use Composer\DependencyResolver\Operation\UpdateOperation; |
| 8 | +use IonBazan\ComposerDiff\Diff\DiffEntries; |
| 9 | +use IonBazan\ComposerDiff\Diff\DiffEntry; |
| 10 | + |
| 11 | +class GitHubFormatter extends AbstractFormatter |
| 12 | +{ |
| 13 | + /** |
| 14 | + * {@inheritdoc} |
| 15 | + */ |
| 16 | + public function render(DiffEntries $prodEntries, DiffEntries $devEntries, $withUrls) |
| 17 | + { |
| 18 | + $this->renderSingle($prodEntries, 'Prod Packages', $withUrls); |
| 19 | + $this->renderSingle($devEntries, 'Dev Packages', $withUrls); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * {@inheritdoc} |
| 24 | + */ |
| 25 | + public function renderSingle(DiffEntries $entries, $title, $withUrls) |
| 26 | + { |
| 27 | + if (!\count($entries)) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + $message = str_replace("\n", '%0A', implode("\n", $this->transformEntries($entries, $withUrls))); |
| 32 | + $this->output->writeln(sprintf('::notice title=%s::%s', $title, $message)); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param bool $withUrls |
| 37 | + * |
| 38 | + * @return string[] |
| 39 | + */ |
| 40 | + private function transformEntries(DiffEntries $entries, $withUrls) |
| 41 | + { |
| 42 | + $rows = array(); |
| 43 | + |
| 44 | + foreach ($entries as $entry) { |
| 45 | + $rows[] = $this->transformEntry($entry, $withUrls); |
| 46 | + } |
| 47 | + |
| 48 | + return $rows; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param bool $withUrls |
| 53 | + * |
| 54 | + * @return string |
| 55 | + */ |
| 56 | + private function transformEntry(DiffEntry $entry, $withUrls) |
| 57 | + { |
| 58 | + $operation = $entry->getOperation(); |
| 59 | + $url = $withUrls ? $this->getUrl($entry) : null; |
| 60 | + $url = (null !== $url) ? ' '.$url : ''; |
| 61 | + |
| 62 | + if ($operation instanceof InstallOperation) { |
| 63 | + return sprintf( |
| 64 | + ' - Install %s (%s)%s', |
| 65 | + $operation->getPackage()->getName(), |
| 66 | + $operation->getPackage()->getFullPrettyVersion(), |
| 67 | + $url |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + if ($operation instanceof UpdateOperation) { |
| 72 | + return sprintf( |
| 73 | + ' - %s %s (%s => %s)%s', |
| 74 | + ucfirst($entry->getType()), |
| 75 | + $operation->getInitialPackage()->getName(), |
| 76 | + $operation->getInitialPackage()->getFullPrettyVersion(), |
| 77 | + $operation->getTargetPackage()->getFullPrettyVersion(), |
| 78 | + $url |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + if ($operation instanceof UninstallOperation) { |
| 83 | + return sprintf( |
| 84 | + ' - Uninstall %s (%s)%s', |
| 85 | + $operation->getPackage()->getName(), |
| 86 | + $operation->getPackage()->getFullPrettyVersion(), |
| 87 | + $url |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + throw new \InvalidArgumentException('Invalid operation'); |
| 92 | + } |
| 93 | +} |
0 commit comments