forked from renovatebot/renovate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitea-helper.ts
439 lines (386 loc) · 10 KB
/
gitea-helper.ts
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import type { BranchStatus } from '../../../types';
import { GiteaHttp, GiteaHttpOptions } from '../../../util/http/gitea';
import { getQueryString } from '../../../util/url';
import type {
Branch,
CombinedCommitStatus,
Comment,
CommentCreateParams,
CommentUpdateParams,
CommitStatus,
CommitStatusCreateParams,
CommitStatusType,
Issue,
IssueCreateParams,
IssueSearchParams,
IssueUpdateLabelsParams,
IssueUpdateParams,
Label,
PR,
PRCreateParams,
PRMergeParams,
PRUpdateParams,
PrReviewersParams,
Repo,
RepoContents,
RepoSearchParams,
RepoSearchResults,
User,
} from './types';
import { API_PATH } from './utils';
export const giteaHttp = new GiteaHttp();
const urlEscape = (raw: string): string => encodeURIComponent(raw);
const commitStatusStates: CommitStatusType[] = [
'unknown',
'success',
'pending',
'warning',
'failure',
'error',
];
export async function getCurrentUser(
options?: GiteaHttpOptions,
): Promise<User> {
const url = `${API_PATH}/user`;
const res = await giteaHttp.getJson<User>(url, options);
return res.body;
}
export async function getVersion(options?: GiteaHttpOptions): Promise<string> {
const url = `${API_PATH}/version`;
const res = await giteaHttp.getJson<{ version: string }>(url, options);
return res.body.version;
}
export async function searchRepos(
params: RepoSearchParams,
options?: GiteaHttpOptions,
): Promise<Repo[]> {
const query = getQueryString(params);
const url = `${API_PATH}/repos/search?${query}`;
const res = await giteaHttp.getJson<RepoSearchResults>(url, {
...options,
paginate: true,
});
if (!res.body.ok) {
throw new Error(
'Unable to search for repositories, ok flag has not been set',
);
}
return res.body.data;
}
export async function orgListRepos(
organization: string,
options?: GiteaHttpOptions,
): Promise<Repo[]> {
const url = `${API_PATH}/orgs/${organization}/repos`;
const res = await giteaHttp.getJson<Repo[]>(url, {
...options,
paginate: true,
});
return res.body;
}
export async function getRepo(
repoPath: string,
options?: GiteaHttpOptions,
): Promise<Repo> {
const url = `${API_PATH}/repos/${repoPath}`;
const res = await giteaHttp.getJson<Repo>(url, options);
return res.body;
}
export async function getRepoContents(
repoPath: string,
filePath: string,
ref?: string | null,
options?: GiteaHttpOptions,
): Promise<RepoContents> {
const query = getQueryString(ref ? { ref } : {});
const url = `${API_PATH}/repos/${repoPath}/contents/${urlEscape(
filePath,
)}?${query}`;
const res = await giteaHttp.getJson<RepoContents>(url, options);
if (res.body.content) {
res.body.contentString = Buffer.from(res.body.content, 'base64').toString();
}
return res.body;
}
export async function createPR(
repoPath: string,
params: PRCreateParams,
options?: GiteaHttpOptions,
): Promise<PR> {
const url = `${API_PATH}/repos/${repoPath}/pulls`;
const res = await giteaHttp.postJson<PR>(url, {
...options,
body: params,
});
return res.body;
}
export async function updatePR(
repoPath: string,
idx: number,
params: PRUpdateParams,
options?: GiteaHttpOptions,
): Promise<PR> {
const url = `${API_PATH}/repos/${repoPath}/pulls/${idx}`;
const res = await giteaHttp.patchJson<PR>(url, {
...options,
body: params,
});
return res.body;
}
export async function closePR(
repoPath: string,
idx: number,
options?: GiteaHttpOptions,
): Promise<void> {
await updatePR(repoPath, idx, {
...options,
state: 'closed',
});
}
export async function mergePR(
repoPath: string,
idx: number,
params: PRMergeParams,
options?: GiteaHttpOptions,
): Promise<void> {
const url = `${API_PATH}/repos/${repoPath}/pulls/${idx}/merge`;
await giteaHttp.postJson(url, {
...options,
body: params,
});
}
export async function getPR(
repoPath: string,
idx: number,
options?: GiteaHttpOptions,
): Promise<PR> {
const url = `${API_PATH}/repos/${repoPath}/pulls/${idx}`;
const res = await giteaHttp.getJson<PR>(url, options);
return res.body;
}
export async function requestPrReviewers(
repoPath: string,
idx: number,
params: PrReviewersParams,
options?: GiteaHttpOptions,
): Promise<void> {
const url = `${API_PATH}/repos/${repoPath}/pulls/${idx}/requested_reviewers`;
await giteaHttp.postJson(url, {
...options,
body: params,
});
}
export async function createIssue(
repoPath: string,
params: IssueCreateParams,
options?: GiteaHttpOptions,
): Promise<Issue> {
const url = `${API_PATH}/repos/${repoPath}/issues`;
const res = await giteaHttp.postJson<Issue>(url, {
...options,
body: params,
});
return res.body;
}
export async function updateIssue(
repoPath: string,
idx: number,
params: IssueUpdateParams,
options?: GiteaHttpOptions,
): Promise<Issue> {
const url = `${API_PATH}/repos/${repoPath}/issues/${idx}`;
const res = await giteaHttp.patchJson<Issue>(url, {
...options,
body: params,
});
return res.body;
}
export async function updateIssueLabels(
repoPath: string,
idx: number,
params: IssueUpdateLabelsParams,
options?: GiteaHttpOptions,
): Promise<Label[]> {
const url = `${API_PATH}/repos/${repoPath}/issues/${idx}/labels`;
const res = await giteaHttp.putJson<Label[]>(url, {
...options,
body: params,
});
return res.body;
}
export async function closeIssue(
repoPath: string,
idx: number,
options?: GiteaHttpOptions,
): Promise<void> {
await updateIssue(repoPath, idx, {
...options,
state: 'closed',
});
}
export async function searchIssues(
repoPath: string,
params: IssueSearchParams,
options?: GiteaHttpOptions,
): Promise<Issue[]> {
const query = getQueryString({ ...params, type: 'issues' });
const url = `${API_PATH}/repos/${repoPath}/issues?${query}`;
const res = await giteaHttp.getJson<Issue[]>(url, {
...options,
paginate: true,
});
return res.body;
}
export async function getIssue(
repoPath: string,
idx: number,
options?: GiteaHttpOptions,
): Promise<Issue> {
const url = `${API_PATH}/repos/${repoPath}/issues/${idx}`;
const res = await giteaHttp.getJson<Issue>(url, options);
return res.body;
}
export async function getRepoLabels(
repoPath: string,
options?: GiteaHttpOptions,
): Promise<Label[]> {
const url = `${API_PATH}/repos/${repoPath}/labels`;
const res = await giteaHttp.getJson<Label[]>(url, options);
return res.body;
}
export async function getOrgLabels(
orgName: string,
options?: GiteaHttpOptions,
): Promise<Label[]> {
const url = `${API_PATH}/orgs/${orgName}/labels`;
const res = await giteaHttp.getJson<Label[]>(url, options);
return res.body;
}
export async function unassignLabel(
repoPath: string,
issue: number,
label: number,
options?: GiteaHttpOptions,
): Promise<void> {
const url = `${API_PATH}/repos/${repoPath}/issues/${issue}/labels/${label}`;
await giteaHttp.deleteJson(url, options);
}
export async function createComment(
repoPath: string,
issue: number,
body: string,
options?: GiteaHttpOptions,
): Promise<Comment> {
const params: CommentCreateParams = { body };
const url = `${API_PATH}/repos/${repoPath}/issues/${issue}/comments`;
const res = await giteaHttp.postJson<Comment>(url, {
...options,
body: params,
});
return res.body;
}
export async function updateComment(
repoPath: string,
idx: number,
body: string,
options?: GiteaHttpOptions,
): Promise<Comment> {
const params: CommentUpdateParams = { body };
const url = `${API_PATH}/repos/${repoPath}/issues/comments/${idx}`;
const res = await giteaHttp.patchJson<Comment>(url, {
...options,
body: params,
});
return res.body;
}
export async function deleteComment(
repoPath: string,
idx: number,
options?: GiteaHttpOptions,
): Promise<void> {
const url = `${API_PATH}/repos/${repoPath}/issues/comments/${idx}`;
await giteaHttp.deleteJson(url, options);
}
export async function getComments(
repoPath: string,
issue: number,
options?: GiteaHttpOptions,
): Promise<Comment[]> {
const url = `${API_PATH}/repos/${repoPath}/issues/${issue}/comments`;
const res = await giteaHttp.getJson<Comment[]>(url, options);
return res.body;
}
export async function createCommitStatus(
repoPath: string,
branchCommit: string,
params: CommitStatusCreateParams,
options?: GiteaHttpOptions,
): Promise<CommitStatus> {
const url = `${API_PATH}/repos/${repoPath}/statuses/${branchCommit}`;
const res = await giteaHttp.postJson<CommitStatus>(url, {
...options,
body: params,
});
return res.body;
}
export const giteaToRenovateStatusMapping: Record<
CommitStatusType,
BranchStatus | null
> = {
unknown: 'yellow',
success: 'green',
pending: 'yellow',
warning: 'red',
failure: 'red',
error: 'red',
};
export const renovateToGiteaStatusMapping: Record<
BranchStatus,
CommitStatusType
> = {
green: 'success',
yellow: 'pending',
red: 'failure',
};
function filterStatus(data: CommitStatus[]): CommitStatus[] {
const ret: Record<string, CommitStatus> = {};
for (const i of data) {
if (
!ret[i.context] ||
new Date(ret[i.context].created_at) < new Date(i.created_at)
) {
ret[i.context] = i;
}
}
return Object.values(ret);
}
export async function getCombinedCommitStatus(
repoPath: string,
branchName: string,
options?: GiteaHttpOptions,
): Promise<CombinedCommitStatus> {
const url = `${API_PATH}/repos/${repoPath}/commits/${urlEscape(
branchName,
)}/statuses`;
const res = await giteaHttp.getJson<CommitStatus[]>(url, {
...options,
paginate: true,
});
let worstState = 0;
for (const cs of filterStatus(res.body)) {
worstState = Math.max(worstState, commitStatusStates.indexOf(cs.status));
}
return {
worstStatus: commitStatusStates[worstState],
statuses: res.body,
};
}
export async function getBranch(
repoPath: string,
branchName: string,
options?: GiteaHttpOptions,
): Promise<Branch> {
const url = `${API_PATH}/repos/${repoPath}/branches/${urlEscape(branchName)}`;
const res = await giteaHttp.getJson<Branch>(url, options);
return res.body;
}