-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
165 lines (149 loc) · 4.09 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const crypto = require('crypto')
const express = require('express')
const {Client} = require('@opensearch-project/opensearch')
const app = express()
const AUTH = process.env.GH_TELEMETRY_AUTH
const HOST = process.env.GH_TELEMETRY_HOST
const PORT = process.env.GH_TELEMETRY_PORT || 3000
const PROTOCOL = process.env.GH_TELEMETRY_PROTOCOL
const FIELD_LIMIT = process.env.GH_TELEMETRY_FIELD_LIMIT || 2500
const SHARDS = process.env.GH_TELEMETRY_SHARDS || 3
const REPLICAS = process.env.GH_TELEMETRY_REPLICAS || 3
const SECRET = process.env.GH_TELEMETRY_SECRET
const client = new Client({
node: `${PROTOCOL}://${AUTH}@${HOST}`
})
const indices = [
'branch_protection_rule',
'check_run',
'check_suite',
'code_scanning_alert',
'commit_comment',
'create',
'delete',
'deploy_key',
'deployment',
'deployment_status',
'discussion',
'discussion_comment',
'fork',
'github_app_authorization',
'gollum',
'installation',
'installation_repositories',
'issue_comment',
'issues',
'label',
'marketplace_purchase',
'member',
'membership',
'meta',
'milestone',
'organization',
'org_block',
'package',
'page_build',
'ping',
'project',
'project_card',
'project_column',
'public',
'pull_request',
'pull_request_review',
'pull_request_review_comment',
'pull_request_review_thread',
'push',
'release',
'repository_dispatch',
'repository',
'repository_import',
'repository_vulnerability_alert',
'security_advisory',
'sponsorship',
'star',
'status',
'team',
'team_add',
'watch',
'workflow_dispatch',
'workflow_job',
'workflow_run'
]
async function seedIndices() {
for (const index of indices) {
const created = await createIndexIfNotExists(index, settings)
if (!created) {
process.exit(1)
}
}
}
async function createIndexIfNotExists(index) {
const settings = {
'settings': {
'index': {
'mapping.total_fields.limit': FIELD_LIMIT,
'number_of_shards': SHARDS,
'number_of_replicas': REPLICAS
}
}
}
try {
const exists = await client.indices.exists({
index: index
})
if (exists.body === false) {
await client.indices.create({
index: index,
body: settings
})
}
return true
} catch (err) {
console.error(`Error creating [${index}] index: ${err}`)
return false
}
}
app.post('/github/webhooks', async (req, res) => {
try {
if (!validateWebhook(req)) {
res.status(400).send({message: 'Invalid signature'})
return
}
const id = req.headers['x-github-delivery']
const index = req.headers['x-github-event']
if (!indices.includes(index)) {
const created = await createIndexIfNotExists(index)
if (created) {
indices.append(index)
} else {
res.status(500).send({message: `Error creating [${index}] index`})
return
}
}
console.log(`[${index}:${id}] Adding document`)
await client.index({
id: id,
index: index,
body: req.body,
refresh: true
})
console.log(`[${index}:${id}] Document added`)
res.status(201).send({message: 'Document added'})
} catch (err) {
console.error(`Error indexing document: ${err.message}`)
res.status(err.status || 413).send({message: err.message})
}
})
function validateWebhook(req) {
const signature = req.headers['x-hub-signature-256']
const digest = crypto.createHmac('sha256', SECRET).update(JSON.stringify(req.body)).digest('hex')
const expectedSignature = `sha256=${digest}`
return signature === expectedSignature
}
(async function main() {
await seedIndices()
app.use(express.json())
app.listen(PORT, () => {
console.log(`Listening for webhooks on port ${PORT}`)
})
})()