@@ -5,6 +5,7 @@ package software.aws.toolkits.jetbrains.services.amazonq.webview
55
66import com.fasterxml.jackson.databind.JsonNode
77import com.fasterxml.jackson.databind.node.ObjectNode
8+ import com.fasterxml.jackson.module.kotlin.readValue
89import com.fasterxml.jackson.module.kotlin.treeToValue
910import com.google.gson.Gson
1011import com.intellij.ide.BrowserUtil
@@ -34,6 +35,7 @@ import org.cef.browser.CefBrowser
3435import org.eclipse.lsp4j.TextDocumentIdentifier
3536import org.eclipse.lsp4j.jsonrpc.ResponseErrorException
3637import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode
38+ import org.intellij.lang.annotations.Language
3739import software.aws.toolkits.core.utils.error
3840import software.aws.toolkits.core.utils.getLogger
3941import software.aws.toolkits.core.utils.info
@@ -115,9 +117,6 @@ import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.auth.isFeature
115117import software.aws.toolkits.jetbrains.services.codemodernizer.utils.isCodeTransformAvailable
116118import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.CodeWhispererCodeScanIssue
117119import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.CodeWhispererCodeScanManager
118- import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.Description
119- import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.Recommendation
120- import software.aws.toolkits.jetbrains.services.codewhisperer.codescan.SuggestedFix
121120import software.aws.toolkits.jetbrains.services.codewhisperer.settings.CodeWhispererConfigurable
122121import software.aws.toolkits.jetbrains.services.codewhisperer.util.CodeWhispererConstants
123122import software.aws.toolkits.jetbrains.settings.MeetQSettings
@@ -582,32 +581,6 @@ class BrowserConnector(
582581 }
583582 }
584583
585- data class FlareCodeScanIssue (
586- val startLine : Int ,
587- val endLine : Int ,
588- val comment : String? ,
589- val title : String ,
590- val description : Description ,
591- val detectorId : String ,
592- val detectorName : String ,
593- val findingId : String ,
594- val ruleId : String? ,
595- val relatedVulnerabilities : List <String >,
596- val severity : String ,
597- val recommendation : Recommendation ,
598- val suggestedFixes : List <SuggestedFix >,
599- val scanJobId : String ,
600- val language : String ,
601- val autoDetected : Boolean ,
602- val filePath : String ,
603- val findingContext : String ,
604- )
605-
606- data class AggregatedCodeScanIssue (
607- val filePath : String ,
608- val issues : List <FlareCodeScanIssue >,
609- )
610-
611584 private fun showResult (
612585 result : CompletableFuture <String >,
613586 partialResultToken : String ,
@@ -621,14 +594,13 @@ class BrowserConnector(
621594 throw error
622595 }
623596 chatCommunicationManager.removePartialChatMessage(partialResultToken)
624- val decryptedMessage = Gson ().fromJson(value?.let { encryptionManager?.decrypt(it) }.orEmpty(), Map ::class .java)
625- as Map <String , * >
597+ val decryptedMessage = value?.let { encryptionManager?.decrypt(it) }.orEmpty()
626598 parseFindingsMessages(decryptedMessage)
627599
628600 val messageToChat = ChatCommunicationManager .convertToJsonToSendToChat(
629601 SEND_CHAT_COMMAND_PROMPT ,
630602 tabId,
631- Gson ().toJson( decryptedMessage) ,
603+ decryptedMessage,
632604 isPartialResult = false
633605 )
634606 browser.postChat(messageToChat)
@@ -642,26 +614,23 @@ class BrowserConnector(
642614 }
643615 }
644616
645- fun parseFindingsMessages (messagesMap : Map <String , * >) {
617+ fun deserializeFindings (@Language(" JSON" ) responsePayload : String ): List <FlareAggregatedFindings > {
618+ val additionalMessages = serializer.objectMapper.readValue<FlareAdditionalMessages >(responsePayload).additionalMessages
619+ ? : return emptyList()
620+
621+ return additionalMessages.filter { message ->
622+ message.messageId.endsWith(CODE_REVIEW_FINDINGS_SUFFIX ) ||
623+ message.messageId.endsWith(DISPLAY_FINDINGS_SUFFIX )
624+ }
625+ }
626+
627+ fun parseFindingsMessages (@Language(" JSON" ) responsePayload : String ) {
646628 try {
647- val additionalMessages = messagesMap[" additionalMessages" ] as ? MutableList <Map <String , Any >>
648- val findingsMessages = additionalMessages?.filter { message ->
649- if (message.contains(" messageId" )) {
650- (message[" messageId" ] as String ).endsWith(CODE_REVIEW_FINDINGS_SUFFIX ) ||
651- (message[" messageId" ] as String ).endsWith(DISPLAY_FINDINGS_SUFFIX )
652- } else {
653- false
654- }
655- }
629+ val findings = deserializeFindings(responsePayload)
656630 val scannedFiles = mutableListOf<VirtualFile >()
657- if (findingsMessages != null ) {
658- for (findingsMessage in findingsMessages) {
659- additionalMessages.remove(findingsMessage)
660- val gson = Gson ()
661- val jsonFindings = gson.fromJson(findingsMessage[" body" ] as String , List ::class .java)
662- val mappedFindings = mutableListOf<CodeWhispererCodeScanIssue >()
663- for (aggregatedIssueUnformatted in jsonFindings) {
664- val aggregatedIssue = gson.fromJson(gson.toJson(aggregatedIssueUnformatted), AggregatedCodeScanIssue ::class .java)
631+ val mappedFindings = buildList {
632+ for (finding in findings) {
633+ for (aggregatedIssue in finding.body) {
665634 val file = LocalFileSystem .getInstance().findFileByIoFile(
666635 Path .of(aggregatedIssue.filePath).toFile()
667636 )
@@ -678,7 +647,8 @@ class BrowserConnector(
678647 if (isIssueIgnored) {
679648 continue
680649 }
681- mappedFindings.add(
650+
651+ add(
682652 CodeWhispererCodeScanIssue (
683653 startLine = issue.startLine,
684654 startCol = 1 ,
@@ -706,18 +676,20 @@ class BrowserConnector(
706676 }
707677 }
708678 }
709-
710- CodeWhispererCodeScanManager .getInstance(project)
711- .addOnDemandIssues(
712- mappedFindings,
713- scannedFiles,
714- CodeWhispererConstants .CodeAnalysisScope .AGENTIC
715- )
716- CodeWhispererCodeScanManager .getInstance(project).showCodeScanUI()
717679 }
718680 }
681+
682+ if (mappedFindings.isNotEmpty()) {
683+ CodeWhispererCodeScanManager .getInstance(project)
684+ .addOnDemandIssues(
685+ mappedFindings,
686+ scannedFiles,
687+ CodeWhispererConstants .CodeAnalysisScope .AGENTIC
688+ )
689+ CodeWhispererCodeScanManager .getInstance(project).showCodeScanUI()
690+ }
719691 } catch (e: Exception ) {
720- LOG .error { " Failed to parse findings message $e " }
692+ LOG .error(e) { " Failed to parse findings message" }
721693 }
722694 }
723695
0 commit comments