Skip to content

Commit

Permalink
fix: fix wrong project name change
Browse files Browse the repository at this point in the history
  • Loading branch information
xzhou-snyk committed Sep 19, 2023
1 parent 5f04e90 commit 0c87ff8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
15 changes: 3 additions & 12 deletions src/cli/commands/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,7 @@ export default async function monitor(...args0: MethodArgs): Promise<any> {
const res: MonitorResult = await promiseOrCleanup(
snykMonitor(
path,
generateMonitorMeta(
options,
extractedPackageManager,
projectName,
),
generateMonitorMeta(options, extractedPackageManager),
projectDeps,
options,
projectDeps.plugin as PluginMetadata,
Expand Down Expand Up @@ -350,17 +346,12 @@ export default async function monitor(...args0: MethodArgs): Promise<any> {
throw new Error(output);
}

function generateMonitorMeta(
options,
packageManager?,
projectName?,
): MonitorMeta {
function generateMonitorMeta(options, packageManager?): MonitorMeta {
return {
method: 'cli',
packageManager,
'policy-path': options['policy-path'],
'project-name':
options['project-name'] || projectName || config.PROJECT_NAME,
'project-name': options['project-name'] || config.PROJECT_NAME,
isDocker: !!options.docker,
prune: !!options.pruneRepeatedSubdependencies,
'remote-repo-url': options['remote-repo-url'],
Expand Down
4 changes: 4 additions & 0 deletions src/lib/monitor/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export function getProjectName(
return scannedProject.meta.projectName;
}

if (scannedProject.meta?.gradleProjectName) {
return scannedProject.meta.gradleProjectName;
}

return meta['project-name'];
}

Expand Down
41 changes: 41 additions & 0 deletions test/jest/unit/cli-monitor-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,47 @@ describe('cli-monitor-utils test', () => {
expect(res).toEqual('project-name-override');
});

it('getProjectName returns gradle project name from scanned project meta', () => {
const scannedProject: ScannedProject = {
depGraph: {} as any,
meta: {
gradleProjectName: 'my-gradle-project',
},
targetFile: '/tmp/build.gradle',
};

const res = utils.getProjectName(scannedProject, {
method: 'cli',
packageManager: 'gradle',
'policy-path': '',
'project-name': '',
isDocker: false,
prune: false,
});
expect(res).toEqual('my-gradle-project');
});

it('getProjectName returns project name from scanned project meta when project-name is provided via option', () => {
const scannedProject: ScannedProject = {
depGraph: {} as any,
meta: {
gradleProjectName: 'my-gradle-project',
projectName: 'meta-gradle-project',
},
targetFile: '/tmp/build.gradle',
};

const res = utils.getProjectName(scannedProject, {
method: 'cli',
packageManager: 'gradle',
'policy-path': '',
'project-name': 'project-name-from-option',
isDocker: false,
prune: false,
});
expect(res).toEqual('meta-gradle-project');
});

it('getTargetFile returns name from scanned project if container', () => {
const scannedProject: ScannedProject = stubScannedProjectContainer();
const res = utils.getTargetFile(scannedProject, getStubPluginMeta());
Expand Down

0 comments on commit 0c87ff8

Please sign in to comment.