Skip to content

Commit 8f35feb

Browse files
committed
Split command into several functions
1 parent 16940a9 commit 8f35feb

File tree

1 file changed

+79
-12
lines changed

1 file changed

+79
-12
lines changed

src/Command/GithubReleaseCommand.php

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33
namespace SymfonyDocsBuilder\Command;
44

55
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Exception\RuntimeException;
67
use Symfony\Component\Console\Input\InputArgument;
78
use Symfony\Component\Console\Input\InputInterface;
89
use Symfony\Component\Console\Output\OutputInterface;
910
use Symfony\Component\Console\Style\SymfonyStyle;
1011
use Symfony\Component\HttpClient\Exception\ClientException;
1112
use Symfony\Component\HttpClient\HttpClient;
13+
use Symfony\Component\Process\Exception\ProcessFailedException;
1214
use Symfony\Component\Process\Process;
15+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1316

1417
class GithubReleaseCommand extends Command
1518
{
1619
protected static $defaultName = 'github:release';
1720

1821
/** @var SymfonyStyle */
1922
private $io;
23+
/** @var HttpClientInterface */
24+
private $client;
25+
26+
private $tag;
27+
private $name;
28+
private $description;
2029

2130
public function __construct()
2231
{
@@ -28,21 +37,44 @@ protected function configure()
2837
$this->setDescription('Create a release on github with a .phar as attachment.');
2938
// todo valid with regex
3039
$this->addArgument('tag', InputArgument::REQUIRED, 'Release\'s tag.');
31-
$this->addArgument('name', InputArgument::REQUIRED, 'Release name');
32-
$this->addArgument('description', InputArgument::REQUIRED, 'Release description');
40+
$this->addArgument('name', InputArgument::OPTIONAL, 'Release name', 'Symfony docs builder %s');
41+
$this->addArgument('description', InputArgument::OPTIONAL, 'Symfony docs builder %s');
3342
}
3443

3544
protected function initialize(InputInterface $input, OutputInterface $output)
3645
{
3746
$this->io = new SymfonyStyle($input, $output);
47+
$this->client = $this->createHttpClient();
48+
49+
$tag = $input->getArgument('tag');
50+
if (!preg_match('/v\d+\.\d+\.\d+/', $tag)) {
51+
throw new RuntimeException(sprintf('"%s" is not a valid tag.', $tag));
52+
}
53+
54+
$this->tag = $tag;
55+
$this->name = sprintf($input->getArgument('name'), $tag);
56+
$this->description = sprintf($input->getArgument('description'), $tag);
3857
}
3958

4059
protected function execute(InputInterface $input, OutputInterface $output)
60+
{
61+
$this->compilePhar();
62+
63+
$this->addAssetToRelease($this->createRelease());
64+
}
65+
66+
/**
67+
* @throws ProcessFailedException
68+
*/
69+
private function compilePhar(): void
4170
{
4271
$process = Process::fromShellCommandline('./bin/compile', __DIR__.'/../..');
4372
$process->mustRun();
73+
}
4474

45-
// TODO token form .env
75+
private function createHttpClient(): HttpClientInterface
76+
{
77+
// TODO token form .env
4678
$client = HttpClient::create(
4779
[
4880
'headers' => [
@@ -51,25 +83,60 @@ protected function execute(InputInterface $input, OutputInterface $output)
5183
]
5284
);
5385

86+
return $client;
87+
}
88+
89+
private function createRelease(): int
90+
{
5491
try {
55-
$client->request(
92+
$response = $this->client->request(
5693
'POST',
94+
// todo change repo
5795
'https://api.github.com/repos/nikophil/test/releases',
5896
[
5997
'json' => [
60-
'tag_name' => $input->getArgument('tag'),
98+
'tag_name' => $this->tag,
6199
'target_commitish' => 'master',
62-
'name' => $input->getArgument('name'),
63-
'body' => $input->getArgument('description'),
64-
'draft' => false,
65-
'prerelease' => false
66-
]
100+
'name' => $this->name,
101+
'description' => $this->description,
102+
'draft' => false,
103+
'prerelease' => false,
104+
],
105+
]
106+
);
107+
108+
return (int) $response->toArray()['id'];
109+
} catch (ClientException $exception) {
110+
throw new RuntimeException('Error while trying to create release. Maybe the tag name already exists?', 0, $exception);
111+
}
112+
}
113+
114+
private function addAssetToRelease(int $releaseId): void
115+
{
116+
try {
117+
$this->client->request(
118+
'POST',
119+
sprintf('https://uploads.github.com/repos/nikophil/test/releases/%s/assets?name=docs.phar', $releaseId),
120+
[
121+
'headers' => ['Content-Type' => 'application/octet-stream'],
122+
'body' => file_get_contents(__DIR__.'/../../docs.phar'),
67123
]
68124
);
69125
} catch (ClientException $exception) {
70-
$this->io->error('Error while trying to create release. Maybe the tag name already exists?');
126+
$this->deleteRelease($releaseId);
127+
throw new RuntimeException('Error while adding asset to release. Maybe the tag name already exists?', 0, $exception);
128+
}
129+
}
71130

72-
return;
131+
private function deleteRelease(int $releaseId): void
132+
{
133+
try {
134+
$this->client->request(
135+
'DELETE',
136+
sprintf('https://api.github.com/repos/nikophil/test/releases/%s', $releaseId)
137+
);
138+
} catch (ClientException $exception) {
139+
throw new RuntimeException('Error while deleting release.', 0, $exception);
73140
}
74141
}
75142
}

0 commit comments

Comments
 (0)