@milkenjoyer/toolchain 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/src/verdict.ts ADDED
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env bun
2
+ import { NodeServices } from "@effect/platform-node";
3
+ import * as Cause from "effect/Cause";
4
+ import * as Console from "effect/Console";
5
+ import * as Effect from "effect/Effect";
6
+ import * as Exit from "effect/Exit";
7
+
8
+ import { autoPass, github, readPullRequest } from "./verification.ts";
9
+
10
+ export const checkVerdict = Effect.gen(function* () {
11
+ const request = yield* readPullRequest;
12
+ if (request === undefined) return 0;
13
+ const { octokit, owner, repo, number, sha, pr } = request;
14
+ const reason = autoPass(pr);
15
+ if (reason === undefined) {
16
+ yield* Console.log(
17
+ `PR #${number} at ${sha} waits for an independent live verdict.`
18
+ );
19
+ return 0;
20
+ }
21
+ const description = `PASS: auto, ${reason}`.slice(0, 139);
22
+ yield* github(() =>
23
+ octokit.rest.repos.createCommitStatus({
24
+ owner,
25
+ repo,
26
+ sha,
27
+ context: "verdict",
28
+ state: "success",
29
+ description,
30
+ })
31
+ );
32
+ yield* Console.log(
33
+ `Posted verdict: success on PR #${number} at ${sha}: ${description}`
34
+ );
35
+ return 0;
36
+ });
37
+
38
+ if (import.meta.main) {
39
+ const exit = await Effect.runPromiseExit(
40
+ checkVerdict.pipe(Effect.provide(NodeServices.layer))
41
+ );
42
+ if (Exit.isSuccess(exit)) process.exitCode = exit.value;
43
+ else {
44
+ const error = Cause.squash(exit.cause);
45
+ process.stderr.write(
46
+ `${error instanceof Error ? error.message : String(error)}\n`
47
+ );
48
+ process.exitCode = 1;
49
+ }
50
+ }
@@ -35,18 +35,22 @@ const exemptions: ReadonlyArray<{
35
35
  applies: (pr) =>
36
36
  pr.files !== undefined &&
37
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
- )
38
+ pr.files.every(
39
+ (file) =>
40
+ !file.split("/").includes("skills") &&
41
+ /^(?:LICENSE.*|.*\.(?:md|mdx|png|jpg|jpeg|gif|webp|avif|svg|ico|mp4|webm|mov|mp3|wav|ogg|woff|woff2|ttf|otf))$/iu.test(
42
+ file.split("/").at(-1) ?? ""
43
+ )
42
44
  ),
43
45
  },
44
46
  ];
45
47
 
48
+ export const autoPass = (pr: PullRequest): string | undefined =>
49
+ exemptions.find((entry) => entry.applies(pr))?.reason;
50
+
46
51
  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 };
52
+ const exemption = autoPass(pr);
53
+ if (exemption !== undefined) return { _tag: "Exempt", reason: exemption };
50
54
  const lines = (pr.body ?? "")
51
55
  .replaceAll(/<!--[\s\S]*?-->/gu, "")
52
56
  .split(/\r?\n/u);
@@ -70,11 +74,11 @@ export const verdict = (pr: PullRequest): Verdict => {
70
74
  : { _tag: "Unproven", reason: "names no run" };
71
75
  };
72
76
 
73
- class VerificationError extends Data.TaggedError("VerificationError")<{
77
+ export class VerificationError extends Data.TaggedError("VerificationError")<{
74
78
  readonly message: string;
75
79
  }> {}
76
80
 
77
- const github = <A>(request: () => Promise<A>) =>
81
+ export const github = <A>(request: () => Promise<A>) =>
78
82
  Effect.tryPromise({
79
83
  try: request,
80
84
  catch: (cause) =>
@@ -96,13 +100,22 @@ const PullRequestEvent = Schema.fromJsonString(
96
100
  })
97
101
  );
98
102
 
99
- export const checkVerification = Effect.gen(function* () {
103
+ export type PullRequestContext = {
104
+ readonly octokit: Octokit;
105
+ readonly owner: string;
106
+ readonly repo: string;
107
+ readonly number: number;
108
+ readonly sha: string;
109
+ readonly pr: PullRequest;
110
+ };
111
+
112
+ export const readPullRequest = Effect.gen(function* () {
100
113
  const event = yield* Config.String("GITHUB_EVENT_NAME").pipe(
101
114
  Config.withDefault("")
102
115
  );
103
116
  if (event !== "pull_request") {
104
117
  yield* Console.log("Not a pull_request event, so there is no PR to check.");
105
- return 0;
118
+ return undefined;
106
119
  }
107
120
  const fs = yield* FileSystem;
108
121
  const eventPath = yield* Config.String("GITHUB_EVENT_PATH");
@@ -135,24 +148,39 @@ export const checkVerification = Effect.gen(function* () {
135
148
  basehead: `${pr.base.sha}...${pr.head.sha}`,
136
149
  })
137
150
  );
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
- });
151
+ const request: PullRequestContext = {
152
+ octokit,
153
+ owner,
154
+ repo,
155
+ number: pr.number,
156
+ sha: pr.head.sha,
157
+ pr: {
158
+ author: pr.user.login,
159
+ body: pr.body,
160
+ files:
161
+ data.files === undefined || data.files.length >= 300
162
+ ? undefined
163
+ : data.files.map((file) => file.filename),
164
+ },
165
+ };
166
+ return request;
167
+ });
168
+
169
+ export const checkVerification = Effect.gen(function* () {
170
+ const request = yield* readPullRequest;
171
+ if (request === undefined) return 0;
172
+ const { number } = request;
173
+ const result = verdict(request.pr);
146
174
  if (result._tag === "Exempt") {
147
- yield* Console.log(`PR #${pr.number} is exempt: ${result.reason}.`);
175
+ yield* Console.log(`PR #${number} is exempt: ${result.reason}.`);
148
176
  return 0;
149
177
  }
150
178
  if (result._tag === "Proven") {
151
- yield* Console.log(`PR #${pr.number}'s ## Verification names a run.`);
179
+ yield* Console.log(`PR #${number}'s ## Verification names a run.`);
152
180
  return 0;
153
181
  }
154
182
  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.`
183
+ `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
184
  );
157
185
  return 1;
158
186
  });