-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Console] Use ES specification for autocomplete definitions #159241
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2cc1e5f
[Console] Create a new package to generate autocomplete definitions
yuliacech bd76003
[Console] Add Kibana a script command and implementation basics for d…
yuliacech 9b58966
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine fdcddb6
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine e6754ee
[Console] Remove the new folder
yuliacech 52051aa
[Console] Add options for source and dest folders
yuliacech e0b3c18
[Console] Export a constant for autocomplete definitions folder and u…
yuliacech 1b63e1e
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 751ac76
[Console] Add functionality to create the definitions folder if doesn…
yuliacech 2340672
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine a237de0
Merge branch 'main' into console/new_definitions
yuliacech 7804ccf
[Console] Add description and usage to the command
yuliacech 5b66edd
Update packages/kbn-generate-console-definitions/src/cli.ts
yuliacech ad59531
Update packages/kbn-generate-console-definitions/src/cli.ts
yuliacech 70045a0
Update packages/kbn-generate-console-definitions/src/cli.ts
yuliacech ddc250f
Merge branch 'main' into console/new_definitions
yuliacech a90111b
[Console] Add back a new line at the end of the definitions files
yuliacech aa781d5
Merge branch 'main' into console/new_definitions
yuliacech 927cb73
[Console] Fix the new line
yuliacech 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
Validating CODEOWNERS rules …
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
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,3 @@ | ||
# @kbn/generate-console-definitions | ||
|
||
Empty package generated by @kbn/generate |
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,9 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export * from './src/cli'; |
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,13 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test/jest_node', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-generate-console-definitions'], | ||
}; |
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,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/generate-console-definitions", | ||
"owner": "@elastic/platform-deployment-management" | ||
} |
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,6 @@ | ||
{ | ||
"name": "@kbn/generate-console-definitions", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0" | ||
} |
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,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import Path from 'path'; | ||
import fs from 'fs'; | ||
import { run } from '@kbn/dev-cli-runner'; | ||
import { createFlagError } from '@kbn/dev-cli-errors'; | ||
import { REPO_ROOT } from '@kbn/repo-info'; | ||
import { AUTOCOMPLETE_DEFINITIONS_FOLDER } from '@kbn/console-plugin/common/constants'; | ||
import { generateConsoleDefinitions } from './generate_console_definitions'; | ||
|
||
export function runGenerateConsoleDefinitionsCli() { | ||
run( | ||
(context) => { | ||
const { log, flags } = context; | ||
log.info('starting console definitions generation'); | ||
const { source, dest, emptyDest } = flags; | ||
if (!source) { | ||
throw createFlagError(`Missing --source argument`); | ||
} | ||
let definitionsFolder = Path.resolve(REPO_ROOT, `${dest}`); | ||
if (!dest) { | ||
definitionsFolder = Path.resolve(AUTOCOMPLETE_DEFINITIONS_FOLDER, 'generated'); | ||
} | ||
log.info(`autocomplete definitions folder ${definitionsFolder}`); | ||
if (!fs.existsSync(definitionsFolder)) { | ||
log.warning(`folder ${definitionsFolder} doesn't exist, creating a new folder`); | ||
fs.mkdirSync(definitionsFolder, { recursive: true }); | ||
log.warning(`created a new folder ${definitionsFolder}`); | ||
} | ||
const files = fs.readdirSync(definitionsFolder); | ||
if (files.length > 0) { | ||
if (!emptyDest) { | ||
throw createFlagError( | ||
`Definitions folder already contain files, use --emptyDest to clean the folder before generation` | ||
); | ||
} | ||
log.warning(`folder ${definitionsFolder} already contains files, emptying the folder`); | ||
for (const file of files) { | ||
fs.unlinkSync(Path.resolve(definitionsFolder, file)); | ||
} | ||
log.warning(`folder ${definitionsFolder} has been emptied`); | ||
} | ||
|
||
const specsRepo = Path.resolve(`${source}`); | ||
if (!fs.existsSync(specsRepo)) { | ||
throw createFlagError(`ES specification folder ${specsRepo} doesn't exist`); | ||
} | ||
log.info(`ES specification repo folder ${source}`); | ||
generateConsoleDefinitions({ specsRepo, definitionsFolder, log }); | ||
log.info('completed console definitions generation'); | ||
}, | ||
{ | ||
description: `Generate Console autocomplete definitions from the ES specification repo`, | ||
usage: ` | ||
node scripts/generate_console_definitions.js --help | ||
node scripts/generate_console_definitions.js --source <ES_SPECIFICATION_REPO> | ||
node scripts/generate_console_definitions.js --source <ES_SPECIFICATION_REPO> [--dest <DEFINITIONS_FOLDER] [--emptyDest] | ||
`, | ||
flags: { | ||
string: ['source', 'dest'], | ||
boolean: ['emptyDest'], | ||
help: ` | ||
--source Folder containing the root of the Elasticsearch specification repo | ||
--dest Folder where console autocomplete definitions will be generated (relative to the Kibana repo root) | ||
--emptyDest Flag to empty definitions folder if it already contains any files | ||
`, | ||
}, | ||
} | ||
); | ||
} |
163 changes: 163 additions & 0 deletions
163
packages/kbn-generate-console-definitions/src/generate_console_definitions.ts
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,163 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import Path, { join } from 'path'; | ||
import { ToolingLog } from '@kbn/tooling-log'; | ||
|
||
interface EndpointRequest { | ||
name: string; | ||
namespace: string; | ||
} | ||
|
||
interface Endpoint { | ||
name: string; | ||
urls: Array<{ | ||
methods: string[]; | ||
path: string; | ||
}>; | ||
docUrl: string; | ||
request: null | EndpointRequest; | ||
} | ||
|
||
interface SchemaType { | ||
name: { | ||
name: string; | ||
namespace: string; | ||
}; | ||
} | ||
|
||
interface Schema { | ||
endpoints: Endpoint[]; | ||
types: SchemaType[]; | ||
} | ||
|
||
interface UrlParams { | ||
[key: string]: number | string; | ||
} | ||
|
||
interface BodyParams { | ||
[key: string]: number | string; | ||
} | ||
|
||
interface Definition { | ||
documentation?: string; | ||
methods: string[]; | ||
patterns: string[]; | ||
url_params?: UrlParams; | ||
data_autocomplete_rules?: BodyParams; | ||
} | ||
|
||
const generateMethods = (endpoint: Endpoint): string[] => { | ||
// this array consists of arrays of strings | ||
const methodsArray = endpoint.urls.map((url) => url.methods); | ||
// flatten to return array of strings | ||
const flattenMethodsArray = ([] as string[]).concat(...methodsArray); | ||
// use set to remove duplication | ||
return [...new Set(flattenMethodsArray)]; | ||
}; | ||
|
||
const generatePatterns = (endpoint: Endpoint): string[] => { | ||
return endpoint.urls.map(({ path }) => { | ||
let pattern = path; | ||
// remove leading / if present | ||
if (path.startsWith('/')) { | ||
pattern = path.substring(1); | ||
} | ||
return pattern; | ||
}); | ||
}; | ||
|
||
const generateDocumentation = (endpoint: Endpoint): string => { | ||
return endpoint.docUrl; | ||
}; | ||
|
||
const generateParams = ( | ||
endpoint: Endpoint, | ||
schema: Schema | ||
): { urlParams: UrlParams; bodyParams: BodyParams } | undefined => { | ||
const { request } = endpoint; | ||
if (!request) { | ||
return; | ||
} | ||
const requestType = schema.types.find( | ||
({ name: { name, namespace } }) => name === request.name && namespace === request.namespace | ||
); | ||
if (!requestType) { | ||
return; | ||
} | ||
|
||
const urlParams = generateUrlParams(requestType); | ||
const bodyParams = generateBodyParams(requestType); | ||
return { urlParams, bodyParams }; | ||
}; | ||
|
||
const generateUrlParams = (requestType: SchemaType): UrlParams => { | ||
return {}; | ||
}; | ||
|
||
const generateBodyParams = (requestType: SchemaType): BodyParams => { | ||
return {}; | ||
}; | ||
|
||
const addParams = ( | ||
definition: Definition, | ||
params: { urlParams: UrlParams; bodyParams: BodyParams } | ||
): Definition => { | ||
const { urlParams, bodyParams } = params; | ||
if (urlParams && Object.keys(urlParams).length > 0) { | ||
definition.url_params = urlParams; | ||
} | ||
if (bodyParams && Object.keys(bodyParams).length > 0) { | ||
definition.data_autocomplete_rules = bodyParams; | ||
} | ||
return definition; | ||
}; | ||
|
||
const generateDefinition = (endpoint: Endpoint, schema: Schema): Definition => { | ||
const methods = generateMethods(endpoint); | ||
const patterns = generatePatterns(endpoint); | ||
const documentation = generateDocumentation(endpoint); | ||
let definition: Definition = { methods, patterns, documentation }; | ||
const params = generateParams(endpoint, schema); | ||
if (params) { | ||
definition = addParams(definition, params); | ||
} | ||
|
||
return definition; | ||
}; | ||
|
||
export function generateConsoleDefinitions({ | ||
specsRepo, | ||
definitionsFolder, | ||
log, | ||
}: { | ||
specsRepo: string; | ||
definitionsFolder: string; | ||
log: ToolingLog; | ||
}) { | ||
const pathToSchemaFile = Path.resolve(specsRepo, 'output/schema/schema.json'); | ||
log.info('loading the ES specification schema file'); | ||
const schema = JSON.parse(fs.readFileSync(pathToSchemaFile, 'utf8')) as Schema; | ||
|
||
const { endpoints } = schema; | ||
log.info(`iterating over endpoints array: ${endpoints.length} endpoints`); | ||
endpoints.forEach((endpoint) => { | ||
const { name } = endpoint; | ||
log.info(name); | ||
const definition = generateDefinition(endpoint, schema); | ||
const fileContent: { [name: string]: Definition } = { | ||
[name]: definition, | ||
}; | ||
fs.writeFileSync( | ||
join(definitionsFolder, `${name}.json`), | ||
JSON.stringify(fileContent + '\n', null, 2), | ||
'utf8' | ||
); | ||
}); | ||
} |
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,23 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [ | ||
"@kbn/dev-cli-runner", | ||
"@kbn/dev-cli-errors", | ||
"@kbn/repo-info", | ||
"@kbn/console-plugin", | ||
"@kbn/tooling-log", | ||
] | ||
} |
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,10 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
require('../src/setup_node_env'); | ||
require('@kbn/generate-console-definitions').runGenerateConsoleDefinitionsCli(); |
14 changes: 14 additions & 0 deletions
14
src/plugins/console/common/constants/autocomplete_definitions.ts
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,14 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { resolve } from 'path'; | ||
|
||
export const AUTOCOMPLETE_DEFINITIONS_FOLDER = resolve( | ||
__dirname, | ||
'../../server/lib/spec_definitions/json' | ||
); |
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
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
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.
This file is mostly WIP and probably will change a lot in follow up PRs. But still feel free to review as usual