@remogram/provider-gitea-api 0.1.0-beta.4 → 0.1.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +324 -3
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -12,6 +12,19 @@ import {
|
|
|
12
12
|
crInventory,
|
|
13
13
|
buildMergePlanBodyFromFacts,
|
|
14
14
|
buildChangeRequestOpenedBody,
|
|
15
|
+
buildCommitStatusSetBody,
|
|
16
|
+
parseStatusSetArgs,
|
|
17
|
+
normalizeStatusSetState,
|
|
18
|
+
buildProviderIdentityFromGiteaUser,
|
|
19
|
+
buildBranchProtectionFromGiteaProtection,
|
|
20
|
+
buildCrFilesBody,
|
|
21
|
+
buildCrFilesFromGiteaFiles,
|
|
22
|
+
buildCrCommentsBody,
|
|
23
|
+
buildCrCommentsFromGiteaComments,
|
|
24
|
+
buildForgeChangesFromGiteaPulls,
|
|
25
|
+
buildChecksConclusionObservedEvent,
|
|
26
|
+
appendForgeChangeEvents,
|
|
27
|
+
parseSinceObservedAt,
|
|
15
28
|
ERROR_CODES,
|
|
16
29
|
forgeError,
|
|
17
30
|
forgeIngestCapabilityFacts,
|
|
@@ -56,9 +69,22 @@ const AUTH_CAPABILITIES = [
|
|
|
56
69
|
'merge_plan',
|
|
57
70
|
'sync_plan',
|
|
58
71
|
'cr_open',
|
|
72
|
+
'status_set',
|
|
73
|
+
'whoami',
|
|
74
|
+
'branch_protection',
|
|
75
|
+
'cr_files',
|
|
76
|
+
'cr_comments',
|
|
77
|
+
'forge_changes',
|
|
59
78
|
];
|
|
60
79
|
|
|
61
|
-
const STRUCTURED_COMMANDS = apiProviderCommands({
|
|
80
|
+
const STRUCTURED_COMMANDS = apiProviderCommands({
|
|
81
|
+
writeCommandsImplemented: true,
|
|
82
|
+
statusSetImplemented: true,
|
|
83
|
+
branchProtectionImplemented: true,
|
|
84
|
+
crFilesImplemented: true,
|
|
85
|
+
crCommentsImplemented: true,
|
|
86
|
+
forgeChangesImplemented: true,
|
|
87
|
+
});
|
|
62
88
|
|
|
63
89
|
export function giteaToken() {
|
|
64
90
|
return process.env.GITEA_TOKEN || null;
|
|
@@ -259,6 +285,195 @@ export async function repoStatus(ctx) {
|
|
|
259
285
|
};
|
|
260
286
|
}
|
|
261
287
|
|
|
288
|
+
export async function whoami(ctx) {
|
|
289
|
+
requireToken();
|
|
290
|
+
const user = await giteaFetch(ctx.config, ctx.parsed, '/user');
|
|
291
|
+
return buildProviderIdentityFromGiteaUser(user);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export async function branchProtection(ctx, { branchRef }) {
|
|
295
|
+
requireToken();
|
|
296
|
+
const protection = await giteaFetch(
|
|
297
|
+
ctx.config,
|
|
298
|
+
ctx.parsed,
|
|
299
|
+
repoApiPath(ctx.config, 'branch_protections', branchRef),
|
|
300
|
+
);
|
|
301
|
+
return buildBranchProtectionFromGiteaProtection(branchRef, protection);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export async function crFiles(ctx, { number }) {
|
|
305
|
+
requireToken();
|
|
306
|
+
if (number == null) {
|
|
307
|
+
throw Object.assign(new Error('--number required'), {
|
|
308
|
+
forgeError: forgeError(ERROR_CODES.INVALID_ARGS, 'Provide --number for PR changed paths'),
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
const path = repoApiPath(ctx.config, 'pulls', number, 'files');
|
|
312
|
+
const pageSep = path.includes('?') ? '&' : '?';
|
|
313
|
+
let activeLimit = DEFAULT_CHECK_STATUS_PAGE_SIZE;
|
|
314
|
+
const allFiles = [];
|
|
315
|
+
let listTruncated = false;
|
|
316
|
+
let entryCount = 0;
|
|
317
|
+
|
|
318
|
+
for (let page = 1; page <= MAX_CHECK_PAGES; page += 1) {
|
|
319
|
+
const { items, usedLimit } = await fetchPageWithIngestBackoff(
|
|
320
|
+
async ({ page: pageNum, limit }) => {
|
|
321
|
+
const body = await giteaFetch(
|
|
322
|
+
ctx.config,
|
|
323
|
+
ctx.parsed,
|
|
324
|
+
`${path}${pageSep}limit=${limit}&page=${pageNum}`,
|
|
325
|
+
);
|
|
326
|
+
if (!Array.isArray(body)) {
|
|
327
|
+
throw Object.assign(new Error('Provider returned non-array pull files list'), {
|
|
328
|
+
forgeError: forgeError(
|
|
329
|
+
ERROR_CODES.UNPARSEABLE_PROVIDER_OUTPUT,
|
|
330
|
+
'Provider returned non-array pull files list',
|
|
331
|
+
),
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
return body;
|
|
335
|
+
},
|
|
336
|
+
page,
|
|
337
|
+
activeLimit,
|
|
338
|
+
);
|
|
339
|
+
activeLimit = usedLimit;
|
|
340
|
+
entryCount += items.length;
|
|
341
|
+
allFiles.push(...items);
|
|
342
|
+
if (items.length < usedLimit) break;
|
|
343
|
+
if (page === MAX_CHECK_PAGES) {
|
|
344
|
+
listTruncated = true;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const body = buildCrFilesFromGiteaFiles(number, allFiles);
|
|
349
|
+
if (listTruncated) {
|
|
350
|
+
return buildCrFilesBody({
|
|
351
|
+
pr_number: body.pr_number,
|
|
352
|
+
changed_paths: body.changed_paths,
|
|
353
|
+
paths_truncated: true,
|
|
354
|
+
path_count: entryCount,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
return body;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export async function crComments(ctx, { number }) {
|
|
361
|
+
requireToken();
|
|
362
|
+
if (number == null) {
|
|
363
|
+
throw Object.assign(new Error('--number required'), {
|
|
364
|
+
forgeError: forgeError(ERROR_CODES.INVALID_ARGS, 'Provide --number for PR review comments'),
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
const path = repoApiPath(ctx.config, 'pulls', number, 'comments');
|
|
368
|
+
const pageSep = path.includes('?') ? '&' : '?';
|
|
369
|
+
let activeLimit = DEFAULT_CHECK_STATUS_PAGE_SIZE;
|
|
370
|
+
const allComments = [];
|
|
371
|
+
let listTruncated = false;
|
|
372
|
+
let entryCount = 0;
|
|
373
|
+
|
|
374
|
+
for (let page = 1; page <= MAX_CHECK_PAGES; page += 1) {
|
|
375
|
+
const { items, usedLimit } = await fetchPageWithIngestBackoff(
|
|
376
|
+
async ({ page: pageNum, limit }) => {
|
|
377
|
+
const body = await giteaFetch(
|
|
378
|
+
ctx.config,
|
|
379
|
+
ctx.parsed,
|
|
380
|
+
`${path}${pageSep}limit=${limit}&page=${pageNum}`,
|
|
381
|
+
);
|
|
382
|
+
if (!Array.isArray(body)) {
|
|
383
|
+
throw Object.assign(new Error('Provider returned non-array pull comments list'), {
|
|
384
|
+
forgeError: forgeError(
|
|
385
|
+
ERROR_CODES.UNPARSEABLE_PROVIDER_OUTPUT,
|
|
386
|
+
'Provider returned non-array pull comments list',
|
|
387
|
+
),
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
return body;
|
|
391
|
+
},
|
|
392
|
+
page,
|
|
393
|
+
activeLimit,
|
|
394
|
+
);
|
|
395
|
+
activeLimit = usedLimit;
|
|
396
|
+
entryCount += items.length;
|
|
397
|
+
allComments.push(...items);
|
|
398
|
+
if (items.length < usedLimit) break;
|
|
399
|
+
if (page === MAX_CHECK_PAGES) {
|
|
400
|
+
listTruncated = true;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const body = buildCrCommentsFromGiteaComments(number, allComments);
|
|
405
|
+
if (listTruncated) {
|
|
406
|
+
return buildCrCommentsBody({
|
|
407
|
+
pr_number: body.pr_number,
|
|
408
|
+
comments: body.comments,
|
|
409
|
+
comments_truncated: true,
|
|
410
|
+
comment_count: entryCount,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
return body;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
export async function forgeChanges(ctx, { since }) {
|
|
417
|
+
requireToken();
|
|
418
|
+
const sinceIso = parseSinceObservedAt(since);
|
|
419
|
+
const path = `${repoApiPath(ctx.config, 'pulls')}?state=all&sort=recentupdate`;
|
|
420
|
+
const pageSep = '&';
|
|
421
|
+
let activeLimit = GITEA_PAGE_SIZE;
|
|
422
|
+
const allPulls = [];
|
|
423
|
+
let listTruncated = false;
|
|
424
|
+
let entryCount = 0;
|
|
425
|
+
|
|
426
|
+
for (let page = 1; page <= MAX_CHECK_PAGES; page += 1) {
|
|
427
|
+
const { items, usedLimit } = await fetchPageWithIngestBackoff(
|
|
428
|
+
async ({ page: pageNum, limit }) => {
|
|
429
|
+
const body = await giteaFetch(
|
|
430
|
+
ctx.config,
|
|
431
|
+
ctx.parsed,
|
|
432
|
+
`${path}${pageSep}limit=${limit}&page=${pageNum}`,
|
|
433
|
+
);
|
|
434
|
+
if (!Array.isArray(body)) {
|
|
435
|
+
throw Object.assign(new Error('Provider returned non-array pull list'), {
|
|
436
|
+
forgeError: forgeError(
|
|
437
|
+
ERROR_CODES.UNPARSEABLE_PROVIDER_OUTPUT,
|
|
438
|
+
'Provider returned non-array pull list',
|
|
439
|
+
),
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
return body;
|
|
443
|
+
},
|
|
444
|
+
page,
|
|
445
|
+
activeLimit,
|
|
446
|
+
);
|
|
447
|
+
activeLimit = usedLimit;
|
|
448
|
+
entryCount += items.length;
|
|
449
|
+
allPulls.push(...items);
|
|
450
|
+
if (items.length < usedLimit) break;
|
|
451
|
+
if (page === MAX_CHECK_PAGES) {
|
|
452
|
+
listTruncated = true;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
let body = buildForgeChangesFromGiteaPulls(sinceIso, allPulls, { listTruncated });
|
|
457
|
+
const checkNumbers = new Set();
|
|
458
|
+
for (const event of body.events) {
|
|
459
|
+
if (event.kind === 'pr_opened' || event.kind === 'head_sha_moved') {
|
|
460
|
+
checkNumbers.add(event.pr_number);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
const checkEvents = [];
|
|
465
|
+
for (const number of checkNumbers) {
|
|
466
|
+
const checks = await prChecks(ctx, { number });
|
|
467
|
+
checkEvents.push(buildChecksConclusionObservedEvent(number, checks));
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if (checkEvents.length > 0) {
|
|
471
|
+
body = appendForgeChangeEvents(body, checkEvents, { listTruncated });
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return body;
|
|
475
|
+
}
|
|
476
|
+
|
|
262
477
|
export function providerCapabilities() {
|
|
263
478
|
const check_sources = ['commit_statuses'];
|
|
264
479
|
return {
|
|
@@ -269,7 +484,7 @@ export function providerCapabilities() {
|
|
|
269
484
|
host_binding: 'verified_remote_host',
|
|
270
485
|
pagination: 'supported',
|
|
271
486
|
write_support: true,
|
|
272
|
-
write_commands: ['cr_open'],
|
|
487
|
+
write_commands: ['cr_open', 'status_set'],
|
|
273
488
|
...forgeIngestCapabilityFacts(),
|
|
274
489
|
...checkPaginationCapabilityFacts({
|
|
275
490
|
strategy: 'offset_limit',
|
|
@@ -369,6 +584,97 @@ export async function findOpenPullByHeadBase(ctx, head, base) {
|
|
|
369
584
|
return null;
|
|
370
585
|
}
|
|
371
586
|
|
|
587
|
+
function statusSetIdempotencyScanIncompleteError(pagesScanned, pageSizeUsed) {
|
|
588
|
+
return forgeError(
|
|
589
|
+
ERROR_CODES.IDEMPOTENCY_SCAN_INCOMPLETE,
|
|
590
|
+
'Cannot prove no commit status exists for sha+context within scan limit; retry or set manually',
|
|
591
|
+
null,
|
|
592
|
+
{
|
|
593
|
+
idempotency_scan: {
|
|
594
|
+
pages: pagesScanned,
|
|
595
|
+
max_pages: MAX_OPEN_PULL_IDEMPOTENCY_PAGES,
|
|
596
|
+
page_size: pageSizeUsed,
|
|
597
|
+
},
|
|
598
|
+
},
|
|
599
|
+
);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** Paginated commit-status scan for idempotent status set; fail-closed when scan cap prevents proof of absence. */
|
|
603
|
+
export async function findCommitStatusByContext(ctx, sha, context) {
|
|
604
|
+
requireToken();
|
|
605
|
+
const path = repoApiPath(ctx.config, 'commits', sha, 'statuses');
|
|
606
|
+
const pageSep = path.includes('?') ? '&' : '?';
|
|
607
|
+
let activeLimit = GITEA_PAGE_SIZE;
|
|
608
|
+
let bestMatch = null;
|
|
609
|
+
|
|
610
|
+
for (let page = 1; page <= MAX_OPEN_PULL_IDEMPOTENCY_PAGES; page += 1) {
|
|
611
|
+
const { items, usedLimit } = await fetchPageWithIngestBackoff(
|
|
612
|
+
async ({ page: pageNum, limit }) => {
|
|
613
|
+
const body = await giteaFetch(
|
|
614
|
+
ctx.config,
|
|
615
|
+
ctx.parsed,
|
|
616
|
+
`${path}${pageSep}limit=${limit}&page=${pageNum}`,
|
|
617
|
+
);
|
|
618
|
+
if (!Array.isArray(body)) {
|
|
619
|
+
throw Object.assign(new Error('Provider returned non-array commit status list'), {
|
|
620
|
+
forgeError: forgeError(
|
|
621
|
+
ERROR_CODES.UNPARSEABLE_PROVIDER_OUTPUT,
|
|
622
|
+
'Provider returned non-array commit status list',
|
|
623
|
+
),
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
return body;
|
|
627
|
+
},
|
|
628
|
+
page,
|
|
629
|
+
activeLimit,
|
|
630
|
+
);
|
|
631
|
+
activeLimit = usedLimit;
|
|
632
|
+
|
|
633
|
+
for (const record of items) {
|
|
634
|
+
if (record?.context !== context) continue;
|
|
635
|
+
if (!bestMatch || giteaStatusRecordOrder(record, bestMatch) > 0) {
|
|
636
|
+
bestMatch = record;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
if (items.length < usedLimit) return bestMatch;
|
|
641
|
+
if (page === MAX_OPEN_PULL_IDEMPOTENCY_PAGES) {
|
|
642
|
+
throw Object.assign(new Error('Commit status idempotency scan incomplete'), {
|
|
643
|
+
forgeError: statusSetIdempotencyScanIncompleteError(page, usedLimit),
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return bestMatch;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
export async function statusSet(ctx, args) {
|
|
651
|
+
const parsed = parseStatusSetArgs(args);
|
|
652
|
+
const existing = await findCommitStatusByContext(ctx, parsed.sha, parsed.context);
|
|
653
|
+
if (existing) {
|
|
654
|
+
const existingState = normalizeStatusSetState(existing.status ?? existing.state);
|
|
655
|
+
if (existingState === parsed.state) {
|
|
656
|
+
return buildCommitStatusSetBody(existing, parsed, { reusedExisting: true });
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
const payload = {
|
|
660
|
+
state: parsed.state,
|
|
661
|
+
context: parsed.context,
|
|
662
|
+
};
|
|
663
|
+
if (parsed.description != null) payload.description = parsed.description;
|
|
664
|
+
if (parsed.target_url != null) payload.target_url = parsed.target_url;
|
|
665
|
+
const response = await giteaFetch(
|
|
666
|
+
ctx.config,
|
|
667
|
+
ctx.parsed,
|
|
668
|
+
repoApiPath(ctx.config, 'statuses', parsed.sha),
|
|
669
|
+
{
|
|
670
|
+
method: 'POST',
|
|
671
|
+
headers: { 'Content-Type': 'application/json' },
|
|
672
|
+
body: JSON.stringify(payload),
|
|
673
|
+
},
|
|
674
|
+
);
|
|
675
|
+
return buildCommitStatusSetBody(response, parsed);
|
|
676
|
+
}
|
|
677
|
+
|
|
372
678
|
export async function crOpen(ctx, { head, base, title, body: prBody }) {
|
|
373
679
|
assertGitRef(head, 'head');
|
|
374
680
|
assertGitRef(base, 'base');
|
|
@@ -666,7 +972,16 @@ export function summarizeChecks(statuses) {
|
|
|
666
972
|
export async function mergePlan(ctx, opts) {
|
|
667
973
|
const view = await prView(ctx, opts);
|
|
668
974
|
const checks = await prChecks(ctx, { number: view.pr_number });
|
|
669
|
-
|
|
975
|
+
let mergeOpts = opts;
|
|
976
|
+
if (opts.allowed_paths) {
|
|
977
|
+
try {
|
|
978
|
+
const crFilesBody = await crFiles(ctx, { number: view.pr_number });
|
|
979
|
+
mergeOpts = { ...opts, changed_paths: crFilesBody.changed_paths };
|
|
980
|
+
} catch {
|
|
981
|
+
// Fall back to local git diff in resolveMergePlanPathScope.
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
return buildMergePlanBodyFromFacts(ctx, view, checks, mergeOpts);
|
|
670
985
|
}
|
|
671
986
|
|
|
672
987
|
export async function syncPlan(ctx, remoteName = 'origin') {
|
|
@@ -709,4 +1024,10 @@ export const provider = {
|
|
|
709
1024
|
mergePlan,
|
|
710
1025
|
syncPlan,
|
|
711
1026
|
crOpen,
|
|
1027
|
+
statusSet,
|
|
1028
|
+
whoami,
|
|
1029
|
+
branchProtection,
|
|
1030
|
+
crFiles,
|
|
1031
|
+
crComments,
|
|
1032
|
+
forgeChanges,
|
|
712
1033
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remogram/provider-gitea-api",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.5",
|
|
4
4
|
"description": "Gitea REST API forge provider for remogram",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"node": ">=20"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@remogram/core": "0.1.0-beta.
|
|
25
|
+
"@remogram/core": "0.1.0-beta.5"
|
|
26
26
|
}
|
|
27
27
|
}
|