Skip to content

Commit ae31b34

Browse files
committed
Improve compatibility for future
1 parent 35ded61 commit ae31b34

3 files changed

Lines changed: 44 additions & 43 deletions

File tree

src/VCS/Adapter/Git/Gitea.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function initializeVariables(string $installationId, string $privateKey,
7171
return;
7272
}
7373

74-
throw new Exception("accessToken is required for Gitea adapter.");
74+
throw new Exception("accessToken is required for this adapter.");
7575
}
7676

7777
/**
@@ -608,7 +608,7 @@ public function getUser(string $username): array
608608
public function getOwnerName(string $installationId, ?int $repositoryId = null): string
609609
{
610610
if ($repositoryId === null || $repositoryId <= 0) {
611-
throw new Exception("repositoryId is required for Gitea");
611+
throw new Exception("repositoryId is required for this adapter");
612612
}
613613

614614
$url = "/repositories/{$repositoryId}";

tests/VCS/Adapter/GiteaTest.php

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class GiteaTest extends Base
1313
{
1414
protected static string $accessToken = '';
1515
protected static string $owner = '';
16+
protected static string $defaultBranch = 'main';
1617

1718
protected function createVCSAdapter(): Git
1819
{
@@ -26,7 +27,7 @@ public function setUp(): void
2627
}
2728

2829
$adapter = new Gitea(new Cache(new None()));
29-
$giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000') ?? '';
30+
$giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000');
3031

3132
$adapter->initializeVariables(
3233
installationId: '',
@@ -106,15 +107,15 @@ public function testCommentWorkflow(): void
106107

107108
try {
108109
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
109-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'comment-test', 'main');
110+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'comment-test', static::$defaultBranch);
110111
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'test.txt', 'test', 'Add test file', 'comment-test');
111112

112113
$pr = $this->vcsAdapter->createPullRequest(
113114
static::$owner,
114115
$repositoryName,
115116
'Comment Test PR',
116117
'comment-test',
117-
'main'
118+
static::$defaultBranch
118119
);
119120

120121
$prNumber = $pr['number'] ?? 0;
@@ -147,7 +148,7 @@ public function testGetComment(): void
147148
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
148149

149150
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
150-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'test-branch', 'main');
151+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'test-branch', static::$defaultBranch);
151152
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'test.txt', 'test', 'Add test', 'test-branch');
152153

153154
// Create PR
@@ -156,7 +157,7 @@ public function testGetComment(): void
156157
$repositoryName,
157158
'Test PR',
158159
'test-branch',
159-
'main'
160+
static::$defaultBranch
160161
);
161162

162163
$prNumber = $pr['number'] ?? 0;
@@ -212,7 +213,7 @@ public function testGetRepositoryTreeWithSlashInBranchName(): void
212213
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
213214

214215
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
215-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature/test-branch', 'main');
216+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature/test-branch', static::$defaultBranch);
216217

217218
$tree = $this->vcsAdapter->getRepositoryTree(static::$owner, $repositoryName, 'feature/test-branch');
218219

@@ -296,15 +297,15 @@ public function testGetRepositoryTree(): void
296297
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'src/lib.php', '<?php // library');
297298

298299
// Test non-recursive (should only show root level)
299-
$tree = $this->vcsAdapter->getRepositoryTree(static::$owner, $repositoryName, 'main', false);
300+
$tree = $this->vcsAdapter->getRepositoryTree(static::$owner, $repositoryName, static::$defaultBranch, false);
300301

301302
$this->assertIsArray($tree);
302303
$this->assertContains('README.md', $tree);
303304
$this->assertContains('src', $tree);
304305
$this->assertCount(2, $tree); // Only README.md and src folder at root
305306

306307
// Test recursive (should show all files including nested)
307-
$treeRecursive = $this->vcsAdapter->getRepositoryTree(static::$owner, $repositoryName, 'main', true);
308+
$treeRecursive = $this->vcsAdapter->getRepositoryTree(static::$owner, $repositoryName, static::$defaultBranch, true);
308309

309310
$this->assertIsArray($treeRecursive);
310311
$this->assertContains('README.md', $treeRecursive);
@@ -358,7 +359,7 @@ public function testGetRepositoryContentWithRef(): void
358359

359360
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'test.txt', 'main branch content');
360361

361-
$result = $this->vcsAdapter->getRepositoryContent(static::$owner, $repositoryName, 'test.txt', 'main');
362+
$result = $this->vcsAdapter->getRepositoryContent(static::$owner, $repositoryName, 'test.txt', static::$defaultBranch);
362363

363364
$this->assertIsArray($result);
364365
$this->assertSame('main branch content', $result['content']);
@@ -447,15 +448,15 @@ public function testGetPullRequest(): void
447448
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
448449

449450
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
450-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-branch', 'main');
451+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-branch', static::$defaultBranch);
451452
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'feature.txt', 'feature content', 'Add feature', 'feature-branch');
452453

453454
$pr = $this->vcsAdapter->createPullRequest(
454455
static::$owner,
455456
$repositoryName,
456457
'Test PR',
457458
'feature-branch',
458-
'main',
459+
static::$defaultBranch,
459460
'Test PR description'
460461
);
461462

@@ -505,7 +506,7 @@ public function testGenerateCloneCommand(): void
505506
$command = $this->vcsAdapter->generateCloneCommand(
506507
static::$owner,
507508
$repositoryName,
508-
'main',
509+
static::$defaultBranch,
509510
\Utopia\VCS\Adapter\Git::CLONE_TYPE_BRANCH,
510511
'/tmp/test-clone-' . \uniqid(),
511512
'/'
@@ -529,7 +530,7 @@ public function testGenerateCloneCommandWithCommitHash(): void
529530
try {
530531
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
531532

532-
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, 'main');
533+
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
533534
$commitHash = $commit['commitHash'];
534535

535536
$command = $this->vcsAdapter->generateCloneCommand(
@@ -557,7 +558,7 @@ public function testGenerateCloneCommandWithTag(): void
557558
// Create initial file and get commit hash
558559
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test Tag');
559560

560-
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, 'main');
561+
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
561562
$commitHash = $commit['commitHash'];
562563

563564
// Create a tag
@@ -593,7 +594,7 @@ public function testGenerateCloneCommandWithInvalidRepository(): void
593594
$command = $this->vcsAdapter->generateCloneCommand(
594595
'nonexistent-owner-' . \uniqid(),
595596
'nonexistent-repo-' . \uniqid(),
596-
'main',
597+
static::$defaultBranch,
597598
\Utopia\VCS\Adapter\Git::CLONE_TYPE_BRANCH,
598599
$directory,
599600
'/'
@@ -622,7 +623,7 @@ public function testUpdateComment(): void
622623

623624
try {
624625
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
625-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'test-branch', 'main');
626+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'test-branch', static::$defaultBranch);
626627
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'test.txt', 'test', 'Add test', 'test-branch');
627628

628629
// Create PR
@@ -631,7 +632,7 @@ public function testUpdateComment(): void
631632
$repositoryName,
632633
'Test PR',
633634
'test-branch',
634-
'main'
635+
static::$defaultBranch
635636
);
636637

637638
$prNumber = $pr['number'] ?? 0;
@@ -661,7 +662,7 @@ public function testUpdateCommitStatus(): void
661662
try {
662663
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
663664

664-
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, 'main');
665+
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
665666
$commitHash = $commit['commitHash'];
666667

667668
$this->vcsAdapter->updateCommitStatus(
@@ -730,7 +731,7 @@ public function testGetCommit(): void
730731
$customMessage = 'Test commit message';
731732
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test Commit', $customMessage);
732733

733-
$latestCommit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, 'main');
734+
$latestCommit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
734735
$commitHash = $latestCommit['commitHash'];
735736

736737
$result = $this->vcsAdapter->getCommit(static::$owner, $repositoryName, $commitHash);
@@ -761,7 +762,7 @@ public function testGetLatestCommit(): void
761762
$secondMessage = 'Second commit';
762763
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test', $firstMessage);
763764

764-
$commit1 = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, 'main');
765+
$commit1 = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
765766

766767
$this->assertIsArray($commit1);
767768
$this->assertArrayHasKey('commitHash', $commit1);
@@ -781,7 +782,7 @@ public function testGetLatestCommit(): void
781782

782783
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'test.txt', 'test content', $secondMessage);
783784

784-
$commit2 = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, 'main');
785+
$commit2 = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
785786

786787
$this->assertIsArray($commit2);
787788
$this->assertNotEmpty($commit2['commitHash']);
@@ -875,7 +876,7 @@ public function testGetEventPush(): void
875876
$this->assertArrayHasKey('owner', $result);
876877
$this->assertArrayHasKey('affectedFiles', $result);
877878

878-
$this->assertSame('main', $result['branch']);
879+
$this->assertSame(static::$defaultBranch, $result['branch']);
879880
$this->assertSame('def456', $result['commitHash']);
880881
$this->assertSame('test-repo', $result['repositoryName']);
881882
$this->assertSame('test-owner', $result['owner']);
@@ -910,7 +911,7 @@ public function testGetEventPullRequest(): void
910911
],
911912
],
912913
'base' => [
913-
'ref' => 'main',
914+
'ref' => static::$defaultBranch,
914915
'sha' => 'def456',
915916
'user' => [
916917
'login' => 'base-owner',
@@ -972,7 +973,7 @@ public function testGetEventPullRequestExternal(): void
972973
],
973974
],
974975
'base' => [
975-
'ref' => 'main',
976+
'ref' => static::$defaultBranch,
976977
],
977978
'user' => [
978979
'avatar_url' => 'http://gitea:3000/avatars/external',
@@ -1235,7 +1236,7 @@ public function testGetPullRequestFromBranch(): void
12351236
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
12361237

12371238
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
1238-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'my-feature', 'main');
1239+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'my-feature', static::$defaultBranch);
12391240
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'feature.txt', 'content', 'Add feature', 'my-feature');
12401241

12411242
// Create PR
@@ -1244,7 +1245,7 @@ public function testGetPullRequestFromBranch(): void
12441245
$repositoryName,
12451246
'Feature PR',
12461247
'my-feature',
1247-
'main'
1248+
static::$defaultBranch
12481249
);
12491250

12501251
$this->assertArrayHasKey('number', $pr);
@@ -1268,7 +1269,7 @@ public function testGetPullRequestFromBranchNoPR(): void
12681269
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
12691270

12701271
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
1271-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'lonely-branch', 'main');
1272+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'lonely-branch', static::$defaultBranch);
12721273

12731274
// Don't create a PR - just test the method
12741275
$result = $this->vcsAdapter->getPullRequestFromBranch(static::$owner, $repositoryName, 'lonely-branch');
@@ -1286,7 +1287,7 @@ public function testCreateComment(): void
12861287

12871288
try {
12881289
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
1289-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'test-branch', 'main');
1290+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'test-branch', static::$defaultBranch);
12901291
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'test.txt', 'test', 'Add test', 'test-branch');
12911292

12921293
// Create PR
@@ -1295,7 +1296,7 @@ public function testCreateComment(): void
12951296
$repositoryName,
12961297
'Test PR',
12971298
'test-branch',
1298-
'main'
1299+
static::$defaultBranch
12991300
);
13001301

13011302
$prNumber = $pr['number'] ?? 0;
@@ -1344,7 +1345,7 @@ public function testCreateFileOnBranch(): void
13441345

13451346
try {
13461347
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Main');
1347-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature', 'main');
1348+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature', static::$defaultBranch);
13481349

13491350
// Create file on specific branch
13501351
$result = $this->vcsAdapter->createFile(
@@ -1381,8 +1382,8 @@ public function testListBranches(): void
13811382
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
13821383

13831384
// Create additional branches
1384-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-1', 'main');
1385-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-2', 'main');
1385+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-1', static::$defaultBranch);
1386+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-2', static::$defaultBranch);
13861387

13871388
$branches = [];
13881389
$maxAttempts = 10;
@@ -1398,7 +1399,7 @@ public function testListBranches(): void
13981399

13991400
$this->assertIsArray($branches);
14001401
$this->assertNotEmpty($branches);
1401-
$this->assertContains('main', $branches);
1402+
$this->assertContains(static::$defaultBranch, $branches);
14021403
$this->assertContains('feature-1', $branches);
14031404
$this->assertContains('feature-2', $branches);
14041405
$this->assertGreaterThanOrEqual(3, count($branches));
@@ -1415,7 +1416,7 @@ public function testCreateTag(): void
14151416
try {
14161417
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
14171418

1418-
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, 'main');
1419+
$commit = $this->vcsAdapter->getLatestCommit(static::$owner, $repositoryName, static::$defaultBranch);
14191420
$commitHash = $commit['commitHash'];
14201421

14211422
$result = $this->vcsAdapter->createTag(
@@ -1479,7 +1480,7 @@ public function testWebhookPushEvent(): void
14791480
$this->vcsAdapter->createRepository(static::$owner, $repositoryName, false);
14801481

14811482
try {
1482-
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
1483+
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000');
14831484
$this->deleteLastWebhookRequest();
14841485
$this->vcsAdapter->createWebhook(static::$owner, $repositoryName, $catcherUrl . '/webhook', $secret);
14851486

@@ -1513,7 +1514,7 @@ public function testWebhookPushEvent(): void
15131514

15141515
$event = $this->vcsAdapter->getEvent('push', $payload);
15151516
$this->assertIsArray($event);
1516-
$this->assertSame('main', $event['branch']);
1517+
$this->assertSame(static::$defaultBranch, $event['branch']);
15171518
$this->assertSame($repositoryName, $event['repositoryName']);
15181519
$this->assertSame(static::$owner, $event['owner']);
15191520
$this->assertNotEmpty($event['commitHash']);
@@ -1533,10 +1534,10 @@ public function testWebhookPullRequestEvent(): void
15331534
// Create all files BEFORE configuring webhook
15341535
// so those push events don't pollute the catcher
15351536
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'README.md', '# Test');
1536-
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-branch', 'main');
1537+
$this->vcsAdapter->createBranch(static::$owner, $repositoryName, 'feature-branch', static::$defaultBranch);
15371538
$this->vcsAdapter->createFile(static::$owner, $repositoryName, 'feature.txt', 'content', 'Add feature', 'feature-branch');
15381539

1539-
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
1540+
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000');
15401541
$this->vcsAdapter->createWebhook(static::$owner, $repositoryName, $catcherUrl . '/webhook', $secret);
15411542

15421543
// Clear after setup so only PR event will arrive
@@ -1548,7 +1549,7 @@ public function testWebhookPullRequestEvent(): void
15481549
$repositoryName,
15491550
'Test Webhook PR',
15501551
'feature-branch',
1551-
'main'
1552+
static::$defaultBranch
15521553
);
15531554

15541555
// Wait for pull_request webhook to arrive automatically

tests/VCS/Base.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract public function testGetRepositoryTree(): void;
3737
/** @return array<mixed> */
3838
protected function getLastWebhookRequest(): array
3939
{
40-
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
40+
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000');
4141

4242
$client = new Client();
4343
$response = $client->fetch(
@@ -78,7 +78,7 @@ protected function assertEventually(callable $probe, int $timeoutMs = 15000, int
7878

7979
protected function deleteLastWebhookRequest(): void
8080
{
81-
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000') ?? '';
81+
$catcherUrl = System::getEnv('TESTS_GITEA_REQUEST_CATCHER_URL', 'http://request-catcher:5000');
8282

8383
$client = new Client();
8484
$client->fetch(

0 commit comments

Comments
 (0)