Skip to content

Commit 55020aa

Browse files
add telemetry to npmV0 task (#15370)
* add telemetry to npmV0 task; bump version
1 parent 9be3ef5 commit 55020aa

File tree

4 files changed

+46
-738
lines changed

4 files changed

+46
-738
lines changed

Tasks/NpmV0/npmtask.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import trm = require('azure-pipelines-task-lib/toolrunner');
66
var extend = require('util')._extend;
77
import * as pkgLocationUtils from "azure-pipelines-tasks-packaging-common/locationUtilities";
88
import { logError } from 'azure-pipelines-tasks-packaging-common/util';
9+
import {IExecSyncResult} from 'azure-pipelines-task-lib/toolrunner';
10+
import * as telemetry from 'azure-pipelines-tasks-utility-common/telemetry';
911

1012
interface EnvironmentDictionary { [key: string]: string; }
1113

@@ -26,11 +28,13 @@ async function executeTask() {
2628

2729
var npmRunner = tl.tool(tl.which('npm', true));
2830
npmRunner.arg(command);
29-
npmRunner.line(tl.getInput('arguments', false));
30-
31+
var args = tl.getInput('arguments', false);
32+
npmRunner.line(args);
3133

34+
3235
if(shouldUseDeprecatedTask()) {
33-
36+
37+
await _logNpmStartupVariables(command, args);
3438
// deprecated version of task, which just runs the npm command with NO auth support.
3539
try{
3640
var code : number = await npmRunner.exec();
@@ -40,33 +44,34 @@ async function executeTask() {
4044
tl.setResult(tl.TaskResult.Failed, tl.loc('NpmFailed', err.message));
4145
}
4246
} else {
43-
4447
// new task version with auth support
4548
try{
46-
49+
4750
var npmrcPath: string = path.join(cwd, '.npmrc');
4851
var tempNpmrcPath : string = getTempNpmrcPath();
49-
52+
5053
var debugLog: boolean = tl.getVariable('system.debug') && tl.getVariable('system.debug').toLowerCase() === 'true';
51-
54+
5255
var shouldRunAuthHelper: boolean = tl.osType().toLowerCase() === 'windows_nt' && tl.exist(npmrcPath);
5356
if(shouldRunAuthHelper) {
5457
copyUserNpmrc(tempNpmrcPath);
5558
await runNpmAuthHelperAsync(getNpmAuthHelperRunner(npmrcPath, tempNpmrcPath, debugLog));
5659
}
57-
60+
5861
// set required environment variables for npm execution
5962
var npmExecOptions = <trm.IExecOptions>{
6063
env: extend({}, process.env)
6164
};
62-
65+
6366
if(shouldRunAuthHelper){
6467
npmExecOptions.env['npm_config_userconfig'] = tempNpmrcPath;
6568
}
66-
69+
6770
if(debugLog) {
6871
npmExecOptions.env['npm_config_loglevel'] = 'verbose';
6972
}
73+
74+
await _logNpmStartupVariables(command, args, npmrcPath, debugLog, shouldRunAuthHelper);
7075

7176
await tryRunNpmConfigAsync(getNpmConfigRunner(debugLog), npmExecOptions);
7277
var code : number = await runNpmCommandAsync(npmRunner, npmExecOptions);
@@ -224,3 +229,31 @@ async function addBuildCredProviderEnv(env: EnvironmentDictionary) : Promise<Env
224229
env['VSS_DISABLE_DEFAULTCREDENTIALPROVIDER'] = '1';
225230
return env;
226231
}
232+
233+
async function _logNpmStartupVariables(command: string, args?: string, npmrcPath?: string, debugLog?: boolean, shouldRunAuthHelper?: boolean) {
234+
try {
235+
// Log the NPM version
236+
let version: string;
237+
try {
238+
const syncResult: IExecSyncResult = tl.execSync('npm', '--version');
239+
if (syncResult.stdout) {
240+
version = syncResult.stdout.trim();
241+
}
242+
} catch (err) {
243+
tl.debug(`Unable to get NPM config info. Err:( ${err} )`);
244+
}
245+
246+
const npmTelem = {
247+
'command': command,
248+
'arguments': args || null,
249+
'USE_DEPRECATED_TASK_VERSION': tl.getVariable('USE_DEPRECATED_TASK_VERSION'),
250+
'npmrcPath': npmrcPath || null,
251+
'debugLog': debugLog || null,
252+
'npmVersion': version,
253+
};
254+
255+
telemetry.emitTelemetry('Packaging', 'npm', npmTelem);
256+
} catch (err) {
257+
tl.debug(`Unable to log NPM task telemetry. Err:( ${err} )`);
258+
}
259+
}

0 commit comments

Comments
 (0)