This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
refactor: generate openapi directly instead of legacy format #482
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
568ad0f
WIP refactor: generate openapi directly instead of legacy format
637c7c6
WIP refactor: generate openapi directly instead of legacy format, part 2
27c32ff
WIP refactor: generate openapi directly instead of legacy format, part 3
0aa2904
WIP refactor: generate openapi directly instead of legacy format, part 4
47697ba
WIP refactor: generate openapi directly instead of legacy format, part 5
f0b99a6
refactor: generate openapi directly instead of legacy format, part 6
2a36efd
refactor: make update script generate same openapi docs (+ deprecated…
50acbf0
refactor: remove legacy format conversion now that it's unused
ff733a9
style: standard
gr2m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
module.exports = addCodeSamples | ||
|
||
const urlTemplate = require('url-template') | ||
const { stringify } = require('javascript-stringify') | ||
|
||
// TODO: find a better place to define parameter examples | ||
const PARAMETER_EXAMPLES = { | ||
owner: 'octocat', | ||
repo: 'hello-world', | ||
issue_number: 1, | ||
title: 'title' | ||
} | ||
|
||
function addCodeSamples ({ routes, scope, baseUrl }) { | ||
routes.forEach(route => { | ||
const codeSampleParams = { route, scope, baseUrl } | ||
route.operation['x-code-samples'] = route.operation['x-code-samples'].concat( | ||
{ lang: 'Shell', source: toShellExample(codeSampleParams) }, | ||
{ lang: 'JS', source: toJsExample(codeSampleParams) } | ||
) | ||
}) | ||
} | ||
|
||
function toShellExample ({ route, scope, baseUrl }) { | ||
const path = urlTemplate.parse(route.path.replace(/:(\w+)/, '{$1}')).expand(PARAMETER_EXAMPLES) | ||
const params = {} | ||
Object.assign(params, getRequiredBodyParamDict(route.operation)) | ||
|
||
const method = route.method.toUpperCase() | ||
const defaultAcceptHeader = route.operation.parameters[0].schema.default | ||
|
||
const args = [ | ||
method !== 'GET' && `-X${method}`, | ||
`-H"Accept: ${defaultAcceptHeader}"`, | ||
new URL(path, baseUrl).href, | ||
Object.keys(params).length && `-d '${JSON.stringify(params)}'` | ||
].filter(Boolean) | ||
return `curl \\\n ${args.join(' \\\n ')}` | ||
} | ||
|
||
function toJsExample ({ route, scope, baseUrl }) { | ||
const params = route.operation.parameters | ||
.filter(param => !param.deprecated) | ||
.filter(param => param.in !== 'header') | ||
.filter(param => param.required) | ||
.reduce((params, param) => Object.assign(params, { | ||
[param.name]: PARAMETER_EXAMPLES[param.name] || param.name | ||
}), {}) | ||
Object.assign(params, getRequiredBodyParamDict(route.operation)) | ||
|
||
return `octokit.${scope}.get(${Object.keys(params).length ? stringify(params, null, 2) : ''})` | ||
} | ||
|
||
function getRequiredBodyParamDict (operation) { | ||
let schema | ||
try { | ||
schema = operation.requestBody.content['application/json'].schema | ||
} catch (noResponseBody) { | ||
return {} | ||
} | ||
if (schema.type === 'string') { | ||
const paramName = operation['x-github'].requestBodyParameterName | ||
return { [paramName]: PARAMETER_EXAMPLES[paramName] || paramName } | ||
} | ||
const requiredBodyParamDict = {} | ||
const requiredBodyParams = [].concat(schema.required || []) | ||
// Temporary workarounds for PR#482: | ||
const { prepareParams, prepareParamDict } = getParamAlterers(operation.operationId) | ||
prepareParams && prepareParams(requiredBodyParams) | ||
requiredBodyParams.length > 0 && Object.assign( | ||
requiredBodyParamDict, | ||
requiredBodyParams.reduce(reduceToParamDict, {}) | ||
) | ||
prepareParamDict && prepareParamDict(requiredBodyParamDict) | ||
return requiredBodyParamDict | ||
|
||
function reduceToParamDict (paramDict, paramName) { | ||
const paramSchema = operation.requestBody.content['application/json'] | ||
.schema.properties[paramName] | ||
if (!paramSchema.deprecated) { | ||
paramDict[paramName] = PARAMETER_EXAMPLES[paramName] || paramName | ||
|
||
// Temporarily add object props as separate string values because we're | ||
// not touching the OpenAPI docs in this PR | ||
// TODO: Make code samples respect parameter types and really just be less weird | ||
let props, prefix | ||
if (paramSchema.type === 'object') { | ||
props = paramSchema.properties | ||
prefix = paramName | ||
} else if (paramSchema.type === 'array' && paramSchema.items.type === 'object') { | ||
props = paramSchema.items.properties | ||
prefix = `${paramName}[]` | ||
} | ||
if (props && prefix) { | ||
for (const propName of Object.keys(props)) { | ||
const fauxPropName = `${prefix}.${propName}` | ||
paramDict[fauxPropName] = fauxPropName | ||
} | ||
} | ||
} | ||
return paramDict | ||
} | ||
} | ||
|
||
function getParamAlterers (operationId) { | ||
switch (operationId) { | ||
case 'checks-create': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
Object.assign(paramDict, { | ||
'output.title': 'output.title', | ||
'output.summary': 'output.summary', | ||
'output.annotations[].path': 'output.annotations[].path', | ||
'output.annotations[].start_line': 'output.annotations[].start_line', | ||
'output.annotations[].end_line': 'output.annotations[].end_line', | ||
'output.annotations[].annotation_level': 'output.annotations[].annotation_level', | ||
'output.annotations[].message': 'output.annotations[].message', | ||
'output.images[].alt': 'output.images[].alt', | ||
'output.images[].image_url': 'output.images[].image_url', | ||
'actions[].label': 'actions[].label', | ||
'actions[].description': 'actions[].description', | ||
'actions[].identifier': 'actions[].identifier' | ||
}) | ||
} | ||
} | ||
case 'checks-update': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
Object.assign(paramDict, { | ||
'output.summary': 'output.summary', | ||
'output.annotations[].path': 'output.annotations[].path', | ||
'output.annotations[].start_line': 'output.annotations[].start_line', | ||
'output.annotations[].end_line': 'output.annotations[].end_line', | ||
'output.annotations[].annotation_level': 'output.annotations[].annotation_level', | ||
'output.annotations[].message': 'output.annotations[].message', | ||
'output.images[].alt': 'output.images[].alt', | ||
'output.images[].image_url': 'output.images[].image_url', | ||
'actions[].label': 'actions[].label', | ||
'actions[].description': 'actions[].description', | ||
'actions[].identifier': 'actions[].identifier' | ||
}) | ||
} | ||
} | ||
case 'checks-set-suites-preferences': | ||
return { | ||
prepareParams: (params) => params.push('auto_trigger_checks'), | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict.auto_trigger_checks | ||
} | ||
} | ||
case 'gists-create': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict['files.content'] | ||
} | ||
} | ||
case 'git-create-tree': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict['tree[].path'] | ||
delete paramDict['tree[].mode'] | ||
delete paramDict['tree[].type'] | ||
delete paramDict['tree[].sha'] | ||
delete paramDict['tree[].content'] | ||
} | ||
} | ||
case 'pulls-create-review': | ||
return { | ||
prepareParams: (params) => params.push('comments'), | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict.comments | ||
} | ||
} | ||
case 'repos-create-or-update-file': | ||
return { | ||
prepareParams: (params) => params.push('committer', 'author'), | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict.committer | ||
delete paramDict.author | ||
} | ||
} | ||
case 'repos-update-branch-protection': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict['required_pull_request_reviews.dismissal_restrictions'] | ||
delete paramDict['required_pull_request_reviews.dismiss_stale_reviews'] | ||
delete paramDict['required_pull_request_reviews.require_code_owner_reviews'] | ||
delete paramDict['required_pull_request_reviews.required_approving_review_count'] | ||
delete paramDict['restrictions.users'] | ||
delete paramDict['restrictions.teams'] | ||
} | ||
} | ||
case 'orgs-create-hook': | ||
case 'repos-create-hook': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict['config.content_type'] | ||
delete paramDict['config.secret'] | ||
delete paramDict['config.insecure_ssl'] | ||
} | ||
} | ||
case 'orgs-update-hook': | ||
case 'repos-update-hook': | ||
return { | ||
prepareParams: (params) => params.push('config'), | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict.config | ||
delete paramDict['config.content_type'] | ||
delete paramDict['config.secret'] | ||
delete paramDict['config.insecure_ssl'] | ||
} | ||
} | ||
case 'repos-add-protected-branch-required-status-checks-contexts': | ||
case 'repos-remove-protected-branch-required-status-checks-contexts': | ||
case 'repos-replace-protected-branch-required-status-checks-contexts': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
Object.assign(paramDict, { | ||
contexts: 'contexts' | ||
}) | ||
} | ||
} | ||
case 'repos-add-protected-branch-team-restrictions': | ||
case 'repos-remove-protected-branch-team-restrictions': | ||
case 'repos-replace-protected-branch-team-restrictions': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
Object.assign(paramDict, { | ||
teams: 'teams' | ||
}) | ||
} | ||
} | ||
case 'repos-add-protected-branch-user-restrictions': | ||
case 'repos-remove-protected-branch-user-restrictions': | ||
case 'repos-replace-protected-branch-user-restrictions': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
Object.assign(paramDict, { | ||
users: 'users' | ||
}) | ||
} | ||
} | ||
case 'markdown-render-raw': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
Object.assign(paramDict, { | ||
data: 'data' | ||
}) | ||
} | ||
} | ||
case 'enterprise-admin-create-global-hook': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
delete paramDict['config.content_type'] | ||
delete paramDict['config.secret'] | ||
delete paramDict['config.insecure_ssl'] | ||
} | ||
} | ||
case 'enterprise-admin-update-global-hook': | ||
return { | ||
prepareParamDict: (paramDict) => { | ||
Object.assign(paramDict, { | ||
'config.url': 'config.url' | ||
}) | ||
} | ||
} | ||
} | ||
return {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module.exports = findAcceptHeader | ||
|
||
function findAcceptHeader (state) { | ||
const previewBlocks = state.blocks.filter(block => block.type === 'preview') | ||
|
||
state.routes.forEach(route => { | ||
// TODO: handle optional preview blocks | ||
const requiredPreviewHeaderBlocks = previewBlocks.filter(block => block.required) | ||
const defaultAcceptHeader = requiredPreviewHeaderBlocks.length | ||
? requiredPreviewHeaderBlocks.map(block => `application/vnd.github.${block.preview}-preview+json`).join(',') | ||
: 'application/vnd.github.v3+json' | ||
const acceptHeaderParam = { | ||
name: 'accept', | ||
description: requiredPreviewHeaderBlocks.length ? 'This API is under preview and subject to change.' : 'Setting to `application/vnd.github.v3+json` is recommended', | ||
in: 'header', | ||
schema: { | ||
type: 'string', | ||
default: defaultAcceptHeader | ||
} | ||
} | ||
|
||
if (requiredPreviewHeaderBlocks.length) { | ||
acceptHeaderParam.required = true | ||
} | ||
|
||
route.operation.parameters.unshift(acceptHeaderParam) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this comment is obsolete and can be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currently, there are 73 routes on
api.github.com
that come through withpreviewBlocks
that haverequired: false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for that long comment...the non-required accept headers for those routes are being ignored right now...after a cursory look, they seem to generally be used for adding extra info in the response...which if that's true, we could just always just get everything...but some analysis may be necessary to figure out exactly how to treat these, and even ensure if they're being parsed correctly as
required: false