Skip to content

Commit dad6279

Browse files
committed
Updates to GitHub hooks
1 parent e97885c commit dad6279

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"polish-plurals": "1.1.0",
3434
"request": "2.88.2",
3535
"request-promise": "4.2.5",
36+
"secure-json-parse": "2.1.0",
3637
"typescript": "3.8.3",
3738
"zlib-sync": "0.1.6"
3839
},
@@ -44,6 +45,7 @@
4445
"@types/nock": "11.1.0",
4546
"@types/node": "13.13.4",
4647
"@types/node-fetch": "2.5.7",
48+
"@types/secure-json-parse": "1.0.2",
4749
"@types/sinon": "9.0.0",
4850
"@types/sinon-chai": "3.2.4",
4951
"chai": "4.2.0",

src/handle-github-webhook.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,30 @@ async function handleGithubWebhook(
3030

3131
const discordWebhookUrl = getConfig('GITHUB_WEBHOOK_DISCORD_URL');
3232

33-
const { status } = await fetch(discordWebhookUrl, {
33+
const newHeaders = {
34+
// 'x-real-ip': headers['x-real-ip'],
35+
// 'x-forwarded-for': headers['x-forwarded-for'],
36+
// host: headers.host,
37+
// 'x-forwarded-proto': headers['x-forwarded-proto'],
38+
// connection: headers.connection,
39+
// 'content-length': headers['content-length'],
40+
// 'x-nginx-ssl': headers['x-nginx-ssl'],
41+
// 'user-agent': headers['user-agent'],
42+
// accept: headers.accept,
43+
'x-github-delivery': headers['x-github-delivery'],
44+
'x-github-event': headers['x-github-event'],
45+
'x-hub-signature': headers['x-hub-signature'],
46+
'content-type': headers['content-type'],
47+
// 'x-forwarded-https': headers['x-forwarded-https'],
48+
} as Record<string, string>;
49+
50+
const res = await fetch(discordWebhookUrl, {
3451
method: 'POST',
35-
body: JSON.stringify(body),
52+
body: rawBody,
53+
headers: newHeaders,
3654
});
3755

38-
return { statusCode: status };
56+
return { statusCode: res.status };
3957
}
4058

4159
function shouldSendWebhook(body: object | GithubWebhookPullRequest) {

src/http-server.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import Http from 'http';
22
import handleGithubWebhook from './handle-github-webhook';
33
import type { Client } from 'discord.js';
4+
import JsonParse from 'secure-json-parse';
45

56
const BAD_REQUEST = 400;
67
const OK = 200;
78

9+
async function parseBody<T = object>(
10+
req: Http.IncomingMessage
11+
): Promise<{ rawBody: Buffer; body: T }> {
12+
const chunks = [];
13+
for await (const chunk of req) {
14+
chunks.push(chunk);
15+
}
16+
const rawBody = Buffer.concat(chunks);
17+
const body = JsonParse.parse(rawBody.toString()) as T;
18+
return { rawBody, body };
19+
}
20+
821
function createHttpServer(
922
discordClient: Client,
1023
errors: Error[],
@@ -13,22 +26,16 @@ function createHttpServer(
1326
): Http.Server {
1427
return Http.createServer(async (req, res) => {
1528
if (req.url?.startsWith('/githubWebhook')) {
16-
const chunks = [];
17-
for await (const chunk of req) {
18-
chunks.push(chunk);
19-
}
20-
2129
try {
22-
const rawBody = Buffer.concat(chunks);
23-
const body = JSON.parse(rawBody.toString());
24-
const headers = req.headers;
30+
const { headers } = req;
31+
const { rawBody, body } = await parseBody(req);
2532

2633
const { statusCode } = await handleGithubWebhook(headers, rawBody, body);
2734

2835
res.statusCode = statusCode;
2936
res.end();
3037
} catch (error) {
31-
console.log(error);
38+
errors.push(error.message ?? error);
3239
res.statusCode = BAD_REQUEST;
3340
res.end();
3441
}

ssh-script-deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fi
1818
git pull origin main
1919

2020
echo "👉 Installing deps…"
21-
npm ci
21+
# npm ci
2222

2323
echo "👉 Bulding…"
2424
NODE_ENV=production ENV=production npm run build

0 commit comments

Comments
 (0)