1 | const { writeFileSync } = require("fs");
|
2 | const { resolve: pathResolve } = require("path");
|
3 |
|
4 | const prettier = require("prettier");
|
5 | const sortKeys = require("sort-keys");
|
6 |
|
7 | const WORKAROUNDS = require("./workarounds");
|
8 |
|
9 | const GHE_VERSIONS = ["2.16", "2.17", "2.18", "2.19"];
|
10 | const newRoutes = {};
|
11 |
|
12 | generateRoutes();
|
13 |
|
14 | async function generateRoutes() {
|
15 | for (const version of GHE_VERSIONS) {
|
16 | const endpoints = require(`./generated/ghe-${version}-endpoints.json`);
|
17 | endpoints.concat(WORKAROUNDS).forEach(endpoint => {
|
18 | const scope = endpoint.scope;
|
19 |
|
20 | if (!newRoutes[scope]) {
|
21 | newRoutes[scope] = {};
|
22 | }
|
23 |
|
24 | const idName = endpoint.id;
|
25 | const url = endpoint.url.toLowerCase().replace(/\{(\w+)\}/g, ":$1");
|
26 |
|
27 |
|
28 | newRoutes[scope][idName] = {
|
29 | method: endpoint.method,
|
30 | headers: endpoint.headers.reduce((result, header) => {
|
31 | if (!result) {
|
32 | result = {};
|
33 | }
|
34 | result[header.name] = header.value;
|
35 | return result;
|
36 | }, undefined),
|
37 | params: endpoint.parameters.reduce((result, param) => {
|
38 | result[param.name] = {
|
39 | type: param.type
|
40 | };
|
41 | if (param.allowNull) {
|
42 | result[param.name].allowNull = true;
|
43 | }
|
44 | if (param.required) {
|
45 | result[param.name].required = true;
|
46 | }
|
47 | if (param.mapToData) {
|
48 | result[param.name].mapTo = "data";
|
49 | }
|
50 | if (param.enum) {
|
51 | result[param.name].enum = param.enum;
|
52 | }
|
53 | if (param.validation) {
|
54 | result[param.name].validation = param.validation;
|
55 | }
|
56 | if (param.deprecated) {
|
57 | result[param.name].deprecated = true;
|
58 |
|
59 | if (param.alias) {
|
60 | result[param.name].alias = param.alias;
|
61 | result[param.name].type = result[param.alias].type;
|
62 | } else {
|
63 | result[param.name].type = param.type;
|
64 | result[param.name].description = param.description;
|
65 | }
|
66 | }
|
67 |
|
68 | return result;
|
69 | }, {}),
|
70 | url
|
71 | };
|
72 |
|
73 | const previewHeaders = endpoint.previews
|
74 | .map(preview => `application/vnd.github.${preview.name}-preview+json`)
|
75 | .join(",");
|
76 |
|
77 | if (previewHeaders) {
|
78 | newRoutes[scope][idName].headers = {
|
79 | accept: previewHeaders
|
80 | };
|
81 | }
|
82 |
|
83 | if (endpoint.renamed) {
|
84 | newRoutes[scope][
|
85 | idName
|
86 | ].deprecated = `octokit.${endpoint.renamed.before.scope}.${endpoint.renamed.before.id}() has been renamed to octokit.${endpoint.renamed.after.scope}.${endpoint.renamed.after.id}() (${endpoint.renamed.date})`;
|
87 | }
|
88 |
|
89 | if (endpoint.isDeprecated) {
|
90 | newRoutes[scope][
|
91 | idName
|
92 | ].deprecated = `octokit.${scope}.${idName}() is deprecated, see ${endpoint.documentationUrl}`;
|
93 | }
|
94 | });
|
95 |
|
96 | const newRoutesSorted = sortKeys(newRoutes, { deep: true });
|
97 |
|
98 | const allResultsPath = pathResolve(
|
99 | process.cwd(),
|
100 | `ghe-${version}/all.json`
|
101 | );
|
102 | writeFileSync(
|
103 | allResultsPath,
|
104 | prettier.format(JSON.stringify(newRoutesSorted), {
|
105 | parser: "json"
|
106 | })
|
107 | );
|
108 | console.log(`${allResultsPath} written.`);
|
109 |
|
110 | const enterpriseAdminResultsPath = pathResolve(
|
111 | process.cwd(),
|
112 | `ghe-${version}/enterprise-admin.json`
|
113 | );
|
114 | writeFileSync(
|
115 | enterpriseAdminResultsPath,
|
116 | prettier.format(JSON.stringify(newRoutesSorted.enterpriseAdmin), {
|
117 | parser: "json"
|
118 | })
|
119 | );
|
120 | console.log(`${enterpriseAdminResultsPath} written.`);
|
121 |
|
122 | const indexPath = pathResolve(process.cwd(), `ghe-${version}/index.js`);
|
123 | writeFileSync(
|
124 | indexPath,
|
125 | `module.exports = octokit =>
|
126 | octokit.registerEndpoints({
|
127 | enterpriseAdmin: require("./enterprise-admin.json")
|
128 | });
|
129 | `
|
130 | );
|
131 | console.log(`${indexPath} written.`);
|
132 |
|
133 | const allPath = pathResolve(process.cwd(), `ghe-${version}/all.js`);
|
134 | writeFileSync(
|
135 | allPath,
|
136 | 'module.exports = octokit => octokit.registerEndpoints(require("./all.json"));\n'
|
137 | );
|
138 | console.log(`${allPath} written.`);
|
139 |
|
140 | const readmePath = pathResolve(process.cwd(), `ghe-${version}/README.md`);
|
141 | const content = `# @octokit/plugin-enterprise-rest/ghe-${version}
|
142 |
|
143 | ## Enterprise Administration
|
144 |
|
145 | \`\`\`js
|
146 | ${Object.keys(newRoutesSorted.enterpriseAdmin)
|
147 | .map(methodName =>
|
148 | endpointToMethod(
|
149 | "enterpriseAdmin",
|
150 | methodName,
|
151 | newRoutesSorted.enterpriseAdmin[methodName]
|
152 | )
|
153 | )
|
154 | .join("\n")}
|
155 | \`\`\`
|
156 |
|
157 | ## Others
|
158 |
|
159 | \`\`\`js
|
160 | ${Object.keys(newRoutesSorted)
|
161 | .filter(scope => scope !== "enterpriseAdmin")
|
162 | .map(scope =>
|
163 | Object.keys(newRoutesSorted[scope])
|
164 | .map(methodName =>
|
165 | endpointToMethod(scope, methodName, newRoutesSorted[scope][methodName])
|
166 | )
|
167 | .join("\n")
|
168 | )
|
169 | .join("\n")}
|
170 | \`\`\`
|
171 | `;
|
172 | writeFileSync(readmePath, prettier.format(content, { parser: "markdown" }));
|
173 | console.log(`${readmePath} written.`);
|
174 | }
|
175 | }
|
176 |
|
177 | function endpointToMethod(scope, methodName, meta) {
|
178 | return `octokit.${scope}.${methodName}(${Object.keys(meta.params)
|
179 | .filter(param => !/\./.test(param) && !meta.params[param].deprecated)
|
180 | .join(", ")});`;
|
181 | }
|