From 4d05f832d40f9664e1895de66e5aa0928a05edcb Mon Sep 17 00:00:00 2001 From: Gerard Soldevila Date: Tue, 7 Jan 2025 14:28:23 +0100 Subject: [PATCH] Misc enhancements --- packages/kbn-relocate/relocate.ts | 72 ++++++++++++++++--------- packages/kbn-relocate/utils/relocate.ts | 35 +++++------- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/packages/kbn-relocate/relocate.ts b/packages/kbn-relocate/relocate.ts index f9ead5f815736..b7a7b4bc7ac85 100644 --- a/packages/kbn-relocate/relocate.ts +++ b/packages/kbn-relocate/relocate.ts @@ -128,7 +128,18 @@ const findModules = ({ teams, paths, included, excluded }: FindModulesParams, lo // the module is not explicitly excluded .filter(({ id }) => !excluded.includes(id)) // exclude modules that are in the correct folder - .filter((module) => !isInTargetFolder(module, log)) + .filter((module) => { + if (isInTargetFolder(module)) { + log.info( + `The module ${ + module.id + } is already in the correct folder: '${calculateModuleTargetFolder(module)}'. Skipping` + ); + return false; + } else { + return true; + } + }) ); }; @@ -159,27 +170,6 @@ export const findAndRelocateModules = async (params: RelocateModulesParams, log: return; } - const toMove = findModules(findParams, log); - if (!toMove.length) { - log.info( - `No packages match the specified filters. Please tune your '--path' and/or '--team' and/or '--include' flags` - ); - return; - } - - relocatePlan(toMove, log); - - const resConfirmPlan = await inquirer.prompt({ - type: 'confirm', - name: 'confirmPlan', - message: `The script will RESET CHANGES in this repository, relocate the modules above and update references. Proceed?`, - }); - - if (!resConfirmPlan.confirmPlan) { - log.info('Aborting'); - return; - } - if (prNumber) { pr = await findPr(prNumber); @@ -187,14 +177,27 @@ export const findAndRelocateModules = async (params: RelocateModulesParams, log: const resOverride = await inquirer.prompt({ type: 'confirm', name: 'overrideManualCommits', - message: 'Detected manual commits in the PR, do you want to override them?', + message: + 'Manual commits detected in the PR, the script will try to cherry-pick them, but it might require manual intervention to resolve conflicts. Continue?', }); if (!resOverride.overrideManualCommits) { + log.info('Aborting'); return; } } } + const resConfirmReset = await inquirer.prompt({ + type: 'confirm', + name: 'confirmReset', + message: `The script will RESET CHANGES in this repository. Proceed?`, + }); + + if (!resConfirmReset.confirmReset) { + log.info('Aborting'); + return; + } + // start with a clean repo await safeExec(`git restore --staged .`); await safeExec(`git restore .`); @@ -222,13 +225,34 @@ export const findAndRelocateModules = async (params: RelocateModulesParams, log: message: `Ready to relocate! You can commit changes previous to the relocation at this point. Confirm to proceed with the relocation`, }); + const toMove = findModules(findParams, log); + if (!toMove.length) { + log.info( + `No packages match the specified filters. Please tune your '--path' and/or '--team' and/or '--include' flags` + ); + return; + } + + relocatePlan(toMove, log); + + const resConfirmPlan = await inquirer.prompt({ + type: 'confirm', + name: 'confirmPlan', + message: `The script will relocate the modules above and update references. Proceed?`, + }); + + if (!resConfirmPlan.confirmPlan) { + log.info('Aborting'); + return; + } + // relocate modules await safeExec(`yarn kbn bootstrap`); const movedCount = await relocateModules(toMove, log); if (movedCount === 0) { log.warning( - 'No modules were relocated, aborting operation to prevent force-pushing empty changes (this would close the existing PR!)' + 'No modules were relocated, aborting operation to prevent force-pushing empty changes' ); return; } diff --git a/packages/kbn-relocate/utils/relocate.ts b/packages/kbn-relocate/utils/relocate.ts index 1e833b8d2539a..f957410f9cd8e 100644 --- a/packages/kbn-relocate/utils/relocate.ts +++ b/packages/kbn-relocate/utils/relocate.ts @@ -19,7 +19,6 @@ import { KIBANA_FOLDER, NO_GREP, SCRIPT_ERRORS, - TARGET_FOLDERS, UPDATED_REFERENCES, UPDATED_RELATIVE_PATHS, } from '../constants'; @@ -39,12 +38,17 @@ export const calculateModuleTargetFolder = (module: Package): string => { const group = module.manifest.group!; const isPlugin = module.manifest.type === 'plugin'; const fullPath = join(BASE_FOLDER, module.directory); + + let moduleDelimiter: string; if (!fullPath.includes('/plugins/') && !fullPath.includes('/packages/')) { throw new Error( `The module ${module.id} is not located under a '*/plugins/*' or '*/packages/*' folder` ); + } else if (fullPath.includes('/plugins/') && fullPath.includes('/packages/')) { + moduleDelimiter = isPlugin ? '/plugins/' : '/packages/'; + } else { + moduleDelimiter = fullPath.includes('/plugins/') ? '/plugins/' : '/packages/'; } - let moduleDelimiter = fullPath.includes('/plugins/') ? '/plugins/' : '/packages/'; // for platform modules that are in a sustainable folder, strip the /private/ or /shared/ part too if (module.directory.includes(`${moduleDelimiter}private/`)) { @@ -60,7 +64,10 @@ export const calculateModuleTargetFolder = (module: Package): string => { let path: string; if (group === 'platform') { - if (fullPath.includes(`/${KIBANA_FOLDER}/packages/core/`)) { + if ( + fullPath.includes(`/${KIBANA_FOLDER}/packages/core/`) || + fullPath.includes(`/${KIBANA_FOLDER}/src/core/packages`) + ) { // packages/core/* => src/core/packages/* path = join(BASE_FOLDER, 'src', 'core', 'packages', moduleFolder); } else { @@ -91,24 +98,8 @@ export const calculateModuleTargetFolder = (module: Package): string => { return applyTransforms(module, path); }; -export const isInTargetFolder = (module: Package, log: ToolingLog): boolean => { - if (!module.group || module.group === 'common' || !module.visibility) { - log.warning(`The module '${module.id}' is missing the group/visibility information`); - return false; - } - - const baseTargetFolders = TARGET_FOLDERS[`${module.group}:${module.visibility}`]; - const baseTargetFolder = baseTargetFolders.find((candidate) => { - return module.directory.includes(candidate); - }); - if (baseTargetFolder) { - log.info( - `The module ${module.id} is already in the correct folder: '${baseTargetFolder}'. Skipping` - ); - return true; - } - - return false; +export const isInTargetFolder = (module: Package): boolean => { + return module.directory.startsWith(calculateModuleTargetFolder(module)); }; export const replaceReferences = async (module: Package, destination: string, log: ToolingLog) => { @@ -184,7 +175,7 @@ const replaceReferencesInternal = async ( const backFwdSrc = relativeSource.replaceAll('/', `\\\\\\/`); const backFwdDst = relativeDestination.replaceAll('/', `\\\\\\/`); await safeExec( - `sed -i '' -E '/${src}[\-_a-zA-Z0-9]/! s/${backFwdSrc}/${backFwdDst}/g' .buildkite/scripts/pipelines/pull_request/pipeline.ts`, + `sed -i '' -E '/${backFwdSrc}[\-_a-zA-Z0-9]/! s/${backFwdSrc}/${backFwdDst}/g' .buildkite/scripts/pipelines/pull_request/pipeline.ts`, false ); };