| 16 | ] |
| 17 | |
| 18 | export default class Webhook { |
| 19 | #webhook |
| 20 | constructor(webhook) { |
| 21 | this.#webhook = webhook |
| 22 | this.descriptionHtml = '' |
| 23 | this.summaryHtml = '' |
| 24 | this.bodyParameters = [] |
| 25 | this.availability = webhook['x-github']['supported-webhook-types'] |
| 26 | this.action = get( |
| 27 | webhook, |
| 28 | `requestBody.content['application/json'].schema.properties.action.enum[0]`, |
| 29 | null |
| 30 | ) |
| 31 | |
| 32 | // for some webhook action types (like some pull-request webhook types) the |
| 33 | // schema properties are under a oneOf so we try and take the action from |
| 34 | // the first one (the action will be the same across oneOf items) |
| 35 | if (!this.action) { |
| 36 | this.action = get( |
| 37 | webhook, |
| 38 | `requestBody.content['application/json'].schema.oneOf[0].properties.action.enum[0]`, |
| 39 | null |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | // The OpenAPI uses hyphens for the webhook names, but the webhooks |
| 44 | // are sent using underscores (e.g. `branch_protection_rule` instead |
| 45 | // of `branch-protection-rule`) |
| 46 | this.category = webhook['x-github'].subcategory.replaceAll('-', '_') |
| 47 | return this |
| 48 | } |
| 49 | |
| 50 | async process() { |
| 51 | await Promise.all([this.renderDescription(), this.renderBodyParameterDescriptions()]) |
| 52 | |
| 53 | const ajv = new Ajv() |
| 54 | const valid = ajv.validate(webhookSchema, this) |
| 55 | if (!valid) { |
| 56 | console.error(JSON.stringify(ajv.errors, null, 2)) |
| 57 | throw new Error(`Invalid OpenAPI webhook found: ${this.category}`) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | async renderDescription() { |
| 62 | this.descriptionHtml = await renderContent(this.#webhook.description) |
| 63 | this.summaryHtml = await renderContent(this.#webhook.summary) |
| 64 | return this |
| 65 | } |
| 66 | |
| 67 | async renderBodyParameterDescriptions() { |
| 68 | if (!this.#webhook.requestBody) return [] |
| 69 | const schema = get(this.#webhook, `requestBody.content.['application/json'].schema`, {}) |
| 70 | this.bodyParameters = isPlainObject(schema) ? await getBodyParams(schema, true, this.title) : [] |
| 71 | |
| 72 | // Removes the children of the common properties |
| 73 | this.bodyParameters.forEach((param) => { |
| 74 | if (NO_CHILD_PROPERTIES.includes(param.name)) { |
| 75 | param.childParamsGroups = [] |
nothing calls this directly
no outgoing calls
no test coverage detected