forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync.js
More file actions
79 lines (69 loc) · 3.02 KB
/
sync.js
File metadata and controls
79 lines (69 loc) · 3.02 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
import { readFile, writeFile } from 'fs/promises'
import { existsSync } from 'fs'
import path from 'path'
import { mkdirp } from 'mkdirp'
import { WEBHOOK_DATA_DIR, WEBHOOK_SCHEMA_FILENAME } from '../lib/index.js'
import Webhook from './webhook.js'
export async function syncWebhookData(sourceDirectory, webhookSchemas) {
await Promise.all(
webhookSchemas.map(async (schemaName) => {
const file = path.join(sourceDirectory, schemaName)
const schema = JSON.parse(await readFile(file, 'utf-8'))
// In OpenAPI version 3.1, the schema data is under the `webhooks`
// key, but in 3.0 the schema data was in `x-webhooks`.
// We just fallback to `x-webhooks` for now since there's
// currently no difference in the schema data between versions.
const webhookSchemaData = schema.webhooks ?? schema['x-webhooks']
if (!webhookSchemaData) {
console.log(
`🟡 No webhooks exist in ${sourceDirectory}/${schemaName}. No static webhook files will be generated.`,
)
return
}
const webhooks = Object.values(webhookSchemaData).map((webhook) => new Webhook(webhook.post))
await processWebhookSchema(webhooks)
const data = await formatWebhookData(webhooks)
if (Object.keys(data).length === 0) {
throw new Error(
`Generating Webhook data failed for ${sourceDirectory}/${schemaName}. The generated data file was empty.`,
)
}
const versionName = path.basename(schemaName, '.json')
const targetDirectory = path.join(WEBHOOK_DATA_DIR, versionName)
if (!existsSync(targetDirectory)) {
await mkdirp(targetDirectory)
}
const targetPath = path.join(targetDirectory, WEBHOOK_SCHEMA_FILENAME)
await writeFile(targetPath, JSON.stringify(data, null, 2))
console.log(`✅ Wrote ${targetPath}`)
}),
)
}
async function processWebhookSchema(webhooks) {
try {
if (webhooks.length) {
await Promise.all(webhooks.map((webhook) => webhook.process()))
}
} catch (error) {
throw new Error(
"🐛 Whoops! It looks like the decorator script wasn't able to parse the dereferenced schema. A recent change may not yet be supported by the decorator. Please reach out in the #docs-engineering slack channel for help.",
)
}
}
// Create an object with all webhooks where the key is the webhook name.
// Webhooks typically have a property called `action` that describes the
// events that trigger the webhook. Some webhooks (like `ping`) don't have
// action types -- in that case we set a the value of action to 'default'.
async function formatWebhookData(webhooks) {
const categorizedWebhooks = {}
for (const webhook of Object.values(webhooks)) {
if (!webhook.action) webhook.action = 'default'
if (categorizedWebhooks[webhook.category]) {
categorizedWebhooks[webhook.category][webhook.action] = webhook
} else {
categorizedWebhooks[webhook.category] = {}
categorizedWebhooks[webhook.category][webhook.action] = webhook
}
}
return categorizedWebhooks
}