Skip to content

Commit

Permalink
SKA: Relocate script v6 (elastic#204929)
Browse files Browse the repository at this point in the history
## 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)

<img width="724" alt="image"
src="https://github.com/user-attachments/assets/9c53665b-e870-4b0d-894a-dd31c004b2f1"
/>
  • Loading branch information
gsoldevila authored Dec 19, 2024
1 parent 3875d26 commit 49f7a8d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
8 changes: 6 additions & 2 deletions packages/kbn-relocate/relocate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ const moveModule = async (module: Package, log: ToolingLog) => {

const relocateModules = async (toMove: Package[], log: ToolingLog): Promise<number> => {
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];

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion packages/kbn-relocate/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};

Expand Down
32 changes: 27 additions & 5 deletions packages/kbn-relocate/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 = (
Expand Down
19 changes: 15 additions & 4 deletions packages/kbn-relocate/utils/relocate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`];
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 49f7a8d

Please sign in to comment.