|
6 | 6 | use Utopia\Cache\Cache; |
7 | 7 | use Utopia\VCS\Adapter\Git; |
8 | 8 | use Utopia\VCS\Exception\RepositoryNotFound; |
| 9 | +use Utopia\VCS\Exception\FileNotFound; |
9 | 10 |
|
10 | 11 | class Gitea extends Git |
11 | 12 | { |
| 13 | + public const CONTENTS_FILE = 'file'; |
| 14 | + |
| 15 | + public const CONTENTS_DIRECTORY = 'dir'; |
| 16 | + |
12 | 17 | protected string $endpoint = 'http://gitea:3000/api/v1'; |
13 | 18 |
|
14 | 19 | protected string $accessToken; |
@@ -147,22 +152,156 @@ public function getRepositoryName(string $repositoryId): string |
147 | 152 |
|
148 | 153 | public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array |
149 | 154 | { |
150 | | - throw new Exception("Not implemented yet"); |
| 155 | + $url = "/repos/{$owner}/{$repositoryName}/git/trees/" . urlencode($branch) . ($recursive ? '?recursive=1' : ''); |
| 156 | + |
| 157 | + $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]); |
| 158 | + |
| 159 | + if (($response['headers']['status-code'] ?? 0) === 404) { |
| 160 | + return []; |
| 161 | + } |
| 162 | + |
| 163 | + return array_column($response['body']['tree'] ?? [], 'path'); |
| 164 | + } |
| 165 | + |
| 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'] ?? []; |
151 | 227 | } |
152 | 228 |
|
153 | 229 | public function listRepositoryLanguages(string $owner, string $repositoryName): array |
154 | 230 | { |
155 | | - throw new Exception("Not implemented yet"); |
| 231 | + $url = "/repos/{$owner}/{$repositoryName}/languages"; |
| 232 | + |
| 233 | + $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]); |
| 234 | + |
| 235 | + if (isset($response['body'])) { |
| 236 | + return array_keys($response['body']); |
| 237 | + } |
| 238 | + |
| 239 | + return []; |
156 | 240 | } |
157 | 241 |
|
158 | 242 | public function getRepositoryContent(string $owner, string $repositoryName, string $path, string $ref = ''): array |
159 | 243 | { |
160 | | - throw new Exception("Not implemented yet"); |
| 244 | + $url = "/repos/{$owner}/{$repositoryName}/contents/{$path}"; |
| 245 | + if (!empty($ref)) { |
| 246 | + $url .= "?ref=" . urlencode($ref); |
| 247 | + } |
| 248 | + |
| 249 | + $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]); |
| 250 | + |
| 251 | + if (($response['headers']['status-code'] ?? 0) !== 200) { |
| 252 | + throw new FileNotFound(); |
| 253 | + } |
| 254 | + |
| 255 | + $encoding = $response['body']['encoding'] ?? ''; |
| 256 | + $content = ''; |
| 257 | + |
| 258 | + if ($encoding === 'base64') { |
| 259 | + $content = base64_decode($response['body']['content'] ?? ''); |
| 260 | + } else { |
| 261 | + throw new FileNotFound(); |
| 262 | + } |
| 263 | + |
| 264 | + return [ |
| 265 | + 'sha' => $response['body']['sha'] ?? '', |
| 266 | + 'size' => $response['body']['size'] ?? 0, |
| 267 | + 'content' => $content |
| 268 | + ]; |
161 | 269 | } |
162 | 270 |
|
163 | 271 | public function listRepositoryContents(string $owner, string $repositoryName, string $path = '', string $ref = ''): array |
164 | 272 | { |
165 | | - throw new Exception("Not implemented yet"); |
| 273 | + $url = "/repos/{$owner}/{$repositoryName}/contents"; |
| 274 | + if (!empty($path)) { |
| 275 | + $url .= "/{$path}"; |
| 276 | + } |
| 277 | + if (!empty($ref)) { |
| 278 | + $url .= "?ref=" . urlencode($ref); |
| 279 | + } |
| 280 | + |
| 281 | + $response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]); |
| 282 | + |
| 283 | + if (($response['headers']['status-code'] ?? 0) === 404) { |
| 284 | + return []; |
| 285 | + } |
| 286 | + |
| 287 | + $items = []; |
| 288 | + if (!empty($response['body'][0])) { |
| 289 | + $items = $response['body']; |
| 290 | + } elseif (!empty($response['body'])) { |
| 291 | + $items = [$response['body']]; |
| 292 | + } |
| 293 | + |
| 294 | + $contents = []; |
| 295 | + foreach ($items as $item) { |
| 296 | + $type = $item['type'] ?? 'file'; |
| 297 | + $contents[] = [ |
| 298 | + 'name' => $item['name'] ?? '', |
| 299 | + 'size' => $item['size'] ?? 0, |
| 300 | + 'type' => $type === 'file' ? self::CONTENTS_FILE : self::CONTENTS_DIRECTORY |
| 301 | + ]; |
| 302 | + } |
| 303 | + |
| 304 | + return $contents; |
166 | 305 | } |
167 | 306 |
|
168 | 307 | public function deleteRepository(string $owner, string $repositoryName): bool |
|
0 commit comments