From 81fe43553c4355a1ed42a629231cca9fd171bcfd Mon Sep 17 00:00:00 2001 From: Ashish Reddy Podduturi Date: Tue, 7 Jan 2025 18:18:24 -0800 Subject: [PATCH 1/9] fix for /test for files outside project scope. --- ...-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json | 4 ++++ .../controller/CodeTestChatController.kt | 20 +++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 .changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json diff --git a/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json b/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json new file mode 100644 index 00000000000..c3d5825f1ff --- /dev/null +++ b/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json @@ -0,0 +1,4 @@ +{ + "type" : "bugfix", + "description" : "Amazon Q /test: Fix to redirect /test to generate tests in chat for external files out of project scope." +} \ No newline at end of file diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt index eba7f1334c5..e6ede358fe1 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt @@ -12,6 +12,7 @@ import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project +import com.intellij.openapi.project.guessProjectDir import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFileManager @@ -157,6 +158,10 @@ class CodeTestChatController( // check if IDE has active file open, yes return (fileName and filePath) else return null val project = context.project val fileInfo = checkActiveFileInIDE(project, message) ?: return + val projectRoot = Path.of( + project.basePath ?: project.guessProjectDir()?.path + ?: error("Cannot guess base directory for project ${project.name}") + ) session.programmingLanguage = fileInfo.fileLanguage if (session.isGeneratingTests === true) { return @@ -184,7 +189,8 @@ class CodeTestChatController( message.tabId, false ) - if (isLanguageSupported(fileInfo.fileLanguage.languageId)) { + val supported = fileInfo.filePath.startsWith(projectRoot.toString()) && isLanguageSupported(fileInfo.fileLanguage.languageId) + if (supported) { // Send Capability card to chat codeTestChatHelper.addNewMessage( CodeTestChatMessageContent(informationCard = true, message = null, type = ChatMessageType.Answer, canBeVoted = false), @@ -230,9 +236,15 @@ class CodeTestChatController( } .build() - val messageContent = " ${fileInfo.fileLanguage.languageId} is not a " + - "language I support specialized unit test generation for at the moment.
The languages " + - "I support now are Python and Java. I can still provide examples, instructions and code suggestions." + val messageContent = if (fileInfo.filePath.startsWith(projectRoot.toString())) { + " ${fileInfo.fileLanguage.languageId} is not a " + + "language I support specialized unit test generation for at the moment.
The languages " + + "I support now are Python and Java. I can still provide examples, instructions and code suggestions." + } else { + "External File Detected: ${fileInfo.fileName} \n" + + " is outside the project directory.
" + + "However, I can still help you create unit tests for ${fileInfo.fileName} here" + } codeTestChatHelper.addNewMessage( CodeTestChatMessageContent( From 7cb1bd286e0c9b6c126364a28479a6f77d459d25 Mon Sep 17 00:00:00 2001 From: Ashish Reddy Podduturi Date: Tue, 7 Jan 2025 18:31:53 -0800 Subject: [PATCH 2/9] Added Telemetry metrics. --- .../amazonqCodeTest/controller/CodeTestChatController.kt | 5 ++++- .../jetbrains-community/resources/telemetryOverride.json | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt index e6ede358fe1..f6aadc2f863 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt @@ -299,7 +299,8 @@ class CodeTestChatController( AmazonqTelemetry.utgGenerateTests( cwsprChatProgrammingLanguage = session.programmingLanguage.languageId, hasUserPromptSupplied = session.hasUserPromptSupplied, - isSupportedLanguage = false, + isSupportedFile = fileInfo.filePath.startsWith(projectRoot.toString()), + isSupportedLanguage = isLanguageSupported(fileInfo.fileLanguage.languageId), credentialStartUrl = getStartUrl(project), result = MetricResult.Succeeded, perfClientLatency = (Instant.now().toEpochMilli() - session.startTimeOfTestGeneration), @@ -600,6 +601,7 @@ class CodeTestChatController( AmazonqTelemetry.utgGenerateTests( cwsprChatProgrammingLanguage = session.programmingLanguage.languageId, hasUserPromptSupplied = session.hasUserPromptSupplied, + isSupportedFile = true, isSupportedLanguage = true, credentialStartUrl = getStartUrl(project = context.project), jobGroup = session.testGenerationJobGroupName, @@ -794,6 +796,7 @@ class CodeTestChatController( AmazonqTelemetry.utgGenerateTests( cwsprChatProgrammingLanguage = session.programmingLanguage.languageId, hasUserPromptSupplied = session.hasUserPromptSupplied, + isSupportedFile = true, isSupportedLanguage = true, credentialStartUrl = getStartUrl(project = context.project), jobGroup = session.testGenerationJobGroupName, diff --git a/plugins/core/jetbrains-community/resources/telemetryOverride.json b/plugins/core/jetbrains-community/resources/telemetryOverride.json index 5637baaa16e..5a35f347a7c 100644 --- a/plugins/core/jetbrains-community/resources/telemetryOverride.json +++ b/plugins/core/jetbrains-community/resources/telemetryOverride.json @@ -215,6 +215,11 @@ "type": "boolean", "description": "True if user selected code snippet as input else false" }, + { + "name": "isSupportedFile", + "type": "boolean", + "description": "Indicate if the file is supported based on file path" + }, { "name": "isSupportedLanguage", "type": "boolean", @@ -573,6 +578,9 @@ "type": "isCodeBlockSelected", "required": false }, + { + "type": "isSupportedFile" + }, { "type": "isSupportedLanguage" }, From 18e826533665cf694865fd470dfaa2eb4717c8cd Mon Sep 17 00:00:00 2001 From: Ashish Reddy Podduturi Date: Tue, 7 Jan 2025 19:55:16 -0800 Subject: [PATCH 3/9] Fix for build failure --- .../services/amazonqCodeTest/CodeWhispererUTGChatManager.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt index b2913d66285..7e71dccd270 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt @@ -512,6 +512,7 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin AmazonqTelemetry.utgGenerateTests( cwsprChatProgrammingLanguage = session.programmingLanguage.languageId, hasUserPromptSupplied = session.hasUserPromptSupplied, + isSupportedFile = true, isSupportedLanguage = true, credentialStartUrl = getStartUrl(project), jobGroup = session.testGenerationJobGroupName, From 6ba26e4b336eb11b073eb108a4a1a7bb00341089 Mon Sep 17 00:00:00 2001 From: Ashish Reddy Podduturi Date: Tue, 7 Jan 2025 22:55:06 -0800 Subject: [PATCH 4/9] Renamed metric isSupportedFile to isFileInWorkspace --- .../services/amazonqCodeTest/CodeWhispererUTGChatManager.kt | 2 +- .../amazonqCodeTest/controller/CodeTestChatController.kt | 6 +++--- .../jetbrains-community/resources/telemetryOverride.json | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt index 7e71dccd270..4db756a73ee 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/CodeWhispererUTGChatManager.kt @@ -512,7 +512,7 @@ class CodeWhispererUTGChatManager(val project: Project, private val cs: Coroutin AmazonqTelemetry.utgGenerateTests( cwsprChatProgrammingLanguage = session.programmingLanguage.languageId, hasUserPromptSupplied = session.hasUserPromptSupplied, - isSupportedFile = true, + isFileInWorkspace = true, isSupportedLanguage = true, credentialStartUrl = getStartUrl(project), jobGroup = session.testGenerationJobGroupName, diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt index f6aadc2f863..d8d0c986054 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt @@ -299,7 +299,7 @@ class CodeTestChatController( AmazonqTelemetry.utgGenerateTests( cwsprChatProgrammingLanguage = session.programmingLanguage.languageId, hasUserPromptSupplied = session.hasUserPromptSupplied, - isSupportedFile = fileInfo.filePath.startsWith(projectRoot.toString()), + isFileInWorkspace = fileInfo.filePath.startsWith(projectRoot.toString()), isSupportedLanguage = isLanguageSupported(fileInfo.fileLanguage.languageId), credentialStartUrl = getStartUrl(project), result = MetricResult.Succeeded, @@ -601,7 +601,7 @@ class CodeTestChatController( AmazonqTelemetry.utgGenerateTests( cwsprChatProgrammingLanguage = session.programmingLanguage.languageId, hasUserPromptSupplied = session.hasUserPromptSupplied, - isSupportedFile = true, + isFileInWorkspace = true, isSupportedLanguage = true, credentialStartUrl = getStartUrl(project = context.project), jobGroup = session.testGenerationJobGroupName, @@ -796,7 +796,7 @@ class CodeTestChatController( AmazonqTelemetry.utgGenerateTests( cwsprChatProgrammingLanguage = session.programmingLanguage.languageId, hasUserPromptSupplied = session.hasUserPromptSupplied, - isSupportedFile = true, + isFileInWorkspace = true, isSupportedLanguage = true, credentialStartUrl = getStartUrl(project = context.project), jobGroup = session.testGenerationJobGroupName, diff --git a/plugins/core/jetbrains-community/resources/telemetryOverride.json b/plugins/core/jetbrains-community/resources/telemetryOverride.json index 5a35f347a7c..b562a82b79b 100644 --- a/plugins/core/jetbrains-community/resources/telemetryOverride.json +++ b/plugins/core/jetbrains-community/resources/telemetryOverride.json @@ -216,7 +216,7 @@ "description": "True if user selected code snippet as input else false" }, { - "name": "isSupportedFile", + "name": "isFileInWorkspace", "type": "boolean", "description": "Indicate if the file is supported based on file path" }, @@ -579,7 +579,7 @@ "required": false }, { - "type": "isSupportedFile" + "type": "isFileInWorkspace" }, { "type": "isSupportedLanguage" From c0499ad6fd703ec127266e2738304fb8ac8c9d9e Mon Sep 17 00:00:00 2001 From: Ashish Reddy Podduturi Date: Tue, 7 Jan 2025 22:56:33 -0800 Subject: [PATCH 5/9] Correction in description. --- .../core/jetbrains-community/resources/telemetryOverride.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/core/jetbrains-community/resources/telemetryOverride.json b/plugins/core/jetbrains-community/resources/telemetryOverride.json index b562a82b79b..1901f917406 100644 --- a/plugins/core/jetbrains-community/resources/telemetryOverride.json +++ b/plugins/core/jetbrains-community/resources/telemetryOverride.json @@ -218,7 +218,7 @@ { "name": "isFileInWorkspace", "type": "boolean", - "description": "Indicate if the file is supported based on file path" + "description": "Indicate if the file is in the current workspace." }, { "name": "isSupportedLanguage", From a6e22ca8fd872655d1e76fa5473b6c48ede264c1 Mon Sep 17 00:00:00 2001 From: invictus <149003065+ashishrp-aws@users.noreply.github.com> Date: Wed, 8 Jan 2025 11:43:53 -0800 Subject: [PATCH 6/9] Update .changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json Co-authored-by: Tai Lai --- .../bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json b/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json index c3d5825f1ff..50b175ba9dd 100644 --- a/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json +++ b/.changes/next-release/bugfix-8c6afa32-afe4-41f2-a216-10d8bf5e2486.json @@ -1,4 +1,4 @@ { "type" : "bugfix", - "description" : "Amazon Q /test: Fix to redirect /test to generate tests in chat for external files out of project scope." + "description" : "Amazon Q /test: Test generation fails for files outside the project" } \ No newline at end of file From d83cfcb590896c4f05fb477a7273c3efcdc3a288 Mon Sep 17 00:00:00 2001 From: invictus <149003065+ashishrp-aws@users.noreply.github.com> Date: Wed, 8 Jan 2025 11:44:09 -0800 Subject: [PATCH 7/9] Update plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt Co-authored-by: Tai Lai --- .../amazonqCodeTest/controller/CodeTestChatController.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt index d8d0c986054..b1989da103a 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt @@ -241,9 +241,7 @@ class CodeTestChatController( "language I support specialized unit test generation for at the moment.
The languages " + "I support now are Python and Java. I can still provide examples, instructions and code suggestions." } else { - "External File Detected: ${fileInfo.fileName} \n" + - " is outside the project directory.
" + - "However, I can still help you create unit tests for ${fileInfo.fileName} here" + " I can't generate tests for ${fileInfo.fileName} because it's outside the project directory. I can still provide examples, instructions and code suggestions." } codeTestChatHelper.addNewMessage( From d8a078d9fa66e3c1b70fb222bd8a2a92e699af30 Mon Sep 17 00:00:00 2001 From: invictus <149003065+ashishrp-aws@users.noreply.github.com> Date: Wed, 8 Jan 2025 11:46:30 -0800 Subject: [PATCH 8/9] Update CodeTestChatController.kt --- .../amazonqCodeTest/controller/CodeTestChatController.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt index b1989da103a..6c2a4a60875 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt @@ -241,7 +241,7 @@ class CodeTestChatController( "language I support specialized unit test generation for at the moment.
The languages " + "I support now are Python and Java. I can still provide examples, instructions and code suggestions." } else { - " I can't generate tests for ${fileInfo.fileName} because it's outside the project directory. I can still provide examples, instructions and code suggestions." + " I can't generate tests for ${fileInfo.fileName} because it's outside the project directory.
I can still provide examples, instructions and code suggestions." } codeTestChatHelper.addNewMessage( From b0682377c4351310e20bdaa754e53d7d6c3c4b5c Mon Sep 17 00:00:00 2001 From: Ashish Reddy Podduturi Date: Wed, 8 Jan 2025 13:12:13 -0800 Subject: [PATCH 9/9] detekt errors fix. --- .../amazonqCodeTest/controller/CodeTestChatController.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt index 6c2a4a60875..27d0913fcdc 100644 --- a/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt +++ b/plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqCodeTest/controller/CodeTestChatController.kt @@ -241,7 +241,9 @@ class CodeTestChatController( "language I support specialized unit test generation for at the moment.

The languages " + "I support now are Python and Java. I can still provide examples, instructions and code suggestions." } else { - " I can't generate tests for ${fileInfo.fileName} because it's outside the project directory.
I can still provide examples, instructions and code suggestions." + " I can't generate tests for ${fileInfo.fileName}" + + " because it's outside the project directory.
" + + "I can still provide examples, instructions and code suggestions." } codeTestChatHelper.addNewMessage(