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
/
find-parameters.js
362 lines (304 loc) · 11.5 KB
/
find-parameters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
module.exports = findParameters
const _ = require('lodash')
const PAGINATION_VARIABLES = [
{
name: 'per_page',
in: 'query',
schema: {
type: 'integer',
default: 30
},
required: false,
description: 'Results per page (max 100)'
},
{
name: 'page',
in: 'query',
schema: {
type: 'integer',
default: 1
},
required: false,
description: 'Page number of the results to fetch.'
}
]
const PARAMETERS_TO_INPUT = require('./overrides/map-parameters-to-input')
const normalizeMarkdown = require('../normalize-markdown')
/**
* Find parameters: most endpoint have either no parameters or a single
* parameters block, but some have two and more: https://github.com/octokit/routes/issues/4
*/
function findParameters (state) {
findInBlocks(state)
findInRoutePath(state)
addPaginationParameters(state)
findInputExamples(state)
normalizeDescription(state)
addLocation(state)
mapToInput(state)
// check for duplicate names
// https://github.com/octokit/routes/issues/48
state.routes.forEach(route => {
const map = {}
route.operation.parameters.forEach(param => {
if (map[param.name]) {
throw new Error(`Duplicate parameter name: ${param.name} in ${route.operation.summary}`)
}
map[param.name] = 1
})
})
}
function findInRoutePath (state) {
state.routes.forEach(route => {
const matches = route.path.match(/(:\w+)/g)
const { parameters } = route.operation
if (!matches) {
return
}
const pathParameters = []
matches.map(match => match.substr(1)).forEach(name => {
// Some url variables are listed in parameters, too.
// related https://github.com/octokit/routes/issues/48
const existingParam = parameters.find(param => param.name === name)
if (existingParam) {
existingParam.required = true
return
}
pathParameters.push({
name,
in: 'path',
schema: {
type: getRoutePathParameterType(name)
},
required: true,
description: `${name} parameter`
})
})
parameters.unshift(...pathParameters)
})
}
function getRoutePathParameterType (name) {
if (/number$/.test(name)) {
return 'integer'
}
// https://github.com/octokit/routes/issues/266
if (/^(client_id|gist_id)$/.test(name)) {
return 'string'
}
if (/_id$/.test(name)) {
return 'integer'
}
return 'string'
}
function findInBlocks (state) {
const parametersBlocks = state.blocks.filter(block => block.type === 'parameters')
if (parametersBlocks.length === 0) {
return
}
const params = parametersBlocks[0].params.map(formatParam)
// remove parameters and heading above from blocks
state.blocks.splice(state.blocks.indexOf(parametersBlocks[0]) - 1, 2)
if (parametersBlocks.length === 1) {
state.routes.forEach(route => {
route.operation.parameters.push(...params)
})
return
}
parametersBlocks.slice(1).forEach(parametersBlock => {
const parametersBlockIndex = state.blocks.indexOf(parametersBlock)
const prevBlocks = state.blocks.slice(0, parametersBlockIndex).reverse()
const alternativeHeaderBlock = prevBlocks.find(block => block.type === 'alternativeParametersTitle')
const optionalHeaderBlock = prevBlocks.find(block => block.type === 'optionalParametersTitle')
// "Alternative input" => make two endpoints
if (alternativeHeaderBlock) {
const route2 = _.cloneDeep(state.routes[0])
state.routes[0].operation.parameters.push(...params)
route2.operation.parameters.push(...parametersBlock.params)
state.routes.push(route2)
if (prevBlocks[0].type === 'description') {
const [newSummary] = (prevBlocks[0].textOnly || prevBlocks[0].text)
.replace(/[^\w]*$/, '')
.replace(/\n/g, ' ')
.split(/\.[\s\n]/) // use first sentence as name if there are multiple
route2.operation.summary = newSummary
.replace(/^You can also /, '')
.replace(/\binstead of.*$/i, '') || `${route2.operation.summary} (alternative)`
if (/\binstead of\b/i.test(newSummary)) {
const otherParameterNames = prevBlocks[0].text.match(/`([^`]+)`/g).map(name => name.replace(/`/g, ''))
const alternativeParameterNames = parametersBlock.params.map(p => p.name)
route2.operation.parameters.push(...params.filter(param => !otherParameterNames.concat(alternativeParameterNames).includes(param.name)))
}
// remove parameters block, description & title above
state.blocks.splice(parametersBlockIndex - 1, 2)
return
}
route2.operation.summary += ' (alternative)'
// remove parameters block, description & title above
state.blocks.splice(parametersBlockIndex, 1)
return
}
// "Optional input" => add author & committer as optional parameters, turn table into description
if (optionalHeaderBlock) {
params.push({
name: 'committer',
in: 'query',
schema: { type: 'object' },
description: 'object containing information about the committer.'
})
params.push({
name: 'author',
in: 'query',
schema: { type: 'object' },
description: 'object containing information about the author.'
})
}
// "The xyz parameter takes the following keys"
// extend params with "<param>.<key>" | "<type>" | "<description>"
// In some cases multiple params need to be extended, see #271
let prevBlockIndex = parametersBlockIndex - 1
let prevBlock = state.blocks[prevBlockIndex]
let descriptionBlock
// sometimes the sub parameters their own description, e.g.
// https://developer.github.com/v3/checks/runs/#actions-object
if (!prevBlock.text.match(/`([^`]+)`/g)) {
descriptionBlock = prevBlock
prevBlock = state.blocks[--prevBlockIndex]
}
const parameterNames = prevBlock.text
.match(/`([^`]+)`/g)
.map(name => name.replace(/`/g, ''))
.reverse()
parameterNames.forEach(parameterName => {
// workaround for output.annotations.* https://github.com/octokit/routes/issues/140
if (/^\/repos\/:owner\/:repo\/check-runs/.test(state.routes[0].path) && ['annotations', 'images'].includes(parameterName)) {
parameterName = `output.${parameterName}`
}
// workaround for required_pull_request_reviews .dismissal_restrictions.* https://github.com/octokit/routes/issues/97
if (state.routes[0].path === '/repos/:owner/:repo/branches/:branch/protection' && parameterName === 'dismissal_restrictions') {
parameterName = `required_pull_request_reviews.dismissal_restrictions`
}
const parameterIndex = _.findIndex(params, (param) => param.name === parameterName)
const parameterType = parameterIndex >= 0 ? params[parameterIndex].schema.type : null
const subParameters = parametersBlock.params.map(formatParam).map(param => {
const newParam = _.clone(param)
const parentParameterName = parameterType === 'object[]'
? parameterName + '[]'
: parameterName
newParam.name = [parentParameterName, param.name].join('.')
return newParam
})
const addSubparametersAt = _.findIndex(params, (param) => param.name === subParameters[0].name.replace(/\.[^.]+$/, '').replace('[]', ''))
params.splice(addSubparametersAt + 1, 0, ...subParameters)
})
// remove parameters block and description block above
const blockLength = parametersBlockIndex - prevBlockIndex
state.blocks.splice(prevBlockIndex, blockLength)
state.routes.forEach(route => {
if (descriptionBlock) {
const parameterName = parameterNames[0]
const param = params.filter((param) => param.name === parameterName)[0]
param.description = `${param.description} ${descriptionBlock.text}`
}
route.operation.parameters = params
})
})
}
function addPaginationParameters (state) {
const hasListHeader = !!state.routes.find(route => /^List\b/.test(route.operation.summary))
const hasResponseBlock = !!state.blocks.find(block => block.type === 'response')
const hasResponseWithPaginationHeader = !!state.blocks.find(block => block.hasPaginationHeader)
if (hasResponseBlock && !hasResponseWithPaginationHeader) {
return
}
if (!hasResponseBlock && !hasListHeader) {
return
}
state.routes.forEach(route => {
route.operation.parameters.push(...PAGINATION_VARIABLES)
})
}
function findInputExamples (state) {
const exampleBlocks = state.blocks.filter(block => block.type === 'inputExample')
const firstExample = exampleBlocks[0]
state.routes.forEach((route, i) => {
const example = exampleBlocks[i] || firstExample
if (example) {
// TODO: put examples from docs in operation
// route.operation.parameters[i || XXX].example = example.data
// route.operation['x-code-samples'].XXX = example.data
}
})
}
function normalizeDescription (state) {
state.routes.forEach(route => {
for (let i = 0; i < route.operation.parameters.length; i++) {
const param = route.operation.parameters[i]
param.description = normalizeMarkdown(state, param.description)
}
return route
})
return state
}
// parameters can be sent in 4 different locations
// 1. URL (when path contains ":<parameter name>")
// 2. Query parameter (GET or HEAD)
// 3. Request body (not GET or HEAD)
// 4. Header
function addLocation (state) {
state.routes.forEach(route => {
const isQueryRequest = ['get', 'head'].includes(route.method)
const urlParameterNames = (route.path.match(/:\w+/g) || []).map(name => name.substr(1))
route.operation.parameters.forEach(param => {
if (urlParameterNames.includes(param.name)) {
param.in = 'path'
return
}
// Only few endpoints define request headers
// - https://developer.github.com/v3/repos/releases/#upload-a-release-asset (Content-Type, Content-Length)
// - https://developer.github.com/v3/markdown/#render-a-markdown-document-in-raw-mode (Content-Type)
if (['Content-Type', 'Content-Length'].includes(param.name)) {
param.in = 'header'
return
}
// Some endpoints have incorrect pagination headers, this is being worked on by GitHub.
// e.g. if https://developer.github.com/v3/projects/#create-a-repository-project no longer
// shows Link: <https://api.github.com/resource?page=2>; rel="next", in theresponse,
// this can be removed
if (['page', 'per_page'].includes(param.name)) {
param.in = 'query'
return
}
param.in = isQueryRequest ? 'query' : 'body'
})
})
}
// Add flag for parameters that is expected to be sent as request body root
// see https://github.com/octokit/routes/issues/280
function mapToInput (state) {
state.routes.forEach(route => {
const mapKey = `${route.method} ${route.path}`
const mapToInputParam = PARAMETERS_TO_INPUT[mapKey]
if (mapToInputParam) {
route.operation.parameters.push(mapToInputParam)
route.operation['x-github'].requestBodyParameterName = mapToInputParam.name
}
})
}
function formatParam (rawParam) {
const param = {
name: rawParam.name,
schema: { type: rawParam.type },
required: rawParam.required,
description: rawParam.description || `${rawParam.name} parameter`
}
if (rawParam.default) {
param.schema.default = rawParam.default
}
if (rawParam.enum) {
param.schema.enum = rawParam.enum
}
if (rawParam.allowNull) {
param.schema.nullable = rawParam.allowNull
}
return param
}