Skip to content

Commit

Permalink
Merge pull request #888 from snyk/feat/analytics-include-org
Browse files Browse the repository at this point in the history
feat: analytics include org
  • Loading branch information
ivanstanev committed Dec 2, 2019
2 parents 8609cdf + 5e960a8 commit d69029c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 8 deletions.
5 changes: 0 additions & 5 deletions src/cli/commands/test/formatters/legacy-format-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ export function formatIssues(
}),
).join(', ');

let version;
if (vuln.metadata.packageManager.toLowerCase() === 'upstream') {
version = vuln.metadata.version;
}

const vulnOutput = {
issueHeading: createSeverityBasedIssueHeading(
vuln.metadata.severity,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chalk from 'chalk';
import * as wrap from 'wrap-ansi';
import * as config from '../../../../lib/config';
import { TestOptions } from '../../../../lib/types';
import {
Expand Down
2 changes: 2 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async function runCommand(args: Args) {
const res = analytics({
args: args.options._,
command: args.command,
org: args.options.org,
});

if (result && !args.options.quiet) {
Expand Down Expand Up @@ -106,6 +107,7 @@ async function handleError(args, error) {
const res = analytics({
args: args.options._,
command,
org: args.options.org,
});

return { res, exitCode };
Expand Down
11 changes: 11 additions & 0 deletions src/lib/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,23 @@ function postAnalytics(data) {
data.ci = isCI();
data.durationMs = Date.now() - startTime;

const queryStringParams = {};
if (data.org) {
queryStringParams.org = data.org;
}

debug('analytics', data);

const queryString =
Object.keys(queryStringParams).length > 0
? queryStringParams
: undefined;

return request({
body: {
data: data,
},
qs: queryString,
url: config.API + '/analytics/cli',
json: true,
method: 'post',
Expand Down
1 change: 0 additions & 1 deletion src/lib/project-metadata/target-builders/git.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs = require('fs');
import gitUrlParse = require('git-url-parse');

import subProcess = require('../../sub-process');
Expand Down
2 changes: 1 addition & 1 deletion src/lib/request/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http';
import { OutgoingHttpHeaders } from 'http';
import { NeedleHttpVerbs } from 'needle';

export interface Payload {
Expand Down
77 changes: 77 additions & 0 deletions test/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,83 @@ test('analytics', (t) => {
].sort(),
'keys as expected',
);

const queryString = spy.lastCall.args[0].qs;
t.deepEqual(queryString, undefined, 'query string is empty');
});
});

test('analytics with args', (t) => {
const spy = sinon.spy();
const analytics = proxyquire('../src/lib/analytics', {
'./request': spy,
});

analytics.add('foo', 'bar');

return analytics({
command: '__test__',
args: [],
}).then(() => {
const body = spy.lastCall.args[0].body.data;
t.deepEqual(
Object.keys(body).sort(),
[
'command',
'os',
'version',
'id',
'ci',
'metadata',
'args',
'nodeVersion',
'durationMs',
].sort(),
'keys as expected',
);

const queryString = spy.lastCall.args[0].qs;
t.deepEqual(queryString, undefined, 'query string is empty');
});
});

test('analytics with args and org', (t) => {
const spy = sinon.spy();
const analytics = proxyquire('../src/lib/analytics', {
'./request': spy,
});

analytics.add('foo', 'bar');

return analytics({
command: '__test__',
args: [],
org: 'snyk',
}).then(() => {
const body = spy.lastCall.args[0].body.data;
t.deepEqual(
Object.keys(body).sort(),
[
'command',
'os',
'version',
'id',
'ci',
'metadata',
'args',
'nodeVersion',
'durationMs',
'org',
].sort(),
'keys as expected',
);

const queryString = spy.lastCall.args[0].qs;
t.deepEqual(
queryString,
{ org: 'snyk' },
'query string has the expected values',
);
});
});

Expand Down

0 comments on commit d69029c

Please sign in to comment.