@samebase/alchemy-convex 0.17.0 → 0.19.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/NOTICE +3 -0
- package/README.md +123 -69
- package/lib/{HubClient-BZKweNfD.js → HubClient-C0XxgkK3.js} +42 -7
- package/lib/HubClient-C0XxgkK3.js.map +1 -0
- package/lib/cli.js +215 -9
- package/lib/cli.js.map +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/sealedSecret-Bf8AeIp_.js +86 -0
- package/lib/sealedSecret-Bf8AeIp_.js.map +1 -0
- package/lib/sealedSecret.d.ts +37 -0
- package/lib/sealedSecret.d.ts.map +1 -0
- package/lib/sealedSecret.js +1 -0
- package/package.json +20 -19
- package/src/Project.ts +1 -1
- package/src/Push.ts +1 -1
- package/src/RunRecord.ts +1 -0
- package/src/StackComment.ts +308 -0
- package/src/State.ts +54 -1
- package/src/cli.ts +50 -32
- package/src/store/component/_generated/api.ts +2 -0
- package/src/store/component/_generated/component.ts +85 -1
- package/src/store/component/keys.ts +73 -0
- package/src/store/component/reader.ts +1 -0
- package/src/store/component/runRecord.ts +1 -0
- package/src/store/component/schema.ts +7 -0
- package/src/store/component/state.ts +86 -4
- package/src/store/convex/_generated/api.d.ts +2 -0
- package/src/store/convex/reader.ts +1 -0
- package/src/store/convex/sealing.ts +102 -0
- package/src/store/convex/state.ts +248 -22
- package/src/store/convex/version.ts +3 -1
- package/src/store/sealedSecret.ts +129 -0
- package/lib/HubClient-BZKweNfD.js.map +0 -1
|
@@ -0,0 +1,308 @@
|
|
|
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 named `owner/name` or
|
|
6
|
+
// `owner/name/<folder>` that has the stage `pr-<number>`. A stack without that stage has no card. A
|
|
7
|
+
// run replaces the whole body, so the last run shows the newest state.
|
|
8
|
+
//
|
|
9
|
+
// It reads only the fields of the rows below, because Alchemy state holds secrets in clear text.
|
|
10
|
+
//
|
|
11
|
+
// GitHub keeps a fixed set of HTML in a comment and removes `style` and `class`, so every choice of
|
|
12
|
+
// the markup is a fact about the sanitizer or the CSS of github.com:
|
|
13
|
+
// - A table has `display: block` and `width: max-content`, so a `width` on it does nothing. The
|
|
14
|
+
// spacer, a floated image one pixel high in a block of its own, gives every card one width, and
|
|
15
|
+
// it shrinks on a narrow page.
|
|
16
|
+
// - The CSS colors the even rows of a group, so each row has its own `tbody`.
|
|
17
|
+
// - An HTML block ends at a blank line, so a table has none and is not indented.
|
|
18
|
+
// - A bare image links to its raw file, and one link around an image and text underlines the space
|
|
19
|
+
// between them, so the icon and the name are two links to one place.
|
|
20
|
+
// - An image with `height` gets a gray background, so each icon is square and has only `width`.
|
|
21
|
+
// - `align="middle"` is `-webkit-baseline-middle` in Chrome, which is below the text.
|
|
22
|
+
// `align="absmiddle"` is the CSS value `middle`.
|
|
23
|
+
import { State } from "alchemy/State";
|
|
24
|
+
import * as Effect from "effect/Effect";
|
|
25
|
+
import * as Schema from "effect/Schema";
|
|
26
|
+
import { RUN_RECORD_KEY, runRecord } from "./RunRecord.ts";
|
|
27
|
+
|
|
28
|
+
/** The first text of the comment, which finds it again. */
|
|
29
|
+
const MARKER = "<!-- samebase-stacks -->";
|
|
30
|
+
|
|
31
|
+
/** GitHub does not proxy this host, so a new icon needs no purge of its image cache. */
|
|
32
|
+
const ICONS =
|
|
33
|
+
"https://raw.githubusercontent.com/samebase/samebase/main/packages/alchemy-convex/assets/stack-comment";
|
|
34
|
+
|
|
35
|
+
const GITHUB_API = "https://api.github.com";
|
|
36
|
+
|
|
37
|
+
/** The resources that a card shows. A row of any other type, or without `attr` from an interrupted deploy, is left out. */
|
|
38
|
+
const Resource = Schema.Union([
|
|
39
|
+
Schema.Struct({
|
|
40
|
+
resourceType: Schema.Literal("Convex.Deployment"),
|
|
41
|
+
attr: Schema.Struct({ name: Schema.String, siteUrl: Schema.String }),
|
|
42
|
+
}),
|
|
43
|
+
Schema.Struct({
|
|
44
|
+
resourceType: Schema.Literal("Cloudflare.Worker"),
|
|
45
|
+
attr: Schema.Struct({ workerName: Schema.String, url: Schema.optional(Schema.String) }),
|
|
46
|
+
}),
|
|
47
|
+
Schema.Struct({
|
|
48
|
+
resourceType: Schema.Literal("Cloudflare.R2.Bucket"),
|
|
49
|
+
attr: Schema.Struct({
|
|
50
|
+
bucketName: Schema.String,
|
|
51
|
+
accountId: Schema.String,
|
|
52
|
+
jurisdiction: Schema.String,
|
|
53
|
+
}),
|
|
54
|
+
}),
|
|
55
|
+
]);
|
|
56
|
+
type Resource = typeof Resource.Type;
|
|
57
|
+
|
|
58
|
+
/** The fields of the run record that a card shows. */
|
|
59
|
+
const StageOutput = Schema.Struct({
|
|
60
|
+
[RUN_RECORD_KEY]: Schema.Struct({
|
|
61
|
+
headCommit: Schema.String,
|
|
62
|
+
runUrl: Schema.NullOr(Schema.String),
|
|
63
|
+
recordedAt: Schema.String,
|
|
64
|
+
}),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
export interface StackCard {
|
|
68
|
+
readonly name: string;
|
|
69
|
+
readonly stage: string;
|
|
70
|
+
readonly resources: readonly Resource[];
|
|
71
|
+
readonly run: typeof StageOutput.Type | null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class StackCommentError extends Schema.TaggedError<StackCommentError>()(
|
|
75
|
+
"StackCommentError",
|
|
76
|
+
{ reason: Schema.String },
|
|
77
|
+
) {
|
|
78
|
+
override get message() {
|
|
79
|
+
return `The stack comment cannot be updated: ${this.reason}`;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The cards of the stacks of `repository` that have `stage`, in name order. */
|
|
84
|
+
export const readStackCards = ({ repository, stage }: { repository: string; stage: string }) =>
|
|
85
|
+
Effect.gen(function* () {
|
|
86
|
+
const state = yield* yield* State;
|
|
87
|
+
const names = (yield* state.listStacks()).filter(
|
|
88
|
+
(name) => name === repository || name.startsWith(`${repository}/`),
|
|
89
|
+
);
|
|
90
|
+
const cards: StackCard[] = [];
|
|
91
|
+
for (const name of names) {
|
|
92
|
+
if (!(yield* state.listStages(name)).includes(stage)) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const request = { stack: name, stage };
|
|
96
|
+
const rows = yield* Effect.forEach(yield* state.list(request), (fqn) =>
|
|
97
|
+
state.get({ ...request, fqn }),
|
|
98
|
+
);
|
|
99
|
+
const output = yield* state.getOutput(request);
|
|
100
|
+
cards.push({
|
|
101
|
+
name,
|
|
102
|
+
stage,
|
|
103
|
+
resources: rows.filter(Schema.is(Resource)),
|
|
104
|
+
run: Schema.is(StageOutput)(output) ? output : null,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return cards;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/** A stack can create a deployment in one resource and push to it in another: a card shows it once. */
|
|
111
|
+
const withoutRepeats = (resources: readonly Resource[]) => {
|
|
112
|
+
const deployments = new Set<string>();
|
|
113
|
+
return resources.filter((resource) => {
|
|
114
|
+
if (resource.resourceType !== "Convex.Deployment") {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
const repeated = deployments.has(resource.attr.name);
|
|
118
|
+
deployments.add(resource.attr.name);
|
|
119
|
+
return !repeated;
|
|
120
|
+
});
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const escapeHtml = (text: string) =>
|
|
124
|
+
text
|
|
125
|
+
.replaceAll("&", "&")
|
|
126
|
+
.replaceAll("<", "<")
|
|
127
|
+
.replaceAll(">", ">")
|
|
128
|
+
.replaceAll('"', """);
|
|
129
|
+
|
|
130
|
+
const link = (href: string, text: string) => `<a href="${escapeHtml(href)}">${text}</a>`;
|
|
131
|
+
|
|
132
|
+
const named = (href: string, icon: string, alt: string, name: string) => {
|
|
133
|
+
const image = `<img src="${ICONS}/${icon}.svg" width="16" align="absmiddle" alt="${alt}">`;
|
|
134
|
+
return `${link(href, image)} ${link(href, escapeHtml(name))}`;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const hostOf = (url: string) => escapeHtml(new URL(url).host);
|
|
138
|
+
|
|
139
|
+
/** Where a stack serves: its Convex deployment, else its first Worker with a URL. */
|
|
140
|
+
const addressOf = (resources: readonly Resource[]) => {
|
|
141
|
+
for (const resource of resources) {
|
|
142
|
+
if (resource.resourceType === "Convex.Deployment") {
|
|
143
|
+
return resource.attr.siteUrl;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (const resource of resources) {
|
|
147
|
+
if (resource.resourceType === "Cloudflare.Worker" && resource.attr.url !== undefined) {
|
|
148
|
+
return resource.attr.url;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const resourceRow = (resource: Resource, address: string | null) => {
|
|
155
|
+
switch (resource.resourceType) {
|
|
156
|
+
case "Convex.Deployment": {
|
|
157
|
+
const { name } = resource.attr;
|
|
158
|
+
return named(
|
|
159
|
+
`https://dashboard.convex.dev/d/${encodeURIComponent(name)}`,
|
|
160
|
+
"convex",
|
|
161
|
+
"Convex",
|
|
162
|
+
name,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
case "Cloudflare.Worker": {
|
|
166
|
+
const { url, workerName } = resource.attr;
|
|
167
|
+
const row = named(
|
|
168
|
+
`https://dash.cloudflare.com/?to=/:account/workers/services/view/${encodeURIComponent(workerName)}`,
|
|
169
|
+
"worker",
|
|
170
|
+
"Worker",
|
|
171
|
+
workerName,
|
|
172
|
+
);
|
|
173
|
+
return url === undefined || url === address
|
|
174
|
+
? row
|
|
175
|
+
: `${row}<br><sub>${link(url, hostOf(url))}</sub>`;
|
|
176
|
+
}
|
|
177
|
+
case "Cloudflare.R2.Bucket": {
|
|
178
|
+
const { accountId, bucketName, jurisdiction } = resource.attr;
|
|
179
|
+
return named(
|
|
180
|
+
`https://dash.cloudflare.com/${encodeURIComponent(accountId)}/r2/${encodeURIComponent(jurisdiction)}/buckets/${encodeURIComponent(bucketName)}`,
|
|
181
|
+
"r2",
|
|
182
|
+
"R2 bucket",
|
|
183
|
+
bucketName,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const runLine = ({ run }: StackCard) => {
|
|
190
|
+
if (run === null) {
|
|
191
|
+
return "";
|
|
192
|
+
}
|
|
193
|
+
const { headCommit, recordedAt, runUrl } = run[RUN_RECORD_KEY];
|
|
194
|
+
const commit = escapeHtml(headCommit.slice(0, 7));
|
|
195
|
+
const time = `${recordedAt.slice(0, 16).replace("T", " ")} UTC`;
|
|
196
|
+
return `<br><sub>${runUrl === null ? commit : link(runUrl, commit)} ${escapeHtml(time)}</sub>`;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const SPACER = `<div><img src="${ICONS}/spacer.svg" width="460" align="left" alt=""></div>`;
|
|
200
|
+
|
|
201
|
+
/** `alone`: the one card of the repository. A card at the root then has no folder, as on the repository page. */
|
|
202
|
+
const renderCard = (card: StackCard, repository: string, alone: boolean) => {
|
|
203
|
+
const folder = card.name.slice(repository.length + 1);
|
|
204
|
+
const heading = [
|
|
205
|
+
folder === "" && alone ? "" : `<b>${escapeHtml(folder || "/")}</b> `,
|
|
206
|
+
`<sub>${escapeHtml(card.stage)}</sub>`,
|
|
207
|
+
].join("");
|
|
208
|
+
const resources = withoutRepeats(card.resources);
|
|
209
|
+
const address = addressOf(resources);
|
|
210
|
+
const addressLine = address === null ? "" : `<br>${link(address, hostOf(address))}`;
|
|
211
|
+
const rows = resources
|
|
212
|
+
.map((resource) => `<tbody><tr><td>${resourceRow(resource, address)}</td></tr></tbody>`)
|
|
213
|
+
.join("\n");
|
|
214
|
+
return `<table>
|
|
215
|
+
<tbody><tr><td>${SPACER}${heading}${addressLine}${runLine(card)}</td></tr></tbody>
|
|
216
|
+
${rows}
|
|
217
|
+
</table>`;
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
export const renderStackComment = (cards: readonly StackCard[], repository: string) => `${MARKER}
|
|
221
|
+
${cards.map((card) => renderCard(card, repository, cards.length === 1)).join("\n\n")}
|
|
222
|
+
`;
|
|
223
|
+
|
|
224
|
+
const Comments = Schema.Array(Schema.Struct({ id: Schema.Number, body: Schema.String }));
|
|
225
|
+
const SavedComment = Schema.Struct({ html_url: Schema.String });
|
|
226
|
+
|
|
227
|
+
const github = (token: string, path: string, init: { method: string; body?: string }) =>
|
|
228
|
+
Effect.tryPromise({
|
|
229
|
+
try: async (): Promise<unknown> => {
|
|
230
|
+
const response = await fetch(`${GITHUB_API}${path}`, {
|
|
231
|
+
...init,
|
|
232
|
+
headers: {
|
|
233
|
+
Accept: "application/vnd.github+json",
|
|
234
|
+
Authorization: `Bearer ${token}`,
|
|
235
|
+
"Content-Type": "application/json",
|
|
236
|
+
"X-GitHub-Api-Version": "2022-11-28",
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
if (!response.ok) {
|
|
240
|
+
throw new Error(`HTTP ${response.status}`);
|
|
241
|
+
}
|
|
242
|
+
return await response.json();
|
|
243
|
+
},
|
|
244
|
+
catch: (error) =>
|
|
245
|
+
new StackCommentError({
|
|
246
|
+
reason: `GitHub ${init.method} ${path} failed (${error instanceof Error ? error.message : String(error)})`,
|
|
247
|
+
}),
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const decode =
|
|
251
|
+
<S extends Schema.Codec<unknown, unknown>>(schema: S, what: string) =>
|
|
252
|
+
(value: unknown) =>
|
|
253
|
+
Schema.decodeUnknownEffect(schema)(value).pipe(
|
|
254
|
+
Effect.mapError(() => new StackCommentError({ reason: `GitHub gave a bad ${what}` })),
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
const findComment = (token: string, path: string) =>
|
|
258
|
+
Effect.gen(function* () {
|
|
259
|
+
for (let page = 1; ; page += 1) {
|
|
260
|
+
const comments = yield* github(token, `${path}?per_page=100&page=${page}`, {
|
|
261
|
+
method: "GET",
|
|
262
|
+
}).pipe(Effect.flatMap(decode(Comments, "comment list")));
|
|
263
|
+
const comment = comments.find(({ body }) => body.startsWith(MARKER));
|
|
264
|
+
if (comment !== undefined || comments.length < 100) {
|
|
265
|
+
return comment;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
/** Replaces the body of the comment of this command, or posts it on the first run, and gives its URL. */
|
|
271
|
+
export const saveStackComment = (args: {
|
|
272
|
+
readonly token: string;
|
|
273
|
+
readonly repository: string;
|
|
274
|
+
readonly pullRequest: number;
|
|
275
|
+
readonly body: string;
|
|
276
|
+
}) =>
|
|
277
|
+
Effect.gen(function* () {
|
|
278
|
+
const { body, pullRequest, repository, token } = args;
|
|
279
|
+
const comments = `/repos/${repository}/issues/${pullRequest}/comments`;
|
|
280
|
+
const comment = yield* findComment(token, comments);
|
|
281
|
+
const reply = yield* comment === undefined
|
|
282
|
+
? github(token, comments, { method: "POST", body: JSON.stringify({ body }) })
|
|
283
|
+
: github(token, `/repos/${repository}/issues/comments/${comment.id}`, {
|
|
284
|
+
method: "PATCH",
|
|
285
|
+
body: JSON.stringify({ body }),
|
|
286
|
+
});
|
|
287
|
+
const saved = yield* decode(SavedComment, "comment")(reply);
|
|
288
|
+
return saved.html_url;
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
/** The step of a workflow: the run record names the repository and the pull request of the run. */
|
|
292
|
+
export const updateStackComment = Effect.gen(function* () {
|
|
293
|
+
const { pullRequest, repository } = yield* runRecord({ cwd: process.cwd(), env: process.env });
|
|
294
|
+
if (pullRequest === null) {
|
|
295
|
+
return yield* new StackCommentError({ reason: "this run has no pull request" });
|
|
296
|
+
}
|
|
297
|
+
const token = process.env["GITHUB_TOKEN"];
|
|
298
|
+
if (token === undefined || token === "") {
|
|
299
|
+
return yield* new StackCommentError({ reason: "GITHUB_TOKEN is not set" });
|
|
300
|
+
}
|
|
301
|
+
const cards = yield* readStackCards({ repository, stage: `pr-${pullRequest}` });
|
|
302
|
+
return yield* saveStackComment({
|
|
303
|
+
token,
|
|
304
|
+
repository,
|
|
305
|
+
pullRequest,
|
|
306
|
+
body: renderStackComment(cards, repository),
|
|
307
|
+
});
|
|
308
|
+
});
|
package/src/State.ts
CHANGED
|
@@ -19,6 +19,10 @@
|
|
|
19
19
|
// repository-scoped subscriptions with secret values removed. A key that stops working during a
|
|
20
20
|
// long run (it expires after two hours) is replaced once per call.
|
|
21
21
|
//
|
|
22
|
+
// The store seals each `Redacted` value in its own functions, with a key in a variable of its
|
|
23
|
+
// deployment, so this client sends and gets clear text. Each run asks whether the sealing is
|
|
24
|
+
// finished, and finishes it: see `ensureSealed`.
|
|
25
|
+
//
|
|
22
26
|
// Two first runs of a team can both create a project named `alchemy-state-store`.
|
|
23
27
|
// The run whose project is not the oldest deletes its own, before it writes
|
|
24
28
|
// anything, and uses the oldest: both runs end on one store.
|
|
@@ -75,9 +79,12 @@ import {
|
|
|
75
79
|
push,
|
|
76
80
|
PUSH_KEY_ACTIONS,
|
|
77
81
|
withPushKey,
|
|
82
|
+
writeVariables,
|
|
78
83
|
} from "./Push.ts";
|
|
79
84
|
import { type RunRecord, runRecord, withRunRecord } from "./RunRecord.ts";
|
|
85
|
+
import { stateKeyName } from "./store/convex/sealing.ts";
|
|
80
86
|
import { STORE_VERSION } from "./store/convex/version.ts";
|
|
87
|
+
import { newKey, newKeyId } from "./store/sealedSecret.ts";
|
|
81
88
|
|
|
82
89
|
/** The name of the state project in each team. */
|
|
83
90
|
export const STORE_PROJECT_NAME = "alchemy-state-store";
|
|
@@ -92,6 +99,9 @@ export const RUN_KEY_ACTIONS = [
|
|
|
92
99
|
"deployment:functions:runInternalMutations",
|
|
93
100
|
] as const;
|
|
94
101
|
|
|
102
|
+
/** The actions of the key of one run on the store: its writes are actions that seal. */
|
|
103
|
+
const STORE_KEY_ACTIONS = [...RUN_KEY_ACTIONS, "deployment:functions:runInternalActions"] as const;
|
|
104
|
+
|
|
95
105
|
/** The body of POST /api/function, with HTTP 200 for a function error too. */
|
|
96
106
|
const Reply = Schema.Union([
|
|
97
107
|
Schema.Struct({ status: Schema.Literal("success"), value: Schema.Unknown }),
|
|
@@ -152,6 +162,12 @@ const decode =
|
|
|
152
162
|
(value: unknown) =>
|
|
153
163
|
Schema.decodeUnknownEffect(schema)(value).pipe(Effect.mapError(() => failure(`bad ${what}`)));
|
|
154
164
|
|
|
165
|
+
/** The reply of `state:sealing`. */
|
|
166
|
+
const Sealing = Schema.Struct({ current: Schema.NullOr(Schema.String), sealed: Schema.Boolean });
|
|
167
|
+
|
|
168
|
+
/** The reply of `state:reseal`. */
|
|
169
|
+
const Resealed = Schema.Struct({ failed: Schema.Array(Schema.String) });
|
|
170
|
+
|
|
155
171
|
/** The function API of the store deployment, with a deploy key. */
|
|
156
172
|
export interface FunctionApi {
|
|
157
173
|
/** POST /api/function, the call that `convex run` makes: the reply, which can be a function error. */
|
|
@@ -295,6 +311,41 @@ const pushStore = (
|
|
|
295
311
|
);
|
|
296
312
|
});
|
|
297
313
|
|
|
314
|
+
/**
|
|
315
|
+
* Finishes the sealing of the store, whatever an earlier run left: a new key when the store has
|
|
316
|
+
* none, then a reseal unless one finished under the current key. A run reads this each time, so a
|
|
317
|
+
* run that stopped after a push cannot leave rows in clear text. This client makes a key once, and
|
|
318
|
+
* it never reads a key back.
|
|
319
|
+
*/
|
|
320
|
+
const ensureSealed = (
|
|
321
|
+
options: StoreOptions,
|
|
322
|
+
deployment: { readonly name: string; readonly deploymentUrl: string },
|
|
323
|
+
functions: FunctionApi,
|
|
324
|
+
) =>
|
|
325
|
+
Effect.gen(function* () {
|
|
326
|
+
const { current, sealed } = yield* functions
|
|
327
|
+
.call("state:sealing", {})
|
|
328
|
+
.pipe(Effect.flatMap(valueOf), Effect.flatMap(decode(Sealing, "state:sealing")));
|
|
329
|
+
if (current === null) {
|
|
330
|
+
const keyId = newKeyId();
|
|
331
|
+
yield* withPushKey(options.api, deployment.name, PUSH_KEY_ACTIONS, (key) =>
|
|
332
|
+
writeVariables(deployment.deploymentUrl, key, [
|
|
333
|
+
{ name: stateKeyName(keyId), value: newKey() },
|
|
334
|
+
]),
|
|
335
|
+
);
|
|
336
|
+
yield* functions.call("state:chooseKey", { keyId }).pipe(Effect.flatMap(valueOf));
|
|
337
|
+
}
|
|
338
|
+
if (sealed) return;
|
|
339
|
+
const { failed } = yield* functions
|
|
340
|
+
.call("state:reseal", {})
|
|
341
|
+
.pipe(Effect.flatMap(valueOf), Effect.flatMap(decode(Resealed, "state:reseal")));
|
|
342
|
+
if (failed.length > 0) {
|
|
343
|
+
yield* Effect.logWarning(
|
|
344
|
+
`Convex state: the store could not seal ${failed.join(", ")}. The next run tries again.`,
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
|
|
298
349
|
/**
|
|
299
350
|
* Creates the state project of the team. Two first runs can both create one:
|
|
300
351
|
* the run whose project is not the oldest deletes its own, which holds
|
|
@@ -357,7 +408,7 @@ export const makeConvexState = (options: StoreOptions) =>
|
|
|
357
408
|
}),
|
|
358
409
|
),
|
|
359
410
|
);
|
|
360
|
-
const mint = createDeployKey(api, deployment.name, STORE_PROJECT_NAME,
|
|
411
|
+
const mint = createDeployKey(api, deployment.name, STORE_PROJECT_NAME, STORE_KEY_ACTIONS).pipe(
|
|
361
412
|
Effect.map((key) => {
|
|
362
413
|
keys.push(key);
|
|
363
414
|
return makeFunctionApi(deployment.deploymentUrl, key);
|
|
@@ -401,6 +452,8 @@ export const makeConvexState = (options: StoreOptions) =>
|
|
|
401
452
|
);
|
|
402
453
|
}
|
|
403
454
|
|
|
455
|
+
yield* ensureSealed(options, deployment, functions);
|
|
456
|
+
|
|
404
457
|
const remote = remoteOf(functions, options.run);
|
|
405
458
|
/** The stack's output plus the record, when the run has one and the output has a place for it. */
|
|
406
459
|
const documentOf = (request: { stack: string; stage: string; value: unknown }) =>
|
package/src/cli.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The
|
|
2
|
+
// The commands of the package:
|
|
3
3
|
//
|
|
4
4
|
// alchemy-convex hub-client --team <team> --app <app> --callback-path <path>
|
|
5
|
+
// alchemy-convex stack-comment [--team <team>]
|
|
5
6
|
//
|
|
6
|
-
//
|
|
7
|
-
// a client of the identity hub of the team: see registerDevelopmentClient.
|
|
8
|
-
//
|
|
7
|
+
// hub-client runs in the directory of a Convex app, and gives the development deployment of that
|
|
8
|
+
// directory a client of the identity hub of the team: see registerDevelopmentClient. stack-comment
|
|
9
|
+
// runs as a step of a pull request workflow after the deploys, and keeps one comment with the stacks
|
|
10
|
+
// of the repository: see src/StackComment.ts. It needs GITHUB_TOKEN and, with a team access token,
|
|
11
|
+
// no --team. Both use CONVEX_ACCESS_TOKEN or the Convex CLI login.
|
|
9
12
|
import { execFileSync } from "node:child_process";
|
|
10
13
|
import { parseArgs } from "node:util";
|
|
14
|
+
import * as NodeServices from "@effect/platform-node/NodeServices";
|
|
11
15
|
import * as Effect from "effect/Effect";
|
|
12
16
|
import * as Layer from "effect/Layer";
|
|
13
17
|
import { fromConfig } from "./Credentials.ts";
|
|
14
18
|
import { registerDevelopmentClient } from "./HubClient.ts";
|
|
15
19
|
import { ManagementApiLive } from "./ManagementApi.ts";
|
|
16
20
|
import { installedCli } from "./Push.ts";
|
|
21
|
+
import { updateStackComment } from "./StackComment.ts";
|
|
22
|
+
import { state } from "./State.ts";
|
|
17
23
|
|
|
18
24
|
const { positionals, values } = parseArgs({
|
|
19
25
|
allowPositionals: true,
|
|
@@ -24,38 +30,50 @@ const { positionals, values } = parseArgs({
|
|
|
24
30
|
},
|
|
25
31
|
});
|
|
26
32
|
const { team, app, "callback-path": callbackPath } = values;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
) {
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
const USAGE = `Usage:
|
|
34
|
+
alchemy-convex hub-client --team <team> --app <app> --callback-path <path>
|
|
35
|
+
alchemy-convex stack-comment [--team <team>]`;
|
|
36
|
+
|
|
37
|
+
const hubClient = (hub: { team: string; app: string; callbackPath: string }) =>
|
|
38
|
+
Effect.gen(function* () {
|
|
39
|
+
const [command, ...prefix] = yield* installedCli(process.cwd());
|
|
40
|
+
const callbackUrl = yield* registerDevelopmentClient({
|
|
41
|
+
...hub,
|
|
42
|
+
convex: (args, input) =>
|
|
43
|
+
execFileSync(command, [...prefix, ...args], {
|
|
44
|
+
encoding: "utf8",
|
|
45
|
+
input,
|
|
46
|
+
stdio: ["pipe", "pipe", "inherit"],
|
|
47
|
+
}).trim(),
|
|
48
|
+
});
|
|
49
|
+
console.log(
|
|
50
|
+
callbackUrl === undefined
|
|
51
|
+
? "Hub client already registered."
|
|
52
|
+
: `Hub client registered for ${callbackUrl}.`,
|
|
53
|
+
);
|
|
54
|
+
}).pipe(Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))));
|
|
55
|
+
|
|
56
|
+
const stackComment = (options: { team?: string }) =>
|
|
57
|
+
updateStackComment.pipe(
|
|
58
|
+
Effect.tap((url) => Effect.sync(() => console.log(url))),
|
|
59
|
+
Effect.provide(state(options).pipe(Layer.provide(NodeServices.layer))),
|
|
35
60
|
);
|
|
61
|
+
|
|
62
|
+
const subcommand = positionals.join(" ");
|
|
63
|
+
const program: Effect.Effect<unknown, unknown> | undefined =
|
|
64
|
+
subcommand === "stack-comment"
|
|
65
|
+
? stackComment(team === undefined ? {} : { team })
|
|
66
|
+
: subcommand === "hub-client" &&
|
|
67
|
+
team !== undefined &&
|
|
68
|
+
app !== undefined &&
|
|
69
|
+
callbackPath !== undefined
|
|
70
|
+
? hubClient({ team, app, callbackPath })
|
|
71
|
+
: undefined;
|
|
72
|
+
if (program === undefined) {
|
|
73
|
+
console.error(USAGE);
|
|
36
74
|
process.exit(1);
|
|
37
75
|
}
|
|
38
76
|
|
|
39
|
-
const program = Effect.gen(function* () {
|
|
40
|
-
const [command, ...prefix] = yield* installedCli(process.cwd());
|
|
41
|
-
const callbackUrl = yield* registerDevelopmentClient({
|
|
42
|
-
team,
|
|
43
|
-
app,
|
|
44
|
-
callbackPath,
|
|
45
|
-
convex: (args, input) =>
|
|
46
|
-
execFileSync(command, [...prefix, ...args], {
|
|
47
|
-
encoding: "utf8",
|
|
48
|
-
input,
|
|
49
|
-
stdio: ["pipe", "pipe", "inherit"],
|
|
50
|
-
}).trim(),
|
|
51
|
-
});
|
|
52
|
-
console.log(
|
|
53
|
-
callbackUrl === undefined
|
|
54
|
-
? "Hub client already registered."
|
|
55
|
-
: `Hub client registered for ${callbackUrl}.`,
|
|
56
|
-
);
|
|
57
|
-
}).pipe(Effect.provide(ManagementApiLive().pipe(Layer.provide(fromConfig()))));
|
|
58
|
-
|
|
59
77
|
await Effect.runPromise(program).catch((error: unknown) => {
|
|
60
78
|
console.error(error instanceof Error ? error.message : String(error));
|
|
61
79
|
process.exit(1);
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* @module
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
import type * as keys from "../keys.js";
|
|
11
12
|
import type * as reader from "../reader.js";
|
|
12
13
|
import type * as runRecord from "../runRecord.js";
|
|
13
14
|
import type * as state from "../state.js";
|
|
@@ -20,6 +21,7 @@ import type {
|
|
|
20
21
|
import { anyApi, componentsGeneric } from "convex/server";
|
|
21
22
|
|
|
22
23
|
const fullApi: ApiFromModules<{
|
|
24
|
+
keys: typeof keys;
|
|
23
25
|
reader: typeof reader;
|
|
24
26
|
runRecord: typeof runRecord;
|
|
25
27
|
state: typeof state;
|
|
@@ -23,6 +23,36 @@ import type { FunctionReference } from "convex/server";
|
|
|
23
23
|
*/
|
|
24
24
|
export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
25
25
|
{
|
|
26
|
+
keys: {
|
|
27
|
+
choose: FunctionReference<
|
|
28
|
+
"mutation",
|
|
29
|
+
"internal",
|
|
30
|
+
{ keyId: string },
|
|
31
|
+
string,
|
|
32
|
+
Name
|
|
33
|
+
>;
|
|
34
|
+
markSealed: FunctionReference<
|
|
35
|
+
"mutation",
|
|
36
|
+
"internal",
|
|
37
|
+
{ generation: number },
|
|
38
|
+
null,
|
|
39
|
+
Name
|
|
40
|
+
>;
|
|
41
|
+
rotate: FunctionReference<
|
|
42
|
+
"mutation",
|
|
43
|
+
"internal",
|
|
44
|
+
{ keyId: string },
|
|
45
|
+
null,
|
|
46
|
+
Name
|
|
47
|
+
>;
|
|
48
|
+
sealing: FunctionReference<
|
|
49
|
+
"query",
|
|
50
|
+
"internal",
|
|
51
|
+
{},
|
|
52
|
+
{ current: string | null; generation: number; sealed: boolean },
|
|
53
|
+
Name
|
|
54
|
+
>;
|
|
55
|
+
};
|
|
26
56
|
reader: {
|
|
27
57
|
repository: FunctionReference<
|
|
28
58
|
"query",
|
|
@@ -76,6 +106,17 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
76
106
|
Array<string>,
|
|
77
107
|
Name
|
|
78
108
|
>;
|
|
109
|
+
outputPage: FunctionReference<
|
|
110
|
+
"query",
|
|
111
|
+
"internal",
|
|
112
|
+
{ cursor: string | null },
|
|
113
|
+
{
|
|
114
|
+
continueCursor: string;
|
|
115
|
+
isDone: boolean;
|
|
116
|
+
page: Array<{ stack: string; stage: string; value: string }>;
|
|
117
|
+
},
|
|
118
|
+
Name
|
|
119
|
+
>;
|
|
79
120
|
readStack: FunctionReference<
|
|
80
121
|
"query",
|
|
81
122
|
"internal",
|
|
@@ -133,7 +174,50 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
133
174
|
"query",
|
|
134
175
|
"internal",
|
|
135
176
|
{ stack: string; stage: string },
|
|
136
|
-
Array<string>,
|
|
177
|
+
Array<{ fqn: string; value: string }>,
|
|
178
|
+
Name
|
|
179
|
+
>;
|
|
180
|
+
replaceOutput: FunctionReference<
|
|
181
|
+
"mutation",
|
|
182
|
+
"internal",
|
|
183
|
+
{
|
|
184
|
+
expected: string;
|
|
185
|
+
generation: number;
|
|
186
|
+
stack: string;
|
|
187
|
+
stage: string;
|
|
188
|
+
value: string;
|
|
189
|
+
},
|
|
190
|
+
"replaced" | "changed" | "stale",
|
|
191
|
+
Name
|
|
192
|
+
>;
|
|
193
|
+
replaceResource: FunctionReference<
|
|
194
|
+
"mutation",
|
|
195
|
+
"internal",
|
|
196
|
+
{
|
|
197
|
+
expected: string;
|
|
198
|
+
fqn: string;
|
|
199
|
+
generation: number;
|
|
200
|
+
stack: string;
|
|
201
|
+
stage: string;
|
|
202
|
+
value: string;
|
|
203
|
+
},
|
|
204
|
+
"replaced" | "changed" | "stale",
|
|
205
|
+
Name
|
|
206
|
+
>;
|
|
207
|
+
resourcePage: FunctionReference<
|
|
208
|
+
"query",
|
|
209
|
+
"internal",
|
|
210
|
+
{ cursor: string | null },
|
|
211
|
+
{
|
|
212
|
+
continueCursor: string;
|
|
213
|
+
isDone: boolean;
|
|
214
|
+
page: Array<{
|
|
215
|
+
fqn: string;
|
|
216
|
+
stack: string;
|
|
217
|
+
stage: string;
|
|
218
|
+
value: string;
|
|
219
|
+
}>;
|
|
220
|
+
},
|
|
137
221
|
Name
|
|
138
222
|
>;
|
|
139
223
|
set: FunctionReference<
|