Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add downgrade logic for branch in DocLinkService #3483

Merged
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
- Add downgrade logic for branch in DocLinkService([#3483](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3483))

### 📝 Documentation

Expand Down
32 changes: 28 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,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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
When the logic goes to here, the dashboard will display such error page. But it won't be here because dashboard will check the version with OpenSearch core and thus it will always be parsed successfully by semver.

`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/`;
Expand Down