Skip to content

Use OpenAPI descriptions instead of octokit/routes #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions .eslintrc

This file was deleted.

2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
npm ci

- name: Test
env:
GITHUB_TOKEN: 987zyx
run: |
npm run lint
npm test
Expand Down
23 changes: 12 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const routes = require('@octokit/routes');
const fs = require('fs/promises');
const meta = require('./package');
const generate = require('./lib/generate');
const { generate, getLatestRoutes } = require('./lib/generate');

Object.entries(routes).forEach(([route, api]) => {
const data = generate({ api, meta });
const destination = path.normalize(path.join(__dirname, 'routes', `${route}.json`));
(async () => {
const routes = await getLatestRoutes();

// Write output straight to file
const output = JSON.stringify(data, null, 2);
fs.writeFileSync(destination, output);
});
Object.entries(routes).forEach(async ([route, api]) => {
const data = generate({ api, meta });
const destination = path.normalize(path.join(__dirname, 'routes', `${route}.json`));

process.exit(0);
// Write output straight to file
const output = JSON.stringify(data, null, 2);
await fs.writeFile(destination, output);
});
})();
46 changes: 42 additions & 4 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
const _ = require('lodash');
const { idGenerator } = require('./id-generator');
const { generateEnvironmentVariables, generateRequestGroups, generateRequests } = require('./generators');
const { Octokit } = require('@octokit/rest');

const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });
const owner = 'github';
const repo = 'rest-api-description';

const fldIdGenerator = idGenerator('FLD');
const reqIdGenerator = idGenerator('REQ');

function generate({ api, meta }) {
// eslint-disable-next-line no-underscore-dangle
const getContentAtPath = async (path) => {
return await octokit.repos.getContent({
owner,
repo,
path
});
};

const getFileContent = async (sha) => {
return await octokit.git.getBlob({
owner,
repo,
file_sha: sha
});
};

const generate = ({ api, meta }) => {
const _api = _(api);

const rootRequestGroup = {
Expand Down Expand Up @@ -35,6 +55,24 @@ function generate({ api, meta }) {
};

return data;
}
};

const getLatestRoutes = async () => {
const routes = {};
const { data: platforms } = await getContentAtPath('descriptions');

await Promise.all(platforms.map(async platform => {
const { data: files } = await getContentAtPath(platform.path + '/dereferenced');
const file = files.find(f => f.name.indexOf('json') >= 0);
const { data: blob } = await getFileContent(file.sha);
const content = Buffer.from(blob.content, 'base64').toString('utf-8');
routes[platform.name] = JSON.parse(content);
}));

return routes;
};

module.exports = generate;
module.exports = {
generate,
getLatestRoutes
};
13 changes: 8 additions & 5 deletions lib/generators.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');
const { requestGroupNameFromSpec, queryParamsFromSpec, previewHeadersFromSpec } = require('./helpers');

function generateEnvironmentVariables(api) {
function generateEnvironmentVariables (api) {
// Use a set so that we de-duplicate
const environmentVariables = new Set();

Expand All @@ -13,7 +13,10 @@ function generateEnvironmentVariables(api) {

Object.values(api.paths).forEach(methods => {
Object.values(methods).forEach(spec => {
spec.parameters
if (!spec.parameters) {
return [];
}
return spec.parameters
.filter(param => param.in === 'path')
.forEach(({ name, schema }) => {
// Add each environment variable as a key-value pair, initialize the value to default
Expand All @@ -27,7 +30,7 @@ function generateEnvironmentVariables(api) {
}
module.exports.generateEnvironmentVariables = generateEnvironmentVariables;

function generateRequestGroups(api, idGenerator, parentId) {
function generateRequestGroups (api, idGenerator, parentId) {
const groups = new Set();

Object.values(api.paths).forEach(methods => {
Expand All @@ -45,7 +48,7 @@ function generateRequestGroups(api, idGenerator, parentId) {
}
module.exports.generateRequestGroups = generateRequestGroups;

function generateRequests(api, idGenerator, requestGroups) {
function generateRequests (api, idGenerator, requestGroups) {
const requests = [];

// Generate an index of requestGroups by group
Expand Down Expand Up @@ -79,7 +82,7 @@ function generateRequests(api, idGenerator, requestGroups) {
_id: idGenerator(),
_type: 'request',
name: spec.summary,
description: `${spec.description}\n\n${spec.externalDocs.url}`,
description: spec.externalDocs ? `${spec.description}\n\n${spec.externalDocs.url}` : '',
headers,
authentication: {
token: '{{ github_token }}',
Expand Down
11 changes: 7 additions & 4 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
function requestGroupNameFromSpec(spec) {
function requestGroupNameFromSpec (spec) {
return spec.operationId.split('/')[0];
}
module.exports.requestGroupNameFromSpec = requestGroupNameFromSpec;

function queryParamsFromSpec(spec) {
function queryParamsFromSpec (spec) {
if (!spec.parameters) {
return [];
}
return spec.parameters
.filter(param => param.in === 'query')
.map(({ name, schema }) => {
Expand All @@ -16,8 +19,8 @@ function queryParamsFromSpec(spec) {
}
module.exports.queryParamsFromSpec = queryParamsFromSpec;

function previewHeadersFromSpec(spec) {
return spec['x-github'].previews.length
function previewHeadersFromSpec (spec) {
return spec['x-github'] && spec['x-github'].previews && spec['x-github'].previews.length
? {
name: 'Accept',
value: spec['x-github'].previews.map(preview => `application/vnd.github.${preview.name}-preview+json`).join(',')
Expand Down
4 changes: 2 additions & 2 deletions lib/id-generator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function idGenerator(kind, initial = 1, increment = 1) {
function idGenerator (kind, initial = 1, increment = 1) {
let current = initial;
return function generateId() {
return function generateId () {
const id = `__${kind}_${current}__`;
current += increment;
return id;
Expand Down
Loading