From 9df87a56d846d677032d6e56eb0db3820d2db0a0 Mon Sep 17 00:00:00 2001 From: suzhou Date: Wed, 22 Feb 2023 17:50:18 +0800 Subject: [PATCH 01/14] feature: patch pkg.branch when needed Signed-off-by: suzhou --- src/dev/build/tasks/create_package_json_task.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/dev/build/tasks/create_package_json_task.ts b/src/dev/build/tasks/create_package_json_task.ts index 8f9b41738eea..2789dfa0233a 100644 --- a/src/dev/build/tasks/create_package_json_task.ts +++ b/src/dev/build/tasks/create_package_json_task.ts @@ -29,6 +29,7 @@ */ import { copyWorkspacePackages } from '@osd/pm'; +import { parse } from 'semver'; import { read, write, Task } from '../lib'; @@ -37,6 +38,16 @@ export const CreatePackageJson: Task = { async run(config, log, build) { const pkg = config.getOpenSearchDashboardsPkg(); + /** + * OpenSearch server has a logic that if the pkg.branch is "main", + * set the docVersion to latest. So here we exclude main. + */ + const semverResult = parse(pkg.version); + const shouldPatch = semverResult && pkg.branch !== 'main'; + const branch = shouldPatch ? `${semverResult.major}.${semverResult.minor}` : pkg.branch; + if (shouldPatch) { + log.info(`Patch branch property: ${branch}`); + } const newPkg = { name: pkg.name, @@ -44,7 +55,7 @@ export const CreatePackageJson: Task = { description: pkg.description, keywords: pkg.keywords, version: config.getBuildVersion(), - branch: pkg.branch, + branch, build: { number: config.getBuildNumber(), sha: config.getBuildSha(), From f533c751a0d538d07d0d5aa9739dc5daffc71f76 Mon Sep 17 00:00:00 2001 From: suzhou Date: Wed, 22 Feb 2023 18:00:34 +0800 Subject: [PATCH 02/14] feat: update CHANGELOG.md Signed-off-by: suzhou --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f0fcd58b885..c929671af16c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -200,6 +200,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Add path ignore for markdown files for CI ([#2312](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2312)) - Updating WS scans to ignore BWC artifacts in `cypress` ([#2408](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2408)) - [CI] Run functional test repo as workflow ([#2503](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2503)) +- [CI] Patch pkg.branch according to pkg.version when build ([#3483](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3483)) ### 📝 Documentation From 730d694a1f261c70a2e732e9e322b61ced7e15fe Mon Sep 17 00:00:00 2001 From: suzhou Date: Wed, 22 Feb 2023 18:02:26 +0800 Subject: [PATCH 03/14] feat: use buildVersion Signed-off-by: suzhou --- src/dev/build/tasks/create_package_json_task.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev/build/tasks/create_package_json_task.ts b/src/dev/build/tasks/create_package_json_task.ts index 2789dfa0233a..32470bebaa36 100644 --- a/src/dev/build/tasks/create_package_json_task.ts +++ b/src/dev/build/tasks/create_package_json_task.ts @@ -42,7 +42,7 @@ export const CreatePackageJson: Task = { * OpenSearch server has a logic that if the pkg.branch is "main", * set the docVersion to latest. So here we exclude main. */ - const semverResult = parse(pkg.version); + const semverResult = parse(config.getBuildVersion()); const shouldPatch = semverResult && pkg.branch !== 'main'; const branch = shouldPatch ? `${semverResult.major}.${semverResult.minor}` : pkg.branch; if (shouldPatch) { From b09c3eb47ea0d1500a66f27953351905e720f8e8 Mon Sep 17 00:00:00 2001 From: suzhou Date: Thu, 23 Feb 2023 11:03:48 +0800 Subject: [PATCH 04/14] Update src/dev/build/tasks/create_package_json_task.ts Signed-off-by: suzhou Co-authored-by: Miki --- .../build/tasks/create_package_json_task.ts | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/dev/build/tasks/create_package_json_task.ts b/src/dev/build/tasks/create_package_json_task.ts index 32470bebaa36..2eea7874e668 100644 --- a/src/dev/build/tasks/create_package_json_task.ts +++ b/src/dev/build/tasks/create_package_json_task.ts @@ -39,14 +39,30 @@ export const CreatePackageJson: Task = { async run(config, log, build) { const pkg = config.getOpenSearchDashboardsPkg(); /** - * OpenSearch server has a logic that if the pkg.branch is "main", - * set the docVersion to latest. So here we exclude main. + * 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. */ - const semverResult = parse(config.getBuildVersion()); - const shouldPatch = semverResult && pkg.branch !== 'main'; - const branch = shouldPatch ? `${semverResult.major}.${semverResult.minor}` : pkg.branch; - if (shouldPatch) { - log.info(`Patch branch property: ${branch}`); + const buildVersion = config.getBuildVersion(); + let branch; + if (pkg.branch === 'main') { + branch = pkg.branch; + } else { + const parsedBuildVersion = parse(buildVersion); + if (parsedBuildVersion) { + branch = `${parsedBuildVersion.major}.${parsedBuildVersion.minor}`; + log.info(`Updating package.branch to ${branch}`); + } else { + const validDocPathsPattern = /^\d+\.\d+$/; + if (validDocPathsPattern.test(pkg.branch)) { + branch = pkg.branch; + } else { + // package version was not parsable and branch is unusable + throw new Error( + `Failed to identify documentation path while generating package.json: encountered invalid build version (${buildVersion}) and branch property (${pkg.branch}).` + ); + } + } } const newPkg = { @@ -54,7 +70,7 @@ export const CreatePackageJson: Task = { private: true, description: pkg.description, keywords: pkg.keywords, - version: config.getBuildVersion(), + version: buildVersion, branch, build: { number: config.getBuildNumber(), From 8f8521d97d098e3520b9ddc435905af39656e6d9 Mon Sep 17 00:00:00 2001 From: suzhou Date: Fri, 3 Mar 2023 11:20:29 +0800 Subject: [PATCH 05/14] feat: pass ts type check Signed-off-by: suzhou --- src/dev/build/tasks/create_package_json_task.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev/build/tasks/create_package_json_task.ts b/src/dev/build/tasks/create_package_json_task.ts index 2eea7874e668..1e16745906ee 100644 --- a/src/dev/build/tasks/create_package_json_task.ts +++ b/src/dev/build/tasks/create_package_json_task.ts @@ -54,7 +54,7 @@ export const CreatePackageJson: Task = { log.info(`Updating package.branch to ${branch}`); } else { const validDocPathsPattern = /^\d+\.\d+$/; - if (validDocPathsPattern.test(pkg.branch)) { + if (validDocPathsPattern.test(pkg.branch as string)) { branch = pkg.branch; } else { // package version was not parsable and branch is unusable From 59897b0a57af77bdfad38ccab3a562d5d0721fd4 Mon Sep 17 00:00:00 2001 From: suzhou Date: Tue, 21 Mar 2023 10:51:41 +0800 Subject: [PATCH 06/14] feat: move branch logic to runtime to restrict impact Signed-off-by: suzhou --- .../public/doc_links/doc_links_service.ts | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/core/public/doc_links/doc_links_service.ts b/src/core/public/doc_links/doc_links_service.ts index 109e3d9c1773..96bddbf46e11 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,33 @@ 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; + if (pkgBranch === 'main') { + branch = 'latest'; + } else { + const parsedBuildVersion = parse(buildVersion); + if (parsedBuildVersion) { + branch = `${parsedBuildVersion.major}.${parsedBuildVersion.minor}`; + } else { + const validDocPathsPattern = /^\d+\.\d+$/; + if (validDocPathsPattern.test(pkgBranch)) { + branch = pkgBranch; + } else { + // package version was not parsable and branch is unusable + throw new Error( + `Failed to identify documentation path while parsing package.json: encountered invalid build version (${buildVersion}) and branch property (${pkgBranch}).` + ); + } + } + } + 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/`; From 361eaf5987d2881e3735207f229a254870221726 Mon Sep 17 00:00:00 2001 From: suzhou Date: Tue, 21 Mar 2023 10:53:27 +0800 Subject: [PATCH 07/14] feat: update changelog Signed-off-by: suzhou --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c929671af16c..d19fe6ae6083 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -200,7 +200,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Add path ignore for markdown files for CI ([#2312](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2312)) - Updating WS scans to ignore BWC artifacts in `cypress` ([#2408](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2408)) - [CI] Run functional test repo as workflow ([#2503](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2503)) -- [CI] Patch pkg.branch according to pkg.version when build ([#3483](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3483)) +- Add downgrade logic for branch in DocLinkService([#3483](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3483)) ### 📝 Documentation From a6c1f8add74e7c4bdf5b68154bc96e6a07d88815 Mon Sep 17 00:00:00 2001 From: suzhou Date: Tue, 21 Mar 2023 10:55:36 +0800 Subject: [PATCH 08/14] Revert "feat: pass ts type check" This reverts commit 8f8521d97d098e3520b9ddc435905af39656e6d9. Signed-off-by: suzhou --- src/dev/build/tasks/create_package_json_task.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev/build/tasks/create_package_json_task.ts b/src/dev/build/tasks/create_package_json_task.ts index 1e16745906ee..2eea7874e668 100644 --- a/src/dev/build/tasks/create_package_json_task.ts +++ b/src/dev/build/tasks/create_package_json_task.ts @@ -54,7 +54,7 @@ export const CreatePackageJson: Task = { log.info(`Updating package.branch to ${branch}`); } else { const validDocPathsPattern = /^\d+\.\d+$/; - if (validDocPathsPattern.test(pkg.branch as string)) { + if (validDocPathsPattern.test(pkg.branch)) { branch = pkg.branch; } else { // package version was not parsable and branch is unusable From 6f970986a9dc3a2d5c3732e40365340194b9c089 Mon Sep 17 00:00:00 2001 From: suzhou Date: Tue, 21 Mar 2023 10:56:12 +0800 Subject: [PATCH 09/14] Revert "Update src/dev/build/tasks/create_package_json_task.ts " This reverts commit b09c3eb47ea0d1500a66f27953351905e720f8e8. Signed-off-by: suzhou --- .../build/tasks/create_package_json_task.ts | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/src/dev/build/tasks/create_package_json_task.ts b/src/dev/build/tasks/create_package_json_task.ts index 2eea7874e668..32470bebaa36 100644 --- a/src/dev/build/tasks/create_package_json_task.ts +++ b/src/dev/build/tasks/create_package_json_task.ts @@ -39,30 +39,14 @@ export const CreatePackageJson: Task = { async run(config, log, build) { const pkg = config.getOpenSearchDashboardsPkg(); /** - * 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. + * OpenSearch server has a logic that if the pkg.branch is "main", + * set the docVersion to latest. So here we exclude main. */ - const buildVersion = config.getBuildVersion(); - let branch; - if (pkg.branch === 'main') { - branch = pkg.branch; - } else { - const parsedBuildVersion = parse(buildVersion); - if (parsedBuildVersion) { - branch = `${parsedBuildVersion.major}.${parsedBuildVersion.minor}`; - log.info(`Updating package.branch to ${branch}`); - } else { - const validDocPathsPattern = /^\d+\.\d+$/; - if (validDocPathsPattern.test(pkg.branch)) { - branch = pkg.branch; - } else { - // package version was not parsable and branch is unusable - throw new Error( - `Failed to identify documentation path while generating package.json: encountered invalid build version (${buildVersion}) and branch property (${pkg.branch}).` - ); - } - } + const semverResult = parse(config.getBuildVersion()); + const shouldPatch = semverResult && pkg.branch !== 'main'; + const branch = shouldPatch ? `${semverResult.major}.${semverResult.minor}` : pkg.branch; + if (shouldPatch) { + log.info(`Patch branch property: ${branch}`); } const newPkg = { @@ -70,7 +54,7 @@ export const CreatePackageJson: Task = { private: true, description: pkg.description, keywords: pkg.keywords, - version: buildVersion, + version: config.getBuildVersion(), branch, build: { number: config.getBuildNumber(), From 1dff042a402347880e7844231400b933ed2474d6 Mon Sep 17 00:00:00 2001 From: suzhou Date: Tue, 21 Mar 2023 10:56:21 +0800 Subject: [PATCH 10/14] Revert "feat: use buildVersion" This reverts commit 730d694a1f261c70a2e732e9e322b61ced7e15fe. Signed-off-by: suzhou --- src/dev/build/tasks/create_package_json_task.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev/build/tasks/create_package_json_task.ts b/src/dev/build/tasks/create_package_json_task.ts index 32470bebaa36..2789dfa0233a 100644 --- a/src/dev/build/tasks/create_package_json_task.ts +++ b/src/dev/build/tasks/create_package_json_task.ts @@ -42,7 +42,7 @@ export const CreatePackageJson: Task = { * OpenSearch server has a logic that if the pkg.branch is "main", * set the docVersion to latest. So here we exclude main. */ - const semverResult = parse(config.getBuildVersion()); + const semverResult = parse(pkg.version); const shouldPatch = semverResult && pkg.branch !== 'main'; const branch = shouldPatch ? `${semverResult.major}.${semverResult.minor}` : pkg.branch; if (shouldPatch) { From 35fff8221443153e52af994cefd0d4402046c415 Mon Sep 17 00:00:00 2001 From: suzhou Date: Tue, 21 Mar 2023 10:56:37 +0800 Subject: [PATCH 11/14] Revert "feature: patch pkg.branch when needed" This reverts commit 9df87a56d846d677032d6e56eb0db3820d2db0a0. Signed-off-by: suzhou --- src/dev/build/tasks/create_package_json_task.ts | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/dev/build/tasks/create_package_json_task.ts b/src/dev/build/tasks/create_package_json_task.ts index 2789dfa0233a..8f9b41738eea 100644 --- a/src/dev/build/tasks/create_package_json_task.ts +++ b/src/dev/build/tasks/create_package_json_task.ts @@ -29,7 +29,6 @@ */ import { copyWorkspacePackages } from '@osd/pm'; -import { parse } from 'semver'; import { read, write, Task } from '../lib'; @@ -38,16 +37,6 @@ export const CreatePackageJson: Task = { async run(config, log, build) { const pkg = config.getOpenSearchDashboardsPkg(); - /** - * OpenSearch server has a logic that if the pkg.branch is "main", - * set the docVersion to latest. So here we exclude main. - */ - const semverResult = parse(pkg.version); - const shouldPatch = semverResult && pkg.branch !== 'main'; - const branch = shouldPatch ? `${semverResult.major}.${semverResult.minor}` : pkg.branch; - if (shouldPatch) { - log.info(`Patch branch property: ${branch}`); - } const newPkg = { name: pkg.name, @@ -55,7 +44,7 @@ export const CreatePackageJson: Task = { description: pkg.description, keywords: pkg.keywords, version: config.getBuildVersion(), - branch, + branch: pkg.branch, build: { number: config.getBuildNumber(), sha: config.getBuildSha(), From 20c04b0f78f780a22e6c5e6a3f3e782c41a66fd2 Mon Sep 17 00:00:00 2001 From: suzhou Date: Tue, 21 Mar 2023 14:03:25 +0800 Subject: [PATCH 12/14] feat: remove error message and update unit test Signed-off-by: suzhou --- .../public/doc_links/doc_links_service.test.ts | 12 ++++++++++++ src/core/public/doc_links/doc_links_service.ts | 15 +++++---------- 2 files changed, 17 insertions(+), 10 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 96bddbf46e11..e5b38a5b7993 100644 --- a/src/core/public/doc_links/doc_links_service.ts +++ b/src/core/public/doc_links/doc_links_service.ts @@ -51,19 +51,14 @@ export class DocLinksService { if (pkgBranch === 'main') { branch = 'latest'; } else { + const validDocPathsPattern = /^\d+\.\d+$/; const parsedBuildVersion = parse(buildVersion); - if (parsedBuildVersion) { + if (validDocPathsPattern.test(pkgBranch)) { + branch = pkgBranch; + } else if (parsedBuildVersion) { branch = `${parsedBuildVersion.major}.${parsedBuildVersion.minor}`; } else { - const validDocPathsPattern = /^\d+\.\d+$/; - if (validDocPathsPattern.test(pkgBranch)) { - branch = pkgBranch; - } else { - // package version was not parsable and branch is unusable - throw new Error( - `Failed to identify documentation path while parsing package.json: encountered invalid build version (${buildVersion}) and branch property (${pkgBranch}).` - ); - } + branch = pkgBranch; } } const DOC_LINK_VERSION = branch; From 2a698e3572a89bca85efa3e0fafeb0c1db77aead Mon Sep 17 00:00:00 2001 From: suzhou Date: Tue, 21 Mar 2023 14:06:08 +0800 Subject: [PATCH 13/14] feat: add default value Signed-off-by: suzhou --- src/core/public/doc_links/doc_links_service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/public/doc_links/doc_links_service.ts b/src/core/public/doc_links/doc_links_service.ts index e5b38a5b7993..119202816fea 100644 --- a/src/core/public/doc_links/doc_links_service.ts +++ b/src/core/public/doc_links/doc_links_service.ts @@ -47,7 +47,7 @@ export class DocLinksService { * 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; + let branch = pkgBranch; if (pkgBranch === 'main') { branch = 'latest'; } else { From f0d619850ec67afb5ac421c0dbdae85bb5d03176 Mon Sep 17 00:00:00 2001 From: suzhou Date: Thu, 23 Mar 2023 10:07:55 +0800 Subject: [PATCH 14/14] feat: optimize code logic Signed-off-by: suzhou --- src/core/public/doc_links/doc_links_service.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/core/public/doc_links/doc_links_service.ts b/src/core/public/doc_links/doc_links_service.ts index 119202816fea..1d621550e4be 100644 --- a/src/core/public/doc_links/doc_links_service.ts +++ b/src/core/public/doc_links/doc_links_service.ts @@ -53,12 +53,8 @@ export class DocLinksService { } else { const validDocPathsPattern = /^\d+\.\d+$/; const parsedBuildVersion = parse(buildVersion); - if (validDocPathsPattern.test(pkgBranch)) { - branch = pkgBranch; - } else if (parsedBuildVersion) { + if (!validDocPathsPattern.test(pkgBranch) && parsedBuildVersion) { branch = `${parsedBuildVersion.major}.${parsedBuildVersion.minor}`; - } else { - branch = pkgBranch; } } const DOC_LINK_VERSION = branch;