|
26 | 26 | headers: {} |
27 | 27 | } |
28 | 28 |
|
29 | | - const payloadOffset = contents.indexOf('\n{') |
30 | | - if (payloadOffset < 0) throw new Error(`Could not find start of payload in ${path}`) |
31 | | - contents.substring(0, payloadOffset).split(/\r?\n/).forEach(line => { |
32 | | - const colon = line.indexOf(':') |
33 | | - if (colon < 0) return |
34 | | - |
35 | | - const key = line.substring(0, colon).toLowerCase() |
36 | | - const value = line.substring(colon + 1).replace(/^\s+/, '') |
37 | | - |
38 | | - if (key === 'request method') req.method = value |
39 | | - else req.headers[key] = value |
40 | | - }) |
41 | | - req.rawBody = contents.substring(payloadOffset + 1) |
42 | | - // In https://github.com/organizations/git-for-windows/settings/apps/gitforwindowshelper/advanced, |
43 | | - // the JSON is pretty-printed, but the actual webhook event avoids any |
44 | | - // unnecessary white-space in the body |
45 | | - .replace(/\r?\n\s*("[^"]*":)\s*/g, '$1') |
46 | | - .replace(/\r?\n\s*/g, '') |
47 | | - req.body = JSON.parse(req.rawBody) |
| 29 | + if (contents.startsWith('event: {')) { |
| 30 | + const event = JSON.parse(contents.substring(7)) |
| 31 | + Object.keys(event.request.headers).forEach(key => { |
| 32 | + req.headers[key.toLowerCase()] = event.request.headers[key] |
| 33 | + }) |
| 34 | + req.body = event.request.payload |
| 35 | + req.rawBody = JSON.stringify(req.body) |
| 36 | + req.method = 'POST' |
| 37 | + } else { |
| 38 | + const payloadOffset = contents.indexOf('\n{') |
| 39 | + if (payloadOffset < 0) throw new Error(`Could not find start of payload in ${path}`) |
| 40 | + contents.substring(0, payloadOffset).split(/\r?\n/).forEach(line => { |
| 41 | + const colon = line.indexOf(':') |
| 42 | + if (colon < 0) return |
| 43 | + |
| 44 | + const key = line.substring(0, colon).toLowerCase() |
| 45 | + const value = line.substring(colon + 1).replace(/^\s+/, '') |
| 46 | + |
| 47 | + if (key === 'request method') req.method = value |
| 48 | + else req.headers[key] = value |
| 49 | + }) |
| 50 | + req.rawBody = contents.substring(payloadOffset + 1) |
| 51 | + // In https://github.com/organizations/git-for-windows/settings/apps/gitforwindowshelper/advanced, |
| 52 | + // the JSON is pretty-printed, but the actual webhook event avoids any |
| 53 | + // unnecessary white-space in the body |
| 54 | + .replace(/\r?\n\s*("[^"]*":)\s*/g, '$1') |
| 55 | + .replace(/\r?\n\s*/g, '') |
| 56 | + req.body = JSON.parse(req.rawBody) |
| 57 | + } |
48 | 58 |
|
49 | 59 | const context = { |
50 | 60 | log: console.log, |
|
0 commit comments