Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(amazonq): Add command to generate unit tests for selected code #4869

Merged
merged 43 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
fbe77d7
feat (Amazon Q): Add basic UX and logic for generating unit tests
bweedop Aug 27, 2024
8c8701f
Merge branch 'aws:main' into add-generate-tests
bweedop Aug 27, 2024
5596ce6
Merge branch 'aws:main' into add-generate-tests
bweedop Aug 28, 2024
2476e8a
Merge branch 'aws:main' into add-generate-tests
bweedop Sep 4, 2024
a200346
Update cwspr model with GENERATE_UNIT_TESTS user intent
bweedop Sep 4, 2024
2673db8
Merge branch 'add-generate-tests' of github.com:bweedop/aws-toolkit-j…
bweedop Sep 4, 2024
2050c28
fix (Amazon Q): Pass context specific intent and trigger type to chat…
bweedop Aug 28, 2024
665a7c4
Added authType to loginWithBrowser metric (#4843)
manodnyab Aug 29, 2024
9a88b0f
Added UI click metrics on all login options (#4838)
manodnyab Aug 29, 2024
b6e4e7b
Updating version to 3.25
Aug 29, 2024
b884109
Updating SNAPSHOT version to 3.26-SNAPSHOT
Aug 29, 2024
edbde53
Revert open and close sign in webview telemetry (#4850)
manodnyab Aug 30, 2024
3cdb18e
Updating version to 3.26
Aug 30, 2024
4a206c1
Updating SNAPSHOT version to 3.27-SNAPSHOT
Aug 30, 2024
a478c5a
telemetry(amazonq): update telemetry (#4847)
dhasani23 Sep 3, 2024
930e706
feat(amazonq): Reduce @workspace indexing time by 50% (#4846)
leigaol Sep 3, 2024
334e8e1
Hash startUrl
bweedop Sep 4, 2024
ac85a6f
Merge branch 'add-generate-tests' of github.com:bweedop/aws-toolkit-j…
bweedop Sep 4, 2024
29e3613
Merge branch 'aws:main' into add-generate-tests
bweedop Sep 6, 2024
edaecdb
Revert CodeWhisperer endpoint back gamma endpoint
bweedop Sep 6, 2024
54816fb
Revert adding user intent to interact with message metadata
bweedop Sep 6, 2024
a8ba166
Merge branch 'add-generate-tests' of github.com:bweedop/aws-toolkit-j…
bweedop Sep 6, 2024
c25dbe5
Add changelog
bweedop Sep 6, 2024
c8ed616
Refactor for consistent hashing of startUrl
bweedop Sep 6, 2024
673c6e1
Remove hashing logic on startUrl and changelog entry
bweedop Sep 10, 2024
d589b60
Add (Beta) suffix to generate unit tests command
bweedop Sep 11, 2024
0d52a23
Add check for startUrl when getting user intent from prompt
bweedop Sep 11, 2024
2408b8c
Merge branch 'aws:main' into add-generate-tests
bweedop Sep 12, 2024
c07fee0
Merge branch 'aws:main' into add-generate-tests
bweedop Sep 17, 2024
8b5f672
Update TestCodeAction to resolve lint failure
bweedop Sep 17, 2024
3c3d9f3
address comments: Better GenerateUnitTest Action naming
daveln-aws Sep 18, 2024
3124e9a
Refactor to use more specific command for UTG
bweedop Sep 20, 2024
c9f1491
Merge branch 'aws:main' into add-generate-tests
bweedop Sep 23, 2024
4808425
Change name of action to GenerateUnitTests
bweedop Sep 24, 2024
061fd25
Fix linting errors
bweedop Sep 24, 2024
9c83687
Merge branch 'main' into add-generate-tests
bweedop Sep 25, 2024
465b2c9
Merge branch 'add-generate-tests' of github.com:bweedop/aws-toolkit-j…
bweedop Sep 25, 2024
ba4329d
Override 'getActionUpdateThread'
bweedop Sep 25, 2024
5317be3
Reorder MessageBundle.properties
bweedop Sep 25, 2024
2c4fe57
Merge branch 'main' into add-generate-tests
bweedop Sep 25, 2024
f3f324d
Fix syntax error
bweedop Sep 25, 2024
88fb53a
Merge branch 'add-generate-tests' of github.com:bweedop/aws-toolkit-j…
bweedop Sep 25, 2024
5abad70
Merge branch 'aws:main' into add-generate-tests
bweedop Sep 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<projectListeners>
<listener class="software.aws.toolkits.jetbrains.services.amazonq.toolwindow.AmazonQToolWindowListener"
topic="com.intellij.openapi.wm.ex.ToolWindowManagerListener"/>
topic="com.intellij.openapi.wm.ex.ToolWindowManagerListener"/>
<listener class="software.aws.toolkits.jetbrains.services.cwc.editor.context.project.ProjectContextEditorListener"
topic="com.intellij.openapi.fileEditor.FileEditorManagerListener"/>
</projectListeners>
Expand Down Expand Up @@ -70,6 +70,11 @@
<keyboard-shortcut keymap="$default" first-keystroke="meta alt A" />
</action>

<action id="aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction"
class="software.aws.toolkits.jetbrains.services.cwc.commands.GenerateUnitTestsAction">
<keyboard-shortcut keymap="$default" first-keystroke="meta alt T" />
</action>

<action id="aws.toolkit.jetbrains.core.services.cwc.commands.SendToPromptAction"
class="software.aws.toolkits.jetbrains.services.cwc.commands.SendToPromptAction">
<keyboard-shortcut keymap="$default" first-keystroke="meta alt S" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ class ChatSessionV1(
UserIntent.EXPLAIN_LINE_BY_LINE -> FollowUpType.LineByLine
UserIntent.EXPLAIN_CODE_SELECTION -> FollowUpType.ExplainInDetail
UserIntent.UNKNOWN_TO_SDK_VERSION -> FollowUpType.Generated
UserIntent.GENERATE_UNIT_TESTS -> FollowUpType.Generated
null -> FollowUpType.Generated
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ enum class EditorContextCommand(
verb = "Optimize",
actionId = "aws.amazonq.optimizeCode",
),
GenerateUnitTests(
verb = "Generate unit tests for",
actionId = "aws.amazonq.generateUnitTests",
),
SendToPrompt(
verb = "SendToPrompt",
actionId = "aws.amazonq.sendToPrompt",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.jetbrains.services.cwc.commands

import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys
import software.aws.toolkits.jetbrains.core.credentials.AwsBearerTokenConnection
import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager
import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection

class GenerateUnitTestsAction : CustomAction(EditorContextCommand.GenerateUnitTests) {

Check warning on line 13 in plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/commands/GenerateUnitTestsAction.kt

View workflow job for this annotation

GitHub Actions / qodana

Component/Action not registered

Action is not registered in plugin.xml
override fun getActionUpdateThread() = ActionUpdateThread.BGT

override fun update(e: AnActionEvent) {
val project = e.getData(CommonDataKeys.PROJECT) ?: return
val connection = ToolkitConnectionManager.getInstance(project).activeConnectionForFeature(QConnection.getInstance()) as? AwsBearerTokenConnection
e.presentation.isEnabledAndVisible = connection?.startUrl == "https://amzn.awsapps.com/start"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class ChatController private constructor(
var shouldAddIndexInProgressMessage: Boolean = false
var shouldUseWorkspaceContext: Boolean = false
val isDataCollectionGroup = CodeWhispererFeatureConfigService.getInstance().getIsDataCollectionEnabled()
val startUrl = getStartUrl(context.project)

if (prompt.contains("@workspace")) {
if (CodeWhispererSettings.getInstance().isProjectContextEnabled()) {
shouldUseWorkspaceContext = true
Expand All @@ -151,7 +153,7 @@ class ChatController private constructor(
triggerId = triggerId,
message = prompt,
activeFileContext = contextExtractor.extractContextForTrigger(ExtractionTriggerType.ChatMessage),
userIntent = intentRecognizer.getUserIntentFromPromptChatMessage(message.chatMessage),
userIntent = intentRecognizer.getUserIntentFromPromptChatMessage(message.chatMessage, startUrl),
TriggerType.Click,
projectContextQueryResult = queryResult,
shouldAddIndexInProgressMessage = shouldAddIndexInProgressMessage,
Expand Down Expand Up @@ -332,7 +334,11 @@ class ChatController private constructor(
}

// Create prompt
val prompt = "${message.command} the following part of my code for me: $codeSelection"
val prompt = if (EditorContextCommand.GenerateUnitTests == message.command) {
"${message.command.verb} the following part of my code for me: $codeSelection"
} else {
"${message.command} the following part of my code for me: $codeSelection"
}

processPromptActions(prompt, message, triggerId, fileContext)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
UserIntent.CITE_SOURCES -> CwsprChatUserIntent.CiteSources
UserIntent.EXPLAIN_LINE_BY_LINE -> CwsprChatUserIntent.ExplainLineByLine
UserIntent.EXPLAIN_CODE_SELECTION -> CwsprChatUserIntent.ExplainCodeSelection
UserIntent.GENERATE_UNIT_TESTS -> CwsprChatUserIntent.GenerateUnitTests
UserIntent.UNKNOWN_TO_SDK_VERSION -> CwsprChatUserIntent.Unknown
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ class UserIntentRecognizer {
EditorContextCommand.Fix -> UserIntent.APPLY_COMMON_BEST_PRACTICES
EditorContextCommand.Optimize -> UserIntent.IMPROVE_CODE
EditorContextCommand.ExplainCodeScanIssue -> UserIntent.EXPLAIN_CODE_SELECTION
EditorContextCommand.GenerateUnitTests -> UserIntent.GENERATE_UNIT_TESTS
EditorContextCommand.SendToPrompt -> null
}

fun getUserIntentFromPromptChatMessage(prompt: String) = when {
fun getUserIntentFromPromptChatMessage(prompt: String, startUrl: String?) = when {
prompt.startsWith("Explain") -> UserIntent.EXPLAIN_CODE_SELECTION
prompt.startsWith("Refactor") -> UserIntent.SUGGEST_ALTERNATE_IMPLEMENTATION
prompt.startsWith("Fix") -> UserIntent.APPLY_COMMON_BEST_PRACTICES
prompt.startsWith("Optimize") -> UserIntent.IMPROVE_CODE
prompt.startsWith("Generate unit tests") && isInternalAmazonUser(startUrl) -> UserIntent.GENERATE_UNIT_TESTS
else -> null
}

Expand All @@ -43,4 +45,6 @@ class UserIntentRecognizer {
fun getUserIntentFromOnboardingPageInteraction(interaction: OnboardingPageInteraction) = when (interaction.type) {
OnboardingPageInteractionType.CwcButtonClick -> null
}

private fun isInternalAmazonUser(startUrl: String?): Boolean = startUrl == "https://amzn.awsapps.com/start"
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"showExample",
"citeSources",
"explainLineByLine",
"explainCodeSelection"
"explainCodeSelection",
"generateUnitTests"
],
"description": "Explict user intent associated with a chat message"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ action.aws.toolkit.jetbrains.core.services.cwc.commands.ExplainCodeAction.descri
action.aws.toolkit.jetbrains.core.services.cwc.commands.ExplainCodeAction.text = Explain Code
action.aws.toolkit.jetbrains.core.services.cwc.commands.FixCodeAction.description = Fixes the selected code
action.aws.toolkit.jetbrains.core.services.cwc.commands.FixCodeAction.text = Fix Code
action.aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction.description = Generates unit tests for the selected code
action.aws.toolkit.jetbrains.core.services.cwc.commands.GenerateUnitTestsAction.text = Generate Tests (Beta)
action.aws.toolkit.jetbrains.core.services.cwc.commands.OptimizeCodeAction.description = Optimizes the selected code
action.aws.toolkit.jetbrains.core.services.cwc.commands.OptimizeCodeAction.text = Optimize Code
action.aws.toolkit.jetbrains.core.services.cwc.commands.RefactorCodeAction.description = Refactors the selected code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1952,7 +1952,8 @@
"SHOW_EXAMPLES",
"CITE_SOURCES",
"EXPLAIN_LINE_BY_LINE",
"EXPLAIN_CODE_SELECTION"
"EXPLAIN_CODE_SELECTION",
"GENERATE_UNIT_TESTS"
]
},
"UserModificationEvent":{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@
"SHOW_EXAMPLES",
"CITE_SOURCES",
"EXPLAIN_LINE_BY_LINE",
"EXPLAIN_CODE_SELECTION"
"EXPLAIN_CODE_SELECTION",
"GENERATE_UNIT_TESTS"
]
},
"ValidationException":{
Expand Down
Loading