@milkenjoyer/toolchain 0.18.0 → 0.20.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.
@@ -1,99 +0,0 @@
1
- import { Octokit } from "@octokit/rest";
2
- import * as Clock from "effect/Clock";
3
- import * as Context from "effect/Context";
4
- import * as Effect from "effect/Effect";
5
- import * as Layer from "effect/Layer";
6
- import * as Redacted from "effect/Redacted";
7
- import * as Schema from "effect/Schema";
8
-
9
- import { StartReadError } from "./boat.ts";
10
- import { type MergeCounts, startRepos } from "./budget.ts";
11
-
12
- const Count = Schema.Struct({
13
- issueCount: Schema.Number.check(
14
- Schema.makeFilter((value) => Number.isSafeInteger(value) && value >= 0)
15
- ),
16
- });
17
- const windows = { h6: 21_600_000, h24: 86_400_000, d7: 604_800_000 };
18
-
19
- export class Merges extends Context.Service<
20
- Merges,
21
- { readonly counts: Effect.Effect<ReadonlyArray<MergeCounts>, StartReadError> }
22
- >()("toolchain/Merges") {
23
- static readonly layer = (
24
- token: Redacted.Redacted<string>,
25
- options: {
26
- readonly fetch?: (
27
- url: string | URL | Request,
28
- init?: RequestInit
29
- ) => Promise<Response>;
30
- } = {}
31
- ) =>
32
- Layer.sync(Merges, () => {
33
- const api = new Octokit({
34
- auth: Redacted.value(token),
35
- ...(options.fetch === undefined
36
- ? {}
37
- : { request: { fetch: options.fetch } }),
38
- });
39
- const counts = Effect.gen(function* () {
40
- const now = yield* Clock.currentTimeMillis;
41
- const query = `query { ${startRepos
42
- .flatMap((repo, index) => {
43
- const [owner, name] = repo.split("/");
44
- return [
45
- `repo${index}: repository(owner: "${owner}", name: "${name}") { nameWithOwner }`,
46
- ...Object.entries(windows).map(([window, millis]) => {
47
- const since = new Date(now - millis)
48
- .toISOString()
49
- .replace(/\.\d{3}Z$/u, "Z");
50
- return `${window}_${index}: search(type: ISSUE, first: 0, query: "repo:${repo} is:pr is:merged base:main merged:>=${since}") { issueCount }`;
51
- }),
52
- ];
53
- })
54
- .join(" ")} }`;
55
- const response = yield* Effect.tryPromise({
56
- try: () => api.graphql<unknown>(query),
57
- catch: () =>
58
- new StartReadError({ reason: "GitHub merges request failed" }),
59
- });
60
- return yield* Schema.decodeUnknownEffect(
61
- Schema.Record(Schema.String, Schema.Unknown)
62
- )(response).pipe(
63
- Effect.flatMap((data) =>
64
- Effect.forEach(
65
- startRepos,
66
- (repo, index) =>
67
- Schema.decodeUnknownEffect(
68
- Schema.Struct({
69
- repository: Schema.Struct({
70
- nameWithOwner: Schema.Literal(repo),
71
- }),
72
- h6: Count,
73
- h24: Count,
74
- d7: Count,
75
- })
76
- )({
77
- repository: data[`repo${index}`],
78
- h6: data[`h6_${index}`],
79
- h24: data[`h24_${index}`],
80
- d7: data[`d7_${index}`],
81
- }).pipe(
82
- Effect.map((row) => ({
83
- repo,
84
- h6: row.h6.issueCount,
85
- h24: row.h24.issueCount,
86
- d7: row.d7.issueCount,
87
- }))
88
- ),
89
- { concurrency: 1 }
90
- )
91
- ),
92
- Effect.mapError(
93
- () => new StartReadError({ reason: "GitHub merges unread" })
94
- )
95
- );
96
- });
97
- return Merges.of({ counts });
98
- });
99
- }