Skip to content

Commit

Permalink
Merge branch 'add-generate-tests' of github.com:bweedop/aws-toolkit-j…
Browse files Browse the repository at this point in the history
…etbrains into add-generate-tests
  • Loading branch information
bweedop committed Sep 25, 2024
2 parents f3f324d + 2c4fe57 commit 88fb53a
Show file tree
Hide file tree
Showing 21 changed files with 162 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "feature",
"description" : "Amazon Q Code Transformation: allow users to skip tests"
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
AmazonqTelemetry.interactWithMessage(
cwsprChatConversationId = getConversationId(message.tabId).orEmpty(),
cwsprChatMessageId = message.messageId,
cwsprChatUserIntent = message.userIntent?.let { getTelemetryUserIntent(it) },
cwsprChatInteractionType = CwsprChatInteractionType.CopySnippet,
cwsprChatAcceptedCharactersLength = message.code.length.toLong(),
cwsprChatInteractionTarget = message.insertionTargetType,
Expand All @@ -234,6 +235,7 @@ class TelemetryHelper(private val context: AmazonQAppInitContext, private val se
AmazonqTelemetry.interactWithMessage(
cwsprChatConversationId = getConversationId(message.tabId).orEmpty(),
cwsprChatMessageId = message.messageId,
cwsprChatUserIntent = message.userIntent?.let { getTelemetryUserIntent(it) },
cwsprChatInteractionType = CwsprChatInteractionType.InsertAtCursor,
cwsprChatAcceptedCharactersLength = message.code.length.toLong(),
cwsprChatAcceptedNumberOfLines = message.code.lines().size.toLong(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import software.amazon.awssdk.services.codewhispererstreaming.model.UserIntent
import software.aws.toolkits.jetbrains.services.amazonq.auth.AuthFollowUpType
import software.aws.toolkits.jetbrains.services.amazonq.messages.AmazonQMessage
import software.aws.toolkits.jetbrains.services.amazonq.onboarding.OnboardingPageInteractionType
Expand Down Expand Up @@ -65,6 +66,7 @@ sealed interface IncomingCwcMessage : CwcMessage {
val command: String?,
@JsonProperty("tabID") val tabId: String,
val messageId: String,
val userIntent: UserIntent?,
val code: String,
val insertionTargetType: String?,
val eventId: String?,
Expand All @@ -75,6 +77,7 @@ sealed interface IncomingCwcMessage : CwcMessage {
data class InsertCodeAtCursorPosition(
@JsonProperty("tabID") val tabId: String,
val messageId: String,
val userIntent: UserIntent?,
val code: String,
val insertionTargetType: String?,
val codeReference: List<CodeReference>?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class TelemetryHelperTest {
private const val mockRegion = "us-east-1"
private const val tabId = "tabId"
private const val messageId = "messageId"
private val userIntent = UserIntent.SHOW_EXAMPLES
private const val conversationId = "conversationId"
private const val triggerId = "triggerId"
private const val customizationArn = "customizationArn"
Expand Down Expand Up @@ -414,6 +415,7 @@ class TelemetryHelperTest {
"command",
tabId,
messageId,
userIntent,
"println()",
"insertionTargetType",
"eventId",
Expand Down Expand Up @@ -476,6 +478,7 @@ class TelemetryHelperTest {
IncomingCwcMessage.InsertCodeAtCursorPosition(
tabId,
messageId,
userIntent,
code,
inserTionTargetType,
emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
private val isJobSuccessfullyResumed = AtomicBoolean(false)

private val transformationStoppedByUsr = AtomicBoolean(false)
private var codeTransformationSession: CodeModernizerSession? = null
var codeTransformationSession: CodeModernizerSession? = null
set(session) {
if (session != null) {
Disposer.register(this, session)
Expand Down Expand Up @@ -383,12 +383,7 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
CodeTransformMessageListener.instance.onMavenBuildResult(mavenCopyCommandsResult)
}

fun runLocalMavenBuild(project: Project, customerSelection: CustomerSelection) {
// Create and set a session
codeTransformationSession = null
val session = createCodeModernizerSession(customerSelection, project)
codeTransformationSession = session

fun runLocalMavenBuild(project: Project, session: CodeModernizerSession) {
projectCoroutineScope(project).launch {
isMvnRunning.set(true)
val result = session.getDependenciesUsingMaven()
Expand Down Expand Up @@ -668,14 +663,16 @@ class CodeModernizerManager(private val project: Project) : PersistentStateCompo
telemetry.totalRunTime(result.toString(), jobId)
}

fun createCodeModernizerSession(customerSelection: CustomerSelection, project: Project) = CodeModernizerSession(
CodeModernizerSessionContext(
project,
customerSelection.configurationFile,
customerSelection.sourceJavaVersion,
customerSelection.targetJavaVersion,
),
)
fun createCodeModernizerSession(customerSelection: CustomerSelection, project: Project) {
codeTransformationSession = CodeModernizerSession(
CodeModernizerSessionContext(
project,
customerSelection.configurationFile,
customerSelection.sourceJavaVersion,
customerSelection.targetJavaVersion,
),
)
}

fun showModernizationProgressUI() = codeModernizerBottomWindowPanelManager.showUnalteredJobUI()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private enum class CodeTransformMessageTypes(val type: String) {
CodeTransformStart("codetransform-start"),
CodeTransformStop("codetransform-stop"),
CodeTransformCancel("codetransform-cancel"),
CodeTransformConfirmSkipTests("codetransform-confirm-skip-tests"),
CodeTransformNew("codetransform-new"),
CodeTransformOpenTransformHub("codetransform-open-transform-hub"),
CodeTransformOpenMvnBuild("codetransform-open-mvn-build"),
Expand Down Expand Up @@ -64,6 +65,7 @@ class CodeTransformChatApp : AmazonQApp {
CodeTransformMessageTypes.CodeTransformStart.type to IncomingCodeTransformMessage.CodeTransformStart::class,
CodeTransformMessageTypes.CodeTransformStop.type to IncomingCodeTransformMessage.CodeTransformStop::class,
CodeTransformMessageTypes.CodeTransformCancel.type to IncomingCodeTransformMessage.CodeTransformCancel::class,
CodeTransformMessageTypes.CodeTransformConfirmSkipTests.type to IncomingCodeTransformMessage.CodeTransformConfirmSkipTests::class,
CodeTransformMessageTypes.CodeTransformNew.type to IncomingCodeTransformMessage.CodeTransformNew::class,
CodeTransformMessageTypes.CodeTransformOpenTransformHub.type to IncomingCodeTransformMessage.CodeTransformOpenTransformHub::class,
CodeTransformMessageTypes.CodeTransformOpenMvnBuild.type to IncomingCodeTransformMessage.CodeTransformOpenMvnBuild::class,
Expand Down Expand Up @@ -153,6 +155,7 @@ class CodeTransformChatApp : AmazonQApp {
is IncomingCodeTransformMessage.CodeTransformStart -> inboundAppMessagesHandler.processCodeTransformStartAction(message)
is IncomingCodeTransformMessage.CodeTransformCancel -> inboundAppMessagesHandler.processCodeTransformCancelAction(message)
is IncomingCodeTransformMessage.CodeTransformStop -> inboundAppMessagesHandler.processCodeTransformStopAction(message.tabId)
is IncomingCodeTransformMessage.CodeTransformConfirmSkipTests -> inboundAppMessagesHandler.processCodeTransformConfirmSkipTests(message)
is IncomingCodeTransformMessage.CodeTransformNew -> inboundAppMessagesHandler.processCodeTransformNewAction(message)
is IncomingCodeTransformMessage.CodeTransformOpenTransformHub -> inboundAppMessagesHandler.processCodeTransformOpenTransformHub(message)
is IncomingCodeTransformMessage.CodeTransformOpenMvnBuild -> inboundAppMessagesHandler.processCodeTransformOpenMvnBuild(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface InboundAppMessagesHandler {

suspend fun processCodeTransformStopAction(tabId: String)

suspend fun processCodeTransformConfirmSkipTests(message: IncomingCodeTransformMessage.CodeTransformConfirmSkipTests)

suspend fun processCodeTransformOpenTransformHub(message: IncomingCodeTransformMessage.CodeTransformOpenTransformHub)

suspend fun processCodeTransformOpenMvnBuild(message: IncomingCodeTransformMessage.CodeTransformOpenMvnBuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ private val confirmUserSelectionButton = Button(
id = CodeTransformButtonId.StartTransformation.id,
)

private val confirmSkipTestsSelectionButton = Button(
keepCardAfterClick = false,
waitMandatoryFormItems = true,
text = message("codemodernizer.chat.message.button.confirm"),
id = CodeTransformButtonId.ConfirmSkipTests.id,
)

private val openMvnBuildButton = Button(
id = CodeTransformButtonId.OpenMvnBuild.id,
text = message("codemodernizer.chat.message.button.view_build"),
Expand Down Expand Up @@ -134,6 +141,22 @@ private val selectTargetVersionFormItem = FormItem(
)
)

private val selectSkipTestsFlagFormItem = FormItem(
id = CodeTransformFormItemId.SelectSkipTestsFlag.id,
title = message("codemodernizer.chat.form.user_selection.item.choose_skip_tests_option"),
mandatory = true,
options = listOf(
FormItemOption(
label = message("codemodernizer.chat.message.skip_tests_form.run_tests"),
value = message("codemodernizer.chat.message.skip_tests_form.run_tests"),
),
FormItemOption(
label = message("codemodernizer.chat.message.skip_tests_form.skip"),
value = message("codemodernizer.chat.message.skip_tests_form.skip"),
)
)
)

private fun getUserSelectionFormattedMarkdown(moduleName: String): String = """
### ${message("codemodernizer.chat.prompt.title.details")}
-------------
Expand Down Expand Up @@ -184,11 +207,26 @@ fun buildStartNewTransformFollowup(): CodeTransformChatMessageContent = CodeTran
)
)

fun buildAuthRestoredFollowup(): CodeTransformChatMessageContent = CodeTransformChatMessageContent(
type = CodeTransformChatMessageType.FinalizedAnswer,
followUps = listOf(
startNewTransformFollowUp
fun buildUserInputSkipTestsFlagChatIntroContent(): CodeTransformChatMessageContent =
CodeTransformChatMessageContent(
message = message("codemodernizer.chat.message.skip_tests"),
type = CodeTransformChatMessageType.FinalizedAnswer,
)

fun buildUserInputSkipTestsFlagChatContent(): CodeTransformChatMessageContent =
CodeTransformChatMessageContent(
message = message("codemodernizer.chat.form.user_selection.title"),
buttons = listOf(
confirmSkipTestsSelectionButton,
cancelUserSelectionButton,
),
formItems = listOf(selectSkipTestsFlagFormItem),
type = CodeTransformChatMessageType.FinalizedAnswer,
)

fun buildUserSkipTestsFlagSelectionChatContent(skipTestsSelection: String) = CodeTransformChatMessageContent(
type = CodeTransformChatMessageType.FinalizedAnswer,
message = message("codemodernizer.chat.message.skip_tests_form.response", skipTestsSelection.lowercase())
)

fun buildUserInputChatContent(project: Project, validationResult: ValidationResult): CodeTransformChatMessageContent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildTr
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildUserCancelledChatContent
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildUserHilSelection
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildUserInputChatContent
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildUserInputSkipTestsFlagChatContent
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildUserInputSkipTestsFlagChatIntroContent
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildUserSelectionSummaryChatContent
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildUserSkipTestsFlagSelectionChatContent
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildUserStopTransformChatContent
import software.aws.toolkits.jetbrains.services.codemodernizer.messages.AuthenticationNeededExceptionMessage
import software.aws.toolkits.jetbrains.services.codemodernizer.messages.CodeTransformChatMessage
Expand All @@ -66,6 +69,8 @@ import software.aws.toolkits.jetbrains.services.codemodernizer.model.CodeTransfo
import software.aws.toolkits.jetbrains.services.codemodernizer.model.CustomerSelection
import software.aws.toolkits.jetbrains.services.codemodernizer.model.DownloadFailureReason
import software.aws.toolkits.jetbrains.services.codemodernizer.model.JobId
import software.aws.toolkits.jetbrains.services.codemodernizer.model.MAVEN_BUILD_RUN_UNIT_TESTS
import software.aws.toolkits.jetbrains.services.codemodernizer.model.MAVEN_BUILD_SKIP_UNIT_TESTS
import software.aws.toolkits.jetbrains.services.codemodernizer.model.MavenCopyCommandsResult
import software.aws.toolkits.jetbrains.services.codemodernizer.model.MavenDependencyReportCommandsResult
import software.aws.toolkits.jetbrains.services.codemodernizer.model.UploadFailureReason
Expand Down Expand Up @@ -208,26 +213,47 @@ class CodeTransformChatController(
val moduleVirtualFile: VirtualFile = modulePath.toVirtualFile() as VirtualFile
val moduleName = context.project.getModuleOrProjectNameForFile(moduleVirtualFile)

codeTransformChatHelper.run {
addNewMessage(buildUserSelectionSummaryChatContent(moduleName))
addNewMessage(buildCompileLocalInProgressChatContent())
}
codeTransformChatHelper.addNewMessage(buildUserSelectionSummaryChatContent(moduleName))

// this should never throw the RuntimeException since invalid JDK case is already handled in previous validation step
val moduleJdkVersion = ModuleUtil.findModuleForFile(moduleVirtualFile, context.project)?.tryGetJdk(context.project)
logger.info { "Found project JDK version: ${context.project.tryGetJdk()}, module JDK version: $moduleJdkVersion. Module JDK version prioritized." }
val sourceJdk = moduleJdkVersion ?: context.project.tryGetJdk() ?: throw RuntimeException("Unable to determine source JDK version")
val sourceJdk = getSourceJdk(moduleVirtualFile)

val selection = CustomerSelection(
moduleVirtualFile,
sourceJdk,
JavaSdkVersion.JDK_17
JavaSdkVersion.JDK_17,
)

// Create and set a session
codeModernizerManager.createCodeModernizerSession(selection, context.project)

// Publish metric to capture user selection before local build starts
telemetry.submitSelection("Confirm", selection)

codeModernizerManager.runLocalMavenBuild(context.project, selection)
codeTransformChatHelper.run {
addNewMessage(buildUserInputSkipTestsFlagChatIntroContent())
addNewMessage(buildUserInputSkipTestsFlagChatContent())
}
}

override suspend fun processCodeTransformConfirmSkipTests(message: IncomingCodeTransformMessage.CodeTransformConfirmSkipTests) {
val customBuildCommand = when (message.skipTestsSelection) {
message("codemodernizer.chat.message.skip_tests_form.skip") -> MAVEN_BUILD_SKIP_UNIT_TESTS
else -> MAVEN_BUILD_RUN_UNIT_TESTS
}
codeTransformChatHelper.addNewMessage(buildUserSkipTestsFlagSelectionChatContent(message.skipTestsSelection))
codeTransformChatHelper.addNewMessage(buildCompileLocalInProgressChatContent())
codeModernizerManager.codeTransformationSession?.let {
it.sessionContext.customBuildCommand = customBuildCommand
codeModernizerManager.runLocalMavenBuild(context.project, it)
}
}

private fun getSourceJdk(moduleConfigurationFile: VirtualFile): JavaSdkVersion {
// this should never throw the RuntimeException since invalid JDK case is already handled in previous validation step
val moduleJdkVersion = ModuleUtil.findModuleForFile(moduleConfigurationFile, context.project)?.tryGetJdk(context.project)
logger.info { "Found project JDK version: ${context.project.tryGetJdk()}, module JDK version: $moduleJdkVersion. Module JDK version prioritized." }
val sourceJdk = moduleJdkVersion ?: context.project.tryGetJdk() ?: error("Unable to determine source JDK version")
return sourceJdk
}

private suspend fun handleMavenBuildResult(mavenBuildResult: MavenCopyCommandsResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sealed interface CodeTransformBaseMessage : AmazonQMessage
enum class CodeTransformButtonId(val id: String) {
StartTransformation("codetransform-input-confirm"),
CancelTransformation("codetransform-input-cancel"),
ConfirmSkipTests("codetransform-input-confirm-skip-tests"),
StopTransformation("stop_transform"),
OpenTransformationHub("open_transformation_hub"),
OpenMvnBuild("open_mvn_build"),
Expand All @@ -32,6 +33,7 @@ enum class CodeTransformButtonId(val id: String) {
enum class CodeTransformFormItemId(val id: String) {
SelectModule("module"),
SelectTargetVersion("targetVersion"),
SelectSkipTestsFlag("skipTestsSelection"),
DependencyVersion("dependencyVersion"),
}

Expand Down Expand Up @@ -81,6 +83,11 @@ sealed interface IncomingCodeTransformMessage : CodeTransformBaseMessage {
@JsonProperty("tabID") val tabId: String,
) : IncomingCodeTransformMessage

data class CodeTransformConfirmSkipTests(
@JsonProperty("tabID") val tabId: String,
val skipTestsSelection: String,
) : IncomingCodeTransformMessage

data class CodeTransformOpenMvnBuild(
@JsonProperty("tabID") val tabId: String,
) : IncomingCodeTransformMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const val ZIP_SOURCES_PATH = "sources"
const val ZIP_DEPENDENCIES_PATH = "dependencies"
const val BUILD_LOG_PATH = "build-logs.txt"
const val MAVEN_CONFIGURATION_FILE_NAME = "pom.xml"
const val MAVEN_BUILD_RUN_UNIT_TESTS = "clean test"
const val MAVEN_BUILD_SKIP_UNIT_TESTS = "clean test-compile"
const val MAVEN_DEFAULT_BUILD_DIRECTORY_NAME = "target"
const val IDEA_DIRECTORY_NAME = ".idea"
const val INVALID_SUFFIX_SHA = "sha1"
Expand All @@ -53,6 +55,7 @@ data class CodeModernizerSessionContext(
val configurationFile: VirtualFile,
val sourceJavaVersion: JavaSdkVersion,
val targetJavaVersion: JavaSdkVersion,
var customBuildCommand: String = MAVEN_BUILD_RUN_UNIT_TESTS // run unit tests by default
) {
private val mapper = jacksonObjectMapper()
private val ignoredDependencyFileExtensions = setOf(INVALID_SUFFIX_SHA, INVALID_SUFFIX_REPOSITORIES)
Expand Down Expand Up @@ -200,7 +203,7 @@ data class CodeModernizerSessionContext(
val outputFile = createTemporaryZipFile { zip ->
// 1) Manifest file
val dependenciesRoot = if (depDirectory != null) "$ZIP_DEPENDENCIES_PATH/${depDirectory.name}" else null
mapper.writeValueAsString(ZipManifest(dependenciesRoot = dependenciesRoot))
mapper.writeValueAsString(ZipManifest(dependenciesRoot = dependenciesRoot, customBuildCommand = customBuildCommand))
.byteInputStream()
.use {
zip.putNextEntry(Path(MANIFEST_PATH).toString(), it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import com.intellij.openapi.vfs.VirtualFile
data class CustomerSelection(
val configurationFile: VirtualFile,
val sourceJavaVersion: JavaSdkVersion,
val targetJavaVersion: JavaSdkVersion
val targetJavaVersion: JavaSdkVersion,
)
Loading

0 comments on commit 88fb53a

Please sign in to comment.