-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[ES|QL] Displays The Command License Availability In Our Inline Docs #230358
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
Merged
bartoval
merged 7 commits into
elastic:main
from
bartoval:esql_add_idoc_command_license_badge
Aug 4, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f260037
feat: add function license info to idoc
bartoval 32eadb3
feat: add idoc command license badge
bartoval 102fec0
refactor: create command doc from template
bartoval 7603b4b
fix: add escape for variables
bartoval 17c24d6
Merge branch 'main' into esql_add_idoc_command_license_badge
stratoula 41e4d8f
Merge branch 'elastic:main' into esql_add_idoc_command_license_badge
bartoval a17b583
refactor: resolve PR comments
bartoval 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 hidden or 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
231 changes: 231 additions & 0 deletions
231
...latform/packages/private/kbn-language-documentation/scripts/generate_esql_command_docs.ts
This file contains hidden or 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,231 @@ | ||
| /* | ||
| * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| /* eslint-disable no-console */ | ||
|
|
||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { | ||
| processingCommandsIntro, | ||
| processingCommandsItems, | ||
| } from './resources/commands/processing_data'; | ||
| import { sourceCommandsIntro, sourceCommandsItems } from './resources/commands/source_data'; | ||
| import { CommandDefinition, MultipleLicenseInfo } from '../src/types'; | ||
| import { getLicenseInfoForCommand } from '../src/utils/get_license_info'; | ||
| import { | ||
| DEFINITION_DIR_SUFFIX, | ||
| ELASTISEARCH_ESQL_DOCS_BASE_PATH, | ||
| OUTPUT_DIR, | ||
| } from './scripts.constants'; | ||
| import { loadElasticDefinitions } from '../src/utils/load_elastic_definitions'; | ||
|
|
||
| const ELASTIC_COMMAND_DIR_PATH = path.join( | ||
| ELASTISEARCH_ESQL_DOCS_BASE_PATH, | ||
| DEFINITION_DIR_SUFFIX, | ||
| 'commands' | ||
| ); | ||
|
|
||
| interface CommandItemMetadata { | ||
| name: string; | ||
| labelDefaultMessage: string; | ||
| descriptionDefaultMessage: string; | ||
| descriptionOptions?: { | ||
| ignoreTag?: boolean; | ||
| description?: string; | ||
| }; | ||
| openLinksInNewTab?: boolean; | ||
| preview?: boolean; | ||
| license?: MultipleLicenseInfo; | ||
| } | ||
|
|
||
| interface CommandSectionMetadata { | ||
| labelKey: string; | ||
| labelDefaultMessage: string; | ||
| descriptionKey: string; | ||
| descriptionDefaultMessage: string; | ||
| items: CommandItemMetadata[]; | ||
| outputFile: string; | ||
| } | ||
|
|
||
| const commandsData: CommandSectionMetadata[] = [ | ||
| { | ||
| ...sourceCommandsIntro, | ||
| items: sourceCommandsItems, | ||
| outputFile: `${OUTPUT_DIR}/source_commands.tsx`, | ||
| }, | ||
| { | ||
| ...processingCommandsIntro, | ||
| items: processingCommandsItems, | ||
| outputFile: `${OUTPUT_DIR}/processing_commands.tsx`, | ||
| }, | ||
| ]; | ||
|
|
||
| /** | ||
| * This script generates the ESQL inline command documentation files by merging | ||
| * the source and processing commands with Elasticsearch definitions. | ||
| * | ||
| * Step 1: Load the Elasticsearch command definitions from the specified path. | ||
| * Step 2: Merge the loaded definitions with the source and processing commands. | ||
| * Step 3: Generate separate documentation files for each command type, including the extra information, such as license details. | ||
| * Step 4: Write the generated content to the output files in the specified directory. | ||
| */ | ||
| (function () { | ||
| try { | ||
| console.log(`Start generating commands documentation`); | ||
|
|
||
| const pathToElasticsearch = process.argv[2]; | ||
| if (!pathToElasticsearch) { | ||
| throw new Error( | ||
| 'No Elasticsearch path provided, generating without license info for testing...' | ||
| ); | ||
| } | ||
|
|
||
| const esCommandDirPath = path.join(pathToElasticsearch, ELASTIC_COMMAND_DIR_PATH); | ||
| const cmdDefinitions = loadElasticDefinitions<CommandDefinition>(esCommandDirPath); | ||
| const commands = commandsData.map((cmd) => addDefinitionsToCommands(cmd, cmdDefinitions)); | ||
| const docContents = commands.map((cmd) => generateDoc(cmd)); | ||
|
|
||
| // Ensure the output directory exists | ||
| if (!fs.existsSync(OUTPUT_DIR)) { | ||
| fs.mkdirSync(OUTPUT_DIR, { recursive: true }); | ||
| } | ||
|
|
||
| // Write each generated documentation file | ||
| docContents.forEach((content, i) => fs.writeFileSync(`${commands[i].outputFile}`, content)); | ||
| console.log(`Sucessfully generated commands documentation files`); | ||
| } catch (error) { | ||
| console.error(`Error writing documentation files: ${error.message}`); | ||
| process.exit(1); | ||
| } | ||
| })(); | ||
|
|
||
| /** | ||
| * Adds Elasticsearch information, such as license details, to commands | ||
| */ | ||
| function addDefinitionsToCommands( | ||
| data: CommandSectionMetadata, | ||
| definitions: Map<string, CommandDefinition> | ||
| ): CommandSectionMetadata { | ||
| return { | ||
| ...data, | ||
| items: data.items.map((item) => { | ||
| const commandDef = definitions.get(item.name); | ||
| const licenseInfo = getLicenseInfoForCommand(commandDef); | ||
|
|
||
| if (licenseInfo) { | ||
| return { | ||
| ...item, | ||
| license: licenseInfo, | ||
| }; | ||
| } | ||
| return item; | ||
| }), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Generates the full content for a single command documentation file. | ||
| */ | ||
| function generateDoc(data: CommandSectionMetadata): string { | ||
| return ` | ||
| import React from 'react'; | ||
| import { i18n } from '@kbn/i18n'; | ||
| import { Markdown as SharedUXMarkdown } from '@kbn/shared-ux-markdown'; | ||
|
|
||
| const Markdown = (props: Parameters<typeof SharedUXMarkdown>[0]) => ( | ||
| <SharedUXMarkdown {...props} readOnly enableSoftLineBreaks /> | ||
| ); | ||
|
|
||
| // Do not edit manually... automatically generated by scripts/generate_esql_command_docs.ts | ||
|
|
||
| export const commands = ${generateCommandSectionDoc(data)}; | ||
| `; | ||
| } | ||
|
|
||
| /** | ||
| * Generates a documentation for a specific group of commands. | ||
| */ | ||
| function generateCommandSectionDoc({ | ||
| items, | ||
| labelKey, | ||
| labelDefaultMessage, | ||
| descriptionKey, | ||
| descriptionDefaultMessage, | ||
| }: CommandSectionMetadata): string { | ||
| const commandsContentDoc = items.map((item) => generateCommandItemDoc(item)).join(',\n '); | ||
|
|
||
| return `{ | ||
| label: i18n.translate('${labelKey}', { | ||
| defaultMessage: '${labelDefaultMessage}', | ||
| }), | ||
| description: i18n.translate('${descriptionKey}', { | ||
| defaultMessage: \`${descriptionDefaultMessage}\`, | ||
| }), | ||
| items: [ | ||
| ${commandsContentDoc} | ||
| ], | ||
| }`; | ||
| } | ||
|
|
||
| /** | ||
| * Generates a single command documentation. | ||
| */ | ||
| function generateCommandItemDoc({ | ||
| name, | ||
| labelDefaultMessage, | ||
| descriptionDefaultMessage, | ||
| openLinksInNewTab, | ||
| descriptionOptions, | ||
| preview, | ||
| license, | ||
| }: CommandItemMetadata): string { | ||
| function generateMarkdownProps(props: { openLinksInNewTab?: boolean }): string { | ||
| return props.openLinksInNewTab ? ' openLinksInNewTab={true}' : ''; | ||
| } | ||
|
|
||
| function generateDescriptionOptions( | ||
| options: { | ||
| ignoreTag?: boolean; | ||
| description?: string; | ||
| } = {} | ||
| ) { | ||
| const formattedDescriptionOptions = Object.entries(options || {}).map(([key, value]) => | ||
| typeof value === 'boolean' | ||
| ? `${key}: ${value},` | ||
| : `${key}: '${String(value).replace(/'/g, "\\'")}',` | ||
| ); | ||
|
|
||
| return formattedDescriptionOptions.length > 0 | ||
| ? `,\n ${formattedDescriptionOptions.join('\n')}` | ||
| : ''; | ||
| } | ||
|
|
||
| // Sample (as a command) use a special suffix to avoid conflict with the same function name | ||
| const labelKey = `languageDocumentation.documentationESQL.${ | ||
| name === 'sample' ? name + 'Command' : name | ||
| }`; | ||
| const descriptionKey = `${labelKey}.markdown`; | ||
| const previewProp = preview !== undefined ? `\n preview: ${preview},` : ''; | ||
| const licenseProp = license ? `\n license: ${JSON.stringify(license)},` : ''; | ||
| // replace(/`/g, '\\`') escape backticks for nested template literals in the generated file | ||
| const description = descriptionDefaultMessage.replace(/`/g, '\\`'); | ||
|
|
||
| return `{ | ||
| label: i18n.translate('${labelKey}', { | ||
| defaultMessage: '${labelDefaultMessage}', | ||
| }),${previewProp} | ||
| description: ( | ||
| <Markdown${generateMarkdownProps({ openLinksInNewTab })} | ||
| markdownContent={i18n.translate('${descriptionKey}', { | ||
| defaultMessage: \`${description}\`${generateDescriptionOptions(descriptionOptions)} | ||
| })} | ||
| /> | ||
| ),${licenseProp} | ||
| }`; | ||
| } |
This file contains hidden or 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
14 changes: 14 additions & 0 deletions
14
src/platform/packages/private/kbn-language-documentation/scripts/make_docs.sh
This file contains hidden or 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 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Wrapper script to handle path parameter for make:defs | ||
| if [ -z "$1" ]; then | ||
| echo "Error: Path to Elasticsearch is required" | ||
| echo "Usage: $0 /path/to/elasticsearch" | ||
| exit 1 | ||
| fi | ||
|
|
||
| ELASTICSEARCH_PATH="$1" | ||
|
|
||
| # Run both scripts with the provided path | ||
| ts-node --transpileOnly ./scripts/generate_esql_command_docs.ts "$ELASTICSEARCH_PATH" && \ | ||
| ts-node --transpileOnly ./scripts/generate_esql_docs.ts "$ELASTICSEARCH_PATH" |
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.
Uh oh!
There was an error while loading. Please reload this page.
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 noticed that this existing script updates an existing functions file. However, if a file in the generated folder is missing or becomes corrupted, the script crashes. We could improve this behavior with a small change —either in this PR, in a separate one, or leave it as is for now, if it is not a relevant case.
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.
Yeah I had also noticed it, it is a case that is not happening in reality (unless you deliberately add a new file) so I don't think it is very important to fix