[workers] Add github-discord-notifier (#1150)
Start migrating workers to the monorepo. This PR brings in github-discord-notifier.
This commit is contained in:
commit
5092b74503
8 changed files with 149 additions and 0 deletions
10
infra/workers/.gitignore
vendored
Normal file
10
infra/workers/.gitignore
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
node_modules/
|
||||
.DS_Store
|
||||
.wrangler/
|
||||
|
||||
# Workers mostly have dev dependencies, and usually only wrangler, so exact
|
||||
# version details and the resulting `yarn.lock` is unnecessary noise.
|
||||
#
|
||||
# If we need to pin some runtime dependency, we can pin it to an exact version
|
||||
# in the package.json itself.
|
||||
yarn.lock
|
3
infra/workers/.prettierrc.json
Normal file
3
infra/workers/.prettierrc.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"tabWidth": 4
|
||||
}
|
32
infra/workers/README.md
Normal file
32
infra/workers/README.md
Normal file
|
@ -0,0 +1,32 @@
|
|||
# Cloudflare Workers
|
||||
|
||||
Source code for our [Cloudflare
|
||||
Workers](https://developers.cloudflare.com/workers/).
|
||||
|
||||
Each worker is a self contained directory with its each `package.json`.
|
||||
|
||||
## Deploying
|
||||
|
||||
* Switch to a worker directory, e.g. `cd github-discord-notifier`.
|
||||
|
||||
* Install dependencies (if needed) with `yarn`
|
||||
|
||||
* Login into wrangler (if needed) using `yarn wrangler login`
|
||||
|
||||
* Deploy! `yarn wrangler deploy`
|
||||
|
||||
Wrangler is the CLI provided by Cloudflare to manage workers. Apart from
|
||||
deploying, it also allows us to stream logs from running workers by using `yarn
|
||||
wrangler tail`.
|
||||
|
||||
## Creating a new worker
|
||||
|
||||
Copy paste an existing one. Unironically this is a good option because
|
||||
Cloudflare's template has a lot of unnecessary noise, but if really do want to
|
||||
create one from scratch, use `npm create cloudflare@latest`.
|
||||
|
||||
To import an existing worker from the Cloudflare dashboard, use
|
||||
|
||||
```sh
|
||||
npm create cloudflare@2 existing-worker-name -- --type pre-existing --existing-script existing-worker-name
|
||||
```
|
9
infra/workers/github-discord-notifier/package.json
Normal file
9
infra/workers/github-discord-notifier/package.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "github-discord-notifier",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20240314.0",
|
||||
"typescript": "^5",
|
||||
"wrangler": "^3"
|
||||
}
|
||||
}
|
49
infra/workers/github-discord-notifier/src/index.ts
Normal file
49
infra/workers/github-discord-notifier/src/index.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* Forward notifications from GitHub to Discord.
|
||||
*
|
||||
* This worker receives webhooks from GitHub, filters out the ones we don't
|
||||
* need, and forwards them to a Discord webhook.
|
||||
*
|
||||
* [Note: GitHub specific Discord Webhooks]
|
||||
*
|
||||
* By appending `/github` to the end of the webhook URL, we can get Discord to
|
||||
* automatically parse the payload sent by GitHub.
|
||||
* https://discord.com/developers/docs/resources/webhook#execute-githubcompatible-webhook
|
||||
*
|
||||
* Note that this doesn't work for all events. And sadly, the events it doesn't
|
||||
* work for get silently ignored (Discord responds with a 204).
|
||||
* https://github.com/discord/discord-api-docs/issues/6203#issuecomment-1608151265
|
||||
*/
|
||||
export default {
|
||||
async fetch(request: Request, env: Env) {
|
||||
return handleRequest(request, env.DISCORD_WEBHOOK_URL);
|
||||
},
|
||||
} satisfies ExportedHandler<Env>;
|
||||
|
||||
interface Env {
|
||||
DISCORD_WEBHOOK_URL: string;
|
||||
}
|
||||
|
||||
const handleRequest = async (request: Request, targetURL: string) => {
|
||||
const requestBody = await request.text();
|
||||
let sender = JSON.parse(requestBody)["sender"]["login"];
|
||||
if (sender === "cloudflare-pages[bot]" || sender === "CLAassistant") {
|
||||
// Ignore pings from CF bot
|
||||
return new Response(null, { status: 200 });
|
||||
}
|
||||
|
||||
const response = await fetch(targetURL, {
|
||||
method: request.method,
|
||||
headers: request.headers,
|
||||
body: requestBody,
|
||||
});
|
||||
|
||||
const responseBody = await response.text();
|
||||
const newResponse = new Response(responseBody, {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: response.headers,
|
||||
});
|
||||
|
||||
return newResponse;
|
||||
};
|
1
infra/workers/github-discord-notifier/tsconfig.json
Normal file
1
infra/workers/github-discord-notifier/tsconfig.json
Normal file
|
@ -0,0 +1 @@
|
|||
{ "extends": "../tsconfig.base.json", "include": ["src/**/*.ts"] }
|
7
infra/workers/github-discord-notifier/wrangler.toml
Normal file
7
infra/workers/github-discord-notifier/wrangler.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
name = "github-discord-notifier"
|
||||
main = "src/index.ts"
|
||||
compatibility_date = "2024-03-14"
|
||||
|
||||
[vars]
|
||||
# Added as a secret via the Cloudflare dashboard
|
||||
# DISCORD_WEBHOOK_URL = "https://discord.com/api/webhooks/{webhook.id}/{webhook.token}/github"
|
38
infra/workers/tsconfig.base.json
Normal file
38
infra/workers/tsconfig.base.json
Normal file
|
@ -0,0 +1,38 @@
|
|||
{
|
||||
/* A shared TSConfig for use by TypeScript Cloudflare Workers */
|
||||
/* TSConfig docs: https://aka.ms/tsconfig.json */
|
||||
"compilerOptions": {
|
||||
/* tsc is used for by us for type checking, not compilation (the
|
||||
Cloudflare workers runtime natively supports TypeScript) */
|
||||
"noEmit": true,
|
||||
|
||||
/* The Workers runtime supports the latest and greatest */
|
||||
/* https://developers.cloudflare.com/workers/reference/languages/#javascript--typescript */
|
||||
"lib": ["esnext"],
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
|
||||
/* Types that are implicitly available */
|
||||
/* https://www.npmjs.com/package/@cloudflare/workers-types */
|
||||
"types": ["@cloudflare/workers-types"],
|
||||
|
||||
/* Tell TypeScript how to lookup the file for a given import */
|
||||
"moduleResolution": "node",
|
||||
|
||||
/* Speed things up by not type checking `node_modules` */
|
||||
"skipLibCheck": true,
|
||||
/* Require the `type` modifier when importing types */
|
||||
"verbatimModuleSyntax": true,
|
||||
/* Enable importing .json files */
|
||||
"resolveJsonModule": true,
|
||||
|
||||
/* strict and then some */
|
||||
"strict": true,
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedParameters": true,
|
||||
"noUnusedLocals": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedIndexedAccess": true,
|
||||
"exactOptionalPropertyTypes": true
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue