Skip to content

Commit

Permalink
expose docLinks from ConfigDeprecationContext (#132424)
Browse files Browse the repository at this point in the history
* expose docLinks from ConfigDeprecationContext

* fix mock

* [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix'

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
pgayvallet and kibanamachine authored May 20, 2022
1 parent f75b6fa commit 569e10a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/kbn-config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ RUNTIME_DEPS = [
"//packages/kbn-utility-types",
"//packages/kbn-i18n",
"//packages/kbn-plugin-discovery",
"//packages/kbn-doc-links",
"@npm//js-yaml",
"@npm//load-json-file",
"@npm//lodash",
Expand All @@ -54,6 +55,7 @@ TYPES_DEPS = [
"//packages/kbn-utility-types:npm_module_types",
"//packages/kbn-i18n:npm_module_types",
"//packages/kbn-plugin-discovery:npm_module_types",
"//packages/kbn-doc-links:npm_module_types",
"@npm//load-json-file",
"@npm//rxjs",
"@npm//@types/jest",
Expand Down
11 changes: 11 additions & 0 deletions packages/kbn-config/src/config_service.test.mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Side Public License, v 1.
*/

import type { DocLinks } from '@kbn/doc-links';

export const mockPackage = new Proxy({ raw: {} as any }, { get: (obj, prop) => obj.raw[prop] });
import type { applyDeprecations } from './deprecation/apply_deprecations';

Expand All @@ -26,3 +28,12 @@ export const mockApplyDeprecations = jest.fn<
jest.mock('./deprecation/apply_deprecations', () => ({
applyDeprecations: mockApplyDeprecations,
}));

export const docLinksMock = {
settings: 'settings',
} as DocLinks;
export const getDocLinksMock = jest.fn().mockReturnValue(docLinksMock);

jest.doMock('@kbn/doc-links', () => ({
getDocLinks: getDocLinksMock,
}));
11 changes: 10 additions & 1 deletion packages/kbn-config/src/config_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import { BehaviorSubject, Observable } from 'rxjs';
import { first, take } from 'rxjs/operators';

import { mockApplyDeprecations, mockedChangedPaths } from './config_service.test.mocks';
import {
mockApplyDeprecations,
mockedChangedPaths,
docLinksMock,
getDocLinksMock,
} from './config_service.test.mocks';
import { rawConfigServiceMock } from './raw/raw_config_service.mock';

import { schema } from '@kbn/config-schema';
Expand Down Expand Up @@ -39,6 +44,7 @@ const getRawConfigProvider = (rawConfig: Record<string, any>) =>
beforeEach(() => {
logger = loggerMock.create();
mockApplyDeprecations.mockClear();
getDocLinksMock.mockClear();
});

test('returns config at path as observable', async () => {
Expand Down Expand Up @@ -469,6 +475,7 @@ test('calls `applyDeprecations` with the correct parameters', async () => {
const context: ConfigDeprecationContext = {
branch: defaultEnv.packageInfo.branch,
version: defaultEnv.packageInfo.version,
docLinks: docLinksMock,
};

const deprecationA = jest.fn();
Expand All @@ -479,6 +486,8 @@ test('calls `applyDeprecations` with the correct parameters', async () => {

await configService.validate();

expect(getDocLinksMock).toHaveBeenCalledTimes(1);

expect(mockApplyDeprecations).toHaveBeenCalledTimes(1);
expect(mockApplyDeprecations).toHaveBeenCalledWith(
cfg,
Expand Down
20 changes: 12 additions & 8 deletions packages/kbn-config/src/config_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { isEqual } from 'lodash';
import { BehaviorSubject, combineLatest, firstValueFrom, Observable } from 'rxjs';
import { distinctUntilChanged, first, map, shareReplay, tap } from 'rxjs/operators';
import { Logger, LoggerFactory } from '@kbn/logging';
import { getDocLinks, DocLinks } from '@kbn/doc-links';

import { Config, ConfigPath, Env } from '.';
import { hasConfigPathIntersection } from './config';
Expand Down Expand Up @@ -42,6 +43,7 @@ export interface ConfigValidateParameters {
export class ConfigService {
private readonly log: Logger;
private readonly deprecationLog: Logger;
private readonly docLinks: DocLinks;

private validated = false;
private readonly config$: Observable<Config>;
Expand All @@ -67,6 +69,7 @@ export class ConfigService {
) {
this.log = logger.get('config');
this.deprecationLog = logger.get('config', 'deprecation');
this.docLinks = getDocLinks({ kibanaBranch: env.packageInfo.branch });

this.config$ = combineLatest([this.rawConfigProvider.getConfig$(), this.deprecations]).pipe(
map(([rawConfig, deprecations]) => {
Expand Down Expand Up @@ -104,7 +107,7 @@ export class ConfigService {
...provider(configDeprecationFactory).map((deprecation) => ({
deprecation,
path: flatPath,
context: createDeprecationContext(this.env),
context: this.createDeprecationContext(),
})),
]);
}
Expand Down Expand Up @@ -262,6 +265,14 @@ export class ConfigService {
handledDeprecatedConfig.push(config);
this.handledDeprecatedConfigs.set(domainId, handledDeprecatedConfig);
}

private createDeprecationContext(): ConfigDeprecationContext {
return {
branch: this.env.packageInfo.branch,
version: this.env.packageInfo.version,
docLinks: this.docLinks,
};
}
}

const pathToString = (path: ConfigPath) => (Array.isArray(path) ? path.join('.') : path);
Expand All @@ -272,10 +283,3 @@ const pathToString = (path: ConfigPath) => (Array.isArray(path) ? path.join('.')
*/
const isPathHandled = (path: string, handledPaths: string[]) =>
handledPaths.some((handledPath) => hasConfigPathIntersection(path, handledPath));

const createDeprecationContext = (env: Env): ConfigDeprecationContext => {
return {
branch: env.packageInfo.branch,
version: env.packageInfo.version,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import type { DocLinks } from '@kbn/doc-links';
import { applyDeprecations } from './apply_deprecations';
import { ConfigDeprecation, ConfigDeprecationContext, ConfigDeprecationWithContext } from './types';
import { configDeprecationFactory as deprecations } from './deprecation_factory';
Expand All @@ -14,6 +15,7 @@ describe('applyDeprecations', () => {
const context: ConfigDeprecationContext = {
version: '7.16.2',
branch: '7.16',
docLinks: {} as DocLinks,
};

const wrapHandler = (
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-config/src/deprecation/deprecations.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
* Side Public License, v 1.
*/

import type { DocLinks } from '@kbn/doc-links';
import type { ConfigDeprecationContext } from './types';

const createMockedContext = (): ConfigDeprecationContext => {
return {
branch: 'master',
version: '8.0.0',
docLinks: {} as DocLinks,
};
};

Expand Down
3 changes: 3 additions & 0 deletions packages/kbn-config/src/deprecation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/
import type { RecursiveReadonly } from '@kbn/utility-types';
import type { DocLinks } from '@kbn/doc-links';

/**
* Config deprecation hook used when invoking a {@link ConfigDeprecation}
Expand Down Expand Up @@ -77,6 +78,8 @@ export interface ConfigDeprecationContext {
version: string;
/** The current Kibana branch, e.g `7.x`, `7.16`, `master` */
branch: string;
/** Allow direct access to the doc links from the deprecation handler */
docLinks: DocLinks;
}

/**
Expand Down

0 comments on commit 569e10a

Please sign in to comment.