Skip to content

Commit

Permalink
[doclinks] propagate build flavor to getDocLinks (elastic#172358)
Browse files Browse the repository at this point in the history
## Summary

Fix elastic#167088

Add a new `buildFlavor` (`traditional` | `serverless`) parameter to
`getDocLinks` and `getDocLinksMeta` so that the doc links generator
logic can leverage the information

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
pgayvallet and kibanamachine authored Dec 12, 2023
1 parent 48875f9 commit c50f3d7
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
*/

import { getDocLinksMock, getDocLinksMetaMock } from './doc_links_service.test.mocks';
import { coreContextMock } from '@kbn/core-base-browser-mocks';
import { DocLinksService } from './doc_links_service';
import { injectedMetadataServiceMock } from '@kbn/core-injected-metadata-browser-mocks';

describe('DocLinksService', () => {
let injectedMetadata: ReturnType<typeof injectedMetadataServiceMock.createStartContract>;
let coreContext: ReturnType<typeof coreContextMock.create>;
let service: DocLinksService;

beforeEach(() => {
Expand All @@ -26,7 +28,8 @@ describe('DocLinksService', () => {
settings: 'http://settings.test.url',
});

service = new DocLinksService();
coreContext = coreContextMock.create();
service = new DocLinksService(coreContext);
});

afterEach(() => {
Expand All @@ -43,6 +46,7 @@ describe('DocLinksService', () => {
expect(getDocLinksMetaMock).toHaveBeenCalledTimes(1);
expect(getDocLinksMetaMock).toHaveBeenCalledWith({
kibanaBranch: 'test-branch',
buildFlavor: coreContext.env.packageInfo.buildFlavor,
});
});

Expand All @@ -64,6 +68,7 @@ describe('DocLinksService', () => {
expect(getDocLinksMock).toHaveBeenCalledTimes(1);
expect(getDocLinksMock).toHaveBeenCalledWith({
kibanaBranch: 'test-branch',
buildFlavor: coreContext.env.packageInfo.buildFlavor,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { getDocLinks, getDocLinksMeta } from '@kbn/doc-links';
import type { CoreContext } from '@kbn/core-base-browser-internal';
import type { InternalInjectedMetadataSetup } from '@kbn/core-injected-metadata-browser-internal';
import type { DocLinksStart } from '@kbn/core-doc-links-browser';

Expand All @@ -17,12 +18,15 @@ export interface DocLinksServiceStartDeps {

/** @internal */
export class DocLinksService {
constructor(private readonly coreContext: CoreContext) {}

public setup() {}

public start({ injectedMetadata }: DocLinksServiceStartDeps): DocLinksStart {
const kibanaBranch = injectedMetadata.getKibanaBranch();
const docMeta = getDocLinksMeta({ kibanaBranch });
const docLinks = getDocLinks({ kibanaBranch });
const buildFlavor = this.coreContext.env.packageInfo.buildFlavor;
const docMeta = getDocLinksMeta({ kibanaBranch, buildFlavor });
const docLinks = getDocLinks({ kibanaBranch, buildFlavor });

return {
DOC_LINK_VERSION: docMeta.version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"@kbn/core-injected-metadata-browser-internal",
"@kbn/core-doc-links-browser",
"@kbn/core-injected-metadata-browser-mocks",
"@kbn/core-base-browser-mocks",
"@kbn/core-base-browser-internal",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import type { PublicMethodsOf } from '@kbn/utility-types';
import { coreContextMock } from '@kbn/core-base-browser-mocks';
import { injectedMetadataServiceMock } from '@kbn/core-injected-metadata-browser-mocks';
import type { DocLinksStart } from '@kbn/core-doc-links-browser';
import { DocLinksService } from '@kbn/core-doc-links-browser-internal';
Expand All @@ -15,7 +16,7 @@ const createStartContractMock = (): DocLinksStart => {
// This service is so simple that we actually use the real implementation
const injectedMetadata = injectedMetadataServiceMock.createStartContract();
injectedMetadata.getKibanaBranch.mockReturnValue('mocked-test-branch');
return new DocLinksService().start({ injectedMetadata });
return new DocLinksService(coreContextMock.create()).start({ injectedMetadata });
};

type DocLinksServiceContract = PublicMethodsOf<DocLinksService>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"@kbn/utility-types",
"@kbn/core-injected-metadata-browser-mocks",
"@kbn/core-doc-links-browser",
"@kbn/core-doc-links-browser-internal"
"@kbn/core-doc-links-browser-internal",
"@kbn/core-base-browser-mocks"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('DocLinksService', () => {
expect(getDocLinksMetaMock).toHaveBeenCalledTimes(1);
expect(getDocLinksMetaMock).toHaveBeenCalledWith({
kibanaBranch: coreContext.env.packageInfo.branch,
buildFlavor: coreContext.env.packageInfo.buildFlavor,
});
});

Expand All @@ -62,6 +63,7 @@ describe('DocLinksService', () => {
expect(getDocLinksMock).toHaveBeenCalledTimes(1);
expect(getDocLinksMock).toHaveBeenCalledWith({
kibanaBranch: coreContext.env.packageInfo.branch,
buildFlavor: coreContext.env.packageInfo.buildFlavor,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import type { DocLinksServiceSetup, DocLinksServiceStart } from '@kbn/core-doc-l

/** @internal */
export class DocLinksService {
private readonly kibanaBranch: string;
private docLinks?: DocLinksServiceSetup;

constructor(core: CoreContext) {
this.kibanaBranch = core.env.packageInfo.branch;
}
constructor(private readonly coreContext: CoreContext) {}

public setup(): DocLinksServiceSetup {
const docMeta = getDocLinksMeta({ kibanaBranch: this.kibanaBranch });
const docLinks = getDocLinks({ kibanaBranch: this.kibanaBranch });
const kibanaBranch = this.coreContext.env.packageInfo.branch;
const buildFlavor = this.coreContext.env.packageInfo.buildFlavor;

const docMeta = getDocLinksMeta({ kibanaBranch, buildFlavor });
const docLinks = getDocLinks({ kibanaBranch, buildFlavor });
this.docLinks = {
...docMeta,
links: docLinks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ type DocLinksServiceContract = PublicMethodsOf<DocLinksService>;

const createSetupMock = (): DocLinksServiceSetup => {
const branch = 'test-branch';
const buildFlavor = 'traditional';
return {
...getDocLinksMeta({ kibanaBranch: branch }),
links: getDocLinks({ kibanaBranch: branch }),
...getDocLinksMeta({ kibanaBranch: branch, buildFlavor }),
links: getDocLinks({ kibanaBranch: branch, buildFlavor }),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class CoreSystem {
browserSupportsCsp,
kibanaVersion: injectedMetadata.version,
});
this.docLinks = new DocLinksService();
this.docLinks = new DocLinksService(this.coreContext);
this.rendering = new RenderingService();
this.application = new ApplicationService();
this.integrations = new IntegrationsService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import fs from 'fs/promises';
import { defaultsDeep } from 'lodash';
import { BehaviorSubject, firstValueFrom, map } from 'rxjs';
import { ConfigService, Env } from '@kbn/config';
import { ConfigService, Env, BuildFlavor } from '@kbn/config';
import { getEnvOptions } from '@kbn/config-mocks';
import { REPO_ROOT } from '@kbn/repo-info';
import { KibanaMigrator } from '@kbn/core-saved-objects-migration-server-internal';
Expand Down Expand Up @@ -216,6 +216,7 @@ const getMigrator = async ({
loggerFactory,
kibanaVersion,
kibanaBranch,
buildFlavor = 'traditional',
nodeRoles,
}: {
configService: ConfigService;
Expand All @@ -226,6 +227,7 @@ const getMigrator = async ({
loggerFactory: LoggerFactory;
kibanaVersion: string;
kibanaBranch: string;
buildFlavor?: BuildFlavor;
nodeRoles: NodeRoles;
}) => {
const savedObjectsConf = await firstValueFrom(
Expand All @@ -237,8 +239,8 @@ const getMigrator = async ({
const soConfig = new SavedObjectConfig(savedObjectsConf, savedObjectsMigrationConf);

const docLinks: DocLinksServiceStart = {
...getDocLinksMeta({ kibanaBranch }),
links: getDocLinks({ kibanaBranch }),
...getDocLinksMeta({ kibanaBranch, buildFlavor }),
links: getDocLinks({ kibanaBranch, buildFlavor }),
};

const esCapabilities = await getCapabilitiesFromClient(client);
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ export { isConfigPath, hasConfigPathIntersection } from './src/config';
export { ObjectToConfigAdapter } from './src/object_to_config_adapter';
export type { CliArgs, RawPackageInfo, EnvOptions } from './src/env';
export { Env } from './src/env';
export type { EnvironmentMode, PackageInfo } from './src/types';
export type { EnvironmentMode, PackageInfo, BuildFlavor } from './src/types';
5 changes: 4 additions & 1 deletion packages/kbn-config/src/config_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export class ConfigService {
) {
this.log = logger.get('config');
this.deprecationLog = logger.get('config', 'deprecation');
this.docLinks = getDocLinks({ kibanaBranch: env.packageInfo.branch });
this.docLinks = getDocLinks({
kibanaBranch: env.packageInfo.branch,
buildFlavor: env.packageInfo.buildFlavor,
});

this.config$ = combineLatest([
this.rawConfigProvider.getConfig$(),
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-doc-links/src/get_doc_links.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getDocLinks } from './get_doc_links';

describe('getDocLinks', () => {
it('returns an immutable object', () => {
const links = getDocLinks({ kibanaBranch: 'test.branch' });
const links = getDocLinks({ kibanaBranch: 'test.branch', buildFlavor: 'traditional' });

expect(() => {
(links as unknown as Record<string, unknown>).settings = 'override';
Expand Down
7 changes: 4 additions & 3 deletions packages/kbn-doc-links/src/get_doc_links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
*/

import { deepFreeze } from '@kbn/std';
import type { DocLinks } from './types';
import type { DocLinks, BuildFlavor } from './types';
import { getDocLinksMeta } from './get_doc_meta';

export interface GetDocLinkOptions {
kibanaBranch: string;
buildFlavor: BuildFlavor;
}

export const getDocLinks = ({ kibanaBranch }: GetDocLinkOptions): DocLinks => {
const meta = getDocLinksMeta({ kibanaBranch });
export const getDocLinks = ({ kibanaBranch, buildFlavor }: GetDocLinkOptions): DocLinks => {
const meta = getDocLinksMeta({ kibanaBranch, buildFlavor });

const DOC_LINK_VERSION = meta.version;
const ELASTIC_WEBSITE_URL = meta.elasticWebsiteUrl;
Expand Down
14 changes: 9 additions & 5 deletions packages/kbn-doc-links/src/get_doc_meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import { getDocLinksMeta } from './get_doc_meta';

describe('getDocLinksMeta', () => {
it('returns the correct version for the `main` branch', () => {
expect(getDocLinksMeta({ kibanaBranch: 'main' }).version).toEqual('master');
expect(getDocLinksMeta({ kibanaBranch: 'main', buildFlavor: 'traditional' }).version).toEqual(
'master'
);
});

it('returns the correct version for other branches', () => {
expect(getDocLinksMeta({ kibanaBranch: '7.x' }).version).toEqual('7.x');
expect(getDocLinksMeta({ kibanaBranch: '7.x', buildFlavor: 'traditional' }).version).toEqual(
'7.x'
);
});

it('returns the correct website url', () => {
expect(getDocLinksMeta({ kibanaBranch: '7.x' }).elasticWebsiteUrl).toEqual(
'https://www.elastic.co/'
);
expect(
getDocLinksMeta({ kibanaBranch: '7.x', buildFlavor: 'traditional' }).elasticWebsiteUrl
).toEqual('https://www.elastic.co/');
});
});
8 changes: 6 additions & 2 deletions packages/kbn-doc-links/src/get_doc_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
* Side Public License, v 1.
*/

import { DocLinksMeta } from './types';
import { DocLinksMeta, BuildFlavor } from './types';

export interface GetDocLinksMetaOptions {
kibanaBranch: string;
buildFlavor: BuildFlavor;
}

export const getDocLinksMeta = ({ kibanaBranch }: GetDocLinksMetaOptions): DocLinksMeta => {
export const getDocLinksMeta = ({
kibanaBranch,
buildFlavor,
}: GetDocLinksMetaOptions): DocLinksMeta => {
return {
version: kibanaBranch === 'main' ? 'master' : kibanaBranch,
elasticWebsiteUrl: 'https://www.elastic.co/',
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-doc-links/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,5 @@ export interface DocLinks {
readonly settings: string;
};
}

export type BuildFlavor = 'serverless' | 'traditional';
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ export const AutocompleteFieldListsComponent: React.FC<AutocompleteFieldListsPro
<EuiLink
external
target="_blank"
href={getDocLinks({ kibanaBranch: 'main' }).securitySolution.exceptions.value_lists}
href={
getDocLinks({
kibanaBranch: 'main',
buildFlavor: 'traditional',
}).securitySolution.exceptions.value_lists
}
>
{i18n.SEE_DOCUMENTATION}
</EuiLink>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SemVer } from 'semver';

import { defaultsDeep } from 'lodash';
import { BehaviorSubject, firstValueFrom, map } from 'rxjs';
import { ConfigService, Env } from '@kbn/config';
import { ConfigService, Env, BuildFlavor } from '@kbn/config';
import { getEnvOptions } from '@kbn/config-mocks';
import { REPO_ROOT } from '@kbn/repo-info';
import { KibanaMigrator } from '@kbn/core-saved-objects-migration-server-internal';
Expand Down Expand Up @@ -278,6 +278,7 @@ interface GetMigratorParams {
loggerFactory: LoggerFactory;
kibanaVersion: string;
kibanaBranch: string;
buildFlavor?: BuildFlavor;
nodeRoles: NodeRoles;
}

Expand All @@ -290,6 +291,7 @@ const getMigrator = async ({
loggerFactory,
kibanaVersion,
kibanaBranch,
buildFlavor = 'traditional',
nodeRoles,
}: GetMigratorParams) => {
const savedObjectsConf = await firstValueFrom(
Expand All @@ -301,8 +303,8 @@ const getMigrator = async ({
const soConfig = new SavedObjectConfig(savedObjectsConf, savedObjectsMigrationConf);

const docLinks: DocLinksServiceStart = {
...getDocLinksMeta({ kibanaBranch }),
links: getDocLinks({ kibanaBranch }),
...getDocLinksMeta({ kibanaBranch, buildFlavor }),
links: getDocLinks({ kibanaBranch, buildFlavor }),
};

const esCapabilities = await getCapabilitiesFromClient(client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ import JSON5 from 'json5';

export const getDocVersion = () => {
const env = Env.createDefault(REPO_ROOT, getEnvOptions());
return getDocLinksMeta({ kibanaBranch: env.packageInfo.branch }).version;
return getDocLinksMeta({
kibanaBranch: env.packageInfo.branch,
buildFlavor: env.packageInfo.buildFlavor,
}).version;
};

export const getMigrationDocLink = () => {
const env = Env.createDefault(REPO_ROOT, getEnvOptions());
const docLinks = getDocLinks({ kibanaBranch: env.packageInfo.branch });
const docLinks = getDocLinks({
kibanaBranch: env.packageInfo.branch,
buildFlavor: env.packageInfo.buildFlavor,
});
return docLinks.kibanaUpgradeSavedObjects;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { DETECTION_ENGINE_RULES_BULK_ACTION } from '../../../../../common/consta
* @returns string
*/
export const buildDeprecatedBulkEndpointMessage = (path: string) => {
const docsLink = getDocLinks({ kibanaBranch: 'main' }).siem.ruleApiOverview;
const docsLink = getDocLinks({ kibanaBranch: 'main', buildFlavor: 'traditional' }).siem
.ruleApiOverview;
return `Deprecated endpoint: ${path} API is deprecated since v8.2. Please use the ${DETECTION_ENGINE_RULES_BULK_ACTION} API instead. See ${docsLink} for more detail.`;
};

Expand Down

0 comments on commit c50f3d7

Please sign in to comment.