@shivaedev/github 0.9.0 → 0.11.0
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/CHANGELOG.md +36 -0
- package/README.md +115 -7
- package/dist/GitHub.d.ts +279 -19
- package/dist/GitHub.d.ts.map +1 -1
- package/dist/GitHub.js +22 -10
- package/dist/GitHub.js.map +1 -1
- package/dist/adapters/manualRedirects.d.ts +4 -0
- package/dist/adapters/manualRedirects.d.ts.map +1 -0
- package/dist/adapters/manualRedirects.js +6 -0
- package/dist/adapters/manualRedirects.js.map +1 -0
- package/dist/adapters/mergeResponseHeaders.d.ts +3 -0
- package/dist/adapters/mergeResponseHeaders.d.ts.map +1 -0
- package/dist/adapters/mergeResponseHeaders.js +5 -0
- package/dist/adapters/mergeResponseHeaders.js.map +1 -0
- package/dist/adapters/resolveApiUrl.d.ts +2 -0
- package/dist/adapters/resolveApiUrl.d.ts.map +1 -0
- package/dist/adapters/resolveApiUrl.js +6 -0
- package/dist/adapters/resolveApiUrl.js.map +1 -0
- package/dist/adapters/signAppJwt.d.ts +5 -0
- package/dist/adapters/signAppJwt.d.ts.map +1 -0
- package/dist/adapters/signAppJwt.js +15 -0
- package/dist/adapters/signAppJwt.js.map +1 -0
- package/dist/adapters/verifySignature.d.ts +4 -0
- package/dist/adapters/verifySignature.d.ts.map +1 -0
- package/dist/adapters/verifySignature.js +13 -0
- package/dist/adapters/verifySignature.js.map +1 -0
- package/dist/adapters/zipEntries.d.ts +17 -0
- package/dist/adapters/zipEntries.d.ts.map +1 -0
- package/dist/adapters/zipEntries.js +57 -0
- package/dist/adapters/zipEntries.js.map +1 -0
- package/dist/appJwt.d.ts +2 -3
- package/dist/appJwt.d.ts.map +1 -1
- package/dist/appJwt.js +3 -12
- package/dist/appJwt.js.map +1 -1
- package/dist/appTokens.d.ts.map +1 -1
- package/dist/appTokens.js +2 -1
- package/dist/appTokens.js.map +1 -1
- package/dist/artifactErrors.d.ts +34 -0
- package/dist/artifactErrors.d.ts.map +1 -0
- package/dist/artifactErrors.js +27 -0
- package/dist/artifactErrors.js.map +1 -0
- package/dist/artifactExtraction.d.ts +12 -0
- package/dist/artifactExtraction.d.ts.map +1 -0
- package/dist/artifactExtraction.js +40 -0
- package/dist/artifactExtraction.js.map +1 -0
- package/dist/artifactPaths.d.ts +4 -0
- package/dist/artifactPaths.d.ts.map +1 -0
- package/dist/artifactPaths.js +35 -0
- package/dist/artifactPaths.js.map +1 -0
- package/dist/artifacts.d.ts +13 -8
- package/dist/artifacts.d.ts.map +1 -1
- package/dist/artifacts.js +18 -10
- package/dist/artifacts.js.map +1 -1
- package/dist/authenticatedRequest.d.ts +5 -5
- package/dist/authenticatedRequest.d.ts.map +1 -1
- package/dist/authenticatedRequest.js +6 -5
- package/dist/authenticatedRequest.js.map +1 -1
- package/dist/cache.d.ts.map +1 -1
- package/dist/cache.js +2 -1
- package/dist/cache.js.map +1 -1
- package/dist/checkRuns.d.ts +4 -4
- package/dist/checkRuns.d.ts.map +1 -1
- package/dist/checkRuns.js +5 -3
- package/dist/checkRuns.js.map +1 -1
- package/dist/errors.d.ts +8 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +6 -0
- package/dist/errors.js.map +1 -1
- package/dist/gitSchemas.d.ts +195 -0
- package/dist/gitSchemas.d.ts.map +1 -0
- package/dist/gitSchemas.js +45 -0
- package/dist/gitSchemas.js.map +1 -0
- package/dist/installationClients.d.ts +291 -15
- package/dist/installationClients.d.ts.map +1 -1
- package/dist/installationClients.js +14 -5
- package/dist/installationClients.js.map +1 -1
- package/dist/markdown.d.ts +1 -0
- package/dist/markdown.d.ts.map +1 -1
- package/dist/markdown.js +3 -0
- package/dist/markdown.js.map +1 -1
- package/dist/markdownRows.d.ts +8 -1
- package/dist/markdownRows.d.ts.map +1 -1
- package/dist/markdownRows.js +39 -8
- package/dist/markdownRows.js.map +1 -1
- package/dist/pagedLists.d.ts +15 -0
- package/dist/pagedLists.d.ts.map +1 -0
- package/dist/pagedLists.js +16 -0
- package/dist/pagedLists.js.map +1 -0
- package/dist/repositoryAddress.d.ts +11 -0
- package/dist/repositoryAddress.d.ts.map +1 -0
- package/dist/repositoryAddress.js +23 -0
- package/dist/repositoryAddress.js.map +1 -0
- package/dist/repositoryReads.d.ts +262 -0
- package/dist/repositoryReads.d.ts.map +1 -0
- package/dist/repositoryReads.js +30 -0
- package/dist/repositoryReads.js.map +1 -0
- package/dist/repositorySchemas.d.ts +73 -0
- package/dist/repositorySchemas.d.ts.map +1 -0
- package/dist/repositorySchemas.js +38 -0
- package/dist/repositorySchemas.js.map +1 -0
- package/dist/representations.d.ts +5 -5
- package/dist/representations.d.ts.map +1 -1
- package/dist/representations.js +3 -2
- package/dist/representations.js.map +1 -1
- package/dist/requests.d.ts +10 -6
- package/dist/requests.d.ts.map +1 -1
- package/dist/requests.js +11 -6
- package/dist/requests.js.map +1 -1
- package/dist/status.d.ts +2 -2
- package/dist/status.d.ts.map +1 -1
- package/dist/status.js +2 -3
- package/dist/status.js.map +1 -1
- package/dist/stickyComments.d.ts +1 -1
- package/dist/verifyWebhook.d.ts +2 -2
- package/dist/verifyWebhook.d.ts.map +1 -1
- package/dist/verifyWebhook.js +3 -10
- package/dist/verifyWebhook.js.map +1 -1
- package/dist/viewerRepositoryAccess.d.ts +46 -0
- package/dist/viewerRepositoryAccess.d.ts.map +1 -0
- package/dist/viewerRepositoryAccess.js +54 -0
- package/dist/viewerRepositoryAccess.js.map +1 -0
- package/package.json +13 -6
- package/src/GitHub.ts +34 -15
- package/src/adapters/manualRedirects.ts +6 -0
- package/src/adapters/mergeResponseHeaders.ts +8 -0
- package/src/adapters/resolveApiUrl.ts +5 -0
- package/src/adapters/signAppJwt.ts +16 -0
- package/src/adapters/verifySignature.ts +13 -0
- package/src/adapters/zipEntries.ts +69 -0
- package/src/appJwt.ts +3 -12
- package/src/appTokens.ts +2 -1
- package/src/artifactErrors.ts +26 -0
- package/src/artifactExtraction.ts +62 -0
- package/src/artifactPaths.ts +36 -0
- package/src/artifacts.ts +35 -40
- package/src/authenticatedRequest.ts +8 -9
- package/src/cache.ts +2 -1
- package/src/checkRuns.ts +5 -5
- package/src/errors.ts +6 -0
- package/src/gitSchemas.ts +52 -0
- package/src/installationClients.ts +18 -12
- package/src/markdown.ts +4 -0
- package/src/markdownRows.ts +54 -9
- package/src/pagedLists.ts +40 -0
- package/src/repositoryAddress.ts +32 -0
- package/src/repositoryReads.ts +62 -0
- package/src/repositorySchemas.ts +43 -0
- package/src/representations.ts +6 -5
- package/src/requests.ts +12 -14
- package/src/status.ts +3 -3
- package/src/verifyWebhook.ts +4 -10
- package/src/viewerRepositoryAccess.ts +66 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
export const User = Schema.Struct({
|
|
4
|
+
avatarUrl: Schema.String,
|
|
5
|
+
htmlUrl: Schema.String,
|
|
6
|
+
id: Schema.Number,
|
|
7
|
+
login: Schema.String,
|
|
8
|
+
type: Schema.String,
|
|
9
|
+
}).pipe(Schema.encodeKeys({ avatarUrl: "avatar_url", htmlUrl: "html_url" }));
|
|
10
|
+
|
|
11
|
+
export const Issue = Schema.Struct({
|
|
12
|
+
body: Schema.NullOr(Schema.String),
|
|
13
|
+
closedAt: Schema.NullOr(Schema.String),
|
|
14
|
+
createdAt: Schema.String,
|
|
15
|
+
htmlUrl: Schema.String,
|
|
16
|
+
id: Schema.Number,
|
|
17
|
+
labels: Schema.Array(Schema.Union([Schema.String, Schema.Struct({ name: Schema.optional(Schema.String) })])),
|
|
18
|
+
number: Schema.Number,
|
|
19
|
+
pullRequest: Schema.optional(Schema.Struct({ url: Schema.String })),
|
|
20
|
+
state: Schema.Literals(["open", "closed"]),
|
|
21
|
+
title: Schema.String,
|
|
22
|
+
updatedAt: Schema.String,
|
|
23
|
+
user: Schema.NullOr(User),
|
|
24
|
+
}).pipe(
|
|
25
|
+
Schema.encodeKeys({ closedAt: "closed_at", createdAt: "created_at", htmlUrl: "html_url", pullRequest: "pull_request", updatedAt: "updated_at" }),
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
export const PullRequestFile = Schema.Struct({
|
|
29
|
+
additions: Schema.Number,
|
|
30
|
+
changes: Schema.Number,
|
|
31
|
+
deletions: Schema.Number,
|
|
32
|
+
filename: Schema.String,
|
|
33
|
+
patch: Schema.optional(Schema.String),
|
|
34
|
+
previousFilename: Schema.optional(Schema.String),
|
|
35
|
+
sha: Schema.String,
|
|
36
|
+
status: Schema.String,
|
|
37
|
+
}).pipe(Schema.encodeKeys({ previousFilename: "previous_filename" }));
|
|
38
|
+
|
|
39
|
+
export const CollaboratorPermission = Schema.Struct({
|
|
40
|
+
permission: Schema.String,
|
|
41
|
+
roleName: Schema.String,
|
|
42
|
+
user: User,
|
|
43
|
+
}).pipe(Schema.encodeKeys({ roleName: "role_name" }));
|
package/src/representations.ts
CHANGED
|
@@ -3,25 +3,26 @@ import { HttpClient, HttpClientRequest, type HttpClientResponse } from "effect/h
|
|
|
3
3
|
import type { ServiceUnavailable } from "@shivaedev/errors/taxonomy.ts";
|
|
4
4
|
import { nextLink } from "@shivaedev/http/nextLink.ts";
|
|
5
5
|
import { paginate } from "@shivaedev/http/paginate.ts";
|
|
6
|
+
import { resolveApiUrl } from "#adapters/resolveApiUrl.ts";
|
|
6
7
|
import type { CachedResponse } from "#cache.ts";
|
|
7
8
|
import { jsonResponse } from "#json.ts";
|
|
8
9
|
import type { requests } from "#requests.ts";
|
|
9
10
|
|
|
10
11
|
export function representations(
|
|
11
12
|
apiUrl: string,
|
|
12
|
-
request: ReturnType<typeof requests
|
|
13
|
+
request: Effect.Success<ReturnType<typeof requests>>,
|
|
13
14
|
resource: (path: string) => string,
|
|
14
15
|
responseByteLimit = 1024 * 1024,
|
|
15
16
|
) {
|
|
16
17
|
function get(path: string, accept = "application/vnd.github+json") {
|
|
17
|
-
return HttpClientRequest.get(
|
|
18
|
+
return HttpClientRequest.get(resolveApiUrl(apiUrl, path), { headers: { Accept: accept } });
|
|
18
19
|
}
|
|
19
20
|
function readRepresentation(path: string, byteLimit = responseByteLimit) {
|
|
20
21
|
return Effect.map(request(get(path), byteLimit), jsonResponse);
|
|
21
22
|
}
|
|
22
|
-
function listRepresentations(
|
|
23
|
+
function listRepresentations<E = ServiceUnavailable>(
|
|
23
24
|
path: string,
|
|
24
|
-
beforeRequest?: (resource: string) => Effect.Effect<void,
|
|
25
|
+
beforeRequest?: (resource: string) => Effect.Effect<void, E>,
|
|
25
26
|
byteLimit = responseByteLimit,
|
|
26
27
|
) {
|
|
27
28
|
const entries = new WeakMap<HttpClientResponse.HttpClientResponse, CachedResponse>();
|
|
@@ -46,7 +47,7 @@ export function representations(
|
|
|
46
47
|
);
|
|
47
48
|
}
|
|
48
49
|
function writeRepresentation(method: "POST" | "PATCH" | "PUT" | "DELETE", path: string, body?: unknown, byteLimit = responseByteLimit) {
|
|
49
|
-
return HttpClientRequest.make(method)(
|
|
50
|
+
return HttpClientRequest.make(method)(resolveApiUrl(apiUrl, path), { headers: { Accept: "application/vnd.github+json" } }).pipe(
|
|
50
51
|
(input) => (body === undefined ? Effect.succeed(input) : HttpClientRequest.bodyJson(input, body)),
|
|
51
52
|
Effect.flatMap((input) => request(input, byteLimit)),
|
|
52
53
|
Effect.map(jsonResponse),
|
package/src/requests.ts
CHANGED
|
@@ -2,20 +2,18 @@ import { Effect } from "effect";
|
|
|
2
2
|
import { HttpClient, HttpClientRequest } from "effect/http";
|
|
3
3
|
import { readBoundedBody } from "@shivaedev/http/body.ts";
|
|
4
4
|
import { ResponseBodyTooLarge } from "@shivaedev/http/errors.ts";
|
|
5
|
-
import { authenticatedRequest } from "
|
|
6
|
-
import { type CachedResponse, responseCache, revalidatedResponse } from "
|
|
7
|
-
import { GitHubTransportError } from "
|
|
8
|
-
import
|
|
9
|
-
import type { RateLimits } from "
|
|
10
|
-
import { isUnchanged } from "
|
|
11
|
-
import
|
|
5
|
+
import { authenticatedRequest } from "#authenticatedRequest.ts";
|
|
6
|
+
import { type CachedResponse, responseCache, revalidatedResponse } from "#cache.ts";
|
|
7
|
+
import { GitHubTransportError } from "#errors.ts";
|
|
8
|
+
import { GitHubToken } from "#GitHubToken.ts";
|
|
9
|
+
import type { RateLimits } from "#rateLimits.ts";
|
|
10
|
+
import { isUnchanged } from "#status.ts";
|
|
11
|
+
import { TokenRefresh } from "#TokenRefresh.ts";
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
client
|
|
15
|
-
token
|
|
16
|
-
|
|
17
|
-
refresh: typeof TokenRefresh.Service = undefined,
|
|
18
|
-
) {
|
|
13
|
+
export const requests = Effect.fnUntraced(function* (rates: RateLimits) {
|
|
14
|
+
const client = yield* HttpClient.HttpClient;
|
|
15
|
+
const token = yield* GitHubToken;
|
|
16
|
+
const refresh = yield* TokenRefresh;
|
|
19
17
|
const cache = responseCache();
|
|
20
18
|
const send = authenticatedRequest(HttpClient.withScope(client), token, rates, refresh);
|
|
21
19
|
return Effect.fn("GitHub.request")(function* (input: HttpClientRequest.HttpClientRequest, limit: number) {
|
|
@@ -43,4 +41,4 @@ export function requests(
|
|
|
43
41
|
}
|
|
44
42
|
return entry;
|
|
45
43
|
}, Effect.scoped);
|
|
46
|
-
}
|
|
44
|
+
});
|
package/src/status.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { DateTime, Effect, Option, Schema } from "effect";
|
|
2
|
-
import {
|
|
2
|
+
import type { HttpClientResponse } from "effect/http";
|
|
3
3
|
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
4
|
import { readBoundedText } from "@shivaedev/http/body.ts";
|
|
5
|
-
import {
|
|
5
|
+
import { checkStatus as checkResponseStatus } from "@shivaedev/http/checkStatus.ts";
|
|
6
6
|
import { parseRetryAfter } from "@shivaedev/http/parseRetryAfter.ts";
|
|
7
7
|
import { GitHubConversationLocked, GitHubPermissionDenied, GitHubRateLimited } from "#errors.ts";
|
|
8
8
|
|
|
@@ -29,7 +29,7 @@ export const checkStatus = Effect.fnUntraced(function* (response: HttpClientResp
|
|
|
29
29
|
return yield* refusal;
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
return yield*
|
|
32
|
+
return yield* checkResponseStatus(response);
|
|
33
33
|
});
|
|
34
34
|
|
|
35
35
|
const Message = Schema.fromJsonString(Schema.Struct({ message: Schema.String }));
|
package/src/verifyWebhook.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { GitHubWebhookSignatureError } from "#errors.ts";
|
|
1
|
+
import { Effect, type Redacted, Schema } from "effect";
|
|
2
|
+
import { verifySignature } from "#adapters/verifySignature.ts";
|
|
3
|
+
import type { GitHubWebhookSignatureError } from "#errors.ts";
|
|
4
4
|
|
|
5
5
|
const verify = Effect.fnUntraced(function* <TSchema extends Schema.Constraint>(
|
|
6
6
|
body: Uint8Array,
|
|
@@ -8,13 +8,7 @@ const verify = Effect.fnUntraced(function* <TSchema extends Schema.Constraint>(
|
|
|
8
8
|
secret: Redacted.Redacted<string>,
|
|
9
9
|
schema?: TSchema,
|
|
10
10
|
) {
|
|
11
|
-
|
|
12
|
-
return yield* new GitHubWebhookSignatureError();
|
|
13
|
-
}
|
|
14
|
-
const expected = createHmac("sha256", Redacted.value(secret)).update(body).digest();
|
|
15
|
-
if (!timingSafeEqual(expected, Buffer.from(signature.slice(7), "hex"))) {
|
|
16
|
-
return yield* new GitHubWebhookSignatureError();
|
|
17
|
-
}
|
|
11
|
+
yield* verifySignature(body, signature, secret);
|
|
18
12
|
if (schema === undefined) {
|
|
19
13
|
return;
|
|
20
14
|
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Cache, Effect, Exit, Schema, Stream } from "effect";
|
|
2
|
+
import { decodedPages } from "#pagedLists.ts";
|
|
3
|
+
import type { Representations } from "#representations.ts";
|
|
4
|
+
|
|
5
|
+
export const InstalledRepository = Schema.Struct({
|
|
6
|
+
htmlUrl: Schema.String,
|
|
7
|
+
id: Schema.Number,
|
|
8
|
+
name: Schema.String,
|
|
9
|
+
owner: Schema.Struct({ login: Schema.String }),
|
|
10
|
+
private: Schema.Boolean,
|
|
11
|
+
}).pipe(Schema.encodeKeys({ htmlUrl: "html_url" }));
|
|
12
|
+
|
|
13
|
+
export interface ViewerRepositoryAccess {
|
|
14
|
+
readonly administrable: readonly (typeof InstalledRepository.Type & { readonly installationId: number })[];
|
|
15
|
+
readonly readable: readonly (typeof InstalledRepository.Type & { readonly installationId: number })[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const Account = Schema.Struct({ login: Schema.String });
|
|
19
|
+
const Installation = Schema.Struct({ id: Schema.Number });
|
|
20
|
+
const Permission = Schema.Struct({ permission: Schema.String });
|
|
21
|
+
|
|
22
|
+
const installationAccess = Effect.fnUntraced(function* (client: Representations, installationId: number, login: string) {
|
|
23
|
+
const pages = yield* decodedPages(client, "/installation/repositories?per_page=100", InstalledRepository, {
|
|
24
|
+
arrayKey: "repositories",
|
|
25
|
+
}).pipe(Stream.runCollect);
|
|
26
|
+
const readable: ViewerRepositoryAccess["readable"][number][] = [];
|
|
27
|
+
const administrable: ViewerRepositoryAccess["administrable"][number][] = [];
|
|
28
|
+
for (const repository of pages.flatMap((page) => page.items)) {
|
|
29
|
+
const permissionPath = `/repos/${encodeURIComponent(repository.owner.login)}/${encodeURIComponent(repository.name)}/collaborators/${encodeURIComponent(login)}/permission`;
|
|
30
|
+
const permission = yield* Effect.flatMap(client.read(permissionPath), (response) => response.decode(Permission)).pipe(
|
|
31
|
+
Effect.map((grant) => grant.permission),
|
|
32
|
+
Effect.catchTag("RemoteNotFound", () => Effect.succeed("none")),
|
|
33
|
+
);
|
|
34
|
+
const installed = { ...repository, installationId };
|
|
35
|
+
if (!repository.private || ["read", "triage", "write", "maintain", "admin"].includes(permission)) {
|
|
36
|
+
readable.push(installed);
|
|
37
|
+
}
|
|
38
|
+
if (permission === "admin") {
|
|
39
|
+
administrable.push(installed);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { administrable, readable } satisfies ViewerRepositoryAccess;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export function viewerRepositoryAccess(app: Representations, installation: (id: number) => Effect.Effect<Representations>) {
|
|
46
|
+
const lookup = Effect.fnUntraced(function* (account: string) {
|
|
47
|
+
const path = account.startsWith("id:") ? `/user/${account.slice(3)}` : `/users/${encodeURIComponent(account.slice(6))}`;
|
|
48
|
+
const installations = yield* decodedPages(app, "/app/installations?per_page=100", Installation).pipe(Stream.runCollect);
|
|
49
|
+
const first = installations.flatMap((page) => page.items)[0];
|
|
50
|
+
if (first === undefined) {
|
|
51
|
+
return { administrable: [], readable: [] } satisfies ViewerRepositoryAccess;
|
|
52
|
+
}
|
|
53
|
+
const resolver = yield* installation(first.id);
|
|
54
|
+
const { login } = yield* Effect.flatMap(resolver.read(path), (response) => response.decode(Account));
|
|
55
|
+
const readable: ViewerRepositoryAccess["readable"][number][] = [];
|
|
56
|
+
const administrable: ViewerRepositoryAccess["administrable"][number][] = [];
|
|
57
|
+
for (const { id } of installations.flatMap((page) => page.items)) {
|
|
58
|
+
const client = yield* installation(id);
|
|
59
|
+
const access = yield* installationAccess(client, id, login);
|
|
60
|
+
readable.push(...access.readable);
|
|
61
|
+
administrable.push(...access.administrable);
|
|
62
|
+
}
|
|
63
|
+
return { administrable, readable } satisfies ViewerRepositoryAccess;
|
|
64
|
+
});
|
|
65
|
+
return Cache.makeWith(lookup, { capacity: 100, timeToLive: (exit) => (Exit.isSuccess(exit) ? "60 seconds" : 0) });
|
|
66
|
+
}
|