-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplay-webhook.js
More file actions
79 lines (69 loc) · 2.28 KB
/
replay-webhook.js
File metadata and controls
79 lines (69 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env node
/**
* Replay a webhook fixture against your local Layne server.
*
* Usage:
* node scripts/replay-webhook.js [fixture-path] [server-url]
*
* Defaults:
* fixture-path fixtures/webhooks/pr_opened.json
* server-url http://localhost:3000
*
* Examples:
* npm run replay
* npm run replay fixtures/webhooks/pr_synchronize.json
* npm run replay fixtures/webhooks/pr_opened.json http://localhost:3001
*/
import 'dotenv/config';
import { readFileSync } from 'fs';
import { createHmac } from 'crypto';
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
const __dirname = dirname(fileURLToPath(import.meta.url));
const fixturePath = process.argv[2]
?? resolve(__dirname, '../fixtures/webhooks/pr_opened.json');
const serverUrl = (process.argv[3] ?? 'http://localhost:3000') + '/webhook';
const secret = process.env.GITHUB_WEBHOOK_SECRET;
if (!secret) {
console.error(
'[replay] Error: GITHUB_WEBHOOK_SECRET is not set.\n' +
' Copy .env.example to .env and fill in the required values.'
);
process.exit(1);
}
// Read the raw bytes of the fixture file.
// The HMAC must be computed over the exact bytes that will be sent in the
// request body. Never JSON.parse + JSON.stringify — whitespace normalisation
// would change the bytes and invalidate the signature.
let rawBody;
try {
rawBody = readFileSync(fixturePath);
} catch (err) {
console.error(`[replay] Error: could not read fixture file: ${fixturePath}`);
console.error(` ${err.message}`);
process.exit(1);
}
const signature = 'sha256=' + createHmac('sha256', secret)
.update(rawBody)
.digest('hex');
console.log(`[replay] fixture → ${fixturePath}`);
console.log(`[replay] target → ${serverUrl}`);
let res;
try {
res = await fetch(serverUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-GitHub-Event': 'pull_request',
'X-Hub-Signature-256': signature,
},
body: rawBody,
});
} catch (err) {
console.error(`[replay] Error: could not reach the server at ${serverUrl}`);
console.error(` ${err.message}`);
console.error(' Is the server running? Try: npm start');
process.exit(1);
}
const text = await res.text();
console.log(`[replay] response → ${res.status} ${text}`);