From 192b431da78579f8e55577a0af261fbbb653c512 Mon Sep 17 00:00:00 2001 From: Knut Funkel Date: Tue, 15 Oct 2024 12:05:54 +0200 Subject: [PATCH] feat: send fix applied analytics (#543) * feat: send fix applied analytics * fix: move the analytics call to after fix applied * fix: lint warnings * chore: update changelog * fix: add error handler on failing to send analytic for autofix applied --- CHANGELOG.md | 1 + CONTRIBUTING.md | 4 +++- src/snyk/common/constants/commands.ts | 1 + .../suggestion/codeSuggestionWebviewProvider.ts | 8 +++++++- .../views/suggestion/codeSuggestionWebviewScript.ts | 12 +++++++----- .../suggestion/codeSuggestionWebviewScriptLS.ts | 4 +++- src/snyk/snykCode/views/suggestion/types.ts | 1 + 7 files changed, 23 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa3ade33e..a823d1896 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - New error message in UI when net new scan is done on an invalid repository. Net new scans only work on Git. - Clear in Memory cache when branch is changed. - Added Clear Persisted Cache command. +- Add support for ai fix feedback analytic when pressing apply on a fix. ## [2.18.2] - Update Language Server Protocol version to 15. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 879704451..382ef7b3b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -25,13 +25,15 @@ Code changes require extension reload when run in debug. ## Run tests and debug - Unit tests - - Run `npm run test:unit` for a single execution, `npm run test:unit:watch` to watch for changes. - Make sure to re-run the command to pick up new files, if new `**.test.ts` is added. - Integration tests - Run `npm run test:integration`. +- Run Lint + - npm `npm run lint` + You can debug tests via VS Code debugger, selecting "Extension Unit Tests" or "Extension Integration Tests" respectively. ## Analytics, experimentation and error reporting diff --git a/src/snyk/common/constants/commands.ts b/src/snyk/common/constants/commands.ts index 73275290a..33208611d 100644 --- a/src/snyk/common/constants/commands.ts +++ b/src/snyk/common/constants/commands.ts @@ -25,6 +25,7 @@ export const SNYK_WORKSPACE_SCAN_COMMAND = 'snyk.workspace.scan'; export const SNYK_TRUST_WORKSPACE_FOLDERS_COMMAND = 'snyk.trustWorkspaceFolders'; export const SNYK_GET_ACTIVE_USER = 'snyk.getActiveUser'; export const SNYK_CODE_FIX_DIFFS_COMMAND = 'snyk.code.fixDiffs'; +export const SNYK_CODE_SUBMIT_FIX_FEEDBACK = 'snyk.code.submitFixFeedback'; export const SNYK_FEATURE_FLAG_COMMAND = 'snyk.getFeatureFlagStatus'; export const SNYK_CLEAR_CACHE_COMMAND = 'snyk.clearCache'; export const SNYK_CLEAR_PERSISTED_CACHE_COMMAND = 'snyk.clearPersistedCache'; diff --git a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewProvider.ts b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewProvider.ts index 5c5cdd33c..95db5847f 100644 --- a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewProvider.ts +++ b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewProvider.ts @@ -5,6 +5,7 @@ import { marked } from 'marked'; import * as vscode from 'vscode'; import { SNYK_CODE_FIX_DIFFS_COMMAND, + SNYK_CODE_SUBMIT_FIX_FEEDBACK, SNYK_IGNORE_ISSUE_COMMAND, SNYK_OPEN_BROWSER_COMMAND, SNYK_OPEN_LOCAL_COMMAND, @@ -277,7 +278,7 @@ export class CodeSuggestionWebviewProvider } case 'applyGitDiff': { - const { patch, filePath } = message.args; + const { patch, filePath, fixId } = message.args; const fileContent = readFileSync(filePath, 'utf8'); const patchedContent = applyPatch(fileContent, patch); @@ -304,6 +305,11 @@ export class CodeSuggestionWebviewProvider this.highlightAddedCode(filePath, patch); this.setupCloseOnSave(filePath); + try { + await vscode.commands.executeCommand(SNYK_CODE_SUBMIT_FIX_FEEDBACK, fixId, 'FIX_APPLIED'); + } catch (e) { + throw new Error('Error in submit fix feedback'); + } break; } diff --git a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts index be3f5d1b1..943f651e7 100644 --- a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts +++ b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts @@ -113,6 +113,7 @@ declare const acquireVsCodeApi: any; args: { patch: string; filePath: string; + fixId: string; }; }; @@ -154,13 +155,13 @@ declare const acquireVsCodeApi: any; | OpenBrowserMessage | IgnoreIssueMessage | GetAutofixDiffsMesssage + | SetAutofixDiffsMessage + | SetAutofixErrorMessage | ApplyGitDiffMessage | SetSuggestionMessage | GetSuggestionMessage | SetLessonMessage - | GetLessonMessage - | SetAutofixDiffsMessage - | SetAutofixErrorMessage; + | GetLessonMessage; const vscode = acquireVsCodeApi(); @@ -187,9 +188,9 @@ declare const acquireVsCodeApi: any; previousDiffElem: document.getElementById('previous-diff') as HTMLElement, diffSelectedIndexElem: document.getElementById('diff-counter') as HTMLElement, + generateAIFixButton: document.getElementById('generate-ai-fix') as HTMLElement, applyFixButton: document.getElementById('apply-fix') as HTMLElement, retryGenerateFixButton: document.getElementById('retry-generate-fix') as HTMLElement, - generateAIFixButton: document.getElementById('generate-ai-fix') as HTMLElement, fixAnalysisTabElem: document.getElementById('fix-analysis-tab') as HTMLElement, fixAnalysisContentElem: document.getElementById('fix-analysis-content') as HTMLElement, @@ -296,10 +297,11 @@ declare const acquireVsCodeApi: any; const diffSuggestion = suggestion.diffs[diffSelectedIndex]; const filePath = suggestion.filePath; const patch = diffSuggestion.unifiedDiffsPerFile[filePath]; + const fixId = suggestion.id; const message: ApplyGitDiffMessage = { type: 'applyGitDiff', - args: { filePath, patch }, + args: { filePath, patch, fixId }, }; sendMessage(message); } diff --git a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScriptLS.ts b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScriptLS.ts index 49f653891..135b9a664 100644 --- a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScriptLS.ts +++ b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScriptLS.ts @@ -86,6 +86,7 @@ args: { patch: string; filePath: string; + fixId: string; }; }; @@ -239,10 +240,11 @@ const diffSuggestion = suggestion.diffs[diffSelectedIndex]; const filePath = suggestion.filePath; const patch = diffSuggestion.unifiedDiffsPerFile[filePath]; + const fixId = suggestion.id; const message: ApplyGitDiffMessage = { type: 'applyGitDiff', - args: { filePath, patch }, + args: { filePath, patch, fixId }, }; sendMessage(message); } diff --git a/src/snyk/snykCode/views/suggestion/types.ts b/src/snyk/snykCode/views/suggestion/types.ts index b814679c1..6c89d6379 100644 --- a/src/snyk/snykCode/views/suggestion/types.ts +++ b/src/snyk/snykCode/views/suggestion/types.ts @@ -64,6 +64,7 @@ export type ApplyGitDiffMessage = { args: { patch: string; filePath: string; + fixId: string; }; };