Skip to content

Commit 8293e7a

Browse files
committed
Load token from .env
1 parent 8f35feb commit 8293e7a

File tree

5 files changed

+97
-17
lines changed

5 files changed

+97
-17
lines changed

.env.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GITHUB_API_TOKEN=

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/tests/_cache
44
/var/
55
/docs.phar
6+
/.env

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"symfony/css-selector": "^4.1",
2121
"symfony/console": "^4.1",
2222
"twig/twig": "^2.7.3",
23-
"symfony/http-client": "^4.3"
23+
"symfony/http-client": "^4.3",
24+
"symfony/dotenv": "^4.3"
2425
},
2526
"require-dev": {
2627
"gajus/dindent": "^2.0",

composer.lock

Lines changed: 58 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Command/GithubReleaseCommand.php

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use Symfony\Component\Console\Style\SymfonyStyle;
11+
use Symfony\Component\Dotenv\Dotenv;
1112
use Symfony\Component\HttpClient\Exception\ClientException;
1213
use Symfony\Component\HttpClient\HttpClient;
1314
use Symfony\Component\Process\Exception\ProcessFailedException;
@@ -16,6 +17,10 @@
1617

1718
class GithubReleaseCommand extends Command
1819
{
20+
// todo change repo
21+
private const GITHUB_USER = 'nikophil';
22+
private const GITHUB_REPO = 'test';
23+
1924
protected static $defaultName = 'github:release';
2025

2126
/** @var SymfonyStyle */
@@ -35,24 +40,30 @@ public function __construct()
3540
protected function configure()
3641
{
3742
$this->setDescription('Create a release on github with a .phar as attachment.');
38-
// todo valid with regex
3943
$this->addArgument('tag', InputArgument::REQUIRED, 'Release\'s tag.');
4044
$this->addArgument('name', InputArgument::OPTIONAL, 'Release name', 'Symfony docs builder %s');
4145
$this->addArgument('description', InputArgument::OPTIONAL, 'Symfony docs builder %s');
4246
}
4347

4448
protected function initialize(InputInterface $input, OutputInterface $output)
4549
{
46-
$this->io = new SymfonyStyle($input, $output);
47-
$this->client = $this->createHttpClient();
50+
$dotenv = new Dotenv();
51+
$dotenv->load(__DIR__.'/../../.env');
52+
53+
if (empty($_ENV['GITHUB_API_TOKEN'])) {
54+
throw new RuntimeException('Please fill "GITHUB_API_TOKEN" in file "[PROJECT_DIR]/.env"');
55+
}
56+
57+
$this->io = new SymfonyStyle($input, $output);
58+
$this->client = $this->createHttpClient($_ENV['GITHUB_API_TOKEN']);
4859

4960
$tag = $input->getArgument('tag');
50-
if (!preg_match('/v\d+\.\d+\.\d+/', $tag)) {
61+
if (!preg_match('/^v\d+\.\d+\.\d+$/', $tag)) {
5162
throw new RuntimeException(sprintf('"%s" is not a valid tag.', $tag));
5263
}
5364

54-
$this->tag = $tag;
55-
$this->name = sprintf($input->getArgument('name'), $tag);
65+
$this->tag = $tag;
66+
$this->name = sprintf($input->getArgument('name'), $tag);
5667
$this->description = sprintf($input->getArgument('description'), $tag);
5768
}
5869

@@ -72,13 +83,12 @@ private function compilePhar(): void
7283
$process->mustRun();
7384
}
7485

75-
private function createHttpClient(): HttpClientInterface
86+
private function createHttpClient(string $githubToken): HttpClientInterface
7687
{
77-
// TODO token form .env
7888
$client = HttpClient::create(
7989
[
8090
'headers' => [
81-
'Authorization' => 'token 52a83ae437c06017d72fc9461392f02b39dc8c0f',
91+
'Authorization' => sprintf('token %s', $githubToken),
8292
],
8393
]
8494
);
@@ -91,8 +101,7 @@ private function createRelease(): int
91101
try {
92102
$response = $this->client->request(
93103
'POST',
94-
// todo change repo
95-
'https://api.github.com/repos/nikophil/test/releases',
104+
sprintf('https://api.github.com/repos/%s/%s/releases', self::GITHUB_USER, self::GITHUB_REPO),
96105
[
97106
'json' => [
98107
'tag_name' => $this->tag,
@@ -107,7 +116,13 @@ private function createRelease(): int
107116

108117
return (int) $response->toArray()['id'];
109118
} catch (ClientException $exception) {
110-
throw new RuntimeException('Error while trying to create release. Maybe the tag name already exists?', 0, $exception);
119+
if (401 === $exception->getCode()) {
120+
$message = 'Invalid token';
121+
} else {
122+
$message = 'Maybe the tag name already exists?';
123+
}
124+
125+
throw new RuntimeException(sprintf('Error while trying to create release: %s.', $message), 0, $exception);
111126
}
112127
}
113128

@@ -116,15 +131,20 @@ private function addAssetToRelease(int $releaseId): void
116131
try {
117132
$this->client->request(
118133
'POST',
119-
sprintf('https://uploads.github.com/repos/nikophil/test/releases/%s/assets?name=docs.phar', $releaseId),
134+
sprintf(
135+
'https://uploads.github.com/repos/%s/%s/releases/%s/assets?name=docs.phar',
136+
self::GITHUB_USER,
137+
self::GITHUB_REPO,
138+
$releaseId
139+
),
120140
[
121141
'headers' => ['Content-Type' => 'application/octet-stream'],
122142
'body' => file_get_contents(__DIR__.'/../../docs.phar'),
123143
]
124144
);
125145
} catch (ClientException $exception) {
126146
$this->deleteRelease($releaseId);
127-
throw new RuntimeException('Error while adding asset to release. Maybe the tag name already exists?', 0, $exception);
147+
throw new RuntimeException('Error while adding asset to release.', 0, $exception);
128148
}
129149
}
130150

@@ -133,7 +153,7 @@ private function deleteRelease(int $releaseId): void
133153
try {
134154
$this->client->request(
135155
'DELETE',
136-
sprintf('https://api.github.com/repos/nikophil/test/releases/%s', $releaseId)
156+
sprintf('https://api.github.com/repos/%s/%s/releases/%s', self::GITHUB_USER, self::GITHUB_REPO, $releaseId)
137157
);
138158
} catch (ClientException $exception) {
139159
throw new RuntimeException('Error while deleting release.', 0, $exception);

0 commit comments

Comments
 (0)