From 4e5d8c74b100fdc8cd41312b267cd330b2f4149f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 23 Mar 2023 16:57:51 +0000 Subject: [PATCH] Add downgrade logic for branch in DocLinkService (#3483) Add downgrade logic for branch in DocLinkService Signed-off-by: suzhou (cherry picked from commit e37ac4c8f69233d6de8e5f83dcae61f922aef783) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md --- .../doc_links/doc_links_service.test.ts | 12 ++++++++++ .../public/doc_links/doc_links_service.ts | 23 +++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/core/public/doc_links/doc_links_service.test.ts b/src/core/public/doc_links/doc_links_service.test.ts index 0ba91c21ccc3..1c1dfc75ace6 100644 --- a/src/core/public/doc_links/doc_links_service.test.ts +++ b/src/core/public/doc_links/doc_links_service.test.ts @@ -64,4 +64,16 @@ describe('DocLinksService#start()', () => { 'https://opensearch.org/docs/1.1/dashboards/index/' ); }); + + it('templates the doc links with the build version from injectedMetadata', () => { + const injectedMetadata = injectedMetadataServiceMock.createStartContract(); + injectedMetadata.getOpenSearchDashboardsBranch.mockReturnValue('test-branch'); + injectedMetadata.getOpenSearchDashboardsVersion.mockReturnValue('1.1.2'); + const service = new DocLinksService(); + const api = service.start({ injectedMetadata }); + expect(api.DOC_LINK_VERSION).toEqual('1.1'); + expect(api.links.opensearchDashboards.introduction).toEqual( + 'https://opensearch.org/docs/1.1/dashboards/index/' + ); + }); }); diff --git a/src/core/public/doc_links/doc_links_service.ts b/src/core/public/doc_links/doc_links_service.ts index 109e3d9c1773..1d621550e4be 100644 --- a/src/core/public/doc_links/doc_links_service.ts +++ b/src/core/public/doc_links/doc_links_service.ts @@ -29,6 +29,7 @@ */ import { deepFreeze } from '@osd/std'; +import { parse } from 'semver'; import { InjectedMetadataSetup } from '../injected_metadata'; interface StartDeps { @@ -39,10 +40,24 @@ interface StartDeps { export class DocLinksService { public setup() {} public start({ injectedMetadata }: StartDeps): DocLinksStart { - const DOC_LINK_VERSION = - injectedMetadata.getOpenSearchDashboardsBranch() === 'main' - ? 'latest' - : injectedMetadata.getOpenSearchDashboardsBranch(); + const buildVersion = injectedMetadata.getOpenSearchDashboardsVersion(); + const pkgBranch = injectedMetadata.getOpenSearchDashboardsBranch(); + /** + * OpenSearch server uses the `branch` property from `package.json` to + * build links to the documentation. If set to `main`, it would use `/latest` + * and if not, it would use the `version` to construct URLs. + */ + let branch = pkgBranch; + if (pkgBranch === 'main') { + branch = 'latest'; + } else { + const validDocPathsPattern = /^\d+\.\d+$/; + const parsedBuildVersion = parse(buildVersion); + if (!validDocPathsPattern.test(pkgBranch) && parsedBuildVersion) { + branch = `${parsedBuildVersion.major}.${parsedBuildVersion.minor}`; + } + } + const DOC_LINK_VERSION = branch; const OPENSEARCH_WEBSITE_URL = 'https://opensearch.org/'; const OPENSEARCH_WEBSITE_DOCS = `${OPENSEARCH_WEBSITE_URL}docs/${DOC_LINK_VERSION}`; const OPENSEARCH_VERSIONED_DOCS = `${OPENSEARCH_WEBSITE_DOCS}/opensearch/`;