-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGitea.php
More file actions
257 lines (203 loc) · 7.85 KB
/
Gitea.php
File metadata and controls
257 lines (203 loc) · 7.85 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
<?php
namespace Utopia\VCS\Adapter\Git;
use Exception;
use Utopia\Cache\Cache;
use Utopia\VCS\Adapter\Git;
use Utopia\VCS\Exception\RepositoryNotFound;
class Gitea extends Git
{
protected string $endpoint = 'http://gitea:3000/api/v1';
protected string $accessToken;
protected ?string $refreshToken = null;
protected string $giteaUrl;
protected Cache $cache;
/**
* Global Headers
*
* @var array<string, string>
*/
protected $headers = ['content-type' => 'application/json'];
public function __construct(Cache $cache)
{
$this->cache = $cache;
}
public function setEndpoint(string $endpoint): void
{
$this->giteaUrl = rtrim($endpoint, '/');
$this->endpoint = $this->giteaUrl . '/api/v1';
}
/**
* Get Adapter Name
*
* @return string
*/
public function getName(): string
{
return 'gitea';
}
/**
* Gitea Initialisation with access token from OAuth2 flow.
*
* Note: Gitea uses OAuth2 instead of GitHub's App Installation flow.
* The parameters are adapted to maintain interface compatibility:
* - $installationId is used to pass the access token
* - $privateKey is used to pass the refresh token
* - $appId is used to pass the Gitea instance URL
* - $accessToken is used to pass the access token
* - $refreshToken is used to pass the refresh token
*/
public function initializeVariables(string $installationId, string $privateKey, ?string $appId = null, ?string $accessToken = null, ?string $refreshToken = null): void
{
if (!empty($accessToken)) {
$this->accessToken = $accessToken;
$this->refreshToken = $refreshToken;
return;
}
throw new Exception("accessToken is required for Gitea adapter.");
}
/**
* Generate Access Token
*
* Note: This method is required by the Adapter interface but is not used for Gitea.
* Gitea uses OAuth2 tokens that are provided directly via initializeVariables().
*/
protected function generateAccessToken(string $privateKey, string $appId): void
{
// Not applicable for Gitea - OAuth2 tokens are passed directly
return;
}
/**
* Create new repository
*
* @return array<mixed> Details of new repository
*/
public function createRepository(string $owner, string $repositoryName, bool $private): array
{
$url = "/orgs/{$owner}/repos";
$response = $this->call(self::METHOD_POST, $url, ['Authorization' => "token $this->accessToken"], [
'name' => $repositoryName,
'private' => $private,
]);
return $response['body'] ?? [];
}
public function createOrganization(string $orgName): string
{
$url = "/orgs";
$response = $this->call(self::METHOD_POST, $url, ['Authorization' => "token $this->accessToken"], [
'username' => $orgName,
'visibility' => 'public',
]);
return $response['body']['name'] ?? '';
}
// Stub methods to satisfy abstract class requirements
// These will be implemented in follow-up PRs
public function searchRepositories(string $installationId, string $owner, int $page, int $per_page, string $search = ''): array
{
throw new Exception("Not implemented yet");
}
public function getInstallationRepository(string $repositoryName): array
{
throw new Exception("Not implemented yet");
}
public function getRepository(string $owner, string $repositoryName): array
{
$url = "/repos/{$owner}/{$repositoryName}";
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
$statusCode = $response['headers']['status-code'] ?? 0;
if ($statusCode >= 400) {
throw new RepositoryNotFound("Repository not found");
}
return $response['body'];
}
public function getRepositoryName(string $repositoryId): string
{
$url = "/repositories/{$repositoryId}";
$response = $this->call(self::METHOD_GET, $url, ['Authorization' => "token $this->accessToken"]);
if (!isset($response['body']['name'])) {
throw new RepositoryNotFound("Repository not found");
}
return $response['body']['name'];
}
public function getRepositoryTree(string $owner, string $repositoryName, string $branch, bool $recursive = false): array
{
throw new Exception("Not implemented yet");
}
public function listRepositoryLanguages(string $owner, string $repositoryName): array
{
throw new Exception("Not implemented yet");
}
public function getRepositoryContent(string $owner, string $repositoryName, string $path, string $ref = ''): array
{
throw new Exception("Not implemented yet");
}
public function listRepositoryContents(string $owner, string $repositoryName, string $path = '', string $ref = ''): array
{
throw new Exception("Not implemented yet");
}
public function deleteRepository(string $owner, string $repositoryName): bool
{
$url = "/repos/{$owner}/{$repositoryName}";
$response = $this->call(self::METHOD_DELETE, $url, ['Authorization' => "token $this->accessToken"]);
$statusCode = $response['headers']['status-code'];
if ($statusCode >= 400) {
throw new Exception("Deleting repository {$repositoryName} failed with status code {$statusCode}");
}
return true;
}
public function createComment(string $owner, string $repositoryName, int $pullRequestNumber, string $comment): string
{
throw new Exception("Not implemented yet");
}
public function getComment(string $owner, string $repositoryName, string $commentId): string
{
throw new Exception("Not implemented yet");
}
public function updateComment(string $owner, string $repositoryName, int $commentId, string $comment): string
{
throw new Exception("Not implemented yet");
}
public function getUser(string $username): array
{
throw new Exception("Not implemented yet");
}
public function getOwnerName(string $installationId): string
{
throw new Exception("Not implemented yet");
}
public function getPullRequest(string $owner, string $repositoryName, int $pullRequestNumber): array
{
throw new Exception("Not implemented yet");
}
public function getPullRequestFromBranch(string $owner, string $repositoryName, string $branch): array
{
throw new Exception("Not implemented yet");
}
public function listBranches(string $owner, string $repositoryName): array
{
throw new Exception("Not implemented yet");
}
public function getCommit(string $owner, string $repositoryName, string $commitHash): array
{
throw new Exception("Not implemented yet");
}
public function getLatestCommit(string $owner, string $repositoryName, string $branch): array
{
throw new Exception("Not implemented yet");
}
public function updateCommitStatus(string $repositoryName, string $commitHash, string $owner, string $state, string $description = '', string $target_url = '', string $context = ''): void
{
throw new Exception("Not implemented yet");
}
public function generateCloneCommand(string $owner, string $repositoryName, string $version, string $versionType, string $directory, string $rootDirectory): string
{
throw new Exception("Not implemented yet");
}
public function getEvent(string $event, string $payload): array
{
throw new Exception("Not implemented yet");
}
public function validateWebhookEvent(string $payload, string $signature, string $signatureKey): bool
{
throw new Exception("Not implemented yet");
}
}