Skip to content

Commit 964d842

Browse files
committed
Fixes
1 parent 966f212 commit 964d842

File tree

9 files changed

+76
-16
lines changed

9 files changed

+76
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,4 @@ x-pack/test/security_solution_playwright/playwright/.cache/
160160
x-pack/test/security_solution_playwright/.auth/
161161
x-pack/test/security_solution_playwright/.env
162162
.codeql
163+
.dependency-graph-log.json

packages/kbn-dependency-usage/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A CLI tool for analyzing dependencies across packages and plugins. This tool pro
99
1. [Usage](#usage)
1010
- [Show all packages/plugins using a dependency](#show-all-packagesplugins-using-a-dependency)
1111
- [Show dependencies grouped by code owner](#show-dependencies-grouped-by-code-owner)
12-
- [List all dependencies for a package or directory](#list-all-dependencies-for-a-package-or-directory)
12+
- [List all dependencies for a package or directory](#list-all-dependencies-for-source-directory)
1313
- [Group by code owner with adjustable collapse depth](#group-by-code-owner-with-adjustable-collapse-depth)
1414
- [Show dependencies matching a pattern](#show-dependencies-matching-a-pattern)
1515
- [Verbose flag to debug dependency graph issues](#verbose-flag-to-debug-dependency-graph-issues)

packages/kbn-dependency-usage/src/cli.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ describe('dependency-usage CLI', () => {
6666
});
6767

6868
it('should use default values when optional arguments are not provided', () => {
69-
const argv = parser.parse(['--paths', './src']);
70-
expect(argv.paths).toEqual(['./src']);
69+
const argv = parser.parse([]);
70+
expect(argv.paths).toEqual(['.']);
7171
expect(argv['dependency-name']).toBeUndefined();
7272
expect(argv['collapse-depth']).toBe(1);
7373
expect(argv.verbose).toBe(false);
@@ -79,11 +79,6 @@ describe('dependency-usage CLI', () => {
7979
}).toThrow('Summary option can only be used when a dependency name is provided');
8080
});
8181

82-
it('should set default values for unspecified options', () => {
83-
const argv = parser.parse(['--paths', './src']);
84-
expect(argv['collapse-depth']).toBe(1);
85-
});
86-
8782
it('should validate collapse-depth as a positive integer', () => {
8883
expect(() => {
8984
parser.parse(['--paths', './src', '--collapse-depth', '0']);

packages/kbn-dependency-usage/src/cli.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ export const configureYargs = () => {
8585

8686
return true;
8787
})
88-
.demandOption(['paths'], 'Please provide at least one path to search within')
8988
.example(
9089
'--dependency-name lodash --paths ./src ./lib',
9190
chalk.blue(

packages/kbn-dependency-usage/src/dependency_graph/common/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const excludePaths = [
2121
'(^|/)target($|/)',
2222
'^kbn',
2323
'^@kbn',
24-
'@elastic',
2524
'^.buildkite',
2625
'^docs',
2726
'^dev_docs',

packages/kbn-dependency-usage/src/dependency_graph/providers/cruiser.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ const codeOwners: Record<string, string[]> = {
1919
'plugins/data_charts': ['team_visualization'],
2020
'plugins/analytics': ['team_analytics'],
2121
'plugins/notification': ['team_alerts', 'team_notifications'],
22+
'plugins/security_solution/public/entity_analytics/components': ['team_security_analytics'],
23+
'plugins/security_solution/public/entity_analytics/components/componentA.ts': [
24+
'team_security_analytics',
25+
],
26+
'plugins/security_solution/public/entity_analytics/components/componentB.ts': [
27+
'team_security_analytics',
28+
],
29+
'plugins/security_solution/server/lib/analytics/analytics.ts': ['team_security_analytics'],
30+
'plugins/security_solution/common/api/detection_engine': ['team_security_solution'],
2231
};
2332

2433
jest.mock('dependency-cruiser', () => ({
@@ -285,4 +294,61 @@ describe('identifyDependencyUsage', () => {
285294
},
286295
});
287296
});
297+
298+
it('should search for specific dependency and group by owner', async () => {
299+
const customCruiseResult = {
300+
output: {
301+
summary: {
302+
violations: [
303+
{
304+
from: 'plugins/security_solution/public/entity_analytics/components/componentA.ts',
305+
to: 'node_modules/lodash/fp.js',
306+
},
307+
{
308+
from: 'plugins/security_solution/public/entity_analytics/components/componentB.ts',
309+
to: 'node_modules/lodash/partition.js',
310+
},
311+
{
312+
from: 'plugins/security_solution/server/lib/analytics/analytics.ts',
313+
to: 'node_modules/lodash/partition.js',
314+
},
315+
{
316+
from: 'plugins/security_solution/server/lib/analytics/analytics.ts',
317+
to: 'node_modules/lodash/cloneDeep.js',
318+
},
319+
{
320+
from: 'plugins/security_solution/common/api/detection_engine',
321+
to: 'node_modules/lodash/sortBy.js',
322+
},
323+
],
324+
},
325+
modules: [],
326+
},
327+
};
328+
(cruise as jest.Mock).mockResolvedValue(customCruiseResult);
329+
330+
const result = await identifyDependencyUsage([], 'lodash', {
331+
groupBy: 'owner',
332+
collapseDepth: 3,
333+
summary: false,
334+
});
335+
336+
expect(cruise).toHaveBeenCalled();
337+
expect(result).toEqual({
338+
team_security_analytics: {
339+
modules: [
340+
'plugins/security_solution/public/entity_analytics/components/componentA.ts',
341+
'plugins/security_solution/public/entity_analytics/components/componentB.ts',
342+
'plugins/security_solution/server/lib/analytics/analytics.ts',
343+
],
344+
deps: ['lodash/fp.js', 'lodash/partition.js', 'lodash/cloneDeep.js'],
345+
teams: ['team_security_analytics'],
346+
},
347+
team_security_solution: {
348+
modules: ['plugins/security_solution/common/api/detection_engine'],
349+
deps: ['lodash/sortBy.js'],
350+
teams: ['team_security_solution'],
351+
},
352+
});
353+
});
288354
});

packages/kbn-dependency-usage/src/dependency_graph/providers/cruiser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export async function identifyDependencyUsageWithCruiser(
112112

113113
const { violations } = result.output.summary;
114114

115+
if (groupBy === 'owner') {
116+
return groupFilesByOwners(violations);
117+
}
118+
115119
if (dependencyName) {
116120
const dependencyRegex = new RegExp(`node_modules/${dependencyName}`);
117121

@@ -139,9 +143,5 @@ export async function identifyDependencyUsageWithCruiser(
139143
};
140144
}
141145

142-
if (groupBy === 'owner') {
143-
return groupFilesByOwners(violations);
144-
}
145-
146146
return groupBySource(violations);
147147
}

packages/kbn-dependency-usage/src/lib/group_by_owners.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function groupFilesByOwners(dependencies: Array<{ from: string; to: strin
2626
const { from, to } = dep;
2727

2828
const owners = getCodeOwnersForFile(from, reversedCodeowners) ?? [UNKNOWN_OWNER];
29-
const ownerKey = owners.length > 1 ? MULTIPLE_TEAMS_OWNER : owners.join(',');
29+
const ownerKey = owners.length > 1 ? MULTIPLE_TEAMS_OWNER : owners[0];
3030

3131
if (ownerKey === MULTIPLE_TEAMS_OWNER) {
3232
if (!ownerFilesMap.has(ownerKey)) {

scripts/dependency_usage.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# Need to tun the script with ts-node/esm since dependency-cruiser is only available as an ESM module.
4-
# We dpecify the correct tsconfig.json file to ensure compatibility, as our current setup doesn’t fully support ESM modules.
4+
# We specify the correct tsconfig.json file to ensure compatibility, as our current setup doesn’t fully support ESM modules.
55
# Should be resolved after https://github.com/elastic/kibana/issues/198790 is done.
66
NODE_NO_WARNINGS=1 TS_NODE_TRANSPILE_ONLY=true TS_NODE_PROJECT=packages/kbn-dependency-usage/tsconfig.json \
77
node --loader ts-node/esm packages/kbn-dependency-usage/src/cli.ts "$@"

0 commit comments

Comments
 (0)