Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(ci): address CodeRabbit review on discord-bot-api

EtienneLescot72cb541df2ab
diff --git a/.github/scripts/discord-bot-api.mjs b/.github/scripts/discord-bot-api.mjs
index 531a98f7e..94e2248fe 100644
--- a/.github/scripts/discord-bot-api.mjs
+++ b/.github/scripts/discord-bot-api.mjs
@@ -1,16 +1,25 @@
import { warning } from "@actions/core";
const API_BASE = "https://discord.com/api/v10";
-
-async function callDiscord(botToken, method, path, body) {
- const res = await fetch(`${API_BASE}${path}`, {
- method,
- headers: {
- Authorization: `Bot ${botToken}`,
- "Content-Type": "application/json",
- },
- body: body !== undefined ? JSON.stringify(body) : undefined,
- });
+const DEFAULT_TIMEOUT_MS = 5_000;
+
+async function callDiscord(botToken, method, path, body, { timeoutMs = DEFAULT_TIMEOUT_MS } = {}) {
+ const controller = new AbortController();
+ const timeout = setTimeout(() => controller.abort(), timeoutMs);
+ let res;
+ try {
+ res = await fetch(`${API_BASE}${path}`, {
+ method,
+ headers: {
+ Authorization: `Bot ${botToken}`,
+ "Content-Type": "application/json",
+ },
+ body: body !== undefined ? JSON.stringify(body) : undefined,
+ signal: controller.signal,
+ });
+ } finally {
+ clearTimeout(timeout);
+ }
if (res.status === 429) {
const txt = await res.text();
@@ -27,14 +36,16 @@ async function callDiscord(botToken, method, path, body) {
return res.json();
}
-export async function createForumThread({ botToken, forumChannelId, payload }) {
- return callDiscord(botToken, "POST", `/channels/${forumChannelId}/threads`, payload);
+export async function createForumThread({ botToken, forumChannelId, payload, timeoutMs }) {
+ return callDiscord(botToken, "POST", `/channels/${forumChannelId}/threads`, payload, {
+ timeoutMs,
+ });
}
-export async function postChannelMessage({ botToken, channelId, payload }) {
- return callDiscord(botToken, "POST", `/channels/${channelId}/messages`, payload);
+export async function postChannelMessage({ botToken, channelId, payload, timeoutMs }) {
+ return callDiscord(botToken, "POST", `/channels/${channelId}/messages`, payload, { timeoutMs });
}
-export async function patchChannel({ botToken, channelId, payload }) {
- return callDiscord(botToken, "PATCH", `/channels/${channelId}`, payload);
+export async function patchChannel({ botToken, channelId, payload, timeoutMs }) {
+ return callDiscord(botToken, "PATCH", `/channels/${channelId}`, payload, { timeoutMs });
}
diff --git a/.github/scripts/discord-bot-api.test.mjs b/.github/scripts/discord-bot-api.test.mjs
index a0a586ac2..fb56f4283 100644
--- a/.github/scripts/discord-bot-api.test.mjs
+++ b/.github/scripts/discord-bot-api.test.mjs
@@ -58,6 +58,7 @@ describe.each(happyCases)("$name", ({ call, args, expectUrl, expectMethod, expec
expect(init.method).toBe(expectMethod);
expect(init.headers.Authorization).toBe(`Bot ${botToken}`);
expect(JSON.parse(init.body)).toEqual(expectBody);
+ expect(init.signal).toBeDefined();
});
it("throws on 429 with rate-limit message", async () => {
@@ -70,3 +71,25 @@ describe.each(happyCases)("$name", ({ call, args, expectUrl, expectMethod, expec
await expect(call({ ...args, botToken })).rejects.toThrow(/failed 403/);
});
});
+
+describe("callDiscord timeout", () => {
+ it("aborts the request after timeoutMs when the fetch hangs", async () => {
+ // fetch that never resolves until aborted
+ vi.mocked(fetch).mockImplementation(
+ (_url, init) =>
+ new Promise((_resolve, reject) => {
+ init.signal?.addEventListener("abort", () => {
+ reject(new DOMException("aborted", "AbortError"));
+ });
+ }),
+ );
+ await expect(
+ postChannelMessage({
+ botToken,
+ channelId: "x",
+ payload: {},
+ timeoutMs: 10,
+ }),
+ ).rejects.toThrow();
+ });
+});
diff --git a/docs/github-actions-workflows.md b/docs/github-actions-workflows.md
index 415445031..e572940f5 100644
--- a/docs/github-actions-workflows.md
+++ b/docs/github-actions-workflows.md
@@ -192,7 +192,7 @@ Triggered by `pull_request_target` (opened, reopened, synchronize, edited, label
Runs `node .github/scripts/discord-pr-sync.mjs`, which creates or updates a Discord forum thread for each PR. Thread state is persisted via an HTML comment (`<!-- discord-thread-id:... -->`) in the PR body. Tag updates (draft, ready, changes requested, approved, merged, closed) are applied via the Discord API. The job is marked `continue-on-error: true` so that Discord failures never block the PR workflow.
-All Discord traffic goes through a single bot (`DISCORD_BOT_TOKEN`, secret). The script creates the forum thread via `POST /channels/{forumChannelId}/threads` and posts review/comment updates into the existing thread via `POST /channels/{threadId}/messages`. Required bot permissions on the PR forum channel: View Channel, Send Messages, Embed Links, Create Public Threads, Manage Threads (for tag/archive/lock). Optional failure alerts can be sent to a separate channel via `DISCORD_ALERT_CHANNEL_ID` (variable); unset to silence.
+All Discord traffic goes through a single bot (`DISCORD_BOT_TOKEN`, secret). The script creates the forum thread via `POST /channels/{forumChannelId}/threads` and posts review/comment updates into the existing thread via `POST /channels/{threadId}/messages`. Required bot permissions on the PR forum channel: View Channel, Send Messages, Embed Links, **Create Public Threads** (initial thread), **Send Messages in Threads** (subsequent updates), Manage Threads (for tag/archive/lock). Optional failure alerts can be sent to a separate channel via `DISCORD_ALERT_CHANNEL_ID` (variable); unset to silence.
### discord-roadmap-sync.yml