Skip to content

Commit

Permalink
Misc enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoldevila committed Jan 7, 2025
1 parent ae6d046 commit 4d05f83
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 46 deletions.
72 changes: 48 additions & 24 deletions packages/kbn-relocate/relocate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
})
);
};

Expand Down Expand Up @@ -159,42 +170,34 @@ 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);

if (getManualCommits(pr.commits).length > 0) {
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 .`);
Expand Down Expand Up @@ -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;
}
Expand Down
35 changes: 13 additions & 22 deletions packages/kbn-relocate/utils/relocate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
KIBANA_FOLDER,
NO_GREP,
SCRIPT_ERRORS,
TARGET_FOLDERS,
UPDATED_REFERENCES,
UPDATED_RELATIVE_PATHS,
} from '../constants';
Expand All @@ -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/`)) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
);
};
Expand Down

0 comments on commit 4d05f83

Please sign in to comment.