From feb5a402377bc3da9cb9ea788964ece4e56f48cd Mon Sep 17 00:00:00 2001 From: SethCohen Date: Tue, 17 Jun 2025 00:46:40 -0400 Subject: [PATCH] feat(index.js): enhance sendWebhook function to handle rate limits with retries for improved reliability when sending requests to Discord --- index.js | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 789b932..bc9cb27 100644 --- a/index.js +++ b/index.js @@ -202,21 +202,42 @@ const buildEmbedMessage = (name, html_url, description) => { }; /** - * Sends the webhook request to Discord. + * Sends the webhook request to Discord, handling rate limits (429) with retries. * @param {string} webhookUrl The URL of the Discord webhook. * @param {object} requestBody The payload to send in the webhook. + * @param {number} [maxRetries=3] Maximum number of retries on rate limit. */ -const sendWebhook = async (webhookUrl, requestBody) => { - try { - const response = await fetch(`${webhookUrl}?wait=true`, { - method: 'POST', - body: JSON.stringify(requestBody), - headers: { 'Content-Type': 'application/json' } - }); - const data = await response.json(); - core.info(JSON.stringify(data)); - } catch (err) { - core.setFailed(err.message); +const sendWebhook = async (webhookUrl, requestBody, maxRetries = 3) => { + let attempt = 0; + while (attempt <= maxRetries) { + try { + const response = await fetch(`${webhookUrl}?wait=true`, { + method: 'POST', + body: JSON.stringify(requestBody), + headers: { 'Content-Type': 'application/json' } + }); + if (response.status === 429) { + // Rate limited, get retry-after + const retryAfter = parseInt(response.headers.get('retry-after') || '1', 10); + core.warning(`Rate limited by Discord. Retrying after ${retryAfter} seconds (attempt ${attempt + 1}/${maxRetries})`); + await new Promise(res => setTimeout(res, retryAfter * 1000)); + attempt++; + continue; + } + const data = await response.json(); + if (!response.ok) { + core.setFailed(`Discord webhook error: ${JSON.stringify(data)}`); + } else { + core.info(JSON.stringify(data)); + } + break; + } catch (err) { + core.setFailed(err.message); + break; + } + } + if (attempt > maxRetries) { + core.setFailed('Exceeded maximum Discord webhook retry attempts due to rate limiting.'); } };