Skip to content

Commit c4767fc

Browse files
authored
Add CodeRemediationEvent for SecurityScan (#4618)
1 parent 3086b0a commit c4767fc

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

packages/core/src/codewhisperer/client/user-service-2.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,46 @@
456456
"max": 128,
457457
"min": 1
458458
},
459+
"CodeScanRemediationsEvent": {
460+
"type": "structure",
461+
"members": {
462+
"programmingLanguage": {
463+
"shape": "ProgrammingLanguage"
464+
},
465+
"CodeScanRemediationsEventType": {
466+
"shape": "CodeScanRemediationsEventType"
467+
},
468+
"timestamp": {
469+
"shape": "Timestamp"
470+
},
471+
"detectorId": {
472+
"shape": "String"
473+
},
474+
"findingId": {
475+
"shape": "String"
476+
},
477+
"ruleId": {
478+
"shape": "String"
479+
},
480+
"component": {
481+
"shape": "String"
482+
},
483+
"reason": {
484+
"shape": "String"
485+
},
486+
"result": {
487+
"shape": "String"
488+
},
489+
"includesFix": {
490+
"shape": "Boolean"
491+
}
492+
}
493+
},
494+
"CodeScanRemediationsEventType": {
495+
"type": "string",
496+
"documentation": "<p>Code Scan Remediations Interaction Type</p>",
497+
"enum": ["CODESCAN_ISSUE_HOVER", "CODESCAN_ISSUE_APPLY_FIX", "CODESCAN_ISSUE_VIEW_DETAILS"]
498+
},
459499
"Completion": {
460500
"type": "structure",
461501
"required": ["content"],
@@ -1327,6 +1367,7 @@
13271367
"codeCoverageEvent": { "shape": "CodeCoverageEvent" },
13281368
"userModificationEvent": { "shape": "UserModificationEvent" },
13291369
"codeScanEvent": { "shape": "CodeScanEvent" },
1370+
"codeScanRemediationsEvent": { "shape": "CodeScanRemediationsEvent" },
13301371
"metricData": { "shape": "MetricData" },
13311372
"chatAddMessageEvent": { "shape": "ChatAddMessageEvent" },
13321373
"chatInteractWithMessageEvent": { "shape": "ChatInteractWithMessageEvent" },

packages/core/src/codewhisperer/commands/basicCommands.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { Mutable } from '../../shared/utilities/tsUtils'
3232
import { CodeWhispererSource } from './types'
3333
import { showManageConnections } from '../../auth/ui/vue/show'
3434
import { FeatureConfigProvider } from '../service/featureConfigProvider'
35+
import { TelemetryHelper } from '../util/telemetryHelper'
3536

3637
export const toggleCodeSuggestions = Commands.declare(
3738
{ id: 'aws.codeWhisperer.toggleCodeSuggestion', compositeKey: { 1: 'source' } },
@@ -224,6 +225,17 @@ export const openSecurityIssuePanel = Commands.declare(
224225
ruleId: issue.ruleId,
225226
credentialStartUrl: AuthUtil.instance.startUrl,
226227
})
228+
TelemetryHelper.instance.sendCodeScanRemediationsEvent(
229+
undefined,
230+
'CODESCAN_ISSUE_VIEW_DETAILS',
231+
issue.detectorId,
232+
issue.findingId,
233+
issue.ruleId,
234+
undefined,
235+
undefined,
236+
undefined,
237+
!!issue.suggestedFixes.length
238+
)
227239
}
228240
)
229241

@@ -259,12 +271,12 @@ export const applySecurityFix = Commands.declare(
259271
result: 'Succeeded',
260272
credentialStartUrl: AuthUtil.instance.startUrl,
261273
}
262-
274+
let languageId = undefined
263275
try {
264276
const patch = suggestedFix.code
265277
const document = await vscode.workspace.openTextDocument(filePath)
266278
const fileContent = document.getText()
267-
279+
languageId = document.languageId
268280
const updatedContent = applyPatch(fileContent, patch)
269281
if (!updatedContent) {
270282
void vscode.window.showErrorMessage(CodeWhispererConstants.codeFixAppliedFailedMessage)
@@ -295,6 +307,17 @@ export const applySecurityFix = Commands.declare(
295307
applyFixTelemetryEntry.reason = err as string
296308
} finally {
297309
telemetry.codewhisperer_codeScanIssueApplyFix.emit(applyFixTelemetryEntry)
310+
TelemetryHelper.instance.sendCodeScanRemediationsEvent(
311+
languageId,
312+
'CODESCAN_ISSUE_APPLY_FIX',
313+
issue.detectorId,
314+
issue.findingId,
315+
issue.ruleId,
316+
source,
317+
applyFixTelemetryEntry.reason,
318+
applyFixTelemetryEntry.result,
319+
!!issue.suggestedFixes.length
320+
)
298321
}
299322
}
300323
)

packages/core/src/codewhisperer/service/securityIssueHoverProvider.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SecurityIssueProvider } from './securityIssueProvider'
99
import { Component, telemetry } from '../../shared/telemetry/telemetry'
1010
import path from 'path'
1111
import { AuthUtil } from '../util/authUtil'
12+
import { TelemetryHelper } from '../util/telemetryHelper'
1213

1314
export class SecurityIssueHoverProvider extends SecurityIssueProvider implements vscode.HoverProvider {
1415
static #instance: SecurityIssueHoverProvider
@@ -40,6 +41,17 @@ export class SecurityIssueHoverProvider extends SecurityIssueProvider implements
4041
includesFix: !!issue.suggestedFixes.length,
4142
credentialStartUrl: AuthUtil.instance.startUrl,
4243
})
44+
TelemetryHelper.instance.sendCodeScanRemediationsEvent(
45+
document.languageId,
46+
'CODESCAN_ISSUE_HOVER',
47+
issue.detectorId,
48+
issue.findingId,
49+
issue.ruleId,
50+
undefined,
51+
undefined,
52+
undefined,
53+
!!issue.suggestedFixes.length
54+
)
4355
}
4456
}
4557
}

packages/core/src/codewhisperer/util/telemetryHelper.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { getLogger } from '../../shared/logger'
2626
import { session } from './codeWhispererSession'
2727
import { CodeWhispererSupplementalContext } from '../models/model'
2828
import { FeatureConfigProvider } from '../service/featureConfigProvider'
29+
import { CodeScanRemediationsEventType } from '../client/codewhispereruserclient'
2930

3031
const performance = globalThis.performance ?? require('perf_hooks').performance
3132

@@ -568,4 +569,52 @@ export class TelemetryHelper {
568569
)
569570
})
570571
}
572+
573+
public sendCodeScanRemediationsEvent(
574+
languageId?: string,
575+
codeScanRemediationEventType?: CodeScanRemediationsEventType,
576+
detectorId?: string,
577+
findingId?: string,
578+
ruleId?: string,
579+
component?: string,
580+
reason?: string,
581+
result?: string,
582+
includesFix?: boolean
583+
) {
584+
client
585+
.sendTelemetryEvent({
586+
telemetryEvent: {
587+
codeScanRemediationsEvent: {
588+
programmingLanguage: languageId
589+
? {
590+
languageName: runtimeLanguageContext.toRuntimeLanguage(
591+
languageId as CodewhispererLanguage
592+
),
593+
}
594+
: undefined,
595+
CodeScanRemediationsEventType: codeScanRemediationEventType,
596+
detectorId: detectorId,
597+
findingId: findingId,
598+
ruleId: ruleId,
599+
component: component,
600+
reason: reason,
601+
result: result,
602+
includesFix: includesFix,
603+
timestamp: new Date(Date.now()),
604+
},
605+
},
606+
})
607+
.then()
608+
.catch(error => {
609+
let requestId: string | undefined
610+
if (isAwsError(error)) {
611+
requestId = error.requestId
612+
}
613+
getLogger().debug(
614+
`Failed to sendCodeScanRemediationsEvent to CodeWhisperer, requestId: ${
615+
requestId ?? ''
616+
}, message: ${error.message}`
617+
)
618+
})
619+
}
571620
}

0 commit comments

Comments
 (0)