forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.js
75 lines (66 loc) · 2.24 KB
/
webhook.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
#!/usr/bin/env node
import Ajv from 'ajv'
import { get, isPlainObject } from 'lodash-es'
import renderContent from '../../../lib/render-content/index.js'
import webhookSchema from './webhook-schema.js'
import { getBodyParams } from './get-body-params.js'
const NO_CHILD_PROPERTIES = [
'action',
'enterprise',
'installation',
'organization',
'repository',
'sender',
]
export default class Webhook {
#webhook
constructor(webhook) {
this.#webhook = webhook
this.descriptionHtml = ''
this.summaryHtml = ''
this.bodyParameters = []
this.availability = webhook['x-github']['supported-webhook-types']
this.action = get(
webhook,
`requestBody.content['application/json'].schema.properties.action.enum[0]`,
null
)
// for some webhook action types (like some pull-request webhook types) the
// schema properties are under a oneOf so we try and take the action from
// the first one (the action will be the same across oneOf items)
if (!this.action) {
this.action = get(
webhook,
`requestBody.content['application/json'].schema.oneOf[0].properties.action.enum[0]`,
null
)
}
this.category = webhook['x-github'].subcategory
return this
}
async process() {
await Promise.all([this.renderDescription(), this.renderBodyParameterDescriptions()])
const ajv = new Ajv()
const valid = ajv.validate(webhookSchema, this)
if (!valid) {
console.error(JSON.stringify(ajv.errors, null, 2))
throw new Error(`Invalid OpenAPI webhook found: ${this.category}`)
}
}
async renderDescription() {
this.descriptionHtml = await renderContent(this.#webhook.description)
this.summaryHtml = await renderContent(this.#webhook.summary)
return this
}
async renderBodyParameterDescriptions() {
if (!this.#webhook.requestBody) return []
const schema = get(this.#webhook, `requestBody.content.['application/json'].schema`, {})
this.bodyParameters = isPlainObject(schema) ? await getBodyParams(schema, true, this.title) : []
// Removes the children of the common properties
this.bodyParameters.forEach((param) => {
if (NO_CHILD_PROPERTIES.includes(param.name)) {
param.childParamsGroups = []
}
})
}
}