8
8
use Symfony \Component \Console \Input \InputInterface ;
9
9
use Symfony \Component \Console \Output \OutputInterface ;
10
10
use Symfony \Component \Console \Style \SymfonyStyle ;
11
+ use Symfony \Component \Dotenv \Dotenv ;
11
12
use Symfony \Component \HttpClient \Exception \ClientException ;
12
13
use Symfony \Component \HttpClient \HttpClient ;
13
14
use Symfony \Component \Process \Exception \ProcessFailedException ;
16
17
17
18
class GithubReleaseCommand extends Command
18
19
{
20
+ // todo change repo
21
+ private const GITHUB_USER = 'nikophil ' ;
22
+ private const GITHUB_REPO = 'test ' ;
23
+
19
24
protected static $ defaultName = 'github:release ' ;
20
25
21
26
/** @var SymfonyStyle */
@@ -35,24 +40,30 @@ public function __construct()
35
40
protected function configure ()
36
41
{
37
42
$ this ->setDescription ('Create a release on github with a .phar as attachment. ' );
38
- // todo valid with regex
39
43
$ this ->addArgument ('tag ' , InputArgument::REQUIRED , 'Release \'s tag. ' );
40
44
$ this ->addArgument ('name ' , InputArgument::OPTIONAL , 'Release name ' , 'Symfony docs builder %s ' );
41
45
$ this ->addArgument ('description ' , InputArgument::OPTIONAL , 'Symfony docs builder %s ' );
42
46
}
43
47
44
48
protected function initialize (InputInterface $ input , OutputInterface $ output )
45
49
{
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 ' ]);
48
59
49
60
$ tag = $ input ->getArgument ('tag ' );
50
- if (!preg_match ('/v\d+\.\d+\.\d+/ ' , $ tag )) {
61
+ if (!preg_match ('/^ v\d+\.\d+\.\d+$ / ' , $ tag )) {
51
62
throw new RuntimeException (sprintf ('"%s" is not a valid tag. ' , $ tag ));
52
63
}
53
64
54
- $ this ->tag = $ tag ;
55
- $ this ->name = sprintf ($ input ->getArgument ('name ' ), $ tag );
65
+ $ this ->tag = $ tag ;
66
+ $ this ->name = sprintf ($ input ->getArgument ('name ' ), $ tag );
56
67
$ this ->description = sprintf ($ input ->getArgument ('description ' ), $ tag );
57
68
}
58
69
@@ -72,13 +83,12 @@ private function compilePhar(): void
72
83
$ process ->mustRun ();
73
84
}
74
85
75
- private function createHttpClient (): HttpClientInterface
86
+ private function createHttpClient (string $ githubToken ): HttpClientInterface
76
87
{
77
- // TODO token form .env
78
88
$ client = HttpClient::create (
79
89
[
80
90
'headers ' => [
81
- 'Authorization ' => 'token 52a83ae437c06017d72fc9461392f02b39dc8c0f ' ,
91
+ 'Authorization ' => sprintf ( 'token %s ' , $ githubToken ) ,
82
92
],
83
93
]
84
94
);
@@ -91,8 +101,7 @@ private function createRelease(): int
91
101
try {
92
102
$ response = $ this ->client ->request (
93
103
'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 ),
96
105
[
97
106
'json ' => [
98
107
'tag_name ' => $ this ->tag ,
@@ -107,7 +116,13 @@ private function createRelease(): int
107
116
108
117
return (int ) $ response ->toArray ()['id ' ];
109
118
} 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 );
111
126
}
112
127
}
113
128
@@ -116,15 +131,20 @@ private function addAssetToRelease(int $releaseId): void
116
131
try {
117
132
$ this ->client ->request (
118
133
'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
+ ),
120
140
[
121
141
'headers ' => ['Content-Type ' => 'application/octet-stream ' ],
122
142
'body ' => file_get_contents (__DIR__ .'/../../docs.phar ' ),
123
143
]
124
144
);
125
145
} catch (ClientException $ exception ) {
126
146
$ 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 );
128
148
}
129
149
}
130
150
@@ -133,7 +153,7 @@ private function deleteRelease(int $releaseId): void
133
153
try {
134
154
$ this ->client ->request (
135
155
'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 )
137
157
);
138
158
} catch (ClientException $ exception ) {
139
159
throw new RuntimeException ('Error while deleting release. ' , 0 , $ exception );
0 commit comments