(event)
| 31 | * @throws GitHub webhook validation error |
| 32 | */ |
| 33 | export const handler = async (event) => { |
| 34 | const githubSignature = event.headers['x-hub-signature'].split('=')[1]; |
| 35 | |
| 36 | console.log(`Checking event signature: ${githubSignature}`); |
| 37 | |
| 38 | const key = process.env.GITHUB_WEBHOOK_SECRET; |
| 39 | const data = event.body; |
| 40 | const check = createHmac('sha1', key).update(data).digest('hex'); |
| 41 | |
| 42 | if (githubSignature !== check) { |
| 43 | throw new Error('Invalid signature!'); |
| 44 | } |
| 45 | |
| 46 | console.log(`Handling event: ${event.headers['x-github-event']}`); |
| 47 | const body = event.isBase64Encoded |
| 48 | ? Buffer.from(event.body, 'base64').toString('utf-8') |
| 49 | : event.body; |
| 50 | |
| 51 | console.log(body); |
| 52 | |
| 53 | switch (event.headers['x-github-event']) { |
| 54 | case 'ping': |
| 55 | // Blackhole `ping` events |
| 56 | break; |
| 57 | default: |
| 58 | await eventbridge.send( |
| 59 | new PutEventsCommand({ |
| 60 | Entries: [ |
| 61 | { |
| 62 | Detail: body, |
| 63 | DetailType: event.headers['x-github-event'], |
| 64 | Source: 'org.prx.ci.github-webhook', |
| 65 | }, |
| 66 | ], |
| 67 | }), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | return OK_RESPONSE; |
| 72 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected