|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace SymfonyDocsBuilder\Command; |
| 4 | + |
| 5 | +use Symfony\Component\Console\Command\Command; |
| 6 | +use Symfony\Component\Console\Input\InputArgument; |
| 7 | +use Symfony\Component\Console\Input\InputInterface; |
| 8 | +use Symfony\Component\Console\Output\OutputInterface; |
| 9 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 10 | +use Symfony\Component\HttpClient\Exception\ClientException; |
| 11 | +use Symfony\Component\HttpClient\HttpClient; |
| 12 | +use Symfony\Component\Process\Process; |
| 13 | + |
| 14 | +class GithubReleaseCommand extends Command |
| 15 | +{ |
| 16 | + protected static $defaultName = 'github:release'; |
| 17 | + |
| 18 | + /** @var SymfonyStyle */ |
| 19 | + private $io; |
| 20 | + |
| 21 | + public function __construct() |
| 22 | + { |
| 23 | + parent::__construct(self::$defaultName); |
| 24 | + } |
| 25 | + |
| 26 | + protected function configure() |
| 27 | + { |
| 28 | + $this->setDescription('Create a release on github with a .phar as attachment.'); |
| 29 | + // todo valid with regex |
| 30 | + $this->addArgument('tag', InputArgument::REQUIRED, 'Release\'s tag.'); |
| 31 | + $this->addArgument('name', InputArgument::REQUIRED, 'Release name'); |
| 32 | + $this->addArgument('description', InputArgument::REQUIRED, 'Release description'); |
| 33 | + } |
| 34 | + |
| 35 | + protected function initialize(InputInterface $input, OutputInterface $output) |
| 36 | + { |
| 37 | + $this->io = new SymfonyStyle($input, $output); |
| 38 | + } |
| 39 | + |
| 40 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 41 | + { |
| 42 | + $process = Process::fromShellCommandline('./bin/compile', __DIR__.'/../..'); |
| 43 | + $process->mustRun(); |
| 44 | + |
| 45 | + // TODO token form .env |
| 46 | + $client = HttpClient::create( |
| 47 | + [ |
| 48 | + 'headers' => [ |
| 49 | + 'Authorization' => 'token 52a83ae437c06017d72fc9461392f02b39dc8c0f', |
| 50 | + ], |
| 51 | + ] |
| 52 | + ); |
| 53 | + |
| 54 | + try { |
| 55 | + $client->request( |
| 56 | + 'POST', |
| 57 | + 'https://api.github.com/repos/nikophil/test/releases', |
| 58 | + [ |
| 59 | + 'json' => [ |
| 60 | + 'tag_name' => $input->getArgument('tag'), |
| 61 | + 'target_commitish' => 'master', |
| 62 | + 'name' => $input->getArgument('name'), |
| 63 | + 'body' => $input->getArgument('description'), |
| 64 | + 'draft' => false, |
| 65 | + 'prerelease' => false |
| 66 | + ] |
| 67 | + ] |
| 68 | + ); |
| 69 | + } catch (ClientException $exception) { |
| 70 | + $this->io->error('Error while trying to create release. Maybe the tag name already exists?'); |
| 71 | + |
| 72 | + return; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments