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))
- [CI] Patch pkg.branch according to pkg.version when build ([#3483](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3483))

### 📝 Documentation

Expand Down
31 changes: 29 additions & 2 deletions src/dev/build/tasks/create_package_json_task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/

import { copyWorkspacePackages } from '@osd/pm';
import { parse } from 'semver';

import { read, write, Task } from '../lib';

Expand All @@ -37,14 +38,40 @@ 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.
*/
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 as string)) {
branch = pkg.branch;
} else {
// package version was not parsable and branch is unusable
throw new Error(
Copy link
Member

Choose a reason for hiding this comment

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

Does this prevent the builds if they pass a non-valid doc paths pattern in the branch in the package?

Copy link
Member Author

@SuZhou-Joe SuZhou-Joe Mar 14, 2023

Choose a reason for hiding this comment

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

Yes, if no version can be specified after so many downgrade logic, we will prevent the build because there will be a doc link error if we continue to build.

`Failed to identify documentation path while generating package.json: encountered invalid build version (${buildVersion}) and branch property (${pkg.branch}).`
);
}
}
}

const newPkg = {
name: pkg.name,
private: true,
description: pkg.description,
keywords: pkg.keywords,
version: config.getBuildVersion(),
branch: pkg.branch,
version: buildVersion,
branch,
build: {
number: config.getBuildNumber(),
sha: config.getBuildSha(),
Expand Down