Skip to content

Commit 2f5fa1c

Browse files
committed
updated with new methods
1 parent a7ba6f3 commit 2f5fa1c

File tree

3 files changed

+110
-70
lines changed

3 files changed

+110
-70
lines changed

src/VCS/Adapter/Git.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Utopia\VCS\Adapter;
66
use Utopia\Cache\Cache;
7+
use Exception;
78

89
abstract class Git extends Adapter
910
{
@@ -34,4 +35,32 @@ public function getType(): string
3435
{
3536
return self::TYPE_GIT;
3637
}
38+
/**
39+
* Create a file in a repository
40+
*
41+
* @param string $owner Owner of the repository
42+
* @param string $repositoryName Name of the repository
43+
* @param string $filepath Path where file should be created
44+
* @param string $content Content of the file
45+
* @param string $message Commit message
46+
* @return array<mixed> Response from API
47+
*/
48+
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file'): array
49+
{
50+
throw new Exception("Not implemented");
51+
}
52+
53+
/**
54+
* Create a branch in a repository
55+
*
56+
* @param string $owner Owner of the repository
57+
* @param string $repositoryName Name of the repository
58+
* @param string $newBranchName Name of the new branch
59+
* @param string $oldBranchName Name of the branch to branch from
60+
* @return array<mixed> Response from API
61+
*/
62+
public function createBranch(string $owner, string $repositoryName, string $newBranchName, string $oldBranchName): array
63+
{
64+
throw new Exception("Not implemented");
65+
}
3766
}

src/VCS/Adapter/Git/Gitea.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,69 @@ public function getRepositoryTree(string $owner, string $repositoryName, string
163163
return array_column($response['body']['tree'] ?? [], 'path');
164164
}
165165

166+
/**
167+
* Create a file in a repository
168+
*
169+
* @param string $owner Owner of the repository
170+
* @param string $repositoryName Name of the repository
171+
* @param string $filepath Path where file should be created
172+
* @param string $content Content of the file
173+
* @param string $message Commit message
174+
* @return array<mixed> Response from API
175+
*/
176+
public function createFile(string $owner, string $repositoryName, string $filepath, string $content, string $message = 'Add file'): array
177+
{
178+
$url = "/repos/{$owner}/{$repositoryName}/contents/{$filepath}";
179+
180+
$response = $this->call(
181+
self::METHOD_POST,
182+
$url,
183+
['Authorization' => "token $this->accessToken"],
184+
[
185+
'content' => base64_encode($content),
186+
'message' => $message
187+
]
188+
);
189+
190+
$statusCode = $response['headers']['status-code'] ?? 0;
191+
if ($statusCode >= 400) {
192+
throw new Exception("Failed to create file {$filepath}: HTTP {$statusCode}");
193+
}
194+
195+
return $response['body'] ?? [];
196+
}
197+
198+
/**
199+
* Create a branch in a repository
200+
*
201+
* @param string $owner Owner of the repository
202+
* @param string $repositoryName Name of the repository
203+
* @param string $newBranchName Name of the new branch
204+
* @param string $oldBranchName Name of the branch to branch from
205+
* @return array<mixed> Response from API
206+
*/
207+
public function createBranch(string $owner, string $repositoryName, string $newBranchName, string $oldBranchName): array
208+
{
209+
$url = "/repos/{$owner}/{$repositoryName}/branches";
210+
211+
$response = $this->call(
212+
self::METHOD_POST,
213+
$url,
214+
['Authorization' => "token $this->accessToken"],
215+
[
216+
'new_branch_name' => $newBranchName,
217+
'old_branch_name' => $oldBranchName
218+
]
219+
);
220+
221+
$statusCode = $response['headers']['status-code'] ?? 0;
222+
if ($statusCode >= 400) {
223+
throw new Exception("Failed to create branch {$newBranchName}: HTTP {$statusCode}");
224+
}
225+
226+
return $response['body'] ?? [];
227+
}
228+
166229
public function listRepositoryLanguages(string $owner, string $repositoryName): array
167230
{
168231
$url = "/repos/{$owner}/{$repositoryName}/languages";

tests/VCS/Adapter/GiteaTest.php

Lines changed: 18 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,6 @@ private function setupGitea(): void
5555
}
5656
}
5757
}
58-
59-
/**
60-
* Helper method to create a file in a repository
61-
*/
62-
private function createFile(string $owner, string $repo, string $filepath, string $content, string $message = 'Add file'): void
63-
{
64-
$giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000') ?? '';
65-
$url = "{$giteaUrl}/api/v1/repos/{$owner}/{$repo}/contents/{$filepath}";
66-
67-
$ch = curl_init($url);
68-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
69-
curl_setopt($ch, CURLOPT_POST, true);
70-
curl_setopt($ch, CURLOPT_HTTPHEADER, [
71-
'Authorization: token ' . self::$accessToken,
72-
'Content-Type: application/json'
73-
]);
74-
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
75-
'content' => base64_encode($content),
76-
'message' => $message
77-
]));
78-
79-
$response = curl_exec($ch);
80-
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
81-
curl_close($ch);
82-
83-
if ($httpCode >= 400) {
84-
throw new \Exception("Failed to create file {$filepath}: HTTP {$httpCode} - {$response}");
85-
}
86-
}
87-
8858
public function testCreateRepository(): void
8959
{
9060
$owner = self::$owner;
@@ -138,30 +108,8 @@ public function testGetRepositoryTreeWithSlashInBranchName(): void
138108
$repositoryName = 'test-branch-with-slash-' . \uniqid();
139109
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
140110

141-
$this->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
142-
143-
$giteaUrl = System::getEnv('TESTS_GITEA_URL', 'http://gitea:3000') ?? '';
144-
$url = "{$giteaUrl}/api/v1/repos/" . self::$owner . "/{$repositoryName}/branches";
145-
146-
$ch = curl_init($url);
147-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
148-
curl_setopt($ch, CURLOPT_POST, true);
149-
curl_setopt($ch, CURLOPT_HTTPHEADER, [
150-
'Authorization: token ' . self::$accessToken,
151-
'Content-Type: application/json'
152-
]);
153-
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
154-
'new_branch_name' => 'feature/test-branch',
155-
'old_branch_name' => 'main'
156-
]));
157-
158-
$response = curl_exec($ch);
159-
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
160-
curl_close($ch);
161-
162-
if ($httpCode >= 400) {
163-
throw new \Exception("Failed to create branch: HTTP {$httpCode} - {$response}");
164-
}
111+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
112+
$this->vcsAdapter->createBranch(self::$owner, $repositoryName, 'feature/test-branch', 'main');
165113

166114
$tree = $this->vcsAdapter->getRepositoryTree(self::$owner, $repositoryName, 'feature/test-branch');
167115

@@ -240,9 +188,9 @@ public function testGetRepositoryTree(): void
240188
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
241189

242190
// Create files in repo
243-
$this->createFile(self::$owner, $repositoryName, 'README.md', '# Test Repo');
244-
$this->createFile(self::$owner, $repositoryName, 'src/main.php', '<?php echo "hello";');
245-
$this->createFile(self::$owner, $repositoryName, 'src/lib.php', '<?php // library');
191+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test Repo');
192+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'src/main.php', '<?php echo "hello";');
193+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'src/lib.php', '<?php // library');
246194

247195
// Test non-recursive (should only show root level)
248196
$tree = $this->vcsAdapter->getRepositoryTree(self::$owner, $repositoryName, 'main', false);
@@ -269,7 +217,7 @@ public function testGetRepositoryTreeWithInvalidBranch(): void
269217
{
270218
$repositoryName = 'test-get-repository-tree-invalid-' . \uniqid();
271219
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
272-
$this->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
220+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
273221

274222
$tree = $this->vcsAdapter->getRepositoryTree(self::$owner, $repositoryName, 'non-existing-branch', false);
275223

@@ -285,7 +233,7 @@ public function testGetRepositoryContent(): void
285233
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
286234

287235
$fileContent = '# Hello World';
288-
$this->createFile(self::$owner, $repositoryName, 'README.md', $fileContent);
236+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', $fileContent);
289237

290238
$result = $this->vcsAdapter->getRepositoryContent(self::$owner, $repositoryName, 'README.md');
291239

@@ -305,7 +253,7 @@ public function testGetRepositoryContentWithRef(): void
305253
$repositoryName = 'test-get-repository-content-ref-' . \uniqid();
306254
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
307255

308-
$this->createFile(self::$owner, $repositoryName, 'test.txt', 'main branch content');
256+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'test.txt', 'main branch content');
309257

310258
$result = $this->vcsAdapter->getRepositoryContent(self::$owner, $repositoryName, 'test.txt', 'main');
311259

@@ -319,7 +267,7 @@ public function testGetRepositoryContentFileNotFound(): void
319267
{
320268
$repositoryName = 'test-get-repository-content-not-found-' . \uniqid();
321269
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
322-
$this->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
270+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
323271

324272
$this->expectException(\Utopia\VCS\Exception\FileNotFound::class);
325273
$this->vcsAdapter->getRepositoryContent(self::$owner, $repositoryName, 'non-existing.txt');
@@ -331,9 +279,9 @@ public function testListRepositoryContents(): void
331279
$repositoryName = 'test-list-repository-contents-' . \uniqid();
332280
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
333281

334-
$this->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
335-
$this->createFile(self::$owner, $repositoryName, 'file1.txt', 'content1');
336-
$this->createFile(self::$owner, $repositoryName, 'src/main.php', '<?php');
282+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
283+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'file1.txt', 'content1');
284+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'src/main.php', '<?php');
337285

338286
// List root directory
339287
$contents = $this->vcsAdapter->listRepositoryContents(self::$owner, $repositoryName);
@@ -361,8 +309,8 @@ public function testListRepositoryContentsInSubdirectory(): void
361309
$repositoryName = 'test-list-repository-contents-subdir-' . \uniqid();
362310
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
363311

364-
$this->createFile(self::$owner, $repositoryName, 'src/file1.php', '<?php');
365-
$this->createFile(self::$owner, $repositoryName, 'src/file2.php', '<?php');
312+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'src/file1.php', '<?php');
313+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'src/file2.php', '<?php');
366314

367315
$contents = $this->vcsAdapter->listRepositoryContents(self::$owner, $repositoryName, 'src');
368316

@@ -380,7 +328,7 @@ public function testListRepositoryContentsNonExistingPath(): void
380328
{
381329
$repositoryName = 'test-list-repository-contents-invalid-' . \uniqid();
382330
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
383-
$this->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
331+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'README.md', '# Test');
384332

385333
$contents = $this->vcsAdapter->listRepositoryContents(self::$owner, $repositoryName, 'non-existing-path');
386334

@@ -485,9 +433,9 @@ public function testListRepositoryLanguages(): void
485433
$repositoryName = 'test-list-repository-languages-' . \uniqid();
486434
$this->vcsAdapter->createRepository(self::$owner, $repositoryName, false);
487435

488-
$this->createFile(self::$owner, $repositoryName, 'main.php', '<?php echo "test";');
489-
$this->createFile(self::$owner, $repositoryName, 'script.js', 'console.log("test");');
490-
$this->createFile(self::$owner, $repositoryName, 'style.css', 'body { margin: 0; }');
436+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'main.php', '<?php echo "test";');
437+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'script.js', 'console.log("test");');
438+
$this->vcsAdapter->createFile(self::$owner, $repositoryName, 'style.css', 'body { margin: 0; }');
491439

492440
sleep(2);
493441

0 commit comments

Comments
 (0)