Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

notify-discord-release.ts

240 lines7.0 KB

1#!/usr/bin/env node
2​
3import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import * as Config from "effect/Config";
6import * as Data from "effect/Data";
7import * as DateTime from "effect/DateTime";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Logger from "effect/Logger";
11import * as Schema from "effect/Schema";
12import { Argument, Command, Flag } from "effect/unstable/cli";
13import {
14 FetchHttpClient,
15 HttpClient,
16 HttpClientRequest,
17 HttpClientResponse,
18} from "effect/unstable/http";
19​
20export type DiscordReleaseTarget = "prerelease" | "latest";
21​
22interface DiscordReleaseAnnouncementOptions {
23 readonly target: DiscordReleaseTarget;
24 readonly roleId: string;
25 readonly releaseName: string;
26 readonly version: string;
27 readonly tag: string;
28 readonly releaseUrl: URL;
29 readonly timestamp: string;
31​
32interface DiscordWebhookPayload {
33 readonly content: string;
34 readonly allowed_mentions: {
35 readonly roles: ReadonlyArray<string>;
36 };
37 readonly embeds: ReadonlyArray<{
38 readonly title: string;
39 readonly url: string;
40 readonly description: string;
41 readonly color: number;
42 readonly fields: ReadonlyArray<{
43 readonly name: string;
44 readonly value: string;
45 readonly inline: boolean;
46 }>;
47 readonly timestamp: string;
48 }>;
50​
51const DISCORD_RELEASE_TARGETS = ["prerelease", "latest"] as const;
52const DiscordRoleIdSchema = Schema.String.check(Schema.isPattern(/^\d+$/));
53const DiscordWebhookUrl = Config.url("DISCORD_WEBHOOK_URL");
54​
55class DiscordReleaseAnnouncementError extends Data.TaggedError("DiscordReleaseAnnouncementError")<{
56 readonly message: string;
57 readonly cause?: unknown;
58}> {}
59​
60const targetLabels = {
61 prerelease: "Prerelease",
62 latest: "Latest",
63} as const satisfies Record<DiscordReleaseTarget, string>;
64​
65const targetColors = {
66 prerelease: 0x5865f2,
67 latest: 0x2ecc71,
68} as const satisfies Record<DiscordReleaseTarget, number>;
69​
70function describeWebhookUrl(webhookUrl: URL) {
71 return {
72 configured: true,
73 origin: webhookUrl.origin,
74 pathnameSegmentCount: webhookUrl.pathname.split("/").filter(Boolean).length,
75 } as const;
77​
78function summarizePayload(payload: DiscordWebhookPayload) {
79 return {
80 contentLength: payload.content.length,
81 embedCount: payload.embeds.length,
82 allowedRoleMentionCount: payload.allowed_mentions.roles.length,
83 hasRoleMentionSyntax: payload.content.includes("<@&"),
84 } as const;
86​
87export const buildDiscordReleaseAnnouncement = (
88 options: DiscordReleaseAnnouncementOptions,
89): DiscordWebhookPayload => ({
90 content: `<@&${options.roleId}> ${targetLabels[options.target]} published: ${options.releaseName}`,
91 allowed_mentions: {
92 roles: [options.roleId],
93 },
94 embeds: [
95 {
96 title: options.releaseName,
97 url: options.releaseUrl.href,
98 description:
99 options.target === "prerelease"
100 ? "A new T3 Code prerelease is available for nightly testers."
101 : "A new T3 Code latest release is available.",
102 color: targetColors[options.target],
103 fields: [
104 {
105 name: "Version",
106 value: options.version,
107 inline: true,
108 },
109 {
110 name: "Tag",
111 value: options.tag,
112 inline: true,
113 },
114 ],
115 timestamp: options.timestamp,
116 },
117 ],
118});
119​
120const postDiscordWebhook = Effect.fn("postDiscordWebhook")(function* (
121 webhookUrl: URL,
122 payload: DiscordWebhookPayload,
123) {
124 const httpClient = (yield* HttpClient.HttpClient).pipe(
125 HttpClient.retryTransient({
126 retryOn: "errors-and-responses",
127 times: 3,
128 }),
129 );
130​
131 yield* Effect.logInfo("discord webhook request dispatching").pipe(
132 Effect.annotateLogs({
133 ...describeWebhookUrl(webhookUrl),
134 ...summarizePayload(payload),
135 }),
136 );
137​
138 const response = yield* HttpClientRequest.post(webhookUrl).pipe(
139 HttpClientRequest.bodyJson(payload),
140 Effect.flatMap(httpClient.execute),
141 Effect.mapError(
142 (cause) =>
143 new DiscordReleaseAnnouncementError({
144 message: "Failed to post Discord release announcement.",
145 cause,
146 }),
147 ),
148 );
149​
150 yield* Effect.logInfo("discord webhook response received").pipe(
151 Effect.annotateLogs({
152 status: response.status,
153 ok: response.status >= 200 && response.status < 300,
154 }),
155 );
156​
157 yield* HttpClientResponse.filterStatusOk(response).pipe(
158 Effect.mapError(
159 (cause) =>
160 new DiscordReleaseAnnouncementError({
161 message: `Discord webhook returned status ${response.status}.`,
162 cause,
163 }),
164 ),
165 );
166});
167​
168export const notifyDiscordReleaseCommand = Command.make(
169 "notify-discord-release",
170 {
171 target: Argument.choice("target", DISCORD_RELEASE_TARGETS).pipe(
172 Argument.withDescription("Discord announcement target: prerelease or latest."),
173 ),
174 roleId: Flag.string("role-id").pipe(
175 Flag.withSchema(DiscordRoleIdSchema),
176 Flag.withDescription("Discord role ID to mention in the release announcement."),
177 ),
178 releaseName: Flag.string("release-name").pipe(
179 Flag.withSchema(Schema.NonEmptyString),
180 Flag.withDescription("Human-readable release name."),
181 ),
182 releaseVersion: Flag.string("release-version").pipe(
183 Flag.withSchema(Schema.NonEmptyString),
184 Flag.withDescription("Release version."),
185 ),
186 tag: Flag.string("tag").pipe(
187 Flag.withSchema(Schema.NonEmptyString),
188 Flag.withDescription("Git tag for the release."),
189 ),
190 releaseUrl: Flag.string("release-url").pipe(
191 Flag.withSchema(Schema.URLFromString),
192 Flag.withDescription("Public GitHub release URL."),
193 ),
194 },
195 ({ target, roleId, releaseName, releaseVersion, tag, releaseUrl }) =>
196 Effect.gen(function* () {
197 yield* Effect.logInfo("discord release announcement starting").pipe(
198 Effect.annotateLogs({
199 target,
200 roleIdProvided: roleId.length > 0,
201 roleIdLength: roleId.length,
202 releaseName,
203 version: releaseVersion,
204 tag,
205 releaseUrl,
206 }),
207 );
208​
209 const webhookUrl = yield* DiscordWebhookUrl;
210 const timestamp = DateTime.formatIso(yield* DateTime.now);
211 const payload = buildDiscordReleaseAnnouncement({
212 target,
213 roleId,
214 releaseName,
215 version: releaseVersion,
216 tag,
217 releaseUrl,
218 timestamp,
219 });
220​
221 yield* Effect.logInfo("discord release announcement payload built").pipe(
222 Effect.annotateLogs(summarizePayload(payload)),
223 );
224 yield* postDiscordWebhook(webhookUrl, payload);
225 yield* Effect.logInfo("discord release announcement completed");
226 }),
227).pipe(Command.withDescription("Post a T3 Code release announcement to Discord."));
228​
229if (import.meta.main) {
230 Command.run(notifyDiscordReleaseCommand, { version: "0.0.0" }).pipe(
231 Effect.provide(
232 Layer.mergeAll(
233 Logger.layer([Logger.consolePretty()]),
234 NodeServices.layer,
235 FetchHttpClient.layer,
236 ),
237 ),
238 NodeRuntime.runMain,
239 );