Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Add downgrade logic for branch in DocLinkService (opensearch-project#…
Browse files Browse the repository at this point in the history
…3483)

Add downgrade logic for branch in DocLinkService

Signed-off-by: suzhou <suzhou@amazon.com>
Signed-off-by: David Sinclair <david@sinclair.tech>
  • Loading branch information
SuZhou-Joe authored and sikhote committed Apr 24, 2023
1 parent 3865ecb commit fcd09fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,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))
- Add downgrade logic for branch in DocLinkService([#3483](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3483))

### 📝 Documentation

Expand Down
12 changes: 12 additions & 0 deletions src/core/public/doc_links/doc_links_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
);
});
});
23 changes: 19 additions & 4 deletions src/core/public/doc_links/doc_links_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

import { deepFreeze } from '@osd/std';
import { parse } from 'semver';
import { InjectedMetadataSetup } from '../injected_metadata';

interface StartDeps {
Expand All @@ -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/`;
Expand Down

0 comments on commit fcd09fc

Please sign in to comment.