Skip to content

Commit

Permalink
Merge branch 'main' into assistant_package_cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Aug 13, 2024
2 parents 7926944 + 2f9dca4 commit 486c828
Show file tree
Hide file tree
Showing 74 changed files with 1,871 additions and 10,099 deletions.
2 changes: 1 addition & 1 deletion .buildkite/scripts/steps/capture_oas_snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -euo pipefail
source .buildkite/scripts/common/util.sh

echo --- Capture OAS snapshot
cmd="node scripts/capture_oas_snapshot --include-path /api/status --include-path /api/alerting/rule/ --include-path /api/alerting/rules"
cmd="node scripts/capture_oas_snapshot --include-path /api/status"
if is_pr && ! is_auto_commit_disabled; then
cmd="$cmd --update"
fi
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/alert-failed-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout kibana-operations
uses: actions/checkout@v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: 'elastic/kibana-operations'
ref: main
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
ref: ${{ matrix.branch }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fix-version-gaps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Actions
uses: actions/checkout@v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: 'elastic/kibana-github-actions'
ref: main
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/label-failed-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout kibana-operations
uses: actions/checkout@v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: 'elastic/kibana-operations'
ref: main
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/launchdarkly-code-references.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
if: github.event.repository.fork == false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
fetch-depth: 11 # This value must be set if the lookback configuration option is not disabled for find-code-references. Read more: https://github.com/launchdarkly/ld-find-code-refs#searching-for-unused-flags-extinctions
- name: LaunchDarkly Code References
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
)
steps:
- name: Checkout Actions
uses: actions/checkout@v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: 'elastic/kibana-github-actions'
ref: main
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/skip-failed-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Actions
uses: actions/checkout@v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: 'elastic/kibana-github-actions'
ref: main
Expand All @@ -30,7 +30,7 @@ jobs:
token: ${{secrets.KIBANAMACHINE_TOKEN}}

- name: Checkout kibana-operations
uses: actions/checkout@v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: 'elastic/kibana-operations'
ref: main
Expand Down
4,709 changes: 1 addition & 4,708 deletions oas_docs/bundle.json

Large diffs are not rendered by default.

4,709 changes: 1 addition & 4,708 deletions oas_docs/bundle.serverless.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('createBrowserConfig', () => {
exposedConfigKeys: {
exposed1: 'string',
'nested.exposed2': 'boolean',
'nested.exposed3': 'number',
'nested.exposed3': 'number?',
// notExposed4 is not present
},
});
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('createBrowserConfig', () => {
exposedConfigKeys: {
exposed1: 'string',
'deeply.exposed2': 'boolean',
'deeply.nested.exposed3': 'number',
'deeply.nested.exposed3': 'number?',
'deeply.nested.structure.exposed4': 'string',
// notExposed5, notExposed6, notExposed7, and notExposed8 are not present
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1046,15 +1046,15 @@ describe('PluginsService', () => {
const prebootUIConfig$ = preboot.uiPlugins.browserConfigs.get('plugin-with-expose-preboot')!;
await expect(prebootUIConfig$.pipe(take(1)).toPromise()).resolves.toEqual({
browserConfig: { sharedProp: 'sharedProp default value plugin-with-expose-preboot' },
exposedConfigKeys: { sharedProp: 'string' },
exposedConfigKeys: { sharedProp: 'string?' },
});

const standardUIConfig$ = standard.uiPlugins.browserConfigs.get(
'plugin-with-expose-standard'
)!;
await expect(standardUIConfig$.pipe(take(1)).toPromise()).resolves.toEqual({
browserConfig: { sharedProp: 'sharedProp default value plugin-with-expose-standard' },
exposedConfigKeys: { sharedProp: 'string' },
exposedConfigKeys: { sharedProp: 'string?' },
});
});

Expand Down
37 changes: 23 additions & 14 deletions packages/kbn-config-schema/src/types/object_type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { get } from 'lodash';
import { expectType } from 'tsd';
import { schema } from '../..';
import { offeringBasedSchema, schema } from '../..';
import { TypeOf } from './object_type';

test('returns value by default', () => {
Expand Down Expand Up @@ -682,6 +682,10 @@ test('returns schema structure', () => {
boolean: schema.boolean(),
buffer: schema.buffer(),
byteSize: schema.byteSize(),
svlConditional: offeringBasedSchema({
serverless: schema.literal('serverless'),
traditional: schema.literal('stateful'),
}),
conditional: schema.conditional(
schema.contextRef('context_value_1'),
schema.contextRef('context_value_2'),
Expand All @@ -699,8 +703,9 @@ test('returns schema structure', () => {
record: schema.recordOf(schema.string(), schema.string()),
stream: schema.stream(),
string: schema.string(),
union: schema.oneOf([schema.string()]),
union: schema.oneOf([schema.string(), schema.number(), schema.boolean()]),
uri: schema.uri(),
null: schema.literal(null),
});
const type = objSchema.extends({
nested: objSchema,
Expand All @@ -711,39 +716,43 @@ test('returns schema structure', () => {
{ path: ['boolean'], type: 'boolean' },
{ path: ['buffer'], type: 'binary' },
{ path: ['byteSize'], type: 'bytes' },
{ path: ['conditional'], type: 'any' },
{ path: ['svlConditional'], type: 'serverless|stateful' },
{ path: ['conditional'], type: 'string' },
{ path: ['duration'], type: 'duration' },
{ path: ['ip'], type: 'string' },
{ path: ['literal'], type: 'any' },
{ path: ['literal'], type: 'foo' },
{ path: ['map'], type: 'map' },
{ path: ['maybe'], type: 'string' },
{ path: ['never'], type: 'any' },
{ path: ['nullable'], type: 'alternatives' },
{ path: ['maybe'], type: 'string?' },
{ path: ['never'], type: 'never' },
{ path: ['nullable'], type: 'string?|null' },
{ path: ['number'], type: 'number' },
{ path: ['record'], type: 'record' },
{ path: ['stream'], type: 'stream' },
{ path: ['string'], type: 'string' },
{ path: ['union'], type: 'alternatives' },
{ path: ['union'], type: 'string|number|boolean' },
{ path: ['uri'], type: 'string' },
{ path: ['null'], type: 'null' },
{ path: ['nested', 'any'], type: 'any' },
{ path: ['nested', 'array'], type: 'array' },
{ path: ['nested', 'boolean'], type: 'boolean' },
{ path: ['nested', 'buffer'], type: 'binary' },
{ path: ['nested', 'byteSize'], type: 'bytes' },
{ path: ['nested', 'conditional'], type: 'any' },
{ path: ['nested', 'svlConditional'], type: 'serverless|stateful' },
{ path: ['nested', 'conditional'], type: 'string' },
{ path: ['nested', 'duration'], type: 'duration' },
{ path: ['nested', 'ip'], type: 'string' },
{ path: ['nested', 'literal'], type: 'any' },
{ path: ['nested', 'literal'], type: 'foo' },
{ path: ['nested', 'map'], type: 'map' },
{ path: ['nested', 'maybe'], type: 'string' },
{ path: ['nested', 'never'], type: 'any' },
{ path: ['nested', 'nullable'], type: 'alternatives' },
{ path: ['nested', 'maybe'], type: 'string?' },
{ path: ['nested', 'never'], type: 'never' },
{ path: ['nested', 'nullable'], type: 'string?|null' },
{ path: ['nested', 'number'], type: 'number' },
{ path: ['nested', 'record'], type: 'record' },
{ path: ['nested', 'stream'], type: 'stream' },
{ path: ['nested', 'string'], type: 'string' },
{ path: ['nested', 'union'], type: 'alternatives' },
{ path: ['nested', 'union'], type: 'string|number|boolean' },
{ path: ['nested', 'uri'], type: 'string' },
{ path: ['nested', 'null'], type: 'null' },
]);
});

Expand Down
79 changes: 74 additions & 5 deletions packages/kbn-config-schema/src/types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
* Side Public License, v 1.
*/

import type { AnySchema, CustomValidator, ErrorReport } from 'joi';
import {
isSchema,
type CustomValidator,
type ErrorReport,
type Schema,
type SchemaLike,
type WhenOptions,
CustomHelpers,
} from 'joi';
import { META_FIELD_X_OAS_DEPRECATED } from '../oas_meta_fields';
import { SchemaTypeError, ValidationError } from '../errors';
import { Reference } from '../references';
Expand Down Expand Up @@ -91,9 +99,9 @@ export abstract class Type<V> {
* Internal "schema" backed by Joi.
* @type {Schema}
*/
protected readonly internalSchema: AnySchema;
protected readonly internalSchema: Schema;

protected constructor(schema: AnySchema, options: TypeOptions<V> = {}) {
protected constructor(schema: Schema, options: TypeOptions<V> = {}) {
if (options.defaultValue !== undefined) {
schema = schema.optional();

Expand Down Expand Up @@ -214,16 +222,77 @@ export abstract class Type<V> {
}
}

function recursiveGetSchemaStructure(internalSchema: AnySchema, path: string[] = []) {
function recursiveGetSchemaStructure(internalSchema: Schema, path: string[] = []) {
const array: SchemaStructureEntry[] = [];
// Note: we are relying on Joi internals to obtain the schema structure (recursive keys).
// This is not ideal, but it works for now and we only need it for some integration test assertions.
// If it breaks in the future, we'll need to update our tests.
for (const [key, val] of (internalSchema as any)._ids._byKey.entries()) {
array.push(...recursiveGetSchemaStructure(val.schema, [...path, key]));
}

if (!array.length) {
array.push({ path, type: internalSchema.type ?? 'unknown' });
let type: string;
try {
type = prettyPrintType(internalSchema, path);
} catch (error) {
// failed to find special type, might need to update for new joi versions or type usages
type = internalSchema.type || 'unknown';
}

array.push({
path,
type,
});
}
return array;
}

/**
* Returns a more accurate type from complex schema definitions.
*
* For example, conditional values resolve to type `any` when the nested value is only ever a `string`.
*
* @param internalSchema
* @param path of current schema
* @returns schema type
*/
function prettyPrintType(schema?: SchemaLike, path: string[] = []): string {
// takes array of possible values and de-dups and joins
return [...new Set([prettyPrintTypeParts(schema, false, path)].flat())].filter(Boolean).join('|');
}

/**
* Recursively collects all possible nested schema types.
*/
function prettyPrintTypeParts(
schema?: SchemaLike,
optional = false,
path: string[] = []
): string | string[] {
if (!isSchema(schema)) {
if (schema === null) return 'null';
return `${schema ?? 'unknown'}${optional ? '?' : ''}`;
}

const isOptionalType = optional || schema._flags?.presence === 'optional';
// For explicit custom schema.never
if (schema._flags?.presence === 'forbidden') return 'never';
// For offeringBasedSchema, schema.when, schema.conditional
if (schema.$_terms?.whens?.length > 0)
return (schema.$_terms.whens as WhenOptions[]).flatMap((when) =>
[when?.then, when?.otherwise].flatMap((s) => prettyPrintTypeParts(s, isOptionalType, path))
);
// schema.oneOf, schema.allOf, etc.
if (schema.$_terms?.matches?.length > 0)
return (schema.$_terms.matches as CustomHelpers[]).flatMap((s) =>
prettyPrintTypeParts(s.schema, isOptionalType, path)
);
// schema.literal
if (schema._flags?.only && (schema as any)._valids?._values?.size > 0)
return [...(schema as any)._valids._values.keys()].flatMap((v) =>
prettyPrintTypeParts(v, isOptionalType, path)
);

return `${schema?.type || 'unknown'}${isOptionalType ? '?' : ''}`;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 486c828

Please sign in to comment.