@theholocron/github-client 1.58.0-alpha.68 → 1.58.0-alpha.70
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/dist/index.d.mts +16 -3
- package/dist/index.mjs +100 -23
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1003,15 +1003,28 @@ interface InstallationAccessToken {
|
|
|
1003
1003
|
* to call the REST API directly.
|
|
1004
1004
|
*/
|
|
1005
1005
|
export declare function createAppJWT(creds: GitHubAppCredentials, now?: number): Promise<string>;
|
|
1006
|
+
/** Options for minting and caching an installation token. */
|
|
1007
|
+
type InstallationTokenOptions = Pick<GitHubClientOptions, "baseUrl" | "fetch"> & {
|
|
1008
|
+
/** Clock in ms since the epoch; injectable for tests. */
|
|
1009
|
+
now?: () => number;
|
|
1010
|
+
};
|
|
1011
|
+
/** Drops every cached installation token (tests, or a rotated App key). */
|
|
1012
|
+
export declare function clearInstallationTokenCache(): void;
|
|
1013
|
+
/** Drops one cached token — call it when GitHub answers 401 for a token that was still cached. */
|
|
1014
|
+
export declare function forgetInstallationToken(creds: GitHubAppCredentials, installationId: number, opts?: InstallationTokenOptions): void;
|
|
1006
1015
|
/**
|
|
1007
1016
|
* Exchanges an App JWT for an installation access token — the credential
|
|
1008
1017
|
* that actually authenticates REST calls, scoped to exactly one
|
|
1009
1018
|
* installation (D10: never a hardcoded org — the caller supplies which
|
|
1010
1019
|
* installation, from the webhook payload that triggered this).
|
|
1020
|
+
*
|
|
1021
|
+
* The token is cached in memory (per app, API host and installation) until
|
|
1022
|
+
* five minutes before it expires; concurrent callers share one mint and a
|
|
1023
|
+
* failure is never cached.
|
|
1011
1024
|
*/
|
|
1012
|
-
export declare function getInstallationAccessToken(creds: GitHubAppCredentials, installationId: number, opts?:
|
|
1025
|
+
export declare function getInstallationAccessToken(creds: GitHubAppCredentials, installationId: number, opts?: InstallationTokenOptions): Promise<InstallationAccessToken>;
|
|
1013
1026
|
/** The whole flow in one call — an already-authenticated `GitHubClient` scoped to one installation. */
|
|
1014
|
-
export declare function createInstallationClient(creds: GitHubAppCredentials, installationId: number, opts?:
|
|
1027
|
+
export declare function createInstallationClient(creds: GitHubAppCredentials, installationId: number, opts?: InstallationTokenOptions): Promise<GitHubClient>;
|
|
1015
1028
|
/** The account an App or one of its installations belongs to. */
|
|
1016
1029
|
interface GitHubAppAccount {
|
|
1017
1030
|
/** The user or organization login. */
|
|
@@ -1382,4 +1395,4 @@ export declare function createGitHubClient(opts: GitHubClientOptions): {
|
|
|
1382
1395
|
};
|
|
1383
1396
|
/** The client returned by `createGitHubClient()`: one group of methods per GitHub API area. */
|
|
1384
1397
|
export type GitHubClient = ReturnType<typeof createGitHubClient>;
|
|
1385
|
-
export type { CheckRunAnnotation, CheckRunAnnotationLevel, CheckRunConclusion, CheckRunOutput, CheckRunStatus, CodeScanningSetupResult, CommitStatusState, CreateCheckRunInput, CreateHookInput, CreatePagesPayload, CreatePullInput, CreatePullRequestInput, CreateReviewComment, CreateReviewInput, GitBlob, GitCommit, GitContents, GitDirectoryEntry, GitHubApp, GitHubAppAccount, GitHubAppCredentials, GitHubAppDelivery, GitHubAppInstallation, GitHubCheckRun, GitHubClientOptions, GitHubCommitStatus, GitHubContents, GitHubEnvironment, GitHubHook, GitHubInstallationWebhookPayload, GitHubIssue, GitHubIssueComment, GitHubIssueField, GitHubIssueFieldOption, GitHubIssueFieldValue, GitHubLabel, GitHubMilestone, GitHubPages, GitHubPublicKey, GitHubPullRequest, GitHubPullRequestCommit, GitHubPullRequestFile, GitHubPullRequestReview, GitHubPullRequestWebhookPayload, GitHubPushWebhookPayload, GitHubRepo, GitHubRuleset, GitHubRulesetDetail, GitHubUser, GitHubUserWithExpiry, GitHubVariable, GitHubWebhookHeaders, GitHubWorkflowRun, GitPull, GitRef, GitTree, GitTreeItem, GraphQLClient, HookScope, InstallationAccessToken, IssueFieldValueInput, IssueSearchParams, IssueTypeColor, IssueTypeInput, ListAppDeliveriesOptions, ListDirectoryOptions, OrgIssueTypeInfo, OrgProperty, OrgSecretInfo, OrgSecretVisibility, PagesBuildStatus, PagesBuildType, PutContentsInput, PutContentsResult, RepoPropertyValue, RepoTeam, ReviewThread, SecretScope, TeamMember, TeamPermission, UpdateCheckRunInput, UpdateHookInput, UpdatePagesPayload, UpdatePullRequestInput, VariableScope, VariableVisibility, WorkflowRunFilter };
|
|
1398
|
+
export type { CheckRunAnnotation, CheckRunAnnotationLevel, CheckRunConclusion, CheckRunOutput, CheckRunStatus, CodeScanningSetupResult, CommitStatusState, CreateCheckRunInput, CreateHookInput, CreatePagesPayload, CreatePullInput, CreatePullRequestInput, CreateReviewComment, CreateReviewInput, GitBlob, GitCommit, GitContents, GitDirectoryEntry, GitHubApp, GitHubAppAccount, GitHubAppCredentials, GitHubAppDelivery, GitHubAppInstallation, GitHubCheckRun, GitHubClientOptions, GitHubCommitStatus, GitHubContents, GitHubEnvironment, GitHubHook, GitHubInstallationWebhookPayload, GitHubIssue, GitHubIssueComment, GitHubIssueField, GitHubIssueFieldOption, GitHubIssueFieldValue, GitHubLabel, GitHubMilestone, GitHubPages, GitHubPublicKey, GitHubPullRequest, GitHubPullRequestCommit, GitHubPullRequestFile, GitHubPullRequestReview, GitHubPullRequestWebhookPayload, GitHubPushWebhookPayload, GitHubRepo, GitHubRuleset, GitHubRulesetDetail, GitHubUser, GitHubUserWithExpiry, GitHubVariable, GitHubWebhookHeaders, GitHubWorkflowRun, GitPull, GitRef, GitTree, GitTreeItem, GraphQLClient, HookScope, InstallationAccessToken, InstallationTokenOptions, IssueFieldValueInput, IssueSearchParams, IssueTypeColor, IssueTypeInput, ListAppDeliveriesOptions, ListDirectoryOptions, OrgIssueTypeInfo, OrgProperty, OrgSecretInfo, OrgSecretVisibility, PagesBuildStatus, PagesBuildType, PutContentsInput, PutContentsResult, RepoPropertyValue, RepoTeam, ReviewThread, SecretScope, TeamMember, TeamPermission, UpdateCheckRunInput, UpdateHookInput, UpdatePagesPayload, UpdatePullRequestInput, VariableScope, VariableVisibility, WorkflowRunFilter };
|
package/dist/index.mjs
CHANGED
|
@@ -161,18 +161,18 @@ function checks(rest) {
|
|
|
161
161
|
};
|
|
162
162
|
}
|
|
163
163
|
// src/environments/environments.ts
|
|
164
|
-
const PAGE_SIZE$
|
|
164
|
+
const PAGE_SIZE$2 = 100;
|
|
165
165
|
/** A stop for a response that never ends; 100 pages is 10,000 environments, far past GitHub's limit. */
|
|
166
|
-
const MAX_PAGES$
|
|
166
|
+
const MAX_PAGES$4 = 100;
|
|
167
167
|
function environments(rest) {
|
|
168
168
|
return {
|
|
169
169
|
/** Every environment, all pages: GitHub lists 30 by default and at most 100 a page. */
|
|
170
170
|
listEnvironments: async (repo) => {
|
|
171
171
|
const all = [];
|
|
172
|
-
for (let page = 1; page <= MAX_PAGES$
|
|
173
|
-
const r = await rest.request(`${repoBase(repo)}/environments?per_page=${PAGE_SIZE$
|
|
172
|
+
for (let page = 1; page <= MAX_PAGES$4; page += 1) {
|
|
173
|
+
const r = await rest.request(`${repoBase(repo)}/environments?per_page=${PAGE_SIZE$2}&page=${page}`);
|
|
174
174
|
all.push(...r.environments);
|
|
175
|
-
if (r.environments.length < PAGE_SIZE$
|
|
175
|
+
if (r.environments.length < PAGE_SIZE$2 || all.length >= r.total_count) break;
|
|
176
176
|
}
|
|
177
177
|
return all;
|
|
178
178
|
},
|
|
@@ -326,7 +326,7 @@ function git(rest) {
|
|
|
326
326
|
/** GitHub's largest page for this listing. */
|
|
327
327
|
const PAGE$2 = 100;
|
|
328
328
|
/** A guard so a runaway listing cannot loop forever. */
|
|
329
|
-
const MAX_PAGES$
|
|
329
|
+
const MAX_PAGES$3 = 50;
|
|
330
330
|
function hooksPath(scope) {
|
|
331
331
|
if (scope.kind === "organization") return `/orgs/${segment(scope.org)}/hooks`;
|
|
332
332
|
const parts = scope.repo.split("/");
|
|
@@ -340,7 +340,7 @@ function hooks(rest) {
|
|
|
340
340
|
listHooks: async (scope) => {
|
|
341
341
|
const path = hooksPath(scope);
|
|
342
342
|
const all = [];
|
|
343
|
-
for (let page = 1; page <= MAX_PAGES$
|
|
343
|
+
for (let page = 1; page <= MAX_PAGES$3; page++) {
|
|
344
344
|
const batch = await rest.request(path, { query: {
|
|
345
345
|
per_page: String(PAGE$2),
|
|
346
346
|
page: String(page)
|
|
@@ -515,10 +515,29 @@ function issues(rest) {
|
|
|
515
515
|
}
|
|
516
516
|
};
|
|
517
517
|
}
|
|
518
|
+
// src/paginate/paginate.ts
|
|
519
|
+
const PAGE_SIZE$1 = 100;
|
|
520
|
+
/** A stop for a response that never ends; 100 pages is 10,000 items, far past what these lists hold. */
|
|
521
|
+
const MAX_PAGES$2 = 100;
|
|
522
|
+
/**
|
|
523
|
+
* Reads a GitHub list page by page (`per_page=100&page=N`) until a short page, the reported total
|
|
524
|
+
* or the page cap, and returns every item. GitHub's default is 30 items a page, so a single
|
|
525
|
+
* request silently drops the rest.
|
|
526
|
+
*/
|
|
527
|
+
async function readAllPages(read) {
|
|
528
|
+
const all = [];
|
|
529
|
+
for (let page = 1; page <= MAX_PAGES$2; page += 1) {
|
|
530
|
+
const batch = await read(`per_page=${PAGE_SIZE$1}&page=${page}`);
|
|
531
|
+
all.push(...batch.items);
|
|
532
|
+
if (batch.items.length < PAGE_SIZE$1 || batch.total !== void 0 && all.length >= batch.total) break;
|
|
533
|
+
}
|
|
534
|
+
return all;
|
|
535
|
+
}
|
|
518
536
|
// src/labels/labels.ts
|
|
519
537
|
function labels(rest) {
|
|
520
538
|
return {
|
|
521
|
-
|
|
539
|
+
/** Every label, all pages. */
|
|
540
|
+
listLabels: (repo) => readAllPages(async (query) => ({ items: await rest.request(`${repoBase(repo)}/labels?${query}`) })),
|
|
522
541
|
createLabel: (repo, body) => rest.request(`${repoBase(repo)}/labels`, {
|
|
523
542
|
method: "POST",
|
|
524
543
|
body
|
|
@@ -789,14 +808,33 @@ function scopeBase$1(repo, scope) {
|
|
|
789
808
|
}
|
|
790
809
|
function secrets(rest) {
|
|
791
810
|
return {
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
811
|
+
/** Every secret name in the scope, all pages: GitHub lists 30 by default and at most 100 a page. */
|
|
812
|
+
listSecrets: (repo, scope) => readAllPages(async (query) => {
|
|
813
|
+
const r = await rest.request(`${scopeBase$1(repo, scope)}/secrets?${query}`);
|
|
814
|
+
return {
|
|
815
|
+
items: r.secrets,
|
|
816
|
+
total: r.total_count
|
|
817
|
+
};
|
|
818
|
+
}).then((all) => all.map((s) => s.name)),
|
|
819
|
+
/** The organization's secrets with their visibility, names only, all pages. */
|
|
820
|
+
listOrgSecretInfo: (org) => readAllPages(async (query) => {
|
|
821
|
+
const r = await rest.request(`/orgs/${org}/actions/secrets?${query}`);
|
|
822
|
+
return {
|
|
823
|
+
items: r.secrets,
|
|
824
|
+
total: r.total_count
|
|
825
|
+
};
|
|
826
|
+
}).then((all) => all.map((s) => ({
|
|
795
827
|
name: s.name,
|
|
796
828
|
visibility: s.visibility
|
|
797
829
|
}))),
|
|
798
|
-
/** The names of the repositories that can read one organization secret of visibility `selected
|
|
799
|
-
listSelectedRepositories: (org, name) =>
|
|
830
|
+
/** The names of the repositories that can read one organization secret of visibility `selected`, all pages. */
|
|
831
|
+
listSelectedRepositories: (org, name) => readAllPages(async (query) => {
|
|
832
|
+
const r = await rest.request(`/orgs/${org}/actions/secrets/${name}/repositories?${query}`);
|
|
833
|
+
return {
|
|
834
|
+
items: r.repositories,
|
|
835
|
+
total: r.total_count
|
|
836
|
+
};
|
|
837
|
+
}).then((all) => all.map((repo) => repo.name)),
|
|
800
838
|
getPublicKey: (repo, scope) => rest.request(`${scopeBase$1(repo, scope)}/secrets/public-key`),
|
|
801
839
|
putSecret: (repo, scope, name, body) => rest.request(`${scopeBase$1(repo, scope)}/secrets/${name}`, {
|
|
802
840
|
method: "PUT",
|
|
@@ -916,7 +954,7 @@ function teams(rest) {
|
|
|
916
954
|
return all;
|
|
917
955
|
},
|
|
918
956
|
/** The teams that have access to a repository, with each team's permission there (read-only). */
|
|
919
|
-
listForRepo: (repo) => rest.request(`${repoBase(repo)}/teams
|
|
957
|
+
listForRepo: (repo) => readAllPages(async (query) => ({ items: await rest.request(`${repoBase(repo)}/teams?${query}`) })),
|
|
920
958
|
addRepo: (org, teamSlug, owner, repo, permission) => rest.request(`/orgs/${org}/teams/${teamSlug}/repos/${owner}/${repo}`, {
|
|
921
959
|
method: "PUT",
|
|
922
960
|
body: { permission }
|
|
@@ -1170,14 +1208,27 @@ async function createAppJWT(creds, now = Date.now()) {
|
|
|
1170
1208
|
const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", key, new TextEncoder().encode(signingInput));
|
|
1171
1209
|
return `${signingInput}.${base64UrlEncode(new Uint8Array(signature))}`;
|
|
1172
1210
|
}
|
|
1173
|
-
/**
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1211
|
+
/** A cached token is dropped this long before GitHub says it expires. */
|
|
1212
|
+
const REFRESH_BEFORE_EXPIRY_MS = 3e5;
|
|
1213
|
+
const tokenCache = /* @__PURE__ */ new Map();
|
|
1214
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
1215
|
+
/** Key by app, API host and installation only — never the private key. */
|
|
1216
|
+
function cacheKey(creds, installationId, opts) {
|
|
1217
|
+
return `${creds.appId}|${opts.baseUrl ?? "https://api.github.com"}|${installationId}`;
|
|
1218
|
+
}
|
|
1219
|
+
/** Drops every cached installation token (tests, or a rotated App key). */
|
|
1220
|
+
function clearInstallationTokenCache() {
|
|
1221
|
+
tokenCache.clear();
|
|
1222
|
+
inFlight.clear();
|
|
1223
|
+
}
|
|
1224
|
+
/** Drops one cached token — call it when GitHub answers 401 for a token that was still cached. */
|
|
1225
|
+
function forgetInstallationToken(creds, installationId, opts = {}) {
|
|
1226
|
+
const key = cacheKey(creds, installationId, opts);
|
|
1227
|
+
tokenCache.delete(key);
|
|
1228
|
+
inFlight.delete(key);
|
|
1229
|
+
}
|
|
1230
|
+
async function mintInstallationAccessToken(creds, installationId, opts) {
|
|
1231
|
+
const jwt = await createAppJWT(creds, opts.now?.());
|
|
1181
1232
|
const baseUrl = opts.baseUrl ?? "https://api.github.com";
|
|
1182
1233
|
const res = await (opts.fetch ?? globalThis.fetch)(`${baseUrl}/app/installations/${installationId}/access_tokens`, {
|
|
1183
1234
|
method: "POST",
|
|
@@ -1197,6 +1248,32 @@ async function getInstallationAccessToken(creds, installationId, opts = {}) {
|
|
|
1197
1248
|
expiresAt: json.expires_at
|
|
1198
1249
|
};
|
|
1199
1250
|
}
|
|
1251
|
+
/**
|
|
1252
|
+
* Exchanges an App JWT for an installation access token — the credential
|
|
1253
|
+
* that actually authenticates REST calls, scoped to exactly one
|
|
1254
|
+
* installation (D10: never a hardcoded org — the caller supplies which
|
|
1255
|
+
* installation, from the webhook payload that triggered this).
|
|
1256
|
+
*
|
|
1257
|
+
* The token is cached in memory (per app, API host and installation) until
|
|
1258
|
+
* five minutes before it expires; concurrent callers share one mint and a
|
|
1259
|
+
* failure is never cached.
|
|
1260
|
+
*/
|
|
1261
|
+
async function getInstallationAccessToken(creds, installationId, opts = {}) {
|
|
1262
|
+
const key = cacheKey(creds, installationId, opts);
|
|
1263
|
+
const now = opts.now?.() ?? Date.now();
|
|
1264
|
+
const cached = tokenCache.get(key);
|
|
1265
|
+
if (cached && Date.parse(cached.expiresAt) - REFRESH_BEFORE_EXPIRY_MS > now) return cached;
|
|
1266
|
+
const pending = inFlight.get(key);
|
|
1267
|
+
if (pending) return pending;
|
|
1268
|
+
const mint = mintInstallationAccessToken(creds, installationId, opts).then((token) => {
|
|
1269
|
+
if (inFlight.get(key) === mint) tokenCache.set(key, token);
|
|
1270
|
+
return token;
|
|
1271
|
+
}).finally(() => {
|
|
1272
|
+
if (inFlight.get(key) === mint) inFlight.delete(key);
|
|
1273
|
+
});
|
|
1274
|
+
inFlight.set(key, mint);
|
|
1275
|
+
return mint;
|
|
1276
|
+
}
|
|
1200
1277
|
/** The whole flow in one call — an already-authenticated `GitHubClient` scoped to one installation. */
|
|
1201
1278
|
async function createInstallationClient(creds, installationId, opts = {}) {
|
|
1202
1279
|
const { token } = await getInstallationAccessToken(creds, installationId, opts);
|
|
@@ -1431,4 +1508,4 @@ function createGitHubClient(opts) {
|
|
|
1431
1508
|
workflows: workflows(rest)
|
|
1432
1509
|
};
|
|
1433
1510
|
}
|
|
1434
|
-
export { MAX_CHECK_RUN_ANNOTATIONS, createAppJWT, createGitHubClient, createInstallationClient, getApp, getInstallationAccessToken, getRepoInstallation, listAppDeliveries, listAppInstallations, parseGitHubWebhookHeaders, parseTokenExpiration, redeliverAppDelivery, suspendAppInstallation, unsuspendAppInstallation, verifyGitHubWebhookSignature };
|
|
1511
|
+
export { MAX_CHECK_RUN_ANNOTATIONS, clearInstallationTokenCache, createAppJWT, createGitHubClient, createInstallationClient, forgetInstallationToken, getApp, getInstallationAccessToken, getRepoInstallation, listAppDeliveries, listAppInstallations, parseGitHubWebhookHeaders, parseTokenExpiration, redeliverAppDelivery, suspendAppInstallation, unsuspendAppInstallation, verifyGitHubWebhookSignature };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theholocron/github-client",
|
|
3
|
-
"version": "1.58.0-alpha.
|
|
3
|
+
"version": "1.58.0-alpha.70",
|
|
4
4
|
"description": "A TypeScript client for the GitHub REST API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"github",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"dist"
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@theholocron/http-client": "^1.58.0-alpha.
|
|
35
|
+
"@theholocron/http-client": "^1.58.0-alpha.70",
|
|
36
36
|
"@theholocron/observability": "^0.7.0-alpha.9"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|