|
| 1 | +#!/usr/bin/env ts-node-transpile-only |
| 2 | + |
| 3 | +import { strict as assert } from "assert"; |
| 4 | +import * as fs from "fs"; |
| 5 | +import { JSONSchema7, JSONSchema7Definition } from "json-schema"; |
| 6 | +import { format } from "prettier"; |
| 7 | + |
| 8 | +type JSONSchemaWithRef = JSONSchema7 & Required<Pick<JSONSchema7, "$ref">>; |
| 9 | + |
| 10 | +interface Schema extends JSONSchema7 { |
| 11 | + definitions: Record<string, JSONSchema7>; |
| 12 | + oneOf: JSONSchemaWithRef[]; |
| 13 | +} |
| 14 | + |
| 15 | +const schema = require("@octokit/webhooks-definitions/schema.json") as Schema; |
| 16 | + |
| 17 | +const titleCase = (str: string) => `${str[0].toUpperCase()}${str.substring(1)}`; |
| 18 | + |
| 19 | +const guessAtInterfaceName = (str: string) => |
| 20 | + str.split(/[$_-]/u).map(titleCase).join(""); |
| 21 | + |
| 22 | +const guessAtEventName = (name: string) => { |
| 23 | + const [, eventName] = /^(.+)[$_-]event/u.exec(name) ?? []; |
| 24 | + |
| 25 | + assert.ok(eventName, `unable to guess event name for "${name}"`); |
| 26 | + |
| 27 | + return eventName; |
| 28 | +}; |
| 29 | +const guessAtActionName = (name: string) => name.replace("$", "."); |
| 30 | + |
| 31 | +const getDefinitionName = (ref: string): string => { |
| 32 | + assert.ok( |
| 33 | + ref.startsWith("#/definitions/"), |
| 34 | + `${ref} does not reference a valid definition` |
| 35 | + ); |
| 36 | + |
| 37 | + const [, name] = /^#\/definitions\/(.+)$/u.exec(ref) ?? []; |
| 38 | + |
| 39 | + assert.ok(name, `unable to find definition name ${ref}`); |
| 40 | + |
| 41 | + return name; |
| 42 | +}; |
| 43 | + |
| 44 | +type NameAndActions = [name: string, actions: string[]]; |
| 45 | +type Property = [key: string, value: string]; |
| 46 | +type ImportsAndProperties = [imports: string[], properties: Property[]]; |
| 47 | + |
| 48 | +const buildEventProperties = ([ |
| 49 | + eventName, |
| 50 | + actions, |
| 51 | +]: NameAndActions): ImportsAndProperties => { |
| 52 | + const interfaceName = guessAtInterfaceName(eventName); |
| 53 | + const importsAndProperties: ImportsAndProperties = [ |
| 54 | + [interfaceName], |
| 55 | + [[guessAtEventName(eventName), interfaceName]], |
| 56 | + ]; |
| 57 | + |
| 58 | + if (actions.length) { |
| 59 | + actions.forEach((actionName) => { |
| 60 | + const actionInterfaceName = guessAtInterfaceName(`${actionName}_event`); |
| 61 | + |
| 62 | + importsAndProperties[0].push(actionInterfaceName); |
| 63 | + importsAndProperties[1].push([ |
| 64 | + guessAtActionName(actionName), |
| 65 | + actionInterfaceName, |
| 66 | + ]); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + return importsAndProperties; |
| 71 | +}; |
| 72 | + |
| 73 | +const isJSONSchemaWithRef = ( |
| 74 | + object: JSONSchema7Definition |
| 75 | +): object is JSONSchemaWithRef => |
| 76 | + typeof object === "object" && object.$ref !== undefined; |
| 77 | + |
| 78 | +const listEvents = () => { |
| 79 | + return schema.oneOf.map<NameAndActions>(({ $ref }) => { |
| 80 | + const name = getDefinitionName($ref); |
| 81 | + const definition = schema.definitions[name]; |
| 82 | + |
| 83 | + assert.ok(definition, `unable to find definition named ${name}`); |
| 84 | + |
| 85 | + if (definition.oneOf?.every(isJSONSchemaWithRef)) { |
| 86 | + return [name, definition.oneOf.map((def) => getDefinitionName(def.$ref))]; |
| 87 | + } |
| 88 | + |
| 89 | + return [name, []]; |
| 90 | + }); |
| 91 | +}; |
| 92 | + |
| 93 | +const getImportsAndProperties = (): ImportsAndProperties => { |
| 94 | + const importsAndProperties = listEvents().map(buildEventProperties); |
| 95 | + |
| 96 | + return importsAndProperties.reduce<ImportsAndProperties>( |
| 97 | + (allImportsAndProperties, [imports, properties]) => { |
| 98 | + return [ |
| 99 | + allImportsAndProperties[0].concat(imports), |
| 100 | + allImportsAndProperties[1].concat(properties), |
| 101 | + ]; |
| 102 | + }, |
| 103 | + [[], []] |
| 104 | + ); |
| 105 | +}; |
| 106 | + |
| 107 | +const outDir = "src/generated/"; |
| 108 | + |
| 109 | +const generateTypeScriptFile = (name: string, contents: string[]) => { |
| 110 | + fs.writeFileSync( |
| 111 | + `${outDir}/${name}.ts`, |
| 112 | + format(contents.join("\n"), { parser: "typescript" }) |
| 113 | + ); |
| 114 | +}; |
| 115 | + |
| 116 | +const run = () => { |
| 117 | + const [imports, properties] = getImportsAndProperties(); |
| 118 | + |
| 119 | + const lines: string[] = [ |
| 120 | + "// THIS FILE IS GENERATED - DO NOT EDIT DIRECTLY", |
| 121 | + "// make edits in scripts/generate-types.js", |
| 122 | + "", |
| 123 | + "import {", |
| 124 | + ...imports.map((str) => ` ${str},`), |
| 125 | + '} from "@octokit/webhooks-definitions/schema";', |
| 126 | + "", |
| 127 | + "export interface EmitterEventWebhookPayloadMap {", |
| 128 | + ...properties.map(([key, value]) => `"${key}": ${value}`), |
| 129 | + "}", |
| 130 | + ]; |
| 131 | + |
| 132 | + generateTypeScriptFile("get-webhook-payload-type-from-event", lines); |
| 133 | + generateTypeScriptFile("webhook-names", [ |
| 134 | + "// THIS FILE IS GENERATED - DO NOT EDIT DIRECTLY", |
| 135 | + "// make edits in scripts/update-known-events.js", |
| 136 | + "", |
| 137 | + "export const emitterEventNames = [", |
| 138 | + '"*",', |
| 139 | + '"error",', |
| 140 | + ...properties.map(([key]) => `"${key}",`), |
| 141 | + "];", |
| 142 | + ]); |
| 143 | +}; |
| 144 | + |
| 145 | +run(); |
0 commit comments