Skip to content

Commit

Permalink
feat: add --remote-repo-url flag
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreDalcher committed Aug 15, 2019
1 parent fe47b46 commit c626be5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions help/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Options:
Prune dependency trees, removing duplicate sub-dependencies.
Will still find all vulnerabilities, but potentially not all
of the vulnerable paths.
--remote-repo-url=<string>
(monitor command only)
Set or override the remote URL for the repository that you would like to monitor.

Gradle options:
--sub-project=<string> (alias: --gradle-sub-project)
Expand Down
5 changes: 5 additions & 0 deletions src/cli/commands/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ async function monitor(...args0: MethodArgs): Promise<any> {
throw new Error('`--all-sub-projects` is currently not compatible with `--project-name`');
}

if (options.docker && options['remote-repo-url']) {
throw new Error('`--remote-repo-url` is not supported for container scans');
}

apiTokenExists();

if (options['experimental-dep-graph']) {
Expand Down Expand Up @@ -148,6 +152,7 @@ async function monitor(...args0: MethodArgs): Promise<any> {
'isDocker': !!options.docker,
'prune': !!options['prune-repeated-subdependencies'],
'experimental-dep-graph': !!options['experimental-dep-graph'],
'remote-repo-url': options['remote-repo-url'],
};

// We send results from "all-sub-projects" scanning as different Monitor objects
Expand Down
15 changes: 13 additions & 2 deletions src/lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {MonitorError, ConnectionTimeoutError} from './errors';
import { countPathsToGraphRoot, pruneGraph } from './prune';
import { GRAPH_SUPPORTED_PACKAGE_MANAGERS } from './package-managers';
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
import { GitTarget } from './project-metadata/types';

const debug = Debug('snyk');

Expand Down Expand Up @@ -160,7 +161,7 @@ export async function monitor(
}
policy = await snyk.policy.load(policyLocations, {loose: true});

const target = await projectMetadata.getInfo(pkg);
const target = await getTarget(pkg, meta);
const targetFileRelativePath = targetFile ? path.relative(root, targetFile) : '';

if (target && target.branch) {
Expand Down Expand Up @@ -258,7 +259,7 @@ export async function monitorGraph(
}
policy = await snyk.policy.load(policyLocations, {loose: true});

const target = await projectMetadata.getInfo(pkg);
const target = await getTarget(pkg, meta);
const targetFileRelativePath = targetFile ? path.relative(root, targetFile) : '';

if (target && target.branch) {
Expand Down Expand Up @@ -351,3 +352,13 @@ function pluckPolicies(pkg) {
return pluckPolicies(pkg.dependencies[name]);
}).filter(Boolean));
}

async function getTarget(pkg: DepTree, meta: MonitorMeta): Promise<GitTarget | null> {
const target = await projectMetadata.getInfo(pkg);

// Override the remoteUrl if the --remote-repo-url flag was set
if (meta['remote-repo-url']) {
return { ...target, remoteUrl: meta['remote-repo-url'] };
}
return target;
}
2 changes: 1 addition & 1 deletion src/lib/project-metadata/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface GitTarget {
remoteUrl: string;
branch: string;
branch?: string;
}
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface MonitorMeta {
isDocker: boolean;
prune: boolean;
'experimental-dep-graph'?: boolean;
'remote-repo-url'?: string;
}

export interface MonitorResult {
Expand Down
9 changes: 9 additions & 0 deletions test/acceptance/cli.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2485,6 +2485,15 @@ test('`monitor npm-package with custom --project-name`', async (t) => {
t.equal(req.body.meta.projectName, 'custom-project-name');
});

test('`monitor npm-package with custom --remote-repo-url`', async (t) => {
chdirWorkspaces();
await cli.monitor('npm-package', {
'remote-repo-url': 'a-fake-remote',
});
const req = server.popRequest();
t.equal(req.body.target.remoteUrl, 'a-fake-remote');
});

test('`monitor npm-package with dev dep flag`', async (t) => {
chdirWorkspaces();
await cli.monitor('npm-package', { dev: true });
Expand Down

0 comments on commit c626be5

Please sign in to comment.