Last active
May 5, 2025 10:31
-
-
Save LostLuma/364e9882e4d5f202af8aebec2b305938 to your computer and use it in GitHub Desktop.
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
/* | |
MIT License | |
Copyright (c) 2021 Lilly Rose Berner | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
// Cloudflare Worker which filters GitHub webhook events before forwarding them to Discord | |
// Thanks to Joe / Python Discord for the idea: https://github.com/python-discord/workers/tree/main/github-filter-worker | |
const CLOUDFLARE_APP_ID = 85455; | |
const CLOUDFLARE_USER_ID = 73139402; | |
const DEPENDABOT_USER_ID = 49699333; | |
async function handleRequest({ request }) { | |
const url = new URL(request.url); | |
if (url.pathname === "/") { | |
return new Response("Worker is active.", { status: 200 }); | |
} | |
const { headers, method } = request; | |
if (method !== "POST") { | |
return new Response("Method not supported.", { status: 405 }); | |
} | |
const data = await request.clone().json(); | |
const event = headers.get("X-GitHub-Event"); | |
// [Mousey] Deployed successfully on stardust | |
const isCloudflareCheck = | |
event === "check_run" && | |
data?.check_run?.conclusion === "success" && | |
data?.check_run?.check_suite?.app?.id === CLOUDFLARE_APP_ID; | |
// [Mousey] Cloudflare Pages checks success on stardust | |
const isCloudflareSuite = | |
event === "check_suite" && | |
data?.check_suite?.conclusion === "success" && | |
data?.check_suite?.app?.id === CLOUDFLARE_APP_ID; | |
// [LostLuma/Mousey] New comment on pull request #24: bot: bump hiredis from 1.1.0 to 2.0.0 in /packages/bot | |
const isCloudflareComment = event === "issue_comment" && data?.sender?.id === CLOUDFLARE_USER_ID; | |
// [Mousey:dependabot/pip/packages/bot/cryptography-3.4.7] 1 new commit | |
// This does not ignore squashing commits to other branches - unsure about merging | |
const isDependabotPush = event === "push" && data?.sender?.id === DEPENDABOT_USER_ID; | |
// [LostLuma/Mousey] New branch created: dependabot/pip/packages/bot/cryptography-3.4.7 | |
const isDependabotBranchCreate = | |
event === "create" && data?.ref_type === "branch" && data?.sender?.id === DEPENDABOT_USER_ID; | |
// [LostLuma/Mousey] branch deleted: dependabot/pip/packages/api/cryptography-3.4.7 | |
const isDependabotBranchDelete = | |
event === "delete" && data?.ref_type === "branch" && data?.sender?.id === DEPENDABOT_USER_ID; | |
if ( | |
isCloudflareCheck || | |
isCloudflareSuite || | |
isCloudflareComment || | |
isDependabotPush || | |
isDependabotBranchCreate || | |
isDependabotBranchDelete | |
) { | |
return new Response("Event intercepted and ignored.", { status: 200 }); | |
} | |
return await fetch(`https://discord.com/api/webhooks${url.pathname}/github`, request); | |
} | |
addEventListener("fetch", (event) => { | |
event.respondWith(handleRequest(event)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i made a simple php script to do this https://gist.github.com/hayden-t/e3a6a85d404897430188368fad53786a