From 49f7a8d43c660ba1f1579a7041607bf5b5773eac Mon Sep 17 00:00:00 2001 From: Gerard Soldevila Date: Thu, 19 Dec 2024 15:07:25 +0100 Subject: [PATCH] SKA: Relocate script v6 (#204929) ## Summary * Update references to core packages in Bazel files, keeping the full package id (thanks @afharo !) * Show list of uncategorised packages in the plan * Make remote detection case insensitive (cc @Dosant) image --- packages/kbn-relocate/relocate.ts | 8 +++++-- packages/kbn-relocate/utils/git.ts | 5 +++- packages/kbn-relocate/utils/logging.ts | 32 +++++++++++++++++++++---- packages/kbn-relocate/utils/relocate.ts | 19 +++++++++++---- 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/packages/kbn-relocate/relocate.ts b/packages/kbn-relocate/relocate.ts index 16b2c17f4b066..a96c58629bab7 100644 --- a/packages/kbn-relocate/relocate.ts +++ b/packages/kbn-relocate/relocate.ts @@ -52,6 +52,12 @@ const moveModule = async (module: Package, log: ToolingLog) => { const relocateModules = async (toMove: Package[], log: ToolingLog): Promise => { let relocated: number = 0; + + // filter out modules that are not categorised (lacking group, visibility) + toMove = toMove.filter( + (module) => module.group && module.group !== 'common' && module.visibility + ); + for (let i = 0; i < toMove.length; ++i) { const module = toMove[i]; @@ -102,8 +108,6 @@ const findModules = ({ teams, paths, included, excluded }: FindModulesParams, lo modules // exclude devOnly modules (they will remain in /packages) .filter(({ manifest }) => !manifest.devOnly) - // exclude modules that do not specify a group - .filter(({ manifest }) => manifest.group) // explicit exclusions .filter(({ id }) => !EXCLUDED_MODULES.includes(id) && !excluded.includes(id)) // we don't want to move test modules (just yet) diff --git a/packages/kbn-relocate/utils/git.ts b/packages/kbn-relocate/utils/git.ts index 0085e07fdd6b5..85e68dccd3f5d 100644 --- a/packages/kbn-relocate/utils/git.ts +++ b/packages/kbn-relocate/utils/git.ts @@ -14,13 +14,16 @@ import { safeExec } from './exec'; export const findRemoteName = async (repo: string) => { const res = await safeExec('git remote -v', true, false); + repo = repo.toLowerCase(); const remotes = res.stdout .trim() .split('\n') .map((line) => line.split(/\t| /).filter(Boolean)) .filter((chunks) => chunks.length >= 2); return remotes.find( - ([, url]) => url.includes(`github.com/${repo}`) || url.includes(`github.com:${repo}`) + ([, url]) => + url.toLowerCase().includes(`github.com/${repo}`) || + url.toLowerCase().includes(`github.com:${repo}`) )?.[0]; }; diff --git a/packages/kbn-relocate/utils/logging.ts b/packages/kbn-relocate/utils/logging.ts index 4aec07a1d9bf9..bd8f8fde42ae0 100644 --- a/packages/kbn-relocate/utils/logging.ts +++ b/packages/kbn-relocate/utils/logging.ts @@ -22,11 +22,15 @@ import { UPDATED_RELATIVE_PATHS, } from '../constants'; -export const createModuleTable = (entries: string[][]) => { +export const createModuleTable = ( + entries: string[][], + head: string[] = ['Id', 'Target folder'] +) => { const table = new Table({ - head: ['Id', 'Target folder'], + head, colAligns: ['left', 'left'], style: { + compact: true, 'padding-left': 2, 'padding-right': 2, }, @@ -37,12 +41,12 @@ export const createModuleTable = (entries: string[][]) => { }; export const relocatePlan = (modules: Package[], log: ToolingLog) => { - const plugins = modules.filter((module) => module.manifest.type === 'plugin'); - const packages = modules.filter((module) => module.manifest.type !== 'plugin'); - const target = (module: Package) => calculateModuleTargetFolder(module).replace(BASE_FOLDER, ''); writeFileSync(DESCRIPTION, GLOBAL_DESCRIPTION); + const plugins = modules.filter( + (module) => module.group && module.group !== 'common' && module.manifest.type === 'plugin' + ); if (plugins.length) { const pluginList = dedent` \n\n#### ${plugins.length} plugin(s) are going to be relocated:\n @@ -56,6 +60,10 @@ export const relocatePlan = (modules: Package[], log: ToolingLog) => { log.info(`${plugins.length} plugin(s) are going to be relocated:\n${plgTable.toString()}`); } + const packages = modules.filter( + (module) => module.group && module.group !== 'common' && module.manifest.type !== 'plugin' + ); + if (packages.length) { const packageList = dedent` \n\n#### ${packages.length} packages(s) are going to be relocated:\n @@ -68,6 +76,20 @@ export const relocatePlan = (modules: Package[], log: ToolingLog) => { const pkgTable = createModuleTable(packages.map((pkg) => [pkg.id, target(pkg)])); log.info(`${packages.length} packages(s) are going to be relocated:\n${pkgTable.toString()}`); } + + const uncategorised = modules.filter((module) => !module.group || module.group === 'common'); + if (uncategorised.length) { + const uncategorisedTable = createModuleTable( + uncategorised.map(({ id, directory }) => [id, directory.replace(BASE_FOLDER, '')]), + ['Id', 'Current folder'] + ); + + log.warning( + `${ + uncategorised.length + } module(s) are missing "group" and/or "visibility" in the manifest, and cannot be relocated:\n${uncategorisedTable.toString()}` + ); + } }; export const appendCollapsible = ( diff --git a/packages/kbn-relocate/utils/relocate.ts b/packages/kbn-relocate/utils/relocate.ts index 2f2b6a78379e6..233c03b84ea05 100644 --- a/packages/kbn-relocate/utils/relocate.ts +++ b/packages/kbn-relocate/utils/relocate.ts @@ -7,7 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { join } from 'path'; +import { basename, join } from 'path'; import type { ToolingLog } from '@kbn/tooling-log'; import { orderBy } from 'lodash'; import type { Package } from '../types'; @@ -87,9 +87,9 @@ export const calculateModuleTargetFolder = (module: Package): string => { }; export const isInTargetFolder = (module: Package, log: ToolingLog): boolean => { - if (!module.group || !module.visibility) { + if (!module.group || module.group === 'common' || !module.visibility) { log.warning(`The module '${module.id}' is missing the group/visibility information`); - return true; + return false; } const baseTargetFolders = TARGET_FOLDERS[`${module.group}:${module.visibility}`]; @@ -154,9 +154,20 @@ const replaceReferencesInternal = async ( continue; } + let d = dst; + // For .bazel references, we need to keep the original name reference if we are renaming the path + // For example, in the move "packages/core/base/core-base-common" to "src/core/packages/base/common", + // we need to keep the reference name to core-base-common by replacing it with "src/core/packages/base/common:core-base-common" + if ( + file.endsWith('.bazel') && + relativeDestination.startsWith('src/core/packages/') && // Only on core packages for now, since are the ones being renamed + basename(relativeSource) !== basename(relativeDestination) + ) { + d = `${dst}:${basename(relativeSource)}`; + } const md5Before = (await quietExec(`md5 ${file} --quiet`)).stdout.trim(); // if we are updating packages/cloud references, we must pay attention to not update packages/cloud_defend too - await safeExec(`sed -i '' -E "/${src}[\-_a-zA-Z0-9]/! s/${src}/${dst}/g" ${file}`, false); + await safeExec(`sed -i '' -E "/${src}[\-_a-zA-Z0-9]/! s/${src}/${d}/g" ${file}`, false); const md5After = (await quietExec(`md5 ${file} --quiet`)).stdout.trim(); if (md5Before !== md5After) {