Skip to content
This repository was archived by the owner on Oct 31, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
which the store pushes itself. Convex codegen writes `src/store/convex/_generated/` and
`src/store/component/_generated/`. The store and its readers (Samebase) are released and upgraded
together; a renamed function is a version bump on both sides. See `docs/design/convex-state.md`.
- `alchemy-convex stack-comment` reads the store and writes a pull request comment. It reads Worker
and R2 rows of the store, and it depends on no Cloudflare package. GitHub removes most HTML, so
`src/StackComment.ts` names the reason for each choice of its markup.
- The Convex CLI owns typecheck, codegen, bundling, and the code push. `Convex.Deployment` only runs
the installed `convex deploy`. Do not move work of the Convex CLI into this package.
- No backwards compatibility with earlier versions of this package, its store, or its data:
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 0.18.0 (unreleased)

The command `alchemy-convex stack-comment` keeps one comment on a pull request with a card for each
stack of the repository at the stage of the pull request.

- Run it as a step of a pull request workflow after every deploy. It needs `CONVEX_ACCESS_TOKEN`,
`GITHUB_TOKEN`, and the permission `pull-requests: write`. The run record gives it the repository
and the pull request.
- A card shows the folder of the stack, its address, its last run, and its Convex deployments,
Workers, and R2 buckets. A stack without the stage of the pull request has no card.
- Remove a `GitHub.Comment` from the stack. A comment that a stack declares can show only that stack.

## 0.17.0 (unreleased)

The hub is the project `identity-hub`, a name that says what the project holds.
Expand Down
3 changes: 3 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ packages/alchemy, under the ISC license:
- the Convex CLI runner in src/Push.ts (internal/CommandRunner.ts),
- the deployment lookup rules in src/Deployment.ts and the project listing in src/Project.ts.

The Workers and R2 icons in assets/stack-comment are by Cloudflare, under CC BY 4.0
(https://creativecommons.org/licenses/by/4.0/), with Cloudflare orange fill.

ISC License

Copyright 2024 RJ Dellecese
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,33 @@ of the app, before the first push: it registers the deployment of that directory
npx alchemy-convex hub-client --team my-team --app scout --callback-path /auth/hub/callback
```

## Pull request comment

`alchemy-convex stack-comment` keeps one comment on a pull request. The comment has a card for each
stack of the repository at the stage `pr-<number>`: the folder, the address, the last run, and the
Convex deployments, Workers, and R2 buckets. A stack sees only itself, so a comment that a stack
declares cannot show this.

Run it after every deploy of the workflow. The run record gives the repository and the pull request,
so the command needs no argument with a team access token:

```yaml
permissions:
pull-requests: write

steps:
# The deploy steps of each stack.
- name: Update the stack comment
if: github.event_name == 'pull_request'
run: pnpm exec alchemy-convex stack-comment
env:
CONVEX_ACCESS_TOKEN: ${{ secrets.CONVEX_ACCESS_TOKEN }}
GITHUB_TOKEN: ${{ github.token }}
```

A stack named `owner/name`, or `owner/name/<folder>` for a stack in a folder, belongs to the
repository `owner/name`. A stack without the stage of the pull request has no card.

## Data safety

- `Convex.Project` and `Convex.Deployment` have the removal policy `retain`. `alchemy destroy`
Expand Down
1 change: 1 addition & 0 deletions assets/stack-comment/convex.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions assets/stack-comment/r2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/stack-comment/spacer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions assets/stack-comment/worker.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@samebase/alchemy-convex",
"version": "0.17.0",
"version": "0.18.0",
"description": "The Convex plane for Alchemy v2: projects, deployments, environment variables, and the push of the functions",
"keywords": [
"alchemy",
Expand Down
307 changes: 307 additions & 0 deletions src/StackComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
// The command `alchemy-convex stack-comment`: one comment on a pull request with the stacks of the
// repository, read from the state store after the deploys.
//
// A stack sees only itself, so a comment that a stack declares cannot show the other stacks of the
// repository. This command runs as a step after them and reads each stack named `owner/name` or
// `owner/name/<folder>` that has the stage `pr-<number>`. A stack without that stage has no card. A
// run replaces the whole body, so the last run shows the newest state.
//
// It reads only the fields of the rows below, because Alchemy state holds secrets in clear text.
//
// GitHub keeps a fixed set of HTML in a comment and removes `style` and `class`, so every choice of
// the markup is a fact about the sanitizer or the CSS of github.com:
// - A table has `display: block` and `width: max-content`, so a `width` on it does nothing. The
// spacer, a floated image one pixel high in a block of its own, gives every card one width, and
// it shrinks on a narrow page.
// - The CSS colors the even rows of a group, so each row has its own `tbody`.
// - An HTML block ends at a blank line, so a table has none and is not indented.
// - A bare image links to its raw file, and one link around an image and text underlines the space
// between them, so the icon and the name are two links to one place.
// - An image with `height` gets a gray background, so each icon is square and has only `width`.
// - `align="middle"` is `-webkit-baseline-middle` in Chrome, which is below the text.
// `align="absmiddle"` is the CSS value `middle`.
import { State } from "alchemy/State";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import { RUN_RECORD_KEY, runRecord } from "./RunRecord.ts";

/** The first text of the comment, which finds it again. */
const MARKER = "<!-- samebase-stacks -->";

/** GitHub does not proxy this host, so a new icon needs no purge of its image cache. */
const ICONS = "https://raw.githubusercontent.com/samebase/alchemy-convex/main/assets/stack-comment";

const GITHUB_API = "https://api.github.com";

/** The resources that a card shows. A row of any other type, or without `attr` from an interrupted deploy, is left out. */
const Resource = Schema.Union([
Schema.Struct({
resourceType: Schema.Literal("Convex.Deployment"),
attr: Schema.Struct({ name: Schema.String, siteUrl: Schema.String }),
}),
Schema.Struct({
resourceType: Schema.Literal("Cloudflare.Worker"),
attr: Schema.Struct({ workerName: Schema.String, url: Schema.optional(Schema.String) }),
}),
Schema.Struct({
resourceType: Schema.Literal("Cloudflare.R2.Bucket"),
attr: Schema.Struct({
bucketName: Schema.String,
accountId: Schema.String,
jurisdiction: Schema.String,
}),
}),
]);
type Resource = typeof Resource.Type;

/** The fields of the run record that a card shows. */
const StageOutput = Schema.Struct({
[RUN_RECORD_KEY]: Schema.Struct({
headCommit: Schema.String,
runUrl: Schema.NullOr(Schema.String),
recordedAt: Schema.String,
}),
});

export interface StackCard {
readonly name: string;
readonly stage: string;
readonly resources: readonly Resource[];
readonly run: typeof StageOutput.Type | null;
}

export class StackCommentError extends Schema.TaggedError<StackCommentError>()(
"StackCommentError",
{ reason: Schema.String },
) {
override get message() {
return `The stack comment cannot be updated: ${this.reason}`;
}
}

/** The cards of the stacks of `repository` that have `stage`, in name order. */
export const readStackCards = ({ repository, stage }: { repository: string; stage: string }) =>
Effect.gen(function* () {
const state = yield* yield* State;
const names = (yield* state.listStacks()).filter(
(name) => name === repository || name.startsWith(`${repository}/`),
);
const cards: StackCard[] = [];
for (const name of names) {
if (!(yield* state.listStages(name)).includes(stage)) {
continue;
}
const request = { stack: name, stage };
const rows = yield* Effect.forEach(yield* state.list(request), (fqn) =>
state.get({ ...request, fqn }),
);
const output = yield* state.getOutput(request);
cards.push({
name,
stage,
resources: rows.filter(Schema.is(Resource)),
run: Schema.is(StageOutput)(output) ? output : null,
});
}
return cards;
});

/** A stack can create a deployment in one resource and push to it in another: a card shows it once. */
const withoutRepeats = (resources: readonly Resource[]) => {
const deployments = new Set<string>();
return resources.filter((resource) => {
if (resource.resourceType !== "Convex.Deployment") {
return true;
}
const repeated = deployments.has(resource.attr.name);
deployments.add(resource.attr.name);
return !repeated;
});
};

const escapeHtml = (text: string) =>
text
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;")
.replaceAll('"', "&quot;");

const link = (href: string, text: string) => `<a href="${escapeHtml(href)}">${text}</a>`;

const named = (href: string, icon: string, alt: string, name: string) => {
const image = `<img src="${ICONS}/${icon}.svg" width="16" align="absmiddle" alt="${alt}">`;
return `${link(href, image)} ${link(href, escapeHtml(name))}`;
};

const hostOf = (url: string) => escapeHtml(new URL(url).host);

/** Where a stack serves: its Convex deployment, else its first Worker with a URL. */
const addressOf = (resources: readonly Resource[]) => {
for (const resource of resources) {
if (resource.resourceType === "Convex.Deployment") {
return resource.attr.siteUrl;
}
}
for (const resource of resources) {
if (resource.resourceType === "Cloudflare.Worker" && resource.attr.url !== undefined) {
return resource.attr.url;
}
}
return null;
};

const resourceRow = (resource: Resource, address: string | null) => {
switch (resource.resourceType) {
case "Convex.Deployment": {
const { name } = resource.attr;
return named(
`https://dashboard.convex.dev/d/${encodeURIComponent(name)}`,
"convex",
"Convex",
name,
);
}
case "Cloudflare.Worker": {
const { url, workerName } = resource.attr;
const row = named(
`https://dash.cloudflare.com/?to=/:account/workers/services/view/${encodeURIComponent(workerName)}`,
"worker",
"Worker",
workerName,
);
return url === undefined || url === address
? row
: `${row}<br><sub>${link(url, hostOf(url))}</sub>`;
}
case "Cloudflare.R2.Bucket": {
const { accountId, bucketName, jurisdiction } = resource.attr;
return named(
`https://dash.cloudflare.com/${encodeURIComponent(accountId)}/r2/${encodeURIComponent(jurisdiction)}/buckets/${encodeURIComponent(bucketName)}`,
"r2",
"R2 bucket",
bucketName,
);
}
}
};

const runLine = ({ run }: StackCard) => {
if (run === null) {
return "";
}
const { headCommit, recordedAt, runUrl } = run[RUN_RECORD_KEY];
const commit = escapeHtml(headCommit.slice(0, 7));
const time = `${recordedAt.slice(0, 16).replace("T", " ")} UTC`;
return `<br><sub>${runUrl === null ? commit : link(runUrl, commit)} ${escapeHtml(time)}</sub>`;
};

const SPACER = `<div><img src="${ICONS}/spacer.svg" width="460" align="left" alt=""></div>`;

/** `alone`: the one card of the repository. A card at the root then has no folder, as on the repository page. */
const renderCard = (card: StackCard, repository: string, alone: boolean) => {
const folder = card.name.slice(repository.length + 1);
const heading = [
folder === "" && alone ? "" : `<b>${escapeHtml(folder || "/")}</b> `,
`<sub>${escapeHtml(card.stage)}</sub>`,
].join("");
const resources = withoutRepeats(card.resources);
const address = addressOf(resources);
const addressLine = address === null ? "" : `<br>${link(address, hostOf(address))}`;
const rows = resources
.map((resource) => `<tbody><tr><td>${resourceRow(resource, address)}</td></tr></tbody>`)
.join("\n");
return `<table>
<tbody><tr><td>${SPACER}${heading}${addressLine}${runLine(card)}</td></tr></tbody>
${rows}
</table>`;
};

export const renderStackComment = (cards: readonly StackCard[], repository: string) => `${MARKER}
${cards.map((card) => renderCard(card, repository, cards.length === 1)).join("\n\n")}
`;

const Comments = Schema.Array(Schema.Struct({ id: Schema.Number, body: Schema.String }));
const SavedComment = Schema.Struct({ html_url: Schema.String });

const github = (token: string, path: string, init: { method: string; body?: string }) =>
Effect.tryPromise({
try: async (): Promise<unknown> => {
const response = await fetch(`${GITHUB_API}${path}`, {
...init,
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
"X-GitHub-Api-Version": "2022-11-28",
},
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return await response.json();
},
catch: (error) =>
new StackCommentError({
reason: `GitHub ${init.method} ${path} failed (${error instanceof Error ? error.message : String(error)})`,
}),
});

const decode =
<S extends Schema.Codec<unknown, unknown>>(schema: S, what: string) =>
(value: unknown) =>
Schema.decodeUnknownEffect(schema)(value).pipe(
Effect.mapError(() => new StackCommentError({ reason: `GitHub gave a bad ${what}` })),
);

const findComment = (token: string, path: string) =>
Effect.gen(function* () {
for (let page = 1; ; page += 1) {
const comments = yield* github(token, `${path}?per_page=100&page=${page}`, {
method: "GET",
}).pipe(Effect.flatMap(decode(Comments, "comment list")));
const comment = comments.find(({ body }) => body.startsWith(MARKER));
if (comment !== undefined || comments.length < 100) {
return comment;
}
}
});

/** Replaces the body of the comment of this command, or posts it on the first run, and gives its URL. */
export const saveStackComment = (args: {
readonly token: string;
readonly repository: string;
readonly pullRequest: number;
readonly body: string;
}) =>
Effect.gen(function* () {
const { body, pullRequest, repository, token } = args;
const comments = `/repos/${repository}/issues/${pullRequest}/comments`;
const comment = yield* findComment(token, comments);
const reply = yield* comment === undefined
? github(token, comments, { method: "POST", body: JSON.stringify({ body }) })
: github(token, `/repos/${repository}/issues/comments/${comment.id}`, {
method: "PATCH",
body: JSON.stringify({ body }),
});
const saved = yield* decode(SavedComment, "comment")(reply);
return saved.html_url;
});

/** The step of a workflow: the run record names the repository and the pull request of the run. */
export const updateStackComment = Effect.gen(function* () {
const { pullRequest, repository } = yield* runRecord({ cwd: process.cwd(), env: process.env });
if (pullRequest === null) {
return yield* new StackCommentError({ reason: "this run has no pull request" });
}
const token = process.env["GITHUB_TOKEN"];
if (token === undefined || token === "") {
return yield* new StackCommentError({ reason: "GITHUB_TOKEN is not set" });
}
const cards = yield* readStackCards({ repository, stage: `pr-${pullRequest}` });
return yield* saveStackComment({
token,
repository,
pullRequest,
body: renderStackComment(cards, repository),
});
});
Loading
Loading