Created
October 16, 2023 17:34
-
-
Save alula/690330d72b656e1bd60126767d54cf74 to your computer and use it in GitHub Desktop.
GitLab merge request pinger Cloudflare Worker
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
// How to use: | |
// 1. Add IDs below | |
// 2. Deploy this on a cloudflare worker | |
// 3. Go to "Discord Notifications" GitLab integration settings. | |
// 4. Put the URL of the webhook, but with "discord.com" replaced with URL of your worker in | |
// "A merge request is created, merged, closed, or reopened" field and if you want to be | |
// pinged for comments, in "A comment is added" as well. | |
const discordIds = [ | |
"219067402174988290", // alula | |
]; | |
const discordIdsStr = discordIds.map(id => `<@${id}>`).join(" "); | |
/** | |
* @type {ExportedHandlerFetchHandler} | |
*/ | |
async function onFetch(request, env, ctx) { | |
const url = new URL(request.url); | |
console.log(request.method, url.toString()); | |
if (!url.pathname.startsWith("/api/webhooks")) { | |
return new Response(JSON.stringify({code: 401, message: "Bad Request"}), {status: 401}); | |
} | |
url.host = "discord.com"; | |
const discordURL = url.toString(); | |
let body = undefined; | |
if (request.method === "POST") { | |
body = await request.json(); | |
if (body.embeds?.length) { | |
const embed = body.embeds[0]; | |
if (embed.description?.includes("merge request")) { | |
body.content = discordIdsStr + " " + body.content; | |
} | |
} | |
console.log("body", body); | |
} | |
const discordResp = await fetch(discordURL, { | |
body: body ? JSON.stringify(body) : undefined, | |
headers: request.headers, | |
method: request.method, | |
}); | |
const discordBody = discordResp.status !== 204 ? await discordResp.text() : null; | |
return new Response(discordBody, { | |
headers: discordResp.headers, | |
status: discordResp.status, | |
}); | |
}; | |
/** | |
* @type {ExportedHandler} | |
*/ | |
export default { | |
fetch: onFetch, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment