Skip to content

Commit 3344b9f

Browse files
authored
fix: bump @octokit/webhooks-methods to ^2.0.0 (#587)
* build(lint): do not prettify JSON event payload fixtures * test: format push payload the way GitHub would send it * fix(package): bump `@octokit/webhooks-methods` to `^2.0.0` * build(package): lock file * refactor: adapt for `@octokit/webhooks-methods` v2
1 parent 435344b commit 3344b9f

File tree

10 files changed

+129
-41
lines changed

10 files changed

+129
-41
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
"build": "pika build",
1111
"coverage": "jest --coverage && open coverage/lcov-report/index.html",
1212
"generate-types": "ts-node --transpile-only scripts/generate-types.ts",
13-
"lint": "prettier --check 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*' README.md package.json",
14-
"lint:fix": "prettier --write 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*' README.md package.json",
13+
"lint": "prettier --check 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*.ts' README.md package.json",
14+
"lint:fix": "prettier --write 'src/**/*.{ts,json}' 'scripts/**/*' 'test/**/*.ts' README.md package.json",
1515
"pretest": "npm run -s lint",
1616
"test": "jest --coverage",
1717
"validate:ts": "tsc --noEmit --noImplicitAny --target es2020 --esModuleInterop --moduleResolution node test/typescript-validate.ts"
1818
},
1919
"prettier": {},
2020
"dependencies": {
2121
"@octokit/request-error": "^2.0.2",
22-
"@octokit/webhooks-methods": "^1.0.0",
22+
"@octokit/webhooks-methods": "^2.0.0",
2323
"@octokit/webhooks-types": "4.0.0",
2424
"aggregate-error": "^3.1.0"
2525
},

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { sign, verify } from "@octokit/webhooks-methods";
2-
31
import { createLogger } from "./createLogger";
42
import { createEventHandler } from "./event-handler/index";
3+
import { sign } from "./sign";
4+
import { verify } from "./verify";
55
import { verifyAndReceive } from "./verify-and-receive";
66
import {
77
EmitterWebhookEvent,

src/sign.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { sign as signMethod } from "@octokit/webhooks-methods";
2+
3+
import { toNormalizedJsonString } from "./to-normalized-json-string";
4+
5+
export async function sign(
6+
secret: string,
7+
payload: string | object
8+
): Promise<any> {
9+
return signMethod(
10+
secret,
11+
typeof payload === "string" ? payload : toNormalizedJsonString(payload)
12+
);
13+
}

src/to-normalized-json-string.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* GitHub sends its JSON with an indentation of 2 spaces and a line break at the end
3+
*/
4+
export function toNormalizedJsonString(payload: object) {
5+
const payloadString = JSON.stringify(payload, null, 2) + "\n";
6+
return payloadString.replace(/[^\\]\\u[\da-f]{4}/g, (s) => {
7+
return s.substr(0, 3) + s.substr(3).toUpperCase();
8+
});
9+
}

src/verify-and-receive.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { verify } from "@octokit/webhooks-methods";
22

3+
import { toNormalizedJsonString } from "./to-normalized-json-string";
34
import {
45
EmitterWebhookEventWithStringPayloadAndSignature,
56
EmitterWebhookEventWithSignature,
@@ -15,7 +16,9 @@ export async function verifyAndReceive(
1516
// verify will validate that the secret is not undefined
1617
const matchesSignature = await verify(
1718
state.secret,
18-
event.payload,
19+
typeof event.payload === "object"
20+
? toNormalizedJsonString(event.payload)
21+
: event.payload,
1922
event.signature
2023
);
2124

src/verify.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { verify as verifyMethod } from "@octokit/webhooks-methods";
2+
3+
import { toNormalizedJsonString } from "./to-normalized-json-string";
4+
5+
export async function verify(
6+
secret: string,
7+
payload: string | object,
8+
signature: string
9+
): Promise<any> {
10+
return verifyMethod(
11+
secret,
12+
typeof payload === "string" ? payload : toNormalizedJsonString(payload),
13+
signature
14+
);
15+
}

test/fixtures/push-payload.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
},
2828
"added": [],
2929
"removed": [],
30-
"modified": ["README.md"]
30+
"modified": [
31+
"README.md"
32+
]
3133
}
3234
],
3335
"head_commit": {
@@ -49,7 +51,9 @@
4951
},
5052
"added": [],
5153
"removed": [],
52-
"modified": ["README.md"]
54+
"modified": [
55+
"README.md"
56+
]
5357
},
5458
"repository": {
5559
"id": 35129377,

test/integration/node-middleware.test.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createServer } from "http";
2+
import { readFileSync } from "fs";
23

34
import fetch from "node-fetch";
45
import { sign } from "@octokit/webhooks-methods";
@@ -7,15 +8,18 @@ import { sign } from "@octokit/webhooks-methods";
78
const express = require("express");
89

910
import { Webhooks, createNodeMiddleware } from "../../src";
10-
import { pushEventPayload } from "../fixtures";
1111

12+
const pushEventPayload = readFileSync(
13+
"test/fixtures/push-payload.json",
14+
"utf-8"
15+
);
1216
let signatureSha256: string;
1317

1418
describe("createNodeMiddleware(webhooks)", () => {
1519
beforeAll(async () => {
1620
signatureSha256 = await sign(
1721
{ secret: "mySecret", algorithm: "sha256" },
18-
JSON.stringify(pushEventPayload)
22+
pushEventPayload
1923
);
2024
});
2125

@@ -48,7 +52,7 @@ describe("createNodeMiddleware(webhooks)", () => {
4852
"X-GitHub-Event": "push",
4953
"X-Hub-Signature-256": signatureSha256,
5054
},
51-
body: JSON.stringify(pushEventPayload),
55+
body: pushEventPayload,
5256
}
5357
);
5458

@@ -92,7 +96,7 @@ describe("createNodeMiddleware(webhooks)", () => {
9296
"X-GitHub-Event": "push",
9397
"X-Hub-Signature-256": signatureSha256,
9498
},
95-
body: JSON.stringify(pushEventPayload),
99+
body: pushEventPayload,
96100
}
97101
);
98102

@@ -256,7 +260,7 @@ describe("createNodeMiddleware(webhooks)", () => {
256260
"X-GitHub-Event": "push",
257261
"X-Hub-Signature-256": signatureSha256,
258262
},
259-
body: JSON.stringify(pushEventPayload),
263+
body: pushEventPayload,
260264
}
261265
);
262266

@@ -292,7 +296,7 @@ describe("createNodeMiddleware(webhooks)", () => {
292296
"X-GitHub-Event": "push",
293297
"X-Hub-Signature-256": signatureSha256,
294298
},
295-
body: JSON.stringify(pushEventPayload),
299+
body: pushEventPayload,
296300
}
297301
);
298302

@@ -327,7 +331,7 @@ describe("createNodeMiddleware(webhooks)", () => {
327331
"X-GitHub-Event": "push",
328332
"X-Hub-Signature-256": signatureSha256,
329333
},
330-
body: JSON.stringify(pushEventPayload),
334+
body: pushEventPayload,
331335
}
332336
);
333337

@@ -352,7 +356,7 @@ describe("createNodeMiddleware(webhooks)", () => {
352356

353357
const response = await fetch(`http://localhost:${port}/test`, {
354358
method: "POST",
355-
body: JSON.stringify(pushEventPayload),
359+
body: pushEventPayload,
356360
});
357361

358362
await expect(response.text()).resolves.toBe("Dafuq");
@@ -376,15 +380,15 @@ describe("createNodeMiddleware(webhooks)", () => {
376380

377381
const response = await fetch(`http://localhost:${port}/test`, {
378382
method: "POST",
379-
body: JSON.stringify(pushEventPayload),
383+
body: pushEventPayload,
380384
});
381385

382386
await expect(response.text()).resolves.toContain("Cannot POST /test");
383387
expect(response.status).toEqual(404);
384388

385389
const responseForFoo = await fetch(`http://localhost:${port}/foo`, {
386390
method: "POST",
387-
body: JSON.stringify(pushEventPayload),
391+
body: pushEventPayload,
388392
});
389393

390394
await expect(responseForFoo.text()).resolves.toContain("ok\n");
@@ -415,7 +419,7 @@ describe("createNodeMiddleware(webhooks)", () => {
415419
"X-GitHub-Event": "push",
416420
"X-Hub-Signature-256": signatureSha256,
417421
},
418-
body: JSON.stringify(pushEventPayload),
422+
body: pushEventPayload,
419423
});
420424

421425
await expect(response.text()).resolves.toBe("ok\n");
@@ -446,7 +450,7 @@ describe("createNodeMiddleware(webhooks)", () => {
446450
"X-GitHub-Event": "push",
447451
"X-Hub-Signature-256": signatureSha256,
448452
},
449-
body: JSON.stringify(pushEventPayload),
453+
body: pushEventPayload,
450454
});
451455

452456
await expect(response.text()).resolves.toBe("ok\n");

0 commit comments

Comments
 (0)