33 * SPDX-License-Identifier: Apache-2.0
44 */
55
6- // import AdmZip from 'adm-zip'
6+ import AdmZip from 'adm-zip'
77import os from 'os'
88import fs from 'fs' // eslint-disable-line no-restricted-imports
99import { parsePatch , applyPatches , ParsedDiff } from 'diff'
1010import path from 'path'
1111import vscode from 'vscode'
12- // import { ExportIntent } from '@amzn/codewhisperer-streaming'
12+ import { ExportIntent } from '@amzn/codewhisperer-streaming'
1313import { TransformByQReviewStatus , transformByQState , PatchInfo } from '../../models/model'
14- import { ExportResultArchiveStructure } from '../../../shared/utilities/download'
14+ import { ExportResultArchiveStructure , downloadExportResultArchive } from '../../../shared/utilities/download'
1515import { getLogger } from '../../../shared/logger'
1616import { telemetry } from '../../../shared/telemetry/telemetry'
1717import { CodeTransformTelemetryState } from '../../../amazonqGumby/telemetry/codeTransformTelemetryState'
1818import { MetadataResult } from '../../../shared/telemetry/telemetryClient'
1919import * as CodeWhispererConstants from '../../models/constants'
20- // import { createCodeWhispererChatStreamingClient } from '../../../shared/clients/codewhispererChatClient'
20+ import { createCodeWhispererChatStreamingClient } from '../../../shared/clients/codewhispererChatClient'
2121import { ChatSessionManager } from '../../../amazonqGumby/chat/storages/chatSession'
2222import { setContext } from '../../../shared/vscode/setContext'
2323
@@ -154,7 +154,11 @@ export class DiffModel {
154154 * @param pathToWorkspace Path to the project that was transformed
155155 * @returns List of nodes containing the paths of files that were modified, added, or removed
156156 */
157- public parseDiff ( pathToDiff : string , pathToWorkspace : string , diffDescription : PatchInfo ) : PatchFileNode {
157+ public parseDiff (
158+ pathToDiff : string ,
159+ pathToWorkspace : string ,
160+ diffDescription : PatchInfo | undefined
161+ ) : PatchFileNode {
158162 this . patchFileNodes = [ ]
159163 const diffContents = fs . readFileSync ( pathToDiff , 'utf8' )
160164 const changedFiles = parsePatch ( diffContents )
@@ -190,7 +194,7 @@ export class DiffModel {
190194 }
191195 } ,
192196 } )
193- const patchFileNode = new PatchFileNode ( diffDescription . name )
197+ const patchFileNode = new PatchFileNode ( diffDescription ? diffDescription . name : pathToDiff )
194198 patchFileNode . children = changedFiles . flatMap ( ( file ) => {
195199 /* ex. file.oldFileName = 'a/src/java/com/project/component/MyFile.java'
196200 * ex. file.newFileName = 'b/src/java/com/project/component/MyFile.java'
@@ -304,26 +308,9 @@ export class ProposedTransformationExplorer {
304308 treeDataProvider : transformDataProvider ,
305309 } )
306310
307- const pathContainingArchive = '/private/var/folders/mn/l6c4t6sd1jn7g4p4nb6wqhh80000gq/T/ExportResultArchive'
308-
311+ // const pathContainingArchive = '/private/var/folders/mn/l6c4t6sd1jn7g4p4nb6wqhh80000gq/T/ExportResultArchive'
309312 const patchFiles : string [ ] = [ ]
310- fs . readdir ( path . join ( pathContainingArchive , 'patch' ) , ( err , files ) => {
311- if ( err ) {
312- getLogger ( ) . error ( err )
313- } else {
314- files . forEach ( ( file ) => {
315- const filePath = path . join ( pathContainingArchive , 'patch' , file )
316- if ( file . endsWith ( '.patch' ) ) {
317- patchFiles . push ( filePath )
318- }
319- } )
320- }
321- } )
322-
323- const pathContainingPatchFileDescriptions = path . join ( pathContainingArchive , 'patch' , 'diff.json' )
324-
325- const jsonData = fs . readFileSync ( pathContainingPatchFileDescriptions , 'utf-8' )
326- const patchFilesDescriptions : PatchInfo [ ] = JSON . parse ( jsonData )
313+ let patchFilesDescriptions : PatchInfo [ ] | undefined = undefined
327314
328315 const reset = async ( ) => {
329316 await setContext ( 'gumby.transformationProposalReviewInProgress' , false )
@@ -373,15 +360,15 @@ export class ProposedTransformationExplorer {
373360 vscode . commands . registerCommand ( 'aws.amazonq.transformationHub.reviewChanges.startReview' , async ( ) => {
374361 await setContext ( 'gumby.reviewState' , TransformByQReviewStatus . PreparingReview )
375362
376- // const pathToArchive = path.join(
377- // ProposedTransformationExplorer.TmpDir,
378- // transformByQState.getJobId(),
379- // 'ExportResultsArchive.zip'
380- // )
381- const exportResultsArchiveSize = 0
363+ const pathToArchive = path . join (
364+ ProposedTransformationExplorer . TmpDir ,
365+ transformByQState . getJobId ( ) ,
366+ 'ExportResultsArchive.zip'
367+ )
368+ let exportResultsArchiveSize = 0
382369 let downloadErrorMessage = undefined
383370
384- // const cwStreamingClient = await createCodeWhispererChatStreamingClient()
371+ const cwStreamingClient = await createCodeWhispererChatStreamingClient ( )
385372 try {
386373 await telemetry . codeTransform_downloadArtifact . run ( async ( ) => {
387374 telemetry . record ( {
@@ -390,17 +377,17 @@ export class ProposedTransformationExplorer {
390377 codeTransformJobId : transformByQState . getJobId ( ) ,
391378 } )
392379
393- // await downloadExportResultArchive(
394- // cwStreamingClient,
395- // {
396- // exportId: transformByQState.getJobId(),
397- // exportIntent: ExportIntent.TRANSFORMATION,
398- // },
399- // pathToArchive
400- // )
380+ await downloadExportResultArchive (
381+ cwStreamingClient ,
382+ {
383+ exportId : transformByQState . getJobId ( ) ,
384+ exportIntent : ExportIntent . TRANSFORMATION ,
385+ } ,
386+ pathToArchive
387+ )
401388
402389 // Update downloaded artifact size
403- // exportResultsArchiveSize = (await fs.promises.stat(pathToArchive)).size
390+ exportResultsArchiveSize = ( await fs . promises . stat ( pathToArchive ) ) . size
404391
405392 telemetry . record ( { codeTransformTotalByteSize : exportResultsArchiveSize } )
406393 } )
@@ -421,18 +408,34 @@ export class ProposedTransformationExplorer {
421408 getLogger ( ) . error ( `CodeTransformation: ExportResultArchive error = ${ downloadErrorMessage } ` )
422409 throw new Error ( 'Error downloading diff' )
423410 } finally {
424- // cwStreamingClient.destroy()
411+ cwStreamingClient . destroy ( )
425412 }
426413
427414 let deserializeErrorMessage = undefined
415+ let pathContainingArchive = ''
428416 try {
429417 // Download and deserialize the zip
430- // pathContainingArchive = path.dirname(pathToArchive)
431- // const zip = new AdmZip(pathToArchive)
432- // zip.extractAllTo(pathContainingArchive)
418+ pathContainingArchive = path . dirname ( pathToArchive )
419+ const zip = new AdmZip ( pathToArchive )
420+ zip . extractAllTo ( pathContainingArchive )
421+
422+ const files = fs . readdirSync ( path . join ( pathContainingArchive , ExportResultArchiveStructure . PathToPatch ) )
423+ files . forEach ( ( file ) => {
424+ const filePath = path . join ( pathContainingArchive , ExportResultArchiveStructure . PathToPatch , file )
425+ if ( file . endsWith ( '.patch' ) ) {
426+ patchFiles . push ( filePath )
427+ } else if ( file . endsWith ( '.json' ) ) {
428+ const jsonData = fs . readFileSync ( filePath , 'utf-8' )
429+ patchFilesDescriptions = JSON . parse ( jsonData )
430+ }
431+ } )
433432
434433 //Because multiple patches are returned once the ZIP is downloaded, we want to show the first one to start
435- diffModel . parseDiff ( patchFiles [ 0 ] , transformByQState . getProjectPath ( ) , patchFilesDescriptions [ 0 ] )
434+ diffModel . parseDiff (
435+ patchFiles [ 0 ] ,
436+ transformByQState . getProjectPath ( ) ,
437+ patchFilesDescriptions ? patchFilesDescriptions [ 0 ] : undefined
438+ )
436439
437440 await setContext ( 'gumby.reviewState' , TransformByQReviewStatus . InReview )
438441 transformDataProvider . refresh ( )
@@ -478,7 +481,9 @@ export class ProposedTransformationExplorer {
478481 diffModel . currentPatchIndex ++
479482 if ( diffModel . currentPatchIndex < patchFiles . length ) {
480483 const nextPatchFile = patchFiles [ diffModel . currentPatchIndex ]
481- const nextPatchFileDescription = patchFilesDescriptions [ diffModel . currentPatchIndex ]
484+ const nextPatchFileDescription = patchFilesDescriptions
485+ ? patchFilesDescriptions [ diffModel . currentPatchIndex ]
486+ : undefined
482487 diffModel . parseDiff ( nextPatchFile , transformByQState . getProjectPath ( ) , nextPatchFileDescription )
483488 transformDataProvider . refresh ( )
484489 } else {
0 commit comments