55'use strict' ;
66import * as path from 'path' ;
77import * as fs from 'fs' ;
8+ import * as uuid from 'uuid' ;
9+ import * as crypto from "crypto" ;
10+
811import {
9- IPCMessageReader , IPCMessageWriter , createConnection , IConnection ,
10- TextDocuments , InitializeResult , CodeLens , CodeAction , CodeActionKind } from 'vscode-languageserver' ;
12+ IPCMessageReader , IPCMessageWriter , createConnection , IConnection ,
13+ TextDocuments , InitializeResult , CodeLens , CodeAction , CodeActionKind } from 'vscode-languageserver' ;
1114import fetch from 'node-fetch' ;
1215import url from 'url' ;
1316
@@ -171,7 +174,7 @@ const getCAmsg = (deps, diagnostics, totalCount): string => {
171174const caDefaultMsg = 'Checking for security vulnerabilities ...' ;
172175
173176/* Fetch Vulnerabilities by component-analysis batch api-call */
174- const fetchVulnerabilities = async ( reqData ) => {
177+ const fetchVulnerabilities = async ( reqData : any , manifestHash : string , requestId : string ) => {
175178 let url = config . server_url ;
176179 if ( config . three_scale_user_token ) {
177180 url += `/component-analyses/?user_key=${ config . three_scale_user_token } ` ;
@@ -181,10 +184,23 @@ const fetchVulnerabilities = async (reqData) => {
181184 const headers = {
182185 'Content-Type' : 'application/json' ,
183186 'Authorization' : 'Bearer ' + config . api_token ,
187+ 'X-Request-Id' : requestId ,
184188 } ;
189+
190+ url += `&utm_content=${ manifestHash } ` ;
191+
192+ if ( config . utm_source ) {
193+ url += `&utm_source=${ config . utm_source } ` ;
194+ }
195+
185196 if ( config . uuid ) {
186197 headers [ 'uuid' ] = config . uuid ;
187198 }
199+
200+ if ( config . telemetry_id ) {
201+ headers [ 'X-Telemetry-Id' ] = config . telemetry_id ;
202+ }
203+
188204 try {
189205 const response = await fetch ( url , {
190206 method : 'post' ,
@@ -271,6 +287,8 @@ const sendDiagnostics = async (ecosystem: string, diagnosticFilePath: string, co
271287 const diagnostics = [ ] ;
272288 const totalCount = new TotalCount ( ) ;
273289 const start = new Date ( ) . getTime ( ) ;
290+ const manifestHash = crypto . createHash ( "sha256" ) . update ( diagnosticFilePath ) . digest ( "hex" ) ;
291+ const requestId = uuid . v4 ( ) ;
274292 // Closure which captures common arg to runPipeline.
275293 const pipeline = response => runPipeline ( response , diagnostics , packageAggregator , diagnosticFilePath , pkgMap , totalCount ) ;
276294 // Get and fire diagnostics for items found in Cache.
@@ -289,7 +307,7 @@ const sendDiagnostics = async (ecosystem: string, diagnosticFilePath: string, co
289307 pipeline ( response ) ;
290308 }
291309 const allRequests = slicePayload ( requestPayload , batchSize , ecosystem ) .
292- map ( request => fetchVulnerabilities ( request ) . then ( cacheAndRunPipeline ) ) ;
310+ map ( request => fetchVulnerabilities ( request , manifestHash , requestId ) . then ( cacheAndRunPipeline ) ) ;
293311
294312 await Promise . allSettled ( allRequests ) ;
295313 const end = new Date ( ) . getTime ( ) ;
@@ -341,7 +359,7 @@ connection.onCodeAction((params, token): CodeAction[] => {
341359 let codeAction = codeActionsMap [ diagnostic . range . start . line + "|" + diagnostic . range . start . character ] ;
342360 if ( codeAction != null ) {
343361 codeActions . push ( codeAction ) ;
344-
362+
345363 }
346364 if ( ! hasAnalyticsDiagonostic ) {
347365 hasAnalyticsDiagonostic = diagnostic . source === AnalyticsSource ;
0 commit comments