forked from GitLabPHP/Client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClient.php
More file actions
355 lines (299 loc) · 8.15 KB
/
Client.php
File metadata and controls
355 lines (299 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
declare(strict_types=1);
/*
* This file is part of the Gitlab API library.
*
* (c) Matt Humphrey <matth@windsor-telecom.co.uk>
* (c) Graham Campbell <hello@gjcampbell.co.uk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Gitlab;
use Gitlab\Api\DeployKeys;
use Gitlab\Api\Deployments;
use Gitlab\Api\Environments;
use Gitlab\Api\Events;
use Gitlab\Api\Groups;
use Gitlab\Api\GroupsBoards;
use Gitlab\Api\GroupsEpics;
use Gitlab\Api\GroupsMilestones;
use Gitlab\Api\IssueBoards;
use Gitlab\Api\IssueLinks;
use Gitlab\Api\Issues;
use Gitlab\Api\IssuesStatistics;
use Gitlab\Api\Jobs;
use Gitlab\Api\Keys;
use Gitlab\Api\MergeRequests;
use Gitlab\Api\Milestones;
use Gitlab\Api\ProjectNamespaces;
use Gitlab\Api\Projects;
use Gitlab\Api\Registry;
use Gitlab\Api\Repositories;
use Gitlab\Api\RepositoryFiles;
use Gitlab\Api\ResourceIterationEvents;
use Gitlab\Api\ResourceLabelEvents;
use Gitlab\Api\ResourceMilestoneEvents;
use Gitlab\Api\ResourceStateEvents;
use Gitlab\Api\ResourceWeightEvents;
use Gitlab\Api\Schedules;
use Gitlab\Api\Search;
use Gitlab\Api\Snippets;
use Gitlab\Api\SystemHooks;
use Gitlab\Api\Tags;
use Gitlab\Api\Users;
use Gitlab\Api\Version;
use Gitlab\Api\Wiki;
use Gitlab\HttpClient\Builder;
use Gitlab\HttpClient\Plugin\Authentication;
use Gitlab\HttpClient\Plugin\ExceptionThrower;
use Gitlab\HttpClient\Plugin\History;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin\AddHostPlugin;
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
use Http\Client\Common\Plugin\HistoryPlugin;
use Http\Client\Common\Plugin\RedirectPlugin;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
/**
* Simple API wrapper for Gitlab.
*
* @author Matt Humphrey <matt@m4tt.co>
*/
class Client
{
/**
* The private token authentication method.
*
* @var string
*/
public const AUTH_HTTP_TOKEN = 'http_token';
/**
* The job token authentication method.
*
* @var string
*/
public const AUTH_HTTP_JOB_TOKEN = 'http_job_token';
/**
* The OAuth 2 token authentication method.
*
* @var string
*/
public const AUTH_OAUTH_TOKEN = 'oauth_token';
/**
* The default base URL.
*
* @var string
*/
private const BASE_URL = 'https://gitlab.com';
/**
* The default user agent header.
*
* @var string
*/
private const USER_AGENT = 'gitlab-php-api-client/12.0';
private readonly Builder $httpClientBuilder;
private readonly History $responseHistory;
public function __construct(?Builder $httpClientBuilder = null)
{
$this->httpClientBuilder = $builder = $httpClientBuilder ?? new Builder();
$this->responseHistory = new History();
$builder->addPlugin(new ExceptionThrower());
$builder->addPlugin(new HistoryPlugin($this->responseHistory));
$builder->addPlugin(new HeaderDefaultsPlugin([
'User-Agent' => self::USER_AGENT,
]));
$builder->addPlugin(new RedirectPlugin());
$this->setUrl(self::BASE_URL);
}
/**
* Create a Gitlab\Client using an HTTP client.
*/
public static function createWithHttpClient(ClientInterface $httpClient): self
{
$builder = new Builder($httpClient);
return new self($builder);
}
public function deployKeys(): DeployKeys
{
return new DeployKeys($this);
}
public function deployments(): Deployments
{
return new Deployments($this);
}
public function environments(): Environments
{
return new Environments($this);
}
public function events(): Events
{
return new Events($this);
}
public function groups(): Groups
{
return new Groups($this);
}
public function groupsBoards(): GroupsBoards
{
return new GroupsBoards($this);
}
public function groupsEpics(): GroupsEpics
{
return new GroupsEpics($this);
}
public function groupsMilestones(): GroupsMilestones
{
return new GroupsMilestones($this);
}
public function issueBoards(): IssueBoards
{
return new IssueBoards($this);
}
public function issueLinks(): IssueLinks
{
return new IssueLinks($this);
}
public function issues(): Issues
{
return new Issues($this);
}
public function resourceIterationEvents(): ResourceIterationEvents
{
return new ResourceIterationEvents($this);
}
public function resourceLabelEvents(): ResourceLabelEvents
{
return new ResourceLabelEvents($this);
}
public function resourceMilestoneEvents(): ResourceMilestoneEvents
{
return new ResourceMilestoneEvents($this);
}
public function resourceStateEvents(): ResourceStateEvents
{
return new ResourceStateEvents($this);
}
public function resourceWeightEvents(): ResourceWeightEvents
{
return new ResourceWeightEvents($this);
}
public function issuesStatistics(): IssuesStatistics
{
return new IssuesStatistics($this);
}
public function jobs(): Jobs
{
return new Jobs($this);
}
public function keys(): Keys
{
return new Keys($this);
}
public function mergeRequests(): MergeRequests
{
return new MergeRequests($this);
}
public function milestones(): Milestones
{
return new Milestones($this);
}
public function namespaces(): ProjectNamespaces
{
return new ProjectNamespaces($this);
}
public function projects(): Projects
{
return new Projects($this);
}
public function registry(): Registry
{
return new Registry($this);
}
public function repositories(): Repositories
{
return new Repositories($this);
}
public function repositoryFiles(): RepositoryFiles
{
return new RepositoryFiles($this);
}
public function search(): Search
{
return new Search($this);
}
public function schedules(): Schedules
{
return new Schedules($this);
}
public function snippets(): Snippets
{
return new Snippets($this);
}
public function systemHooks(): SystemHooks
{
return new SystemHooks($this);
}
public function tags(): Tags
{
return new Tags($this);
}
public function users(): Users
{
return new Users($this);
}
public function version(): Version
{
return new Version($this);
}
public function wiki(): Wiki
{
return new Wiki($this);
}
/**
* Authenticate a user for all next requests.
*
* @param string $token Gitlab private token
* @param string $authMethod One of the AUTH_* class constants
*/
public function authenticate(string $token, string $authMethod, ?string $sudo = null): void
{
$this->getHttpClientBuilder()->removePlugin(Authentication::class);
$this->getHttpClientBuilder()->addPlugin(new Authentication($authMethod, $token, $sudo));
}
public function setUrl(string $url): void
{
$uri = $this->getHttpClientBuilder()->getUriFactory()->createUri($url);
$this->getHttpClientBuilder()->removePlugin(AddHostPlugin::class);
$this->getHttpClientBuilder()->addPlugin(new AddHostPlugin($uri));
}
/**
* Get the last response.
*/
public function getLastResponse(): ?ResponseInterface
{
return $this->responseHistory->getLastResponse();
}
/**
* Get the HTTP client.
*/
public function getHttpClient(): HttpMethodsClientInterface
{
return $this->getHttpClientBuilder()->getHttpClient();
}
/**
* Get the stream factory.
*/
public function getStreamFactory(): StreamFactoryInterface
{
return $this->getHttpClientBuilder()->getStreamFactory();
}
/**
* Get the HTTP client builder.
*/
protected function getHttpClientBuilder(): Builder
{
return $this->httpClientBuilder;
}
}