Skip to content

Commit

Permalink
refactor: target-builders minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anthogez committed Jun 15, 2020
1 parent e0b7f19 commit 1f96517
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/lib/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async function monitorDepTree(
}
const policy = await snyk.policy.load(policyLocations, { loose: true });

const target = await projectMetadata.getInfo(scannedProject, depTree, meta);
const target = await projectMetadata.getInfo(scannedProject, meta, depTree);

if (isGitTarget(target) && target.branch) {
analytics.add('targetBranch', target.branch);
Expand Down Expand Up @@ -318,7 +318,7 @@ export async function experimentalMonitorDepGraph(
}
const policy = await snyk.policy.load(policyLocations, { loose: true });

const target = await projectMetadata.getInfo(scannedProject, depTree, meta);
const target = await projectMetadata.getInfo(scannedProject, meta, depTree);

if (isGitTarget(target) && target.branch) {
analytics.add('targetBranch', target.branch);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/project-metadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ interface Options {
}
export async function getInfo(
scannedProject: ScannedProject,
packageInfo: DepTree,
options: Options,
packageInfo?: DepTree,
): Promise<GitTarget | ContainerTarget | null> {
const isFromContainer = options.docker || options.isDocker || false;
for (const builder of TARGET_BUILDERS) {
const target = await builder.getInfo(
isFromContainer,
scannedProject,
packageInfo,
isFromContainer,
);

if (target) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/project-metadata/target-builders/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { ContainerTarget } from '../types';
import { ScannedProject } from '@snyk/cli-interface/legacy/common';

export async function getInfo(
scannedProject: ScannedProject,
packageInfo: DepTree,
isFromContainer: boolean,
scannedProject: ScannedProject,
packageInfo?: DepTree,
): Promise<ContainerTarget | null> {
// safety check
if (!isFromContainer) {
Expand All @@ -16,6 +16,6 @@ export async function getInfo(
scannedProject.meta && scannedProject.meta.imageName;
return {
image:
imageNameOnProjectMeta || (packageInfo as any).image || packageInfo.name,
imageNameOnProjectMeta || (packageInfo as any)?.image || packageInfo?.name,
};
}
4 changes: 0 additions & 4 deletions src/lib/project-metadata/target-builders/git.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import * as url from 'url';
import subProcess = require('../../sub-process');
import { DepTree } from '../../types';
import { GitTarget } from '../types';
import { ScannedProject } from '@snyk/cli-interface/legacy/common';

// for scp-like syntax [user@]server:project.git
const originRegex = /(.+@)?(.+):(.+$)/;

export async function getInfo(
scannedProject: ScannedProject,
packageInfo: DepTree,
isFromContainer: boolean,
): Promise<GitTarget | null> {
// safety check
Expand Down
16 changes: 8 additions & 8 deletions test/get-remote-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test('getInfo returns null for isFromContainer=true', async (t) => {
const {
getInfo,
} = require('../src/lib/project-metadata/target-builders/git');
const gitInfo = await getInfo(null as any, null as any, true);
const gitInfo = await getInfo(true);
t.same(gitInfo, null);
});

Expand All @@ -22,7 +22,7 @@ test('getInfo handles provided https remote url as http', async (t) => {
},
},
);
const gitInfo = await getInfo(null as any, null as any, false);
const gitInfo = await getInfo(false);
t.same(gitInfo.remoteUrl, 'http://myserver.local/myproject.git');
});

Expand All @@ -38,7 +38,7 @@ test('getInfo handles provided http remote url', async (t) => {
},
},
);
const gitInfo = await getInfo(null as any, null as any, false);
const gitInfo = await getInfo(false);
t.same(gitInfo.remoteUrl, providedUrl);
});

Expand All @@ -54,7 +54,7 @@ test('getInfo handles provided ssh remote url as http', async (t) => {
},
},
);
const gitInfo = await getInfo(null as any, null as any, false);
const gitInfo = await getInfo(false);
t.same(gitInfo.remoteUrl, 'http://myserver.local/myproject.git');
});

Expand All @@ -70,7 +70,7 @@ test('getInfo handles provided scp-like syntax with user in remote url', async (
},
},
);
const gitInfo = await getInfo(null as any, null as any, false);
const gitInfo = await getInfo(false);
t.same(gitInfo.remoteUrl, 'http://myserver.local/myproject.git');
});

Expand All @@ -86,7 +86,7 @@ test('getInfo handles provided scp-like syntax without user in remote url', asyn
},
},
);
const gitInfo = await getInfo(null as any, null as any, false);
const gitInfo = await getInfo(false);
t.same(gitInfo.remoteUrl, 'http://myserver.local/folder/myproject.git');
});

Expand All @@ -102,7 +102,7 @@ test('getInfo handles invalid URL by keeping it as is', async (t) => {
},
},
);
const gitInfo = await getInfo(null as any, null as any, false);
const gitInfo = await getInfo(false);
t.same(gitInfo.remoteUrl, providedUrl);
});

Expand All @@ -118,6 +118,6 @@ test('getInfo handles undefined by returning undefined', async (t) => {
},
},
);
const gitInfo = await getInfo(null as any, null as any, false);
const gitInfo = await getInfo(false);
t.same(gitInfo.remoteUrl, undefined);
});

0 comments on commit 1f96517

Please sign in to comment.