|
| 1 | +import { readFileSync } from "fs"; |
| 2 | +import { resolve, join, dirname } from "path"; |
| 3 | +import { fileURLToPath } from "url"; |
| 4 | + |
| 5 | +const SUPPORTED_GHES_OPERATIONS = ["3.6", "3.7", "3.8", "3.9", "3.10"]; |
| 6 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 7 | + |
| 8 | +function isDeferenced(filename) { |
| 9 | + return /deref/.test(filename); |
| 10 | +} |
| 11 | + |
| 12 | +// Updates the operation ID for a specific operation. Useful if you want to maintain |
| 13 | +// the function name in `plugin-rest-endpoint-methods.js` when the operation ID has |
| 14 | +// been changed in the OpenAPI specification. |
| 15 | +// |
| 16 | +// Throws an error if an operation is not found for the specified path and HTTP method. |
| 17 | +function rewriteOperationId(schema, path, httpMethod, operationId) { |
| 18 | + if (!schema.paths[path]) { |
| 19 | + throw `Path ${path} found not found in schema`; |
| 20 | + } |
| 21 | + |
| 22 | + if (!schema.paths[path][httpMethod]) { |
| 23 | + throw `HTTP method ${httpMethod} not found for path ${path} in schema`; |
| 24 | + } |
| 25 | + |
| 26 | + schema.paths[path][httpMethod].operationId = operationId; |
| 27 | +} |
| 28 | + |
| 29 | +// Adds an operation to the OpenAPI specification using JSON data stored in a file. |
| 30 | +// |
| 31 | +// Throws an error if an operation already exists for the specified path and HTTP method. |
| 32 | +function addOperation(schema, path, httpMethod, overridePath) { |
| 33 | + if (schema.paths[path] && schema.paths[path][httpMethod]) { |
| 34 | + throw `Operation \`${httpMethod} ${path}\` already exists`; |
| 35 | + } |
| 36 | + |
| 37 | + if (!schema.paths[path]) { |
| 38 | + schema.paths[path] = {}; |
| 39 | + } |
| 40 | + |
| 41 | + schema.paths[path][httpMethod] = JSON.parse( |
| 42 | + readFileSync(resolve(join(__dirname, overridePath)), "utf8"), |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +// Replaces a given operation using JSON data stored in a file. |
| 47 | +// |
| 48 | +// Throws an error if an operation is not found for the specified path and HTTP method. |
| 49 | +function replaceOperation(schema, path, httpMethod, overridePath) { |
| 50 | + if (!schema.paths[path]) { |
| 51 | + throw `Path ${path} not found in schema`; |
| 52 | + } |
| 53 | + |
| 54 | + if (!schema.paths[path][httpMethod]) { |
| 55 | + throw `HTTP method ${httpMethod} not found for path ${path} in schema`; |
| 56 | + } |
| 57 | + |
| 58 | + schema.paths[path][httpMethod] = JSON.parse( |
| 59 | + readFileSync(resolve(join(__dirname, overridePath)), "utf8"), |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +export default function overrides(file, schema) { |
| 64 | + const isGHES = file.startsWith("ghes-"); |
| 65 | + const isAE = file.startsWith("github.ae"); |
| 66 | + const isDotcom = file.startsWith("api.github.com"); |
| 67 | + const ghesVersion = isGHES ? file.match(/(?<=^ghes-)\d+\.\d+/)[0] : null; |
| 68 | + |
| 69 | + if (isGHES && SUPPORTED_GHES_OPERATIONS.indexOf(ghesVersion) == -1) { |
| 70 | + throw ( |
| 71 | + `GHES version ${ghesVersion} is not yet supported. Please check the overrides ` + |
| 72 | + `in \`scripts/overrides/index.js\` to check if they are relevant to this version, ` + |
| 73 | + `and then update \`SUPPORTED_GHES_VERSION\`.` |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +} |
0 commit comments