55
66import * as vscode from 'vscode'
77import * as fs from 'fs' // eslint-disable-line no-restricted-imports
8- import os from 'os'
98import path from 'path'
109import { getLogger } from '../../shared/logger/logger'
1110import * as CodeWhispererConstants from '../models/constants'
@@ -79,6 +78,12 @@ import { convertDateToTimestamp } from '../../shared/datetime'
7978import { findStringInDirectory } from '../../shared/utilities/workspaceUtils'
8079import { makeTemporaryToolkitFolder } from '../../shared/filesystemUtilities'
8180import { AuthUtil } from '../util/authUtil'
81+ import {
82+ cleanupTempJobFiles ,
83+ createMetadataFile ,
84+ JobMetadata ,
85+ writeToHistoryFile ,
86+ } from '../service/transformByQ/transformationHistoryHandler'
8287
8388export function getFeedbackCommentData ( ) {
8489 const jobId = transformByQState . getJobId ( )
@@ -477,28 +482,21 @@ export async function startTransformationJob(
477482 } )
478483
479484 // create local history folder(s) and store metadata
480- const jobHistoryPath = path . join ( os . homedir ( ) , '.aws' , 'transform' , transformByQState . getProjectName ( ) , jobId )
481- if ( ! fs . existsSync ( jobHistoryPath ) ) {
482- fs . mkdirSync ( jobHistoryPath , { recursive : true } )
485+ const metadata : JobMetadata = {
486+ jobId : jobId ,
487+ projectName : transformByQState . getProjectName ( ) ,
488+ transformationType : transformByQState . getTransformationType ( ) ?? TransformationType . LANGUAGE_UPGRADE ,
489+ sourceJDKVersion : transformByQState . getSourceJDKVersion ( ) ?? JDKVersion . JDK8 ,
490+ targetJDKVersion : transformByQState . getTargetJDKVersion ( ) ?? JDKVersion . JDK17 ,
491+ customDependencyVersionFilePath : transformByQState . getCustomDependencyVersionFilePath ( ) ,
492+ customBuildCommand : transformByQState . getCustomBuildCommand ( ) ,
493+ targetJavaHome : transformByQState . getTargetJavaHome ( ) ?? '' ,
494+ projectPath : transformByQState . getProjectPath ( ) ,
495+ startTime : transformByQState . getStartTime ( ) ,
483496 }
484- transformByQState . setJobHistoryPath ( jobHistoryPath )
485- // save a copy of the upload zip
486- fs . copyFileSync ( transformByQState . getPayloadFilePath ( ) , path . join ( jobHistoryPath , 'zipped-code.zip' ) )
487497
488- const fields = [
489- jobId ,
490- transformByQState . getTransformationType ( ) ,
491- transformByQState . getSourceJDKVersion ( ) ,
492- transformByQState . getTargetJDKVersion ( ) ,
493- transformByQState . getCustomDependencyVersionFilePath ( ) ,
494- transformByQState . getCustomBuildCommand ( ) ,
495- transformByQState . getTargetJavaHome ( ) ,
496- transformByQState . getProjectPath ( ) ,
497- transformByQState . getStartTime ( ) ,
498- ]
499-
500- const jobDetails = fields . join ( '\t' )
501- fs . writeFileSync ( path . join ( jobHistoryPath , 'metadata.txt' ) , jobDetails )
498+ const jobHistoryPath = await createMetadataFile ( transformByQState . getPayloadFilePath ( ) , metadata )
499+ transformByQState . setJobHistoryPath ( jobHistoryPath )
502500 } catch ( error ) {
503501 getLogger ( ) . error ( `CodeTransformation: ${ CodeWhispererConstants . failedToStartJobNotification } ` , error )
504502 const errorMessage = ( error as Error ) . message . toLowerCase ( )
@@ -749,24 +747,11 @@ export async function postTransformationJob() {
749747 } )
750748 }
751749
752- // delete original upload ZIP at very end of transformation
753- fs . rmSync ( transformByQState . getPayloadFilePath ( ) , { force : true } )
754-
755- if (
756- transformByQState . isSucceeded ( ) ||
757- transformByQState . isPartiallySucceeded ( ) ||
758- transformByQState . isCancelled ( )
759- ) {
760- // delete the copy of the upload ZIP
761- fs . rmSync ( path . join ( transformByQState . getJobHistoryPath ( ) , 'zipped-code.zip' ) , { force : true } )
762- // delete transformation job metadata file (no longer needed)
763- fs . rmSync ( path . join ( transformByQState . getJobHistoryPath ( ) , 'metadata.txt' ) , { force : true } )
764- }
765- // delete temporary build logs file
766- const logFilePath = path . join ( os . tmpdir ( ) , 'build-logs.txt' )
767- if ( fs . existsSync ( logFilePath ) ) {
768- fs . rmSync ( logFilePath , { force : true } )
769- }
750+ await cleanupTempJobFiles (
751+ transformByQState . getJobHistoryPath ( ) ,
752+ transformByQState . getPolledJobStatus ( ) ,
753+ transformByQState . getPayloadFilePath ( )
754+ )
770755
771756 // attempt download for user
772757 // TODO: refactor as explained here https://github.com/aws/aws-toolkit-vscode/pull/6519/files#r1946873107
@@ -777,35 +762,14 @@ export async function postTransformationJob() {
777762 // store job details and diff path locally (history)
778763 // TODO: ideally when job is cancelled, should be stored as CANCELLED instead of FAILED (remove this if statement after bug is fixed)
779764 if ( ! transformByQState . isCancelled ( ) ) {
780- const historyLogFilePath = path . join ( os . homedir ( ) , '.aws' , 'transform' , 'transformation_history.tsv' )
781- // create transform folder if necessary
782- if ( ! fs . existsSync ( historyLogFilePath ) ) {
783- fs . mkdirSync ( path . dirname ( historyLogFilePath ) , { recursive : true } )
784- // create headers of new transformation history file
785- fs . writeFileSync ( historyLogFilePath , 'date\tproject_name\tstatus\tduration\tdiff_patch\tsummary\tjob_id\n' )
786- }
787765 const latest = sessionJobHistory [ transformByQState . getJobId ( ) ]
788- const fields = [
766+ await writeToHistoryFile (
789767 latest . startTime ,
790768 latest . projectName ,
791769 latest . status ,
792770 latest . duration ,
793- transformByQState . isSucceeded ( ) || transformByQState . isPartiallySucceeded ( )
794- ? path . join ( transformByQState . getJobHistoryPath ( ) , 'diff.patch' )
795- : '' ,
796- transformByQState . isSucceeded ( ) || transformByQState . isPartiallySucceeded ( )
797- ? path . join ( transformByQState . getJobHistoryPath ( ) , 'summary' , 'summary.md' )
798- : '' ,
799771 transformByQState . getJobId ( ) ,
800- ]
801-
802- const jobDetails = fields . join ( '\t' ) + '\n'
803- fs . writeFileSync ( historyLogFilePath , jobDetails , { flag : 'a' } ) // 'a' flag used to append to file
804- await vscode . commands . executeCommand (
805- 'aws.amazonq.transformationHub.updateContent' ,
806- 'job history' ,
807- undefined ,
808- true
772+ transformByQState . getJobHistoryPath ( )
809773 )
810774 }
811775}
0 commit comments