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(),