forked from utopia-php/vcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitLabTest.php
More file actions
406 lines (329 loc) · 13.9 KB
/
GitLabTest.php
File metadata and controls
406 lines (329 loc) · 13.9 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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
namespace Utopia\Tests\Adapter;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
use Utopia\System\System;
use Utopia\Tests\Base;
use Utopia\VCS\Adapter\Git;
use Utopia\VCS\Adapter\Git\GitLab;
class GitLabTest extends Base
{
protected static string $accessToken = '';
protected static string $owner = '';
protected static string $defaultBranch = 'main';
protected function createVCSAdapter(): Git
{
return new GitLab(new Cache(new None()));
}
public function setUp(): void
{
if (empty(static::$accessToken)) {
$this->setupGitLab();
}
if (empty(static::$accessToken)) {
$this->markTestSkipped('GitLab access token not available');
}
$adapter = new GitLab(new Cache(new None()));
$gitlabUrl = System::getEnv('TESTS_GITLAB_URL', 'http://gitlab:80');
$adapter->initializeVariables(
installationId: '',
privateKey: '',
appId: '',
accessToken: static::$accessToken,
refreshToken: ''
);
$adapter->setEndpoint($gitlabUrl);
if (empty(static::$owner)) {
$orgName = 'test-org-' . \uniqid();
static::$owner = $adapter->createOrganization($orgName);
}
$this->vcsAdapter = $adapter;
}
protected function setupGitLab(): void
{
$tokenFile = '/gitlab-data/token.txt';
if (file_exists($tokenFile)) {
$contents = file_get_contents($tokenFile);
if ($contents !== false) {
static::$accessToken = trim($contents);
}
}
}
public function testCreateRepository(): void
{
$repositoryName = 'test-create-repository-' . \uniqid();
$result = $this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
$this->assertIsArray($result);
$this->assertArrayHasKey('name', $result);
$this->assertSame($repositoryName, $result['name']);
$this->assertFalse($result['visibility'] === 'private');
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testGetRepository(): void
{
$repositoryName = 'test-get-repository-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
$result = $this->vcsAdapter->getRepository(static::$owner, $repositoryName);
$this->assertIsArray($result);
$this->assertSame($repositoryName, $result['name']);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testDeleteRepository(): void
{
$repositoryName = 'test-delete-repository-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
$result = $this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
$this->assertTrue($result);
}
public function testGetDeletedRepositoryFails(): void
{
$repositoryName = 'non-existing-repository-' . \uniqid();
$this->expectException(\Exception::class);
$this->vcsAdapter->getRepository(static::$owner, $repositoryName);
}
public function testGetPullRequestFromBranch(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGetOwnerName(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testSearchRepositories(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testCreateComment(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testUpdateComment(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGenerateCloneCommand(): void
{
$repositoryName = 'test-clone-command-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
$directory = '/tmp/test-clone-' . \uniqid();
try {
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
$command = $this->vcsAdapter->generateCloneCommand(
static::$owner,
$repositoryName,
static::$defaultBranch,
\Utopia\VCS\Adapter\Git::CLONE_TYPE_BRANCH,
$directory,
'/'
);
$this->assertIsString($command);
$this->assertStringContainsString('git init', $command);
$this->assertStringContainsString('git remote add origin', $command);
$this->assertStringContainsString('git config core.sparseCheckout true', $command);
$this->assertStringContainsString($repositoryName, $command);
$output = [];
\exec($command . ' 2>&1', $output, $exitCode);
$this->assertSame(0, $exitCode, implode("\n", $output));
$this->assertFileExists($directory . '/README.md');
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
if (\is_dir($directory)) {
\exec('rm -rf ' . escapeshellarg($directory));
}
}
}
public function testGenerateCloneCommandWithCommitHash(): void
{
$repositoryName = 'test-clone-commit-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
$commitHash = $commit['commitHash'];
$directory = '/tmp/test-clone-commit-' . \uniqid();
$command = $this->vcsAdapter->generateCloneCommand(
static::$owner,
$repositoryName,
$commitHash,
\Utopia\VCS\Adapter\Git::CLONE_TYPE_COMMIT,
$directory,
'/'
);
$this->assertIsString($command);
$this->assertStringContainsString('git fetch --depth=1', $command);
$this->assertStringContainsString($commitHash, $command);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testGenerateCloneCommandWithTag(): void
{
$repositoryName = 'test-clone-tag-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
$commitHash = $commit['commitHash'];
$this->vcsAdapter->createTag(static::$owner, $repositoryName, 'v1.0.0', $commitHash);
$directory = '/tmp/test-clone-tag-' . \uniqid();
$command = $this->vcsAdapter->generateCloneCommand(
static::$owner,
$repositoryName,
'v1.0.0',
\Utopia\VCS\Adapter\Git::CLONE_TYPE_TAG,
$directory,
'/'
);
$this->assertIsString($command);
$this->assertStringContainsString('refs/tags', $command);
$this->assertStringContainsString('v1.0.0', $command);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testGenerateCloneCommandWithInvalidRepository(): void
{
$directory = '/tmp/test-clone-invalid-' . \uniqid();
try {
$command = $this->vcsAdapter->generateCloneCommand(
static::$owner,
'nonexistent-repo-' . \uniqid(),
static::$defaultBranch,
\Utopia\VCS\Adapter\Git::CLONE_TYPE_BRANCH,
$directory,
'/'
);
$output = [];
\exec($command . ' 2>&1', $output, $exitCode);
$cloneFailed = ($exitCode !== 0) || !file_exists($directory . '/README.md');
$this->assertTrue($cloneFailed, 'Clone should have failed for nonexistent repository');
} finally {
if (\is_dir($directory)) {
\exec('rm -rf ' . escapeshellarg($directory));
}
}
}
public function testGetCommit(): void
{
$repositoryName = 'test-get-commit-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
$customMessage = 'Test commit message';
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test', $customMessage);
$latestCommit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
$commitHash = $latestCommit['commitHash'];
$result = $this->vcsAdapter->getCommit(static::$owner, $repositoryName, $commitHash);
$this->assertIsArray($result);
$this->assertArrayHasKey('commitHash', $result);
$this->assertArrayHasKey('commitMessage', $result);
$this->assertArrayHasKey('commitAuthor', $result);
$this->assertArrayHasKey('commitUrl', $result);
$this->assertArrayHasKey('commitAuthorAvatar', $result);
$this->assertArrayHasKey('commitAuthorUrl', $result);
$this->assertSame($commitHash, $result['commitHash']);
$this->assertStringStartsWith($customMessage, $result['commitMessage']);
$this->assertNotEmpty($result['commitUrl']);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testGetLatestCommit(): void
{
$repositoryName = 'test-get-latest-commit-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
$firstMessage = 'First commit';
$secondMessage = 'Second commit';
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test', $firstMessage);
$commit1 = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
$this->assertIsArray($commit1);
$this->assertNotEmpty($commit1['commitHash']);
$this->assertStringStartsWith($firstMessage, $commit1['commitMessage']);
$this->assertNotEmpty($commit1['commitUrl']);
$commit1Hash = $commit1['commitHash'];
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'test.txt', 'test', $secondMessage);
$commit2 = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
$this->assertStringStartsWith($secondMessage, $commit2['commitMessage']);
$this->assertNotSame($commit1Hash, $commit2['commitHash']);
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testGetCommitWithInvalidHash(): void
{
$repositoryName = 'test-get-commit-invalid-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
try {
$this->expectException(\Exception::class);
$this->vcsAdapter->getCommit(static::$owner, $repositoryName, 'invalid-sha-12345');
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testGetLatestCommitWithInvalidBranch(): void
{
$repositoryName = 'test-get-latest-commit-invalid-' . \uniqid();
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
try {
$this->expectException(\Exception::class);
$this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, 'non-existing-branch');
} finally {
$this->vcsAdapter->deleteRepository(static::$owner, $repositoryName);
}
}
public function testWebhookPushEvent(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testWebhookPullRequestEvent(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGetEventPush(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGetRepositoryName(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGetComment(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGetPullRequest(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGetPullRequestFiles(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGetPullRequestWithInvalidNumber(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testGetRepositoryTree(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testListBranches(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testListRepositoryLanguages(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
public function testListRepositoryContents(): void
{
$this->markTestSkipped('Not implemented for GitLab yet');
}
}