The best way to control Claude Code, Codex and more from any computer or phone

main
ts 138 lines 5.56 KB
Raw
1 // Posts a release announcement to Discord as a single embed. The body is
2 // read verbatim from the CHANGELOG.md entry the release-notes step just
3 // wrote, so Discord matches the GitHub Release copy exactly.
4 //
5 // The webhook posts under `<RepoName> Releases` (e.g. "Alchemy-Effect
6 // Releases", "Distilled Releases"). The embed title uses the first
7 // hyphen-segment of the repo name as the project prefix
8 // (alchemy-effect → "Alchemy", distilled → "Distilled") and omits the
9 // channel suffix for the stable `release` channel so you don't get
10 // "...(release) released" doubling up.
11 //
12 // alchemy-effect + beta → Alchemy v2.0.0-beta.42 (beta) released
13 // distilled + release → Distilled v0.21.3 released
14 //
15 // Reads DISCORD_WEBHOOK from the environment and no-ops if it is unset.
16 // Reads `ALCHEMY_REPO` (`<owner>/<repo>`) for the GitHub links.
17 //
18 // Usage: node scripts/release/discord-notify.ts <tag> <release|beta|alpha|rc|tag>
19 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
20 import * as NodeServices from "@effect/platform-node/NodeServices";
21 import { Argument, Command } from "effect/cli";
22 import * as Config from "effect/Config";
23 import * as Console from "effect/Console";
24 import * as Data from "effect/Data";
25 import * as Effect from "effect/Effect";
26 import * as FileSystem from "effect/FileSystem";
27 import * as FetchHttpClient from "effect/http/FetchHttpClient";
28 import * as HttpClient from "effect/http/HttpClient";
29 import * as HttpClientRequest from "effect/http/HttpClientRequest";
30 import * as Option from "effect/Option";
31 import * as Path from "effect/Path";
32 import { repository } from "./config.ts";
33 import { extractTagBody, toDiscordBody } from "./discord-body.ts";
34
35 const EMBED_DESCRIPTION_LIMIT = 4096;
36
37 export class MissingChangelogEntry extends Data.TaggedError("MissingChangelogEntry")<{
38 readonly message: string;
39 readonly tag: string;
40 }> {}
41
42 export class WebhookFailed extends Data.TaggedError("WebhookFailed")<{
43 readonly message: string;
44 readonly status: number;
45 }> {}
46
47 const capitalize = (s: string) => (s.length === 0 ? s : s[0]!.toUpperCase() + s.slice(1));
48
49 /**
50 * Discord embed descriptions cap at 4096 chars. If the body plus the footer
51 * would overflow, truncate the body to fit and prepend a notice so the reader
52 * knows there's more on GitHub. Cuts at the last newline before the budget so
53 * a bullet is never sliced, falling back to a hard cut if no newline lands in
54 * range.
55 */
56 const fitDescription = (body: string, footer: string) => {
57 const description = `${body}${footer}`;
58 if (description.length <= EMBED_DESCRIPTION_LIMIT) return { description, truncated: false };
59 const notice = "_Release notes truncated — see the full changelog on GitHub._\n\n";
60 const budget = EMBED_DESCRIPTION_LIMIT - footer.length - notice.length;
61 let cut = body.lastIndexOf("\n", budget);
62 if (cut < budget / 2) cut = budget;
63 return { description: `${notice}${body.slice(0, cut).trimEnd()}${footer}`, truncated: true };
64 };
65
66 const command = Command.make(
67 "discord-notify",
68 {
69 tag: Argument.String("tag").pipe(Argument.withDescription("Release tag, e.g. v1.0.0-rc.14")),
70 channel: Argument.String("channel").pipe(Argument.withDescription("Release channel")),
71 },
72 Effect.fn(function* ({ tag, channel }) {
73 const webhook = yield* Config.option(Config.String("DISCORD_WEBHOOK"));
74 if (Option.isNone(webhook) || !webhook.value.trim()) {
75 yield* Console.log("DISCORD_WEBHOOK not set, skipping Discord notification");
76 return;
77 }
78
79 const repo = yield* repository;
80 const repoSlug = repo.split("/")[1]!;
81 // "alchemy-effect" -> "Alchemy-Effect"
82 const fullRepoName = repoSlug.split("-").map(capitalize).join("-");
83 // "alchemy-effect" -> "Alchemy"
84 const projectName = capitalize(repoSlug.split("-")[0]!);
85 const titleChannel = channel === "release" ? "" : ` (${channel})`;
86
87 const fs = yield* FileSystem.FileSystem;
88 const path = yield* Path.Path;
89 const rawBody = extractTagBody(yield* fs.readFileString(path.resolve("CHANGELOG.md")), tag);
90 if (rawBody === undefined) {
91 return yield* new MissingChangelogEntry({
92 message: `CHANGELOG.md has no entry for ${tag}`,
93 tag,
94 });
95 }
96
97 const body = toDiscordBody(rawBody);
98 const releaseUrl = `https://github.com/${repo}/releases/tag/${tag}`;
99 const { description, truncated } = fitDescription(
100 body,
101 `\n\n[Full release notes →](${releaseUrl})`,
102 );
103 if (truncated) {
104 yield* Console.log(
105 `Changelog body (${body.length} chars) exceeded Discord limit; truncated and linked to GitHub for the full notes.`,
106 );
107 }
108
109 const client = yield* HttpClient.HttpClient;
110 const response = yield* client.execute(
111 HttpClientRequest.post(webhook.value.trim()).pipe(
112 HttpClientRequest.bodyJsonUnsafe({
113 username: `${fullRepoName} Releases`,
114 embeds: [
115 {
116 title: `${projectName} ${tag}${titleChannel} released`,
117 url: releaseUrl,
118 description,
119 },
120 ],
121 allowed_mentions: { parse: [] },
122 }),
123 ),
124 );
125 if (response.status < 200 || response.status >= 300) {
126 return yield* new WebhookFailed({
127 message: `Discord webhook failed: ${response.status} ${yield* response.text}`,
128 status: response.status,
129 });
130 }
131 yield* Console.log(`Posted Discord release notification for ${tag}`);
132 }),
133 ).pipe(Command.withDescription("Post a release announcement to Discord"));
134
135 Command.run(command, { version: "0.0.0" }).pipe(
136 Effect.provide([NodeServices.layer, FetchHttpClient.layer]),
137 NodeRuntime.runMain,
138 );