GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — t3code/scripts/notify-discord-release.ts at main · saba-ch/t3code

Expand file tree

/

notify-discord-release.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
240 lines (220 loc) · 7 KB
/

notify-discord-release.ts

Copy path

File metadata and controls

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