Skip to content

Commit 9abee99

Browse files
committed
Create helper functions for history operations
1 parent ed381aa commit 9abee99

File tree

5 files changed

+229
-152
lines changed

5 files changed

+229
-152
lines changed

packages/core/src/codewhisperer/commands/startTransformByQ.ts

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import * as vscode from 'vscode'
77
import * as fs from 'fs' // eslint-disable-line no-restricted-imports
8-
import os from 'os'
98
import path from 'path'
109
import { getLogger } from '../../shared/logger/logger'
1110
import * as CodeWhispererConstants from '../models/constants'
@@ -79,6 +78,12 @@ import { convertDateToTimestamp } from '../../shared/datetime'
7978
import { findStringInDirectory } from '../../shared/utilities/workspaceUtils'
8079
import { makeTemporaryToolkitFolder } from '../../shared/filesystemUtilities'
8180
import { AuthUtil } from '../util/authUtil'
81+
import {
82+
cleanupTempJobFiles,
83+
createMetadataFile,
84+
JobMetadata,
85+
writeToHistoryFile,
86+
} from '../service/transformByQ/transformationHistoryHandler'
8287

8388
export 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
}

packages/core/src/codewhisperer/models/constants.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -941,13 +941,3 @@ export const displayFindingsSuffix = '_displayFindings'
941941

942942
export const displayFindingsDetectorName = 'DisplayFindings'
943943
export const findingsSuffix = '_codeReviewFindings'
944-
945-
export interface HistoryObject {
946-
startTime: string
947-
projectName: string
948-
status: string
949-
duration: string
950-
diffPath: string
951-
summaryPath: string
952-
jobId: string
953-
}

0 commit comments

Comments
 (0)