-
Notifications
You must be signed in to change notification settings - Fork 68.3k
Expand file tree
/
Copy pathwebhook.ts
More file actions
111 lines (98 loc) · 3.39 KB
/
Copy pathwebhook.ts
File metadata and controls
111 lines (98 loc) · 3.39 KB
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
import { get, isPlainObject } from 'lodash-es'
import { getJsonValidator } from '@/tests/lib/validate-json-schema'
import { renderContent } from '@/content-render/index'
import { normalizeDocsUrls } from '../../rest/scripts/utils/normalize-docs-urls'
import webhookSchema from './webhook-schema'
import { getBodyParams, TransformedParam } from '../../rest/scripts/utils/get-body-params'
const NO_CHILD_PROPERTIES = [
'action',
'comment',
'enterprise',
'installation',
'organization',
'repository',
'sender',
]
const validate = getJsonValidator(webhookSchema)
export interface WebhookSchema {
description: string
summary: string
requestBody?: {
content: {
'application/json': {
schema: Record<string, unknown>
}
}
}
'x-github': {
'supported-webhook-types': string[]
subcategory: string
}
}
interface WebhookInterface {
descriptionHtml: string
summaryHtml: string
bodyParameters: TransformedParam[]
availability: string[]
action: string | null
category: string
process(): Promise<void>
renderDescription(): Promise<this>
renderBodyParameterDescriptions(): Promise<void>
}
export default class Webhook implements WebhookInterface {
#webhook: WebhookSchema
descriptionHtml: string = ''
summaryHtml: string = ''
bodyParameters: TransformedParam[] = []
availability: string[]
action: string | null
category: string
constructor(webhook: WebhookSchema) {
this.#webhook = webhook
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,
)
}
// The OpenAPI uses hyphens for the webhook names, but the webhooks
// are sent using underscores (e.g. `branch_protection_rule` instead
// of `branch-protection-rule`)
this.category = webhook['x-github'].subcategory.replace(/-/g, '_')
}
async process(): Promise<void> {
await Promise.all([this.renderDescription(), this.renderBodyParameterDescriptions()])
const isValid = validate(this as WebhookInterface) // Add type assertion here
if (!isValid) {
console.error(JSON.stringify(validate.errors, null, 2))
throw new Error(`Invalid OpenAPI webhook found: ${this.category}`)
}
}
async renderDescription(): Promise<this> {
this.descriptionHtml = normalizeDocsUrls(await renderContent(this.#webhook.description))
this.summaryHtml = normalizeDocsUrls(await renderContent(this.#webhook.summary))
return this
}
async renderBodyParameterDescriptions(): Promise<void> {
if (!this.#webhook.requestBody) return
const schema = get(this.#webhook, `requestBody.content['application/json'].schema`, {})
this.bodyParameters = isPlainObject(schema) ? await getBodyParams(schema, true) : []
// Removes the children of the common properties
for (const param of this.bodyParameters) {
if (NO_CHILD_PROPERTIES.includes(param.name)) {
param.childParamsGroups = []
}
}
}
}