|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\Project; |
|
6
|
|
|
use GuzzleHttp\ClientInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\StreamInterface; |
|
9
|
|
|
use ReflectionClass; |
|
10
|
|
|
use Ronanchilvers\Utility\Str; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Base provider class |
|
15
|
|
|
* |
|
16
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractProvider |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ClientInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $client; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $token; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $typesHandled = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $headUrl = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $commitUrl = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $downloadUrl = null; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $configUrl = null; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $repoUrl = null; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $branchUrl = null; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $shaUrl = null; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Class constructor |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $token |
|
74
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct(ClientInterface $client, string $token) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->client = $client; |
|
79
|
|
|
$this->token = $token; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @see \App\Provider\ProviderInterface::handles() |
|
84
|
|
|
*/ |
|
85
|
|
|
public function handles(Project $project) |
|
86
|
|
|
{ |
|
87
|
|
|
return in_array( |
|
88
|
|
|
$project->provider, |
|
89
|
|
|
$this->typesHandled |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getLabel() |
|
97
|
|
|
{ |
|
98
|
|
|
$reflection = new ReflectionClass($this); |
|
99
|
|
|
|
|
100
|
|
|
return strtolower($reflection->getShortName()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get a repository link for a given repository |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $repository |
|
107
|
|
|
* @return string |
|
108
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getRepositoryLink(string $repository) |
|
111
|
|
|
{ |
|
112
|
|
|
$params = [ |
|
113
|
|
|
'repository' => $repository, |
|
114
|
|
|
]; |
|
115
|
|
|
|
|
116
|
|
|
return Str::moustaches( |
|
117
|
|
|
$this->repoUrl, |
|
118
|
|
|
$params |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get a link to a repository branch |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $repository |
|
126
|
|
|
* @param string $branch |
|
127
|
|
|
* @return string |
|
128
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getBranchLink(string $repository, string $branch) |
|
131
|
|
|
{ |
|
132
|
|
|
$params = [ |
|
133
|
|
|
'repository' => $repository, |
|
134
|
|
|
'branch' => $branch, |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
|
|
return Str::moustaches( |
|
138
|
|
|
$this->branchUrl, |
|
139
|
|
|
$params |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get a link for a given repository and sha |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $repository |
|
147
|
|
|
* @param string $sha |
|
148
|
|
|
* @return string |
|
149
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getShaLink(string $repository, string $sha) |
|
152
|
|
|
{ |
|
153
|
|
|
$params = [ |
|
154
|
|
|
'repository' => $repository, |
|
155
|
|
|
'sha' => $sha, |
|
156
|
|
|
]; |
|
157
|
|
|
|
|
158
|
|
|
return Str::moustaches( |
|
159
|
|
|
$this->shaUrl, |
|
160
|
|
|
$params |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get a curl handle |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $url |
|
168
|
|
|
* @return resource |
|
169
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getCurlHandle($url) |
|
172
|
|
|
{ |
|
173
|
|
|
if (!$curl = curl_init($url)) { |
|
174
|
|
|
throw new RuntimeException('Unable to initialise CURL Github API request'); |
|
175
|
|
|
} |
|
176
|
|
|
curl_setopt_array($curl, [ |
|
177
|
|
|
CURLOPT_USERAGENT => 'ronanchilvers/deploy - curl ' . curl_version()['version'], |
|
178
|
|
|
CURLOPT_FOLLOWLOCATION => false, |
|
179
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
180
|
|
|
CURLOPT_TIMEOUT => 5, |
|
181
|
|
|
]); |
|
182
|
|
|
|
|
183
|
|
|
return $curl; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Send a GET request to a URL and get back a JSON array |
|
188
|
|
|
* |
|
189
|
|
|
* @return array |
|
190
|
|
|
* @throws RuntimeException |
|
191
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function getJSON($url, array $options = []): array |
|
194
|
|
|
{ |
|
195
|
|
|
$response = $this->get($url, $options); |
|
196
|
|
|
$body = $response->getBody(); |
|
197
|
|
|
if (!$body instanceof StreamInterface) { |
|
|
|
|
|
|
198
|
|
|
throw new RuntimeException($this->getLabel() . ' : Unable to read response body'); |
|
199
|
|
|
} |
|
200
|
|
|
if (!$data = json_decode($body->getContents(), true)) { |
|
201
|
|
|
throw new RuntimeException($this->getLabel() . ' : Invalid JSON response for HEAD information request'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $data; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Send a GET request to a URL |
|
209
|
|
|
* |
|
210
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
211
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function get($url, array $options = []): ResponseInterface |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->client()->request( |
|
216
|
|
|
'GET', |
|
217
|
|
|
$url, |
|
218
|
|
|
$options |
|
219
|
|
|
); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Get the HTTP client object |
|
224
|
|
|
* |
|
225
|
|
|
* @return \GuzzleHttp\ClientInterface |
|
226
|
|
|
* @author Ronan Chilvers <[email protected]> |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function client(): ClientInterface |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->client; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|