From 57dc7189eb6c353041b8526af3fe939b0526d996 Mon Sep 17 00:00:00 2001 From: 37IulianPopovici <136596702+37IulianPopovici@users.noreply.github.com> Date: Mon, 10 Jun 2024 18:11:55 +0300 Subject: [PATCH] fix: proj name when using assets-project-name flag (#5077) --- src/cli/commands/monitor/index.ts | 1 + src/lib/monitor/utils.ts | 4 ++ src/lib/types.ts | 1 + test/jest/unit/cli-monitor-utils.spec.ts | 58 ++++++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/src/cli/commands/monitor/index.ts b/src/cli/commands/monitor/index.ts index 1b519aee3a..6e16b3cc7e 100644 --- a/src/cli/commands/monitor/index.ts +++ b/src/cli/commands/monitor/index.ts @@ -371,6 +371,7 @@ function generateMonitorMeta(options, packageManager?): MonitorMeta { prune: !!options.pruneRepeatedSubdependencies, 'remote-repo-url': options['remote-repo-url'], targetReference: options['target-reference'], + assetsProjectName: options['assets-project-name'], }; } diff --git a/src/lib/monitor/utils.ts b/src/lib/monitor/utils.ts index 831b3207af..0041ec7110 100644 --- a/src/lib/monitor/utils.ts +++ b/src/lib/monitor/utils.ts @@ -47,6 +47,10 @@ export function getProjectName( return scannedProject.meta.gradleProjectName; } + if (meta.assetsProjectName && !meta['project-name']) { + return scannedProject.depTree?.name; + } + return meta['project-name']; } diff --git a/src/lib/types.ts b/src/lib/types.ts index f8d869633a..6e40ca59a3 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -157,6 +157,7 @@ export interface MonitorMeta { prune: boolean; 'remote-repo-url'?: string; targetReference?: string; + assetsProjectName?: boolean; } export interface Tag { diff --git a/test/jest/unit/cli-monitor-utils.spec.ts b/test/jest/unit/cli-monitor-utils.spec.ts index d53e9c70f6..2516f9de04 100644 --- a/test/jest/unit/cli-monitor-utils.spec.ts +++ b/test/jest/unit/cli-monitor-utils.spec.ts @@ -114,6 +114,64 @@ describe('cli-monitor-utils test', () => { expect(res).toEqual('project-name-override'); }); + it('getProjectName returns nuget project name from scanned project meta when --assets-project-name is provided via options', () => { + const scannedProject: ScannedProject = { + depTree: { + dependencies: { + 'Microsoft.Extensions.FileProviders.Embedded': { + name: 'Microsoft.Extensions.FileProviders.Embedded', + version: '6.0.22', + }, + }, + name: 'nuget-project-assets-name', + packageFormatVersion: 'nuget:0.0.0', + version: '0.0.1', + targetFile: 'project.assets.json', + }, + targetFile: 'project.assets.json', + }; + + const res = utils.getProjectName(scannedProject, { + method: 'cli', + packageManager: 'nuget', + 'policy-path': '', + 'project-name': '', + isDocker: false, + prune: false, + assetsProjectName: true, + }); + expect(res).toEqual('nuget-project-assets-name'); + }); + + it('getProjectName overrides --assets-project-name with value from --project-name flag', () => { + const scannedProject: ScannedProject = { + depTree: { + dependencies: { + 'Microsoft.Extensions.FileProviders.Embedded': { + name: 'Microsoft.Extensions.FileProviders.Embedded', + version: '6.0.22', + }, + }, + name: 'nuget-project-assets-name', + packageFormatVersion: 'nuget:0.0.0', + version: '0.0.1', + targetFile: 'project.assets.json', + }, + targetFile: 'project.assets.json', + }; + + const res = utils.getProjectName(scannedProject, { + method: 'cli', + packageManager: 'nuget', + 'policy-path': '', + 'project-name': 'project-name-from-option', + isDocker: false, + prune: false, + assetsProjectName: true, + }); + expect(res).toEqual('project-name-from-option'); + }); + it('getProjectName returns gradle project name from scanned project meta', () => { const scannedProject: ScannedProject = { depGraph: {} as any,