Skip to content

Commit 56ee038

Browse files
committed
test-pr-comment-delivery: accept payload via get-webhook-event-payload.js
The output of `get-webhook-event-payload.js` is in a different format than when copy/pasting from the web UI of the GitHub App's webhook deliveries. Let's prepare the `test-pr-comment-delivery.js` script to accept its input in that format, too. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 2dde629 commit 56ee038

1 file changed

Lines changed: 29 additions & 19 deletions

File tree

test-pr-comment-delivery.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,35 @@
2626
headers: {}
2727
}
2828

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+
}
4858

4959
const context = {
5060
log: console.log,

0 commit comments

Comments
 (0)