-
Notifications
You must be signed in to change notification settings - Fork 6
/
test-pr-comment-delivery.js
executable file
·73 lines (62 loc) · 2.41 KB
/
test-pr-comment-delivery.js
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
#!/usr/bin/env node
(async () => {
const fs = require('fs')
// Expect a path as command-line parameter that points to a file containing
// an event copy/pasted from
// https://github.com/organizations/git-for-windows/settings/apps/gitforwindowshelper/advanced
// in the form:
//
// Headers
//
// Request method: POST
// [...]
//
// Payload
//
// {
// [...]
// }
const path = process.argv[2]
const contents = fs.readFileSync(path).toString('utf-8')
const req = {
headers: {}
}
if (contents.startsWith('event: {')) {
const event = JSON.parse(contents.substring(7))
Object.keys(event.request.headers).forEach(key => {
req.headers[key.toLowerCase()] = event.request.headers[key]
})
req.body = event.request.payload
req.rawBody = JSON.stringify(req.body)
req.method = 'POST'
} else {
const payloadOffset = contents.indexOf('\n{')
if (payloadOffset < 0) throw new Error(`Could not find start of payload in ${path}`)
contents.substring(0, payloadOffset).split(/\r?\n/).forEach(line => {
const colon = line.indexOf(':')
if (colon < 0) return
const key = line.substring(0, colon).toLowerCase()
const value = line.substring(colon + 1).replace(/^\s+/, '')
if (key === 'request method') req.method = value
else req.headers[key] = value
})
req.rawBody = contents.substring(payloadOffset + 1)
// In https://github.com/organizations/git-for-windows/settings/apps/gitforwindowshelper/advanced,
// the JSON is pretty-printed, but the actual webhook event avoids any
// unnecessary white-space in the body
.replace(/\r?\n\s*("[^"]*":)\s*/g, '$1')
.replace(/\r?\n\s*/g, '')
req.body = JSON.parse(req.rawBody)
}
const context = {
log: console.log,
req,
}
const localSettings = JSON.parse(fs.readFileSync('local.settings.json'))
Object.entries(localSettings.Values).forEach(([key, value]) => process.env[key] = value)
// avoid accidentally triggering anything
delete process.env.GITHUB_APP_PRIVATE_KEY
delete process.env.AZURE_PIPELINE_TRIGGER_TOKEN
const index = require('./GitForWindowsHelper/index')
console.log(await index(context, req) || context.res)
})().catch(console.log)