Skip to content

Commit

Permalink
feat(analytics): track integration environment
Browse files Browse the repository at this point in the history
This will help us capture if CLI is running e.g. in a specific IDE version
  • Loading branch information
JackuB committed Oct 19, 2020
1 parent ac87ce0 commit a63850d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
23 changes: 19 additions & 4 deletions src/lib/analytics-sources.ts
Expand Up @@ -13,8 +13,11 @@ import * as fs from 'fs';
import { ArgsOptions } from '../cli/args';
import { join } from 'path';

export const INTEGRATION_NAME_HEADER = 'SNYK_INTEGRATION_NAME';
export const INTEGRATION_VERSION_HEADER = 'SNYK_INTEGRATION_VERSION';
export const INTEGRATION_NAME_ENVVAR = 'SNYK_INTEGRATION_NAME';
export const INTEGRATION_VERSION_ENVVAR = 'SNYK_INTEGRATION_VERSION';
export const INTEGRATION_ENVIRONMENT_ENVVAR = 'SNYK_INTEGRATION_ENVIRONMENT';
export const INTEGRATION_ENVIRONMENT_VERSION_ENVVAR =
'SNYK_INTEGRATION_ENVIRONMENT_VERSION';

enum TrackedIntegration {
// Distribution builds/packages
Expand Down Expand Up @@ -56,7 +59,7 @@ export const getIntegrationName = (args: ArgsOptions[]): string => {

const integrationName = String(
args[0]?.integrationName || // Integration details passed through CLI flag
process.env[INTEGRATION_NAME_HEADER] ||
process.env[INTEGRATION_NAME_ENVVAR] ||
maybeHomebrew ||
maybeScoop ||
'',
Expand All @@ -72,13 +75,25 @@ export const getIntegrationVersion = (args: ArgsOptions[]): string => {
// Integration details passed through CLI flag
const integrationVersion = String(
args[0]?.integrationVersion ||
process.env[INTEGRATION_VERSION_HEADER] ||
process.env[INTEGRATION_VERSION_ENVVAR] ||
'',
);

return integrationVersion;
};

export const getIntegrationEnvironment = (args: ArgsOptions[]): string | void =>
(args[0]?.integrationEnvironment as string) ||
process.env[INTEGRATION_ENVIRONMENT_ENVVAR] ||
undefined;

export const getIntegrationEnvironmentVersion = (
args: ArgsOptions[],
): string | void =>
(args[0]?.integrationEnvironmentVersion as string) ||
process.env[INTEGRATION_ENVIRONMENT_VERSION_ENVVAR] ||
undefined;

export function isScoop(): boolean {
const currentProcessPath = process.execPath;
const looksLikeScoop =
Expand Down
6 changes: 6 additions & 0 deletions src/lib/analytics.js
Expand Up @@ -8,6 +8,8 @@ const request = require('./request');
const {
getIntegrationName,
getIntegrationVersion,
getIntegrationEnvironment,
getIntegrationEnvironmentVersion,
} = require('./analytics-sources');
const isCI = require('./is-ci').isCI;
const debug = require('debug')('snyk');
Expand Down Expand Up @@ -67,6 +69,10 @@ function postAnalytics(data) {
data.standalone = isStandalone;
data.integrationName = getIntegrationName(data.args);
data.integrationVersion = getIntegrationVersion(data.args);
data.integrationEnvironment = getIntegrationEnvironment(data.args);
data.integrationEnvironmentVersion = getIntegrationEnvironmentVersion(
data.args,
);

const seed = uuid.v4();
const shasum = crypto.createHash('sha1');
Expand Down
60 changes: 52 additions & 8 deletions test/analytics-sources.spec.ts
@@ -1,8 +1,12 @@
import {
getIntegrationName,
getIntegrationVersion,
INTEGRATION_NAME_HEADER,
INTEGRATION_VERSION_HEADER,
getIntegrationEnvironment,
getIntegrationEnvironmentVersion,
INTEGRATION_NAME_ENVVAR,
INTEGRATION_VERSION_ENVVAR,
INTEGRATION_ENVIRONMENT_ENVVAR,
INTEGRATION_ENVIRONMENT_VERSION_ENVVAR,
isScoop,
isHomebrew,
validateHomebrew,
Expand All @@ -18,8 +22,10 @@ const defaultArgsParams = {
};

beforeEach(() => {
delete process.env[INTEGRATION_NAME_HEADER];
delete process.env[INTEGRATION_VERSION_HEADER];
delete process.env[INTEGRATION_NAME_ENVVAR];
delete process.env[INTEGRATION_VERSION_ENVVAR];
delete process.env[INTEGRATION_ENVIRONMENT_ENVVAR];
delete process.env[INTEGRATION_ENVIRONMENT_VERSION_ENVVAR];
});

describe('analytics-sources - scoop detection', () => {
Expand Down Expand Up @@ -99,15 +105,15 @@ describe('analytics-sources - getIntegrationName', () => {
});

it('integration name is loaded from envvar', () => {
process.env[INTEGRATION_NAME_HEADER] = 'NPM';
process.env[INTEGRATION_NAME_ENVVAR] = 'NPM';
expect(getIntegrationName(emptyArgs)).toBe('NPM');

process.env[INTEGRATION_NAME_HEADER] = 'STANDALONE';
process.env[INTEGRATION_NAME_ENVVAR] = 'STANDALONE';
expect(getIntegrationName(emptyArgs)).toBe('STANDALONE');
});

it('integration name is empty when envvar is not recognized', () => {
process.env[INTEGRATION_NAME_HEADER] = 'INVALID';
process.env[INTEGRATION_NAME_ENVVAR] = 'INVALID';
expect(getIntegrationName(emptyArgs)).toBe('');
});

Expand Down Expand Up @@ -150,7 +156,7 @@ describe('analytics-sources - getIntegrationVersion', () => {
});

it('integration version is loaded from envvar', () => {
process.env[INTEGRATION_VERSION_HEADER] = '1.2.3';
process.env[INTEGRATION_VERSION_ENVVAR] = '1.2.3';
expect(getIntegrationVersion(emptyArgs)).toBe('1.2.3');
});

Expand All @@ -162,3 +168,41 @@ describe('analytics-sources - getIntegrationVersion', () => {
).toBe('1.2.3-Crystal');
});
});

describe('analytics-sources - getIntegrationEnvironment', () => {
it('integration environment is empty by default', () => {
expect(getIntegrationEnvironment(emptyArgs)).toBe(undefined);
});

it('integration environment is loaded from envvar', () => {
process.env[INTEGRATION_ENVIRONMENT_ENVVAR] = 'WebStorm';
expect(getIntegrationEnvironment(emptyArgs)).toBe('WebStorm');
});

it('integration environment is loaded from CLI flag', () => {
expect(
getIntegrationEnvironment([
{ integrationEnvironment: 'PhpStorm', ...defaultArgsParams },
]),
).toBe('PhpStorm');
});
});

describe('analytics-sources - getIntegrationEnvironment', () => {
it('integration environment version is empty by default', () => {
expect(getIntegrationEnvironmentVersion(emptyArgs)).toBe(undefined);
});

it('integration environment version is loaded from envvar', () => {
process.env[INTEGRATION_ENVIRONMENT_VERSION_ENVVAR] = '2020.2';
expect(getIntegrationEnvironmentVersion(emptyArgs)).toBe('2020.2');
});

it('integration environment version is loaded from CLI flag', () => {
expect(
getIntegrationEnvironmentVersion([
{ integrationEnvironmentVersion: '7.0.0', ...defaultArgsParams },
]),
).toBe('7.0.0');
});
});

0 comments on commit a63850d

Please sign in to comment.