Skip to content

Commit a1de80e

Browse files
committed
updated with some fixes
1 parent 9448c32 commit a1de80e

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

src/VCS/Adapter/Git/Gitea.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Gitea extends Git
1313
public const CONTENTS_FILE = 'file';
1414

1515
public const CONTENTS_DIRECTORY = 'dir';
16-
16+
1717
protected string $endpoint = 'http://gitea:3000/api/v1';
1818

1919
protected string $accessToken;
@@ -152,7 +152,7 @@ public function getRepositoryName(string $repositoryId): string
152152

153153
public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array
154154
{
155-
$url = "/repos/{$owner}/{$repositoryName}/git/trees/{$branch}" . ($recursive ? '?recursive=1' : '');
155+
$url = "/repos/{$owner}/{$repositoryName}/git/trees/" . urlencode($branch) . ($recursive ? '?recursive=1' : '');
156156

157157
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
158158

@@ -180,7 +180,7 @@ public function getRepositoryContent(string $owner, string $repositoryName, stri
180180
{
181181
$url = "/repos/{$owner}/{$repositoryName}/contents/{$path}";
182182
if (!empty($ref)) {
183-
$url .= "?ref={$ref}";
183+
$url .= "?ref=" . urlencode($ref);
184184
}
185185

186186
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
@@ -212,7 +212,7 @@ public function listRepositoryContents(string $owner, string $repositoryName, st
212212
$url .= "/{$path}";
213213
}
214214
if (!empty($ref)) {
215-
$url .= "?ref={$ref}";
215+
$url .= "?ref=" . urlencode($ref);
216216
}
217217

218218
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);

tests/VCS/Adapter/GiteaTest.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private function createFile(string $owner, string $repo, string $filepath, strin
6363
{
6464
$giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000') ?? '';
6565
$url = "{$giteaUrl}/api/v1/repos/{$owner}/{$repo}/contents/{$filepath}";
66-
66+
6767
$ch = curl_init($url);
6868
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
6969
curl_setopt($ch, CURLOPT_POST, true);
@@ -75,9 +75,14 @@ private function createFile(string $owner, string $repo, string $filepath, strin
7575
'content' => base64_encode($content),
7676
'message' => $message
7777
]));
78-
79-
curl_exec($ch);
78+
79+
$response = curl_exec($ch);
80+
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
8081
curl_close($ch);
82+
83+
if ($httpCode >= 400) {
84+
throw new \Exception("Failed to create file {$filepath}: HTTP {$httpCode}");
85+
}
8186
}
8287

8388
public function testCreateRepository(): void
@@ -128,6 +133,42 @@ public function testGetComment(): void
128133
$this->markTestSkipped('Will be implemented in follow-up PR');
129134
}
130135

136+
public function testGetRepositoryTreeWithSlashInBranchName(): void
137+
{
138+
$repositoryName = 'test-branch-with-slash-' . \uniqid();
139+
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
140+
141+
// Create a file on main branch first
142+
$this->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
143+
144+
// Create a branch with a slash in the name using curl
145+
$giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000') ?? '';
146+
$url = "{$giteaUrl}/api/v1/repos/" . self::$owner . "/{$repositoryName}/branches";
147+
148+
$ch = curl_init($url);
149+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
150+
curl_setopt($ch, CURLOPT_POST, true);
151+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
152+
'Authorization: token ' . self::$accessToken,
153+
'Content-Type: application/json'
154+
]);
155+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
156+
'new_branch_name' => 'feature/test-branch',
157+
'old_branch_name' => 'main'
158+
]));
159+
curl_exec($ch);
160+
curl_close($ch);
161+
162+
// Now try to get tree from the branch with slash
163+
$tree = $this->vcsAdapter->getRepositoryTree(self::$owner, $repositoryName, 'feature/test-branch');
164+
165+
$this->assertIsArray($tree);
166+
$this->assertNotEmpty($tree); // Should have README.md
167+
$this->assertContains('README.md', $tree);
168+
169+
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
170+
}
171+
131172
public function testGetRepository(): void
132173
{
133174
$repositoryName = 'test-get-repository-' . \uniqid();
@@ -280,7 +321,6 @@ public function testGetRepositoryContentFileNotFound(): void
280321
$this->expectException(\Utopia\VCS\Exception\FileNotFound::class);
281322
$this->vcsAdapter->getRepositoryContent(self::$owner, $repositoryName, 'non-existing.txt');
282323

283-
$this->vcsAdapter->deleteRepository(self::$owner, $repositoryName);
284324
}
285325

286326
public function testListRepositoryContents(): void
@@ -446,6 +486,8 @@ public function testListRepositoryLanguages(): void
446486
$this->createFile(self::$owner, $repositoryName, 'script.js', 'console.log("test");');
447487
$this->createFile(self::$owner, $repositoryName, 'style.css', 'body { margin: 0; }');
448488

489+
sleep(2);
490+
449491
$languages = $this->vcsAdapter->listRepositoryLanguages(self::$owner, $repositoryName);
450492

451493
$this->assertIsArray($languages);

0 commit comments

Comments
 (0)