Skip to content

Commit

Permalink
feat: initial gradle graph support
Browse files Browse the repository at this point in the history
  • Loading branch information
anthogez committed Jun 15, 2020
1 parent 1f96517 commit 4fc47a3
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/lib/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ export async function monitor(
analytics.add('isDocker', !!meta.isDocker);

if (GRAPH_SUPPORTED_PACKAGE_MANAGERS.includes(packageManager)) {
const isGradlePkgManager: boolean = packageManager === 'gradle';

// TODO @boost: delete this condition once 'experimental-dep-graph' ff is deleted
if (isGradlePkgManager) {
return await monitorDepGraph(
root,
meta,
scannedProject,
pluginMeta,
targetFileRelativePath,
contributors,
);
}

// TODO @boost: remove the code below once 'experimental-dep-graph' is deleted
const monitorGraphSupportedRes = await isFeatureFlagSupportedForOrg(
_.camelCase('experimental-dep-graph'),
Expand Down Expand Up @@ -274,6 +288,102 @@ async function monitorDepTree(
});
}

export async function monitorDepGraph(
root: string,
meta: MonitorMeta,
scannedProject: ScannedProject,
pluginMeta: PluginMetadata,
targetFileRelativePath?: string,
contributors?: { userId: string; lastCommitDate: string }[],
): Promise<MonitorResult> {
const packageManager = meta.packageManager;
analytics.add('monitorDepGraph', true);

let depGraph = scannedProject.depGraph;

//TODO @boost: create a customer error msg new InvalidDepGraph()???
if (!depGraph) {
throw new Error('Invalid DepGraph');
}

const policyPath = meta['policy-path'] || root;
const policyLocations = [policyPath]
.concat(pluckPolicies(depGraph))
.filter(Boolean);

if (!policyLocations.length) {
await snyk.policy.create();
}

const policy = await snyk.policy.load(policyLocations, { loose: true });
const target = await projectMetadata.getInfo(scannedProject, meta);
if (isGitTarget(target) && target.branch) {
analytics.add('targetBranch', target.branch);
}

return new Promise((resolve, reject) => {
if (!depGraph) {
//TODO @boost: create a customer error msg new InvalidDepGraph()???
return reject(new Error('Invalid DepGraph'));
}
request(
{
body: {
meta: {
method: meta.method,
hostname: os.hostname(),
id: snyk.id || depGraph.rootPkg.name,
ci: isCI(),
pid: process.pid,
node: process.version,
master: snyk.config.isMaster,
name: getNameDepGraph(scannedProject, depGraph, meta),
version: depGraph.rootPkg.version,
org: config.org ? decodeURIComponent(config.org) : undefined,
pluginName: pluginMeta.name,
pluginRuntime: pluginMeta.runtime,
projectName: getProjectName(scannedProject, meta),
monitorGraph: true,
},
policy: policy ? policy.toString() : undefined,
depGraphJSON: depGraph, // depGraph will be auto serialized to JSON on send
// we take the targetFile from the plugin,
// because we want to send it only for specific package-managers
target,
targetFile: getTargetFile(scannedProject, pluginMeta),
targetFileRelativePath,
contributors: contributors,
} as MonitorBody,
gzip: true,
method: 'PUT',
headers: {
authorization: 'token ' + snyk.api,
'content-encoding': 'gzip',
},
url: `${config.API}/monitor/${packageManager}/graph`,
json: true,
},
(error, res, body) => {
if (error) {
return reject(error);
}
if (res.statusCode >= 200 && res.statusCode <= 299) {
resolve(body as MonitorResult);
} else {
let err;
const userMessage = body && body.userMessage;
if (!userMessage && res.statusCode === 504) {
err = new ConnectionTimeoutError();
} else {
err = new MonitorError(res.statusCode, userMessage);
}
reject(err);
}
},
);
});
}

// @deprecated: it will be deleted once experimentalDepGraph FF will be deleted
// and npm, yarn, sbt and rubygems usage of `experimentalMonitorDepGraph`
// will be replaced with `monitorDepGraph` method
Expand Down
1 change: 1 addition & 0 deletions src/lib/package-managers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const GRAPH_SUPPORTED_PACKAGE_MANAGERS: SupportedPackageManagers[] = [
'npm',
'sbt',
'yarn',
'gradle',
'rubygems',
];
// For ecosystems with a flat set of libraries (e.g. Python, JVM), one can
Expand Down

0 comments on commit 4fc47a3

Please sign in to comment.