forked from siddharthvaddem/openscreen
-
Notifications
You must be signed in to change notification settings - Fork 18
refactor(ci): switch Discord automation from webhooks to single bot #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EtienneLescot
wants to merge
3
commits into
main
Choose a base branch
from
refactor/discord-bot-only
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ec667bb
refactor(ci): switch Discord automation from webhooks to single bot
EtienneLescot eb42661
fix(ci): restore mention suppression and patch resilience after webho…
EtienneLescot 0c5c1b8
test(ci): collapse discord-bot-api tests via describe.each
EtienneLescot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 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, | ||
| }); | ||
|
|
||
| if (res.status === 429) { | ||
| const txt = await res.text(); | ||
| warning(`Discord rate-limited (429) on ${method} ${path}: ${txt}`); | ||
| throw new Error(`Discord rate-limited (429) on ${method} ${path}`); | ||
| } | ||
|
|
||
| if (!res.ok) { | ||
| const txt = await res.text(); | ||
| throw new Error(`Discord API ${method} ${path} failed ${res.status}: ${txt}`); | ||
| } | ||
|
|
||
| if (res.status === 204) return null; | ||
| return res.json(); | ||
| } | ||
|
|
||
| export async function createForumThread({ botToken, forumChannelId, payload }) { | ||
| return callDiscord(botToken, "POST", `/channels/${forumChannelId}/threads`, payload); | ||
| } | ||
|
|
||
| export async function postChannelMessage({ botToken, channelId, payload }) { | ||
| return callDiscord(botToken, "POST", `/channels/${channelId}/messages`, payload); | ||
| } | ||
|
|
||
| export async function patchChannel({ botToken, channelId, payload }) { | ||
| return callDiscord(botToken, "PATCH", `/channels/${channelId}`, payload); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { createForumThread, patchChannel, postChannelMessage } from "./discord-bot-api.mjs"; | ||
|
|
||
| const botToken = "test-token"; | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubGlobal("fetch", vi.fn()); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| function mockResponse({ status = 200, body = { id: "x" } } = {}) { | ||
| vi.mocked(fetch).mockResolvedValue({ | ||
| ok: status >= 200 && status < 300, | ||
| status, | ||
| text: vi.fn().mockResolvedValue(JSON.stringify(body)), | ||
| json: vi.fn().mockResolvedValue(body), | ||
| }); | ||
| } | ||
|
|
||
| const happyCases = [ | ||
| { | ||
| name: "createForumThread", | ||
| call: (args) => createForumThread(args), | ||
| args: { forumChannelId: "forum-1", payload: { name: "PR #1" } }, | ||
| expectUrl: "https://discord.com/api/v10/channels/forum-1/threads", | ||
| expectMethod: "POST", | ||
| expectBody: { name: "PR #1" }, | ||
| }, | ||
| { | ||
| name: "postChannelMessage", | ||
| call: (args) => postChannelMessage(args), | ||
| args: { channelId: "thread-1", payload: { content: "hello" } }, | ||
| expectUrl: "https://discord.com/api/v10/channels/thread-1/messages", | ||
| expectMethod: "POST", | ||
| expectBody: { content: "hello" }, | ||
| }, | ||
| { | ||
| name: "patchChannel", | ||
| call: (args) => patchChannel(args), | ||
| args: { channelId: "thread-1", payload: { archived: true } }, | ||
| expectUrl: "https://discord.com/api/v10/channels/thread-1", | ||
| expectMethod: "PATCH", | ||
| expectBody: { archived: true }, | ||
| }, | ||
| ]; | ||
|
|
||
| describe.each(happyCases)("$name", ({ call, args, expectUrl, expectMethod, expectBody }) => { | ||
| it("calls Discord with the right URL, method, bot auth, and payload", async () => { | ||
| mockResponse(); | ||
|
|
||
| await call({ ...args, botToken }); | ||
|
|
||
| const [url, init] = vi.mocked(fetch).mock.calls[0]; | ||
| expect(url).toBe(expectUrl); | ||
| expect(init.method).toBe(expectMethod); | ||
| expect(init.headers.Authorization).toBe(`Bot ${botToken}`); | ||
| expect(JSON.parse(init.body)).toEqual(expectBody); | ||
| }); | ||
|
|
||
| it("throws on 429 with rate-limit message", async () => { | ||
| mockResponse({ status: 429, body: { retry_after: 1 } }); | ||
| await expect(call({ ...args, botToken })).rejects.toThrow(/rate-limited \(429\)/); | ||
| }); | ||
|
|
||
| it("throws on non-ok responses with status in message", async () => { | ||
| mockResponse({ status: 403, body: { message: "Missing Permissions" } }); | ||
| await expect(call({ ...args, botToken })).rejects.toThrow(/failed 403/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: getopenscreen/openscreen
Length of output: 3478
Add a timeout to
callDiscord()Every Discord request can hang indefinitely here, which can stall the PR sync and leaderboard workflows until the job times out. Add an
AbortController-based timeout like the other Discord helper uses.🤖 Prompt for AI Agents