@samebase/alchemy-convex 0.47.0 → 0.48.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/README.md +0 -32
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1689 -2
- package/lib/index.js.map +1 -1
- package/lib/sealedSecret.js +78 -1
- package/lib/sealedSecret.js.map +1 -0
- package/package.json +2 -5
- package/lib/State-Dd88J2_z.js +0 -1699
- package/lib/State-Dd88J2_z.js.map +0 -1
- package/lib/cli.d.ts +0 -1
- package/lib/cli.js +0 -218
- package/lib/cli.js.map +0 -1
- package/lib/sealedSecret-B7HlvIg2.js +0 -78
- package/lib/sealedSecret-B7HlvIg2.js.map +0 -1
- package/src/StackComment.ts +0 -329
- package/src/cli.ts +0 -33
package/src/StackComment.ts
DELETED
|
@@ -1,329 +0,0 @@
|
|
|
1
|
-
// The command `alchemy-convex stack-comment`: one comment on a pull request with the stacks of the
|
|
2
|
-
// repository, read from the state store after the deploys.
|
|
3
|
-
//
|
|
4
|
-
// A stack sees only itself, so a comment that a stack declares cannot show the other stacks of the
|
|
5
|
-
// repository. This command runs as a step after them and reads each stack of the repository that
|
|
6
|
-
// has the stage `pr-<number>`: by the repository id of the stage's run record, or, for a stage
|
|
7
|
-
// without a version 2 record, by the name `owner/name` or `owner/name/<folder>`. A stack without
|
|
8
|
-
// that stage has no card. A run replaces the whole body, so the last run shows the newest state.
|
|
9
|
-
//
|
|
10
|
-
// The rows of a card are the boxes of `stageDiagram` from `@samebase/alchemy-kit/stack`, the model
|
|
11
|
-
// of Samebase's stack diagram. Alchemy state holds secrets in clear text, and that model keeps no
|
|
12
|
-
// secret of a row.
|
|
13
|
-
//
|
|
14
|
-
// GitHub keeps a fixed set of HTML in a comment and removes `style` and `class`, so every choice of
|
|
15
|
-
// the markup is a fact about the sanitizer or the CSS of github.com:
|
|
16
|
-
// - A table has `display: block` and `width: max-content`, so a `width` on it does nothing. The
|
|
17
|
-
// spacer, a floated image one pixel high in a block of its own, gives every card one width, and
|
|
18
|
-
// it shrinks on a narrow page.
|
|
19
|
-
// - The CSS colors the even rows of a group, so each row has its own `tbody`.
|
|
20
|
-
// - An HTML block ends at a blank line, so a table has none and is not indented.
|
|
21
|
-
// - A bare image links to its raw file, and one link around an image and text underlines the space
|
|
22
|
-
// between them, so the icon and the name are two links to one place.
|
|
23
|
-
// - An image with `height` gets a gray background, so each icon is square and has only `width`.
|
|
24
|
-
// - `align="middle"` is `-webkit-baseline-middle` in Chrome, which is below the text.
|
|
25
|
-
// `align="absmiddle"` is the CSS value `middle`.
|
|
26
|
-
import { State } from "alchemy/State";
|
|
27
|
-
import * as Effect from "effect/Effect";
|
|
28
|
-
import * as Option from "effect/Option";
|
|
29
|
-
import * as Schema from "effect/Schema";
|
|
30
|
-
import { RUN_RECORD_KEY, runRecord } from "@samebase/alchemy-kit/run-record";
|
|
31
|
-
import {
|
|
32
|
-
boxSubtitle,
|
|
33
|
-
stageDiagram,
|
|
34
|
-
stageTree,
|
|
35
|
-
type DiagramBox,
|
|
36
|
-
type StageDiagram,
|
|
37
|
-
type StageTreeNode,
|
|
38
|
-
} from "@samebase/alchemy-kit/stack";
|
|
39
|
-
import { resourceTypeUI } from "@samebase/alchemy-kit/ui";
|
|
40
|
-
|
|
41
|
-
/** The first text of the comment, which finds it again. */
|
|
42
|
-
const MARKER = "<!-- samebase-stacks -->";
|
|
43
|
-
|
|
44
|
-
/** GitHub does not proxy this host, so a new icon needs no purge of its image cache. */
|
|
45
|
-
const ICONS =
|
|
46
|
-
"https://raw.githubusercontent.com/samebase/samebase/main/packages/alchemy-convex/assets/stack-comment";
|
|
47
|
-
|
|
48
|
-
const GITHUB_API = "https://api.github.com";
|
|
49
|
-
|
|
50
|
-
/** The fields of the run record that a card shows. */
|
|
51
|
-
const StageOutput = Schema.Struct({
|
|
52
|
-
[RUN_RECORD_KEY]: Schema.Struct({
|
|
53
|
-
headCommit: Schema.String,
|
|
54
|
-
runUrl: Schema.NullOr(Schema.String),
|
|
55
|
-
recordedAt: Schema.String,
|
|
56
|
-
}),
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
/** The part of a stage output that says which repository deployed it. */
|
|
60
|
-
const RepositoryOfOutput = Schema.Struct({
|
|
61
|
-
[RUN_RECORD_KEY]: Schema.Struct({
|
|
62
|
-
version: Schema.Literal(2),
|
|
63
|
-
repositoryId: Schema.String,
|
|
64
|
-
stackFile: Schema.String,
|
|
65
|
-
}),
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
export interface StackCard {
|
|
69
|
-
readonly name: string;
|
|
70
|
-
/** The folder of the stack file in the repository, "" at the root. */
|
|
71
|
-
readonly folder: string;
|
|
72
|
-
readonly stage: string;
|
|
73
|
-
readonly diagram: StageDiagram;
|
|
74
|
-
readonly run: typeof StageOutput.Type | null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export class StackCommentError extends Schema.TaggedError<StackCommentError>()(
|
|
78
|
-
"StackCommentError",
|
|
79
|
-
{ reason: Schema.String },
|
|
80
|
-
) {
|
|
81
|
-
override get message() {
|
|
82
|
-
return `The stack comment cannot be updated: ${this.reason}`;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* The cards of the stacks of the repository that have `stage`, in name order. A stage with a version
|
|
88
|
-
* 2 record belongs to the repository of its `repositoryId`, under any stack name; any other stage
|
|
89
|
-
* belongs to the stacks named `repository` or `repository/<folder>`.
|
|
90
|
-
*/
|
|
91
|
-
export const readStackCards = ({
|
|
92
|
-
repository,
|
|
93
|
-
repositoryId,
|
|
94
|
-
stage,
|
|
95
|
-
}: {
|
|
96
|
-
repository: string;
|
|
97
|
-
repositoryId: string;
|
|
98
|
-
stage: string;
|
|
99
|
-
}) =>
|
|
100
|
-
Effect.gen(function* () {
|
|
101
|
-
const state = yield* yield* State;
|
|
102
|
-
const cards: StackCard[] = [];
|
|
103
|
-
for (const name of yield* state.listStacks()) {
|
|
104
|
-
if (!(yield* state.listStages(name)).includes(stage)) {
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
const request = { stack: name, stage };
|
|
108
|
-
const output = yield* state.getOutput(request);
|
|
109
|
-
const underName = name === repository || name.startsWith(`${repository}/`);
|
|
110
|
-
const record = Option.map(
|
|
111
|
-
Schema.decodeUnknownOption(RepositoryOfOutput)(output),
|
|
112
|
-
(decoded) => decoded[RUN_RECORD_KEY],
|
|
113
|
-
);
|
|
114
|
-
if (Option.isSome(record) ? record.value.repositoryId !== repositoryId : !underName) {
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
const rows = yield* Effect.forEach(yield* state.list(request), (fqn) =>
|
|
118
|
-
state.get({ ...request, fqn }).pipe(Effect.map((row) => ({ fqn, state: row }))),
|
|
119
|
-
);
|
|
120
|
-
cards.push({
|
|
121
|
-
name,
|
|
122
|
-
folder:
|
|
123
|
-
underName || Option.isNone(record)
|
|
124
|
-
? name.slice(repository.length + 1)
|
|
125
|
-
: record.value.stackFile.split("/").slice(0, -1).join("/"),
|
|
126
|
-
stage,
|
|
127
|
-
diagram: stageDiagram(rows),
|
|
128
|
-
run: Schema.is(StageOutput)(output) ? output : null,
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
return cards;
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
const escapeHtml = (text: string) =>
|
|
135
|
-
text
|
|
136
|
-
.replaceAll("&", "&")
|
|
137
|
-
.replaceAll("<", "<")
|
|
138
|
-
.replaceAll(">", ">")
|
|
139
|
-
.replaceAll('"', """);
|
|
140
|
-
|
|
141
|
-
const link = (href: string, text: string) => `<a href="${escapeHtml(href)}">${text}</a>`;
|
|
142
|
-
|
|
143
|
-
const hostOf = (url: string) => escapeHtml(new URL(url).host);
|
|
144
|
-
|
|
145
|
-
/** The icon file of a resource type, in `ICONS`. A box of any other type has no icon. */
|
|
146
|
-
const ICON_FILES = new Map([
|
|
147
|
-
["Convex.Deployment", "convex"],
|
|
148
|
-
["Cloudflare.Worker", "worker"],
|
|
149
|
-
["Cloudflare.R2.Bucket", "r2"],
|
|
150
|
-
]);
|
|
151
|
-
|
|
152
|
-
/** The name of a box, as a link to its dashboard page when it has one. */
|
|
153
|
-
const boxName = ({ name, resourceType, rows: [first] }: DiagramBox) => {
|
|
154
|
-
const icon = ICON_FILES.get(resourceType);
|
|
155
|
-
const image =
|
|
156
|
-
icon === undefined
|
|
157
|
-
? null
|
|
158
|
-
: `<img src="${ICONS}/${icon}.svg" width="16" align="absmiddle" alt="">`;
|
|
159
|
-
const text = escapeHtml(name);
|
|
160
|
-
const href = first.dashboard?.href;
|
|
161
|
-
if (href === undefined) {
|
|
162
|
-
return image === null ? text : `${image} ${text}`;
|
|
163
|
-
}
|
|
164
|
-
return image === null ? link(href, text) : `${link(href, image)} ${link(href, text)}`;
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
A box of the tree: its name with the labels of its line as code, its type and schedule, and its link
|
|
169
|
-
on the next line unless the link is the stage address. Below it, in a list: the boxes that it uses,
|
|
170
|
-
its bindings that name no box, the rows folded into it, and the boxes that share a resource with it.
|
|
171
|
-
A repeated box has only its name.
|
|
172
|
-
*/
|
|
173
|
-
const treeNode = (node: StageTreeNode, address: string | null): string => {
|
|
174
|
-
const { box, labels, repeated } = node;
|
|
175
|
-
const title = `${boxName(box)}${labels.map((label) => ` <code>${escapeHtml(label)}</code>`).join("")}`;
|
|
176
|
-
if (repeated) {
|
|
177
|
-
return title;
|
|
178
|
-
}
|
|
179
|
-
const linkLine =
|
|
180
|
-
box.link === null || box.link === address
|
|
181
|
-
? ""
|
|
182
|
-
: `<br><sub>${link(box.link, hostOf(box.link))}</sub>`;
|
|
183
|
-
const below = [
|
|
184
|
-
...node.children.map((child) => treeNode(child, address)),
|
|
185
|
-
...box.bindings
|
|
186
|
-
.filter((binding) => binding.box === null)
|
|
187
|
-
.map(
|
|
188
|
-
(binding) =>
|
|
189
|
-
`<code>${escapeHtml(binding.name)}</code> <sub>${escapeHtml([binding.kind, binding.target].filter((part) => part !== null).join(", "))}</sub>`,
|
|
190
|
-
),
|
|
191
|
-
...box.folded.map(
|
|
192
|
-
(row) =>
|
|
193
|
-
`<sub>${escapeHtml(`${row.logicalId}, ${resourceTypeUI(row.resourceType).name}`)}</sub>`,
|
|
194
|
-
),
|
|
195
|
-
...node.shared.map((text) => `<sub>${escapeHtml(text)}</sub>`),
|
|
196
|
-
];
|
|
197
|
-
const list =
|
|
198
|
-
below.length === 0 ? "" : `<ul>${below.map((item) => `<li>${item}</li>`).join("")}</ul>`;
|
|
199
|
-
return `${title} <sub>${escapeHtml(boxSubtitle(box))}</sub>${linkLine}${list}`;
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
/** Where a stage serves: its Convex app, else the first box with a link. */
|
|
203
|
-
const addressOf = ({ boxes }: StageDiagram) =>
|
|
204
|
-
(boxes.find((box) => box.resourceType === "Convex.Deployment") ?? boxes.find((box) => box.link))
|
|
205
|
-
?.link ?? null;
|
|
206
|
-
|
|
207
|
-
const runLine = ({ run }: StackCard) => {
|
|
208
|
-
if (run === null) {
|
|
209
|
-
return "";
|
|
210
|
-
}
|
|
211
|
-
const { headCommit, recordedAt, runUrl } = run[RUN_RECORD_KEY];
|
|
212
|
-
const commit = escapeHtml(headCommit.slice(0, 7));
|
|
213
|
-
const time = `${recordedAt.slice(0, 16).replace("T", " ")} UTC`;
|
|
214
|
-
return `<br><sub>${runUrl === null ? commit : link(runUrl, commit)} ${escapeHtml(time)}</sub>`;
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
const SPACER = `<div><img src="${ICONS}/spacer.svg" width="460" align="left" alt=""></div>`;
|
|
218
|
-
|
|
219
|
-
/** `alone`: the one card of the repository. A card at the root then has no folder, as on the repository page. */
|
|
220
|
-
const renderCard = (card: StackCard, alone: boolean) => {
|
|
221
|
-
const { folder } = card;
|
|
222
|
-
const heading = [
|
|
223
|
-
folder === "" && alone ? "" : `<b>${escapeHtml(folder || "/")}</b> `,
|
|
224
|
-
`<sub>${escapeHtml(card.stage)}</sub>`,
|
|
225
|
-
].join("");
|
|
226
|
-
const address = addressOf(card.diagram);
|
|
227
|
-
const addressLine = address === null ? "" : `<br>${link(address, hostOf(address))}`;
|
|
228
|
-
const rows = stageTree(card.diagram)
|
|
229
|
-
.map((node) => `<tbody><tr><td>${treeNode(node, address)}</td></tr></tbody>`)
|
|
230
|
-
.join("\n");
|
|
231
|
-
return `<table>
|
|
232
|
-
<tbody><tr><td>${SPACER}${heading}${addressLine}${runLine(card)}</td></tr></tbody>
|
|
233
|
-
${rows}
|
|
234
|
-
</table>`;
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
export const renderStackComment = (cards: readonly StackCard[]) => `${MARKER}
|
|
238
|
-
${cards.map((card) => renderCard(card, cards.length === 1)).join("\n\n")}
|
|
239
|
-
`;
|
|
240
|
-
|
|
241
|
-
const Comments = Schema.Array(Schema.Struct({ id: Schema.Number, body: Schema.String }));
|
|
242
|
-
const SavedComment = Schema.Struct({ html_url: Schema.String });
|
|
243
|
-
|
|
244
|
-
const github = (token: string, path: string, init: { method: string; body?: string }) =>
|
|
245
|
-
Effect.tryPromise({
|
|
246
|
-
try: async (): Promise<unknown> => {
|
|
247
|
-
const response = await fetch(`${GITHUB_API}${path}`, {
|
|
248
|
-
...init,
|
|
249
|
-
headers: {
|
|
250
|
-
Accept: "application/vnd.github+json",
|
|
251
|
-
Authorization: `Bearer ${token}`,
|
|
252
|
-
"Content-Type": "application/json",
|
|
253
|
-
"X-GitHub-Api-Version": "2022-11-28",
|
|
254
|
-
},
|
|
255
|
-
});
|
|
256
|
-
if (!response.ok) {
|
|
257
|
-
throw new Error(`HTTP ${response.status}`);
|
|
258
|
-
}
|
|
259
|
-
return await response.json();
|
|
260
|
-
},
|
|
261
|
-
catch: (error) =>
|
|
262
|
-
new StackCommentError({
|
|
263
|
-
reason: `GitHub ${init.method} ${path} failed (${error instanceof Error ? error.message : String(error)})`,
|
|
264
|
-
}),
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
const decode =
|
|
268
|
-
<S extends Schema.Codec<unknown, unknown>>(schema: S, what: string) =>
|
|
269
|
-
(value: unknown) =>
|
|
270
|
-
Schema.decodeUnknownEffect(schema)(value).pipe(
|
|
271
|
-
Effect.mapError(() => new StackCommentError({ reason: `GitHub gave a bad ${what}` })),
|
|
272
|
-
);
|
|
273
|
-
|
|
274
|
-
const findComment = (token: string, path: string) =>
|
|
275
|
-
Effect.gen(function* () {
|
|
276
|
-
for (let page = 1; ; page += 1) {
|
|
277
|
-
const comments = yield* github(token, `${path}?per_page=100&page=${page}`, {
|
|
278
|
-
method: "GET",
|
|
279
|
-
}).pipe(Effect.flatMap(decode(Comments, "comment list")));
|
|
280
|
-
const comment = comments.find(({ body }) => body.startsWith(MARKER));
|
|
281
|
-
if (comment !== undefined || comments.length < 100) {
|
|
282
|
-
return comment;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
/** Replaces the body of the comment of this command, or posts it on the first run, and gives its URL. */
|
|
288
|
-
export const saveStackComment = (args: {
|
|
289
|
-
readonly token: string;
|
|
290
|
-
readonly repository: string;
|
|
291
|
-
readonly pullRequest: number;
|
|
292
|
-
readonly body: string;
|
|
293
|
-
}) =>
|
|
294
|
-
Effect.gen(function* () {
|
|
295
|
-
const { body, pullRequest, repository, token } = args;
|
|
296
|
-
const comments = `/repos/${repository}/issues/${pullRequest}/comments`;
|
|
297
|
-
const comment = yield* findComment(token, comments);
|
|
298
|
-
const reply = yield* comment === undefined
|
|
299
|
-
? github(token, comments, { method: "POST", body: JSON.stringify({ body }) })
|
|
300
|
-
: github(token, `/repos/${repository}/issues/comments/${comment.id}`, {
|
|
301
|
-
method: "PATCH",
|
|
302
|
-
body: JSON.stringify({ body }),
|
|
303
|
-
});
|
|
304
|
-
const saved = yield* decode(SavedComment, "comment")(reply);
|
|
305
|
-
return saved.html_url;
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
/** The step of a workflow: the run record names the repository and the pull request of the run. */
|
|
309
|
-
export const updateStackComment = Effect.gen(function* () {
|
|
310
|
-
const {
|
|
311
|
-
pullRequest,
|
|
312
|
-
repositoryId,
|
|
313
|
-
repositoryName: repository,
|
|
314
|
-
} = yield* runRecord({ cwd: process.cwd(), env: process.env });
|
|
315
|
-
if (pullRequest === null) {
|
|
316
|
-
return yield* new StackCommentError({ reason: "this run has no pull request" });
|
|
317
|
-
}
|
|
318
|
-
const token = process.env["GITHUB_TOKEN"];
|
|
319
|
-
if (token === undefined || token === "") {
|
|
320
|
-
return yield* new StackCommentError({ reason: "GITHUB_TOKEN is not set" });
|
|
321
|
-
}
|
|
322
|
-
const cards = yield* readStackCards({ repository, repositoryId, stage: `pr-${pullRequest}` });
|
|
323
|
-
return yield* saveStackComment({
|
|
324
|
-
token,
|
|
325
|
-
repository,
|
|
326
|
-
pullRequest,
|
|
327
|
-
body: renderStackComment(cards),
|
|
328
|
-
});
|
|
329
|
-
});
|
package/src/cli.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// The command of the package:
|
|
3
|
-
//
|
|
4
|
-
// alchemy-convex stack-comment
|
|
5
|
-
//
|
|
6
|
-
// stack-comment runs as a step of a pull request workflow after the deploys, and keeps one comment
|
|
7
|
-
// with the stacks of the repository: see src/StackComment.ts. It needs GITHUB_TOKEN, and reads the
|
|
8
|
-
// store of the team of CONVEX_ACCESS_TOKEN or of the Convex CLI login.
|
|
9
|
-
import { parseArgs } from "node:util";
|
|
10
|
-
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
11
|
-
import * as Effect from "effect/Effect";
|
|
12
|
-
import * as Layer from "effect/Layer";
|
|
13
|
-
import { updateStackComment } from "./StackComment.ts";
|
|
14
|
-
import { state } from "./State.ts";
|
|
15
|
-
|
|
16
|
-
const { positionals } = parseArgs({ allowPositionals: true, options: {} });
|
|
17
|
-
const USAGE = `Usage:
|
|
18
|
-
alchemy-convex stack-comment`;
|
|
19
|
-
|
|
20
|
-
const stackComment = updateStackComment.pipe(
|
|
21
|
-
Effect.tap((url) => Effect.sync(() => console.log(url))),
|
|
22
|
-
Effect.provide(state().pipe(Layer.provide(NodeServices.layer))),
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
if (positionals.join(" ") !== "stack-comment") {
|
|
26
|
-
console.error(USAGE);
|
|
27
|
-
process.exit(1);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
await Effect.runPromise(stackComment).catch((error: unknown) => {
|
|
31
|
-
console.error(error instanceof Error ? error.message : String(error));
|
|
32
|
-
process.exit(1);
|
|
33
|
-
});
|