@remogram/provider-github-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 +256 -3
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -37,6 +37,21 @@ import {
|
|
|
37
37
|
orderOpenPullNumbers,
|
|
38
38
|
buildOpenPullListMeta,
|
|
39
39
|
githubOpenPullSortQuery,
|
|
40
|
+
buildProviderIdentityFromGitHubUser,
|
|
41
|
+
buildBranchProtectionFromGitHubProtection,
|
|
42
|
+
buildCrFilesBody,
|
|
43
|
+
buildCrFilesFromGiteaFiles,
|
|
44
|
+
buildCrCommentsBody,
|
|
45
|
+
buildCrCommentsFromGiteaComments,
|
|
46
|
+
parseSinceObservedAt,
|
|
47
|
+
buildForgeChangesFromGiteaPulls,
|
|
48
|
+
buildChecksConclusionObservedEvent,
|
|
49
|
+
appendForgeChangeEvents,
|
|
50
|
+
buildCommitStatusSetBody,
|
|
51
|
+
parseStatusSetArgs,
|
|
52
|
+
normalizeStatusSetState,
|
|
53
|
+
statusSetIdempotencyScanCapabilityFacts,
|
|
54
|
+
MAX_OPEN_PULL_IDEMPOTENCY_PAGES,
|
|
40
55
|
} from '@remogram/core';
|
|
41
56
|
|
|
42
57
|
const PUBLIC_GITHUB_HOST = 'github.com';
|
|
@@ -70,9 +85,21 @@ const AUTH_CAPABILITIES = [
|
|
|
70
85
|
'pr_checks',
|
|
71
86
|
'merge_plan',
|
|
72
87
|
'sync_plan',
|
|
88
|
+
'status_set',
|
|
89
|
+
'whoami',
|
|
90
|
+
'branch_protection',
|
|
91
|
+
'cr_files',
|
|
92
|
+
'cr_comments',
|
|
93
|
+
'forge_changes',
|
|
73
94
|
];
|
|
74
95
|
|
|
75
|
-
const STRUCTURED_COMMANDS = apiProviderCommands(
|
|
96
|
+
const STRUCTURED_COMMANDS = apiProviderCommands({
|
|
97
|
+
branchProtectionImplemented: true,
|
|
98
|
+
crFilesImplemented: true,
|
|
99
|
+
crCommentsImplemented: true,
|
|
100
|
+
forgeChangesImplemented: true,
|
|
101
|
+
statusSetImplemented: true,
|
|
102
|
+
});
|
|
76
103
|
|
|
77
104
|
export function githubToken() {
|
|
78
105
|
if (process.env.GITHUB_TOKEN) return { token: process.env.GITHUB_TOKEN, env: 'GITHUB_TOKEN' };
|
|
@@ -379,8 +406,10 @@ export function providerCapabilities() {
|
|
|
379
406
|
mergeability_confidence: 'derived',
|
|
380
407
|
host_binding: 'verified_remote_host',
|
|
381
408
|
pagination: 'supported',
|
|
382
|
-
write_support:
|
|
409
|
+
write_support: true,
|
|
410
|
+
write_commands: ['status_set'],
|
|
383
411
|
...forgeIngestCapabilityFacts(),
|
|
412
|
+
...statusSetIdempotencyScanCapabilityFacts(),
|
|
384
413
|
...checkPaginationCapabilityFacts({
|
|
385
414
|
strategy: 'link_header',
|
|
386
415
|
pageSizeParam: 'per_page',
|
|
@@ -530,7 +559,103 @@ export async function prChecks(ctx, opts) {
|
|
|
530
559
|
export async function mergePlan(ctx, opts) {
|
|
531
560
|
const view = await prView(ctx, opts);
|
|
532
561
|
const checks = await prChecks(ctx, { number: view.pr_number });
|
|
533
|
-
|
|
562
|
+
let mergeOpts = opts;
|
|
563
|
+
if (opts.allowed_paths) {
|
|
564
|
+
try {
|
|
565
|
+
const crFilesBody = await crFiles(ctx, { number: view.pr_number });
|
|
566
|
+
mergeOpts = { ...opts, changed_paths: crFilesBody.changed_paths };
|
|
567
|
+
} catch {
|
|
568
|
+
// Fall back to local git diff in resolveMergePlanPathScope.
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
return buildMergePlanBodyFromFacts(ctx, view, checks, mergeOpts);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
export async function crFiles(ctx, { number }) {
|
|
575
|
+
if (number == null) {
|
|
576
|
+
throw Object.assign(new Error('--number required'), {
|
|
577
|
+
forgeError: forgeError(ERROR_CODES.INVALID_ARGS, 'Provide --number for PR changed paths'),
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
const base = apiBase(ctx.config, ctx.parsed);
|
|
581
|
+
const trustedOrigin = new URL(base).origin;
|
|
582
|
+
const { token } = requireToken();
|
|
583
|
+
const startUrl = `${base}${repoApiPath(ctx.config, 'pulls', number, 'files')}?per_page=${DEFAULT_CHECK_STATUS_PAGE_SIZE}`;
|
|
584
|
+
const { items, truncated, walked_count } = await paginateGitHubLinkPages({
|
|
585
|
+
trustedOrigin,
|
|
586
|
+
startUrl,
|
|
587
|
+
token,
|
|
588
|
+
mapPageItems: (body) => (Array.isArray(body) ? body : []),
|
|
589
|
+
});
|
|
590
|
+
const body = buildCrFilesFromGiteaFiles(number, items);
|
|
591
|
+
if (truncated) {
|
|
592
|
+
return buildCrFilesBody({
|
|
593
|
+
pr_number: body.pr_number,
|
|
594
|
+
changed_paths: body.changed_paths,
|
|
595
|
+
paths_truncated: true,
|
|
596
|
+
path_count: walked_count,
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
return body;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
export async function crComments(ctx, { number }) {
|
|
603
|
+
if (number == null) {
|
|
604
|
+
throw Object.assign(new Error('--number required'), {
|
|
605
|
+
forgeError: forgeError(ERROR_CODES.INVALID_ARGS, 'Provide --number for PR review comments'),
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
const base = apiBase(ctx.config, ctx.parsed);
|
|
609
|
+
const trustedOrigin = new URL(base).origin;
|
|
610
|
+
const { token } = requireToken();
|
|
611
|
+
const startUrl = `${base}${repoApiPath(ctx.config, 'pulls', number, 'comments')}?per_page=${DEFAULT_CHECK_STATUS_PAGE_SIZE}`;
|
|
612
|
+
const { items, truncated, walked_count } = await paginateGitHubLinkPages({
|
|
613
|
+
trustedOrigin,
|
|
614
|
+
startUrl,
|
|
615
|
+
token,
|
|
616
|
+
mapPageItems: (body) => (Array.isArray(body) ? body : []),
|
|
617
|
+
});
|
|
618
|
+
const body = buildCrCommentsFromGiteaComments(number, items);
|
|
619
|
+
if (truncated) {
|
|
620
|
+
return buildCrCommentsBody({
|
|
621
|
+
pr_number: body.pr_number,
|
|
622
|
+
comments: body.comments,
|
|
623
|
+
comments_truncated: true,
|
|
624
|
+
comment_count: walked_count,
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
return body;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
export async function forgeChanges(ctx, { since }) {
|
|
631
|
+
requireToken();
|
|
632
|
+
const sinceIso = parseSinceObservedAt(since);
|
|
633
|
+
const base = apiBase(ctx.config, ctx.parsed);
|
|
634
|
+
const trustedOrigin = new URL(base).origin;
|
|
635
|
+
const { token } = requireToken();
|
|
636
|
+
const startUrl = `${base}${repoApiPath(ctx.config, 'pulls')}?state=all&sort=updated&direction=desc&per_page=${DEFAULT_CHECK_STATUS_PAGE_SIZE}`;
|
|
637
|
+
const { items, truncated } = await paginateGitHubLinkPages({
|
|
638
|
+
trustedOrigin,
|
|
639
|
+
startUrl,
|
|
640
|
+
token,
|
|
641
|
+
mapPageItems: (body) => (Array.isArray(body) ? body : []),
|
|
642
|
+
});
|
|
643
|
+
let body = buildForgeChangesFromGiteaPulls(sinceIso, items, { listTruncated: truncated });
|
|
644
|
+
const checkNumbers = new Set();
|
|
645
|
+
for (const event of body.events) {
|
|
646
|
+
if (event.kind === 'pr_opened' || event.kind === 'head_sha_moved') {
|
|
647
|
+
checkNumbers.add(event.pr_number);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
const checkEvents = [];
|
|
651
|
+
for (const number of checkNumbers) {
|
|
652
|
+
const checks = await prChecks(ctx, { number });
|
|
653
|
+
checkEvents.push(buildChecksConclusionObservedEvent(number, checks));
|
|
654
|
+
}
|
|
655
|
+
if (checkEvents.length > 0) {
|
|
656
|
+
body = appendForgeChangeEvents(body, checkEvents, { listTruncated: truncated });
|
|
657
|
+
}
|
|
658
|
+
return body;
|
|
534
659
|
}
|
|
535
660
|
|
|
536
661
|
const GITHUB_OPEN_PULL_COMPLIANT_MAX =
|
|
@@ -802,6 +927,128 @@ export async function syncPlan(ctx, remoteName = 'origin') {
|
|
|
802
927
|
};
|
|
803
928
|
}
|
|
804
929
|
|
|
930
|
+
export async function whoami(ctx) {
|
|
931
|
+
const base = apiBase(ctx.config, ctx.parsed);
|
|
932
|
+
const { token } = requireToken();
|
|
933
|
+
const url = `${base}/user`;
|
|
934
|
+
const { body, headers } = await fetchJsonWithMeta(url, {
|
|
935
|
+
headers: authHeaders(token),
|
|
936
|
+
});
|
|
937
|
+
const scopeHeader =
|
|
938
|
+
headers?.get?.('x-oauth-scopes') ?? headers?.get?.('X-OAuth-Scopes') ?? null;
|
|
939
|
+
return buildProviderIdentityFromGitHubUser(body, scopeHeader);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function githubStatusRecordOrder(a, b) {
|
|
943
|
+
const aUpdated = Date.parse(a?.updated_at ?? '') || 0;
|
|
944
|
+
const bUpdated = Date.parse(b?.updated_at ?? '') || 0;
|
|
945
|
+
if (aUpdated !== bUpdated) return aUpdated - bUpdated;
|
|
946
|
+
const aId = Number(a.id) || 0;
|
|
947
|
+
const bId = Number(b.id) || 0;
|
|
948
|
+
return aId - bId;
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
function statusSetIdempotencyScanIncompleteError(pagesScanned, pageSizeUsed) {
|
|
952
|
+
return forgeError(
|
|
953
|
+
ERROR_CODES.IDEMPOTENCY_SCAN_INCOMPLETE,
|
|
954
|
+
'Cannot prove no commit status exists for sha+context within scan limit; retry or set manually',
|
|
955
|
+
null,
|
|
956
|
+
{
|
|
957
|
+
idempotency_scan: {
|
|
958
|
+
pages: pagesScanned,
|
|
959
|
+
max_pages: MAX_OPEN_PULL_IDEMPOTENCY_PAGES,
|
|
960
|
+
page_size: pageSizeUsed,
|
|
961
|
+
},
|
|
962
|
+
},
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/** Paginated commit-status scan for idempotent status set; fail-closed when scan cap prevents proof of absence. */
|
|
967
|
+
export async function findCommitStatusByContext(ctx, sha, context) {
|
|
968
|
+
const { token } = requireToken();
|
|
969
|
+
const base = apiBase(ctx.config, ctx.parsed);
|
|
970
|
+
const trustedOrigin = new URL(base).origin;
|
|
971
|
+
const path = repoApiPath(ctx.config, 'commits', sha, 'statuses');
|
|
972
|
+
const pageQuery = `${path.includes('?') ? '&' : '?'}per_page=${DEFAULT_CHECK_STATUS_PAGE_SIZE}`;
|
|
973
|
+
let url = `${base}${path}${pageQuery}`;
|
|
974
|
+
let bestMatch = null;
|
|
975
|
+
const activeLimit = DEFAULT_CHECK_STATUS_PAGE_SIZE;
|
|
976
|
+
|
|
977
|
+
for (let page = 1; page <= MAX_OPEN_PULL_IDEMPOTENCY_PAGES; page += 1) {
|
|
978
|
+
const { body, headers } = await fetchJsonWithMeta(url, {
|
|
979
|
+
headers: authHeaders(token),
|
|
980
|
+
});
|
|
981
|
+
const items = Array.isArray(body) ? body : [];
|
|
982
|
+
for (const record of items) {
|
|
983
|
+
if (record?.context !== context) continue;
|
|
984
|
+
if (!bestMatch || githubStatusRecordOrder(record, bestMatch) > 0) {
|
|
985
|
+
bestMatch = record;
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
const linkPage = resolveGitHubLinkNextPage({
|
|
989
|
+
trustedOrigin,
|
|
990
|
+
currentUrl: url,
|
|
991
|
+
linkHeader: headers?.get?.('link') ?? headers?.get?.('Link'),
|
|
992
|
+
pageIndex: page - 1,
|
|
993
|
+
maxPages: MAX_OPEN_PULL_IDEMPOTENCY_PAGES,
|
|
994
|
+
});
|
|
995
|
+
if (items.length < activeLimit) {
|
|
996
|
+
return bestMatch;
|
|
997
|
+
}
|
|
998
|
+
if (page === MAX_OPEN_PULL_IDEMPOTENCY_PAGES) {
|
|
999
|
+
throw Object.assign(new Error('Commit status idempotency scan incomplete'), {
|
|
1000
|
+
forgeError: statusSetIdempotencyScanIncompleteError(page, activeLimit),
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
if (!linkPage.nextUrl) {
|
|
1004
|
+
return bestMatch;
|
|
1005
|
+
}
|
|
1006
|
+
url = withPerPageParam(linkPage.nextUrl, activeLimit);
|
|
1007
|
+
}
|
|
1008
|
+
return bestMatch;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
export async function statusSet(ctx, args) {
|
|
1012
|
+
const parsed = parseStatusSetArgs(args);
|
|
1013
|
+
const existing = await findCommitStatusByContext(ctx, parsed.sha, parsed.context);
|
|
1014
|
+
if (existing) {
|
|
1015
|
+
const existingState = normalizeStatusSetState(existing.state ?? existing.status);
|
|
1016
|
+
if (existingState === parsed.state) {
|
|
1017
|
+
return buildCommitStatusSetBody(existing, parsed, { reusedExisting: true });
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
const payload = {
|
|
1021
|
+
state: parsed.state,
|
|
1022
|
+
context: parsed.context,
|
|
1023
|
+
};
|
|
1024
|
+
if (parsed.description != null) payload.description = parsed.description;
|
|
1025
|
+
if (parsed.target_url != null) payload.target_url = parsed.target_url;
|
|
1026
|
+
const response = await githubFetch(ctx.config, ctx.parsed, repoApiPath(ctx.config, 'statuses', parsed.sha), {
|
|
1027
|
+
method: 'POST',
|
|
1028
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1029
|
+
body: JSON.stringify(payload),
|
|
1030
|
+
});
|
|
1031
|
+
return buildCommitStatusSetBody(response, parsed);
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
export async function branchProtection(ctx, { branchRef }) {
|
|
1035
|
+
assertGitRef(branchRef, '--branch-ref');
|
|
1036
|
+
const base = apiBase(ctx.config, ctx.parsed);
|
|
1037
|
+
const { token } = requireToken();
|
|
1038
|
+
const url = `${base}${repoApiPath(ctx.config, 'branches', branchRef, 'protection')}`;
|
|
1039
|
+
try {
|
|
1040
|
+
const { body } = await fetchJsonWithMeta(url, {
|
|
1041
|
+
headers: authHeaders(token),
|
|
1042
|
+
});
|
|
1043
|
+
return buildBranchProtectionFromGitHubProtection(branchRef, body);
|
|
1044
|
+
} catch (err) {
|
|
1045
|
+
if (err?.status === 404) {
|
|
1046
|
+
return buildBranchProtectionFromGitHubProtection(branchRef, null);
|
|
1047
|
+
}
|
|
1048
|
+
throw err;
|
|
1049
|
+
}
|
|
1050
|
+
}
|
|
1051
|
+
|
|
805
1052
|
export const provider = {
|
|
806
1053
|
id: 'github-api',
|
|
807
1054
|
providerCapabilities,
|
|
@@ -814,4 +1061,10 @@ export const provider = {
|
|
|
814
1061
|
prChecks,
|
|
815
1062
|
mergePlan,
|
|
816
1063
|
syncPlan,
|
|
1064
|
+
whoami,
|
|
1065
|
+
branchProtection,
|
|
1066
|
+
crFiles,
|
|
1067
|
+
crComments,
|
|
1068
|
+
forgeChanges,
|
|
1069
|
+
statusSet,
|
|
817
1070
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remogram/provider-github-api",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.5",
|
|
4
4
|
"description": "GitHub API 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
|
}
|