@milkenjoyer/toolchain 0.7.3 → 0.9.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/src/proof.ts ADDED
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env bun
2
+ import { NodeServices } from "@effect/platform-node";
3
+ import { Octokit } from "@octokit/rest";
4
+ import * as Cause from "effect/Cause";
5
+ import * as Config from "effect/Config";
6
+ import * as Data from "effect/Data";
7
+ import * as Effect from "effect/Effect";
8
+ import { Base64 } from "effect/encoding";
9
+ import * as Exit from "effect/Exit";
10
+ import { FileSystem } from "effect/FileSystem";
11
+ import { Path } from "effect/Path";
12
+ import { ChildProcess } from "effect/process";
13
+ import * as Redacted from "effect/Redacted";
14
+ import * as Schema from "effect/Schema";
15
+ import * as Stream from "effect/Stream";
16
+
17
+ class ProofError extends Data.TaggedError("ProofError")<{
18
+ readonly message: string;
19
+ }> {}
20
+
21
+ type Kind = "image" | "video";
22
+
23
+ const kinds = new Map<string, Kind>([
24
+ [".png", "image"],
25
+ [".jpg", "image"],
26
+ [".jpeg", "image"],
27
+ [".gif", "image"],
28
+ [".webp", "image"],
29
+ [".mp4", "video"],
30
+ [".webm", "video"],
31
+ [".mov", "video"],
32
+ ]);
33
+
34
+ const github = <A>(request: () => Promise<A>) =>
35
+ Effect.tryPromise({
36
+ try: request,
37
+ catch: (cause) =>
38
+ new ProofError({
39
+ message: cause instanceof Error ? cause.message : String(cause),
40
+ }),
41
+ });
42
+
43
+ const preview = Effect.fn("proof.preview")(function* (
44
+ file: string,
45
+ gif: string
46
+ ) {
47
+ const ffmpeg = yield* ChildProcess.make(
48
+ "ffmpeg",
49
+ [
50
+ "-loglevel",
51
+ "error",
52
+ "-i",
53
+ file,
54
+ "-t",
55
+ "30",
56
+ "-vf",
57
+ "fps=8,scale='min(720,iw)':-2:flags=lanczos,split[a][b];[a]palettegen[p];[b][p]paletteuse",
58
+ "-loop",
59
+ "0",
60
+ gif,
61
+ ],
62
+ { stdin: "ignore" }
63
+ ).pipe(
64
+ Effect.mapError(
65
+ () => new ProofError({ message: `A video needs ffmpeg: ${file}` })
66
+ )
67
+ );
68
+ const [stderr, code] = yield* Effect.all(
69
+ [ffmpeg.stderr.pipe(Stream.decodeText, Stream.mkString), ffmpeg.exitCode],
70
+ { concurrency: "unbounded" }
71
+ );
72
+ if (Number(code) !== 0)
73
+ return yield* new ProofError({
74
+ message: `ffmpeg failed for ${file}: ${stderr}`,
75
+ });
76
+ return gif;
77
+ });
78
+
79
+ const proof = Effect.fn("proof")(function* (
80
+ octokit: Octokit,
81
+ owner: string,
82
+ repo: string,
83
+ files: ReadonlyArray<string>
84
+ ) {
85
+ const fs = yield* FileSystem;
86
+ const path = yield* Path;
87
+ const media = yield* Effect.forEach(
88
+ files,
89
+ (file, index) => {
90
+ const kind = kinds.get(path.extname(file).toLowerCase());
91
+ return kind === undefined
92
+ ? new ProofError({ message: `Unknown extension: ${file}` })
93
+ : Effect.succeed({
94
+ file,
95
+ kind,
96
+ name: `${index + 1}-${path.basename(file).replaceAll(/[^\w.-]/gu, "-")}`,
97
+ });
98
+ },
99
+ { concurrency: 1 }
100
+ );
101
+ const { data: repository } = yield* github(() =>
102
+ octokit.rest.repos.get({ owner, repo })
103
+ );
104
+ if (repository.visibility !== "private")
105
+ return yield* new ProofError({
106
+ message: `${owner}/${repo} is not private, so proof stays local`,
107
+ });
108
+ const temporary = yield* fs.makeTempDirectoryScoped({ prefix: "proof-" });
109
+ const uploads = yield* Effect.forEach(
110
+ media,
111
+ ({ file, kind, name }) => {
112
+ if (kind === "image") return Effect.succeed([{ file, name }]);
113
+ return Effect.map(
114
+ preview(file, path.join(temporary, `${name}.gif`)),
115
+ (gif) => [
116
+ { file, name },
117
+ { file: gif, name: `${name}.gif` },
118
+ ]
119
+ );
120
+ },
121
+ { concurrency: 1 }
122
+ );
123
+ const tree = yield* Effect.forEach(
124
+ uploads.flat(),
125
+ ({ file, name }) =>
126
+ Effect.gen(function* () {
127
+ const content = Base64.encode(yield* fs.readFile(file));
128
+ const { data } = yield* github(() =>
129
+ octokit.rest.git.createBlob({
130
+ owner,
131
+ repo,
132
+ encoding: "base64",
133
+ content,
134
+ })
135
+ );
136
+ return {
137
+ path: name,
138
+ mode: "100644" as const,
139
+ type: "blob" as const,
140
+ sha: data.sha,
141
+ };
142
+ }),
143
+ { concurrency: 1 }
144
+ );
145
+ const { data: root } = yield* github(() =>
146
+ octokit.rest.git.createTree({ owner, repo, tree })
147
+ );
148
+ const { data: commit } = yield* github(() =>
149
+ octokit.rest.git.createCommit({
150
+ owner,
151
+ repo,
152
+ tree: root.sha,
153
+ parents: [],
154
+ message: "Verifier proof",
155
+ })
156
+ );
157
+ yield* github(() =>
158
+ octokit.rest.git.createRef({
159
+ owner,
160
+ repo,
161
+ ref: `refs/proof/${commit.sha}`,
162
+ sha: commit.sha,
163
+ })
164
+ );
165
+ const url = (name: string) =>
166
+ `https://github.com/${[owner, repo, "raw", commit.sha, name].map(encodeURIComponent).join("/")}`;
167
+ return media.map(({ name, kind }) =>
168
+ kind === "image"
169
+ ? `![${name}](${url(name)})`
170
+ : `[![${name}](${url(`${name}.gif`)})](${url(name)})`
171
+ );
172
+ }, Effect.scoped);
173
+
174
+ if (import.meta.main) {
175
+ const [repository = "", ...files] = process.argv.slice(2);
176
+ const [, owner, repo] = /^([^/#\s]+)\/([^/#\s]+)$/u.exec(repository) ?? [];
177
+ if (owner === undefined || repo === undefined || files.length === 0) {
178
+ process.stderr.write("usage: proof <owner/repo> <file>...\n");
179
+ process.exitCode = 2;
180
+ } else {
181
+ const exit = await Effect.runPromiseExit(
182
+ Effect.gen(function* () {
183
+ const token = yield* Config.schema(
184
+ Schema.Redacted(Schema.NonEmptyString),
185
+ "GH_TOKEN"
186
+ ).pipe(
187
+ Config.orElse(() =>
188
+ Config.schema(
189
+ Schema.Redacted(Schema.NonEmptyString),
190
+ "GITHUB_TOKEN"
191
+ )
192
+ ),
193
+ Effect.mapError(
194
+ () =>
195
+ new ProofError({ message: "Missing GH_TOKEN or GITHUB_TOKEN" })
196
+ )
197
+ );
198
+ return yield* proof(
199
+ new Octokit({ auth: Redacted.value(token) }),
200
+ owner,
201
+ repo,
202
+ files
203
+ );
204
+ }).pipe(Effect.provide(NodeServices.layer))
205
+ );
206
+ if (Exit.isSuccess(exit))
207
+ process.stdout.write(`${exit.value.join("\n")}\n`);
208
+ else {
209
+ const error = Cause.squash(exit.cause);
210
+ process.stderr.write(
211
+ `${error instanceof Error ? error.message : String(error)}\n`
212
+ );
213
+ process.exitCode = 1;
214
+ }
215
+ }
216
+ }
@@ -0,0 +1,172 @@
1
+ #!/usr/bin/env bun
2
+ import { NodeServices } from "@effect/platform-node";
3
+ import { Octokit } from "@octokit/rest";
4
+ import * as Cause from "effect/Cause";
5
+ import * as Config from "effect/Config";
6
+ import * as Console from "effect/Console";
7
+ import * as Data from "effect/Data";
8
+ import * as Effect from "effect/Effect";
9
+ import * as Exit from "effect/Exit";
10
+ import { FileSystem } from "effect/FileSystem";
11
+ import * as Redacted from "effect/Redacted";
12
+ import * as Schema from "effect/Schema";
13
+
14
+ export type PullRequest = {
15
+ readonly author: string;
16
+ readonly body: string | null;
17
+ readonly files: ReadonlyArray<string> | undefined;
18
+ };
19
+
20
+ export type Verdict =
21
+ | { readonly _tag: "Exempt"; readonly reason: string }
22
+ | { readonly _tag: "Proven" }
23
+ | { readonly _tag: "Unproven"; readonly reason: string };
24
+
25
+ const exemptions: ReadonlyArray<{
26
+ readonly reason: string;
27
+ readonly applies: (pr: PullRequest) => boolean;
28
+ }> = [
29
+ {
30
+ reason: "Renovate PR",
31
+ applies: (pr) => pr.author === "renovate[bot]",
32
+ },
33
+ {
34
+ reason: "changes only docs, content or art",
35
+ applies: (pr) =>
36
+ pr.files !== undefined &&
37
+ pr.files.length > 0 &&
38
+ pr.files.every((file) =>
39
+ /^(?:LICENSE.*|.*\.(?:md|mdx|png|jpg|jpeg|gif|webp|avif|svg|ico|mp4|webm|mov|mp3|wav|ogg|woff|woff2|ttf|otf))$/iu.test(
40
+ file.split("/").at(-1) ?? ""
41
+ )
42
+ ),
43
+ },
44
+ ];
45
+
46
+ export const verdict = (pr: PullRequest): Verdict => {
47
+ const exemption = exemptions.find((entry) => entry.applies(pr));
48
+ if (exemption !== undefined)
49
+ return { _tag: "Exempt", reason: exemption.reason };
50
+ const lines = (pr.body ?? "")
51
+ .replaceAll(/<!--[\s\S]*?-->/gu, "")
52
+ .split(/\r?\n/u);
53
+ const start = lines.findIndex((line) =>
54
+ /^##\s+Verification\s*$/iu.test(line)
55
+ );
56
+ if (start === -1)
57
+ return { _tag: "Unproven", reason: "has no ## Verification section" };
58
+ const fencedRun = "\u0000";
59
+ const rest = lines
60
+ .slice(start + 1)
61
+ .join("\n")
62
+ .replaceAll(
63
+ /^ {0,3}(`{3,}|~{3,})[^\n]*\n([\s\S]*?)^ {0,3}\1[ \t]*$/gmu,
64
+ (_fence, _marker, content: string) =>
65
+ content.trim() === "" ? "" : fencedRun
66
+ );
67
+ const [section = ""] = rest.split(/^(?:# |## )/mu);
68
+ return section.includes(fencedRun) || /`[^`\s][^`]*`/u.test(section)
69
+ ? { _tag: "Proven" }
70
+ : { _tag: "Unproven", reason: "names no run" };
71
+ };
72
+
73
+ class VerificationError extends Data.TaggedError("VerificationError")<{
74
+ readonly message: string;
75
+ }> {}
76
+
77
+ const github = <A>(request: () => Promise<A>) =>
78
+ Effect.tryPromise({
79
+ try: request,
80
+ catch: (cause) =>
81
+ new VerificationError({
82
+ message: cause instanceof Error ? cause.message : String(cause),
83
+ }),
84
+ });
85
+
86
+ const PullRequestEvent = Schema.fromJsonString(
87
+ Schema.Struct({
88
+ pull_request: Schema.Struct({
89
+ number: Schema.Finite,
90
+ body: Schema.NullOr(Schema.String),
91
+ user: Schema.Struct({ login: Schema.String }),
92
+ base: Schema.Struct({ sha: Schema.String }),
93
+ head: Schema.Struct({ sha: Schema.String }),
94
+ }),
95
+ repository: Schema.Struct({ full_name: Schema.String }),
96
+ })
97
+ );
98
+
99
+ export const checkVerification = Effect.gen(function* () {
100
+ const event = yield* Config.String("GITHUB_EVENT_NAME").pipe(
101
+ Config.withDefault("")
102
+ );
103
+ if (event !== "pull_request") {
104
+ yield* Console.log("Not a pull_request event, so there is no PR to check.");
105
+ return 0;
106
+ }
107
+ const fs = yield* FileSystem;
108
+ const eventPath = yield* Config.String("GITHUB_EVENT_PATH");
109
+ const payload = yield* fs.readFileString(eventPath);
110
+ const { pull_request: pr, repository } =
111
+ yield* Schema.decodeEffect(PullRequestEvent)(payload);
112
+ const [, owner, repo] =
113
+ /^([^/\s]+)\/([^/\s]+)$/u.exec(repository.full_name) ?? [];
114
+ if (owner === undefined || repo === undefined)
115
+ return yield* new VerificationError({
116
+ message: "Invalid repository.full_name",
117
+ });
118
+ const token = yield* Config.schema(
119
+ Schema.Redacted(Schema.NonEmptyString),
120
+ "GH_TOKEN"
121
+ ).pipe(
122
+ Config.orElse(() =>
123
+ Config.schema(Schema.Redacted(Schema.NonEmptyString), "GITHUB_TOKEN")
124
+ ),
125
+ Effect.mapError(
126
+ () =>
127
+ new VerificationError({ message: "Missing GH_TOKEN or GITHUB_TOKEN" })
128
+ )
129
+ );
130
+ const octokit = new Octokit({ auth: Redacted.value(token) });
131
+ const { data } = yield* github(() =>
132
+ octokit.rest.repos.compareCommitsWithBasehead({
133
+ owner,
134
+ repo,
135
+ basehead: `${pr.base.sha}...${pr.head.sha}`,
136
+ })
137
+ );
138
+ const result = verdict({
139
+ author: pr.user.login,
140
+ body: pr.body,
141
+ files:
142
+ data.files === undefined || data.files.length >= 300
143
+ ? undefined
144
+ : data.files.map((file) => file.filename),
145
+ });
146
+ if (result._tag === "Exempt") {
147
+ yield* Console.log(`PR #${pr.number} is exempt: ${result.reason}.`);
148
+ return 0;
149
+ }
150
+ if (result._tag === "Proven") {
151
+ yield* Console.log(`PR #${pr.number}'s ## Verification names a run.`);
152
+ return 0;
153
+ }
154
+ yield* Console.error(
155
+ `PR #${pr.number} ${result.reason}. Add a ## Verification section naming each real run (the command or verifier recipe in backticks) and its outcome, then push again after editing the body: a rerun reads the body from the original event.`
156
+ );
157
+ return 1;
158
+ });
159
+
160
+ if (import.meta.main) {
161
+ const exit = await Effect.runPromiseExit(
162
+ checkVerification.pipe(Effect.provide(NodeServices.layer))
163
+ );
164
+ if (Exit.isSuccess(exit)) process.exitCode = exit.value;
165
+ else {
166
+ const error = Cause.squash(exit.cause);
167
+ process.stderr.write(
168
+ `${error instanceof Error ? error.message : String(error)}\n`
169
+ );
170
+ process.exitCode = 1;
171
+ }
172
+ }