-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGitLab.php
More file actions
307 lines (249 loc) · 9.92 KB
/
GitLab.php
File metadata and controls
307 lines (249 loc) · 9.92 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
<?php
namespace Utopia\VCS\Adapter\Git;
use Exception;
use Utopia\Cache\Cache;
use Utopia\VCS\Adapter\Git;
use Utopia\VCS\Exception\RepositoryNotFound;
class GitLab extends Git
{
public const CONTENTS_FILE = 'file';
public const CONTENTS_DIRECTORY = 'dir';
protected string $endpoint = 'http://gitlab:80/api/v4';
protected string $gitlabUrl = 'http://gitlab:80';
protected string $accessToken;
protected Cache $cache;
/**
* @var array<string, string>
*/
protected $headers = ['content-type' => 'application/json'];
public function __construct(Cache $cache)
{
$this->cache = $cache;
}
public function setEndpoint(string $endpoint): void
{
$this->gitlabUrl = rtrim($endpoint, '/');
$this->endpoint = $this->gitlabUrl . '/api/v4';
}
public function getName(): string
{
return 'gitlab';
}
public function initializeVariables(string $installationId, string $privateKey, ?string $appId = null, ?string $accessToken = null, ?string $refreshToken = null): void
{
if (!empty($accessToken)) {
$this->accessToken = $accessToken;
return;
}
throw new Exception("accessToken is required for this adapter.");
}
protected function generateAccessToken(string $privateKey, string $appId): void
{
return;
}
/**
* Create a new group/organization
* Returns "id:path" format so both numeric ID and path are available
*/
public function createOrganization(string $orgName): string
{
$url = "/groups";
$response = $this->call(self::METHOD_POST, $url, ['PRIVATE-TOKEN' => $this->accessToken], [
'name' => $orgName,
'path' => $orgName,
'visibility' => 'public',
]);
$responseBody = $response['body'] ?? [];
$responseHeaders = $response['headers'] ?? [];
$statusCode = $responseHeaders['status-code'] ?? 0;
if ($statusCode >= 400) {
throw new Exception("Creating organization {$orgName} failed with status code {$statusCode}");
}
return ($responseBody['id'] ?? '') . ':' . ($responseBody['path'] ?? '');
}
/**
* Extract owner path from "id:path" format
*/
private function getOwnerPath(string $owner): string
{
if (strstr($owner, ':') !== false) {
return substr($owner, strpos($owner, ':') + 1);
}
return $owner;
}
/**
* Extract namespace ID from "id:path" format
*/
private function getNamespaceId(string $owner): string
{
$pos = strpos($owner, ':');
if ($pos !== false) {
return substr($owner, 0, $pos);
}
return $owner;
}
public function createRepository(string $owner, string $repositoryName, bool $private): array
{
$namespaceId = (int) $this->getNamespaceId($owner);
$url = "/projects";
$response = $this->call(self::METHOD_POST, $url, ['PRIVATE-TOKEN' => $this->accessToken], [
'name' => $repositoryName,
'path' => $repositoryName,
'namespace_id' => $namespaceId,
'visibility' => $private ? 'private' : 'public',
]);
$body = $response['body'] ?? [];
$responseHeaders = $response['headers'] ?? [];
$statusCode = $responseHeaders['status-code'] ?? 0;
if ($statusCode >= 400) {
throw new Exception("Creating repository {$repositoryName} failed with status code {$statusCode}");
}
$result = is_array($body) ? $body : [];
$result['pushed_at'] = $result['last_activity_at'] ?? '';
return $result;
}
public function deleteRepository(string $owner, string $repositoryName): bool
{
$ownerPath = $this->getOwnerPath($owner);
$projectPath = urlencode("{$ownerPath}/{$repositoryName}");
$url = "/projects/{$projectPath}";
$response = $this->call(self::METHOD_DELETE, $url, ['PRIVATE-TOKEN' => $this->accessToken]);
$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new Exception("Deleting repository {$repositoryName} failed with status code {$responseHeadersStatusCode}");
}
return true;
}
public function getRepository(string $owner, string $repositoryName): array
{
$ownerPath = $this->getOwnerPath($owner);
$projectPath = urlencode("{$ownerPath}/{$repositoryName}");
$url = "/projects/{$projectPath}";
$response = $this->call(self::METHOD_GET, $url, ['PRIVATE-TOKEN' => $this->accessToken]);
$responseHeaders = $response['headers'] ?? [];
$responseHeadersStatusCode = $responseHeaders['status-code'] ?? 0;
if ($responseHeadersStatusCode >= 400) {
throw new RepositoryNotFound("Repository not found");
}
$result = $response['body'] ?? [];
$result['pushed_at'] = $result['last_activity_at'] ?? '';
return $result;
}
public function hasAccessToAllRepositories(): bool
{
return true;
}
public function getInstallationRepository(string $repositoryName): array
{
throw new Exception("getInstallationRepository is not applicable for this adapter");
}
public function searchRepositories(string $owner, int $page, int $per_page, string $search = ''): array
{
throw new Exception("Not implemented");
}
public function getRepositoryName(string $repositoryId): string
{
throw new Exception("Not implemented");
}
public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array
{
throw new Exception("Not implemented");
}
public function getRepositoryContent(string $owner, string $repositoryName, string $path, string $ref = ''): array
{
throw new Exception("Not implemented");
}
public function listRepositoryContents(string $owner, string $repositoryName, string $path = '', string $ref = ''): array
{
throw new Exception("Not implemented");
}
public function listRepositoryLanguages(string $owner, string $repositoryName): array
{
throw new Exception("Not implemented");
}
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file', string $branch = ''): array
{
throw new Exception("Not implemented");
}
public function createBranch(string $owner, string $repositoryName, string $newBranchName, string $oldBranchName): array
{
throw new Exception("Not implemented");
}
public function createPullRequest(string $owner, string $repositoryName, string $title, string $head, string $base, string $body = ''): array
{
throw new Exception("Not implemented");
}
public function createWebhook(string $owner, string $repositoryName, string $url, string $secret, array $events = ['push', 'pull_request']): int
{
throw new Exception("Not implemented");
}
public function createComment(string $owner, string $repositoryName, int $pullRequestNumber, string $comment): string
{
throw new Exception("Not implemented");
}
public function getComment(string $owner, string $repositoryName, string $commentId): string
{
throw new Exception("Not implemented");
}
public function updateComment(string $owner, string $repositoryName, int $commentId, string $comment): string
{
throw new Exception("Not implemented");
}
public function getUser(string $username): array
{
throw new Exception("Not implemented");
}
public function getOwnerName(string $installationId, ?int $repositoryId = null): string
{
throw new Exception("Not implemented");
}
public function getPullRequest(string $owner, string $repositoryName, int $pullRequestNumber): array
{
throw new Exception("Not implemented");
}
public function getPullRequestFiles(string $owner, string $repositoryName, int $pullRequestNumber): array
{
throw new Exception("Not implemented");
}
public function getPullRequestFromBranch(string $owner, string $repositoryName, string $branch): array
{
throw new Exception("Not implemented");
}
public function listBranches(string $owner, string $repositoryName): array
{
throw new Exception("Not implemented");
}
public function getCommit(string $owner, string $repositoryName, string $commitHash): array
{
throw new Exception("Not implemented");
}
public function getLatestCommit(string $owner, string $repositoryName, string $branch): array
{
throw new Exception("Not implemented");
}
public function updateCommitStatus(string $repositoryName, string $commitHash, string $owner, string $state, string $description = '', string $target_url = '', string $context = ''): void
{
throw new Exception("Not implemented");
}
public function generateCloneCommand(string $owner, string $repositoryName, string $version, string $versionType, string $directory, string $rootDirectory): string
{
throw new Exception("Not implemented");
}
public function getEvent(string $event, string $payload): array
{
throw new Exception("Not implemented");
}
public function validateWebhookEvent(string $payload, string $signature, string $signatureKey): bool
{
throw new Exception("Not implemented");
}
public function createTag(string $owner, string $repositoryName, string $tagName, string $target, string $message = ''): array
{
throw new Exception("Not implemented");
}
public function getCommitStatuses(string $owner, string $repositoryName, string $commitHash): array
{
throw new Exception("Not implemented");
}
}