suspend fun downloadArtifact()

in plugins/amazonq/codetransform/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codemodernizer/ArtifactHandler.kt [160:277]


    suspend fun downloadArtifact(
        job: JobId,
        artifactType: TransformationDownloadArtifactType,
        isPreFetch: Boolean = false,
    ): DownloadArtifactResult {
        isCurrentlyDownloading.set(true)
        val downloadStartTime = Instant.now()
        try {
            // 1. Attempt reusing previously downloaded artifact for job
            val previousArtifact = if (artifactType == TransformationDownloadArtifactType.LOGS) {
                downloadedBuildLogPath.getOrDefault(job, null)
            } else {
                downloadedArtifacts.getOrDefault(job, null)
            }
            if (previousArtifact != null && previousArtifact.exists()) {
                val zipPath = previousArtifact.toAbsolutePath().toString()
                return try {
                    if (artifactType == TransformationDownloadArtifactType.LOGS) {
                        DownloadArtifactResult.Success(CodeTransformFailureBuildLog.create(zipPath), zipPath)
                    } else {
                        val artifact = CodeModernizerArtifact.create(zipPath)
                        downloadedSummaries[job] = artifact.summary
                        DownloadArtifactResult.Success(artifact, zipPath)
                    }
                } catch (e: RuntimeException) {
                    LOG.error { e.message.toString() }
                    DownloadArtifactResult.ParseZipFailure(ParseZipFailureReason(artifactType, e.message.orEmpty()))
                }
            }

            // 2. Download the data
            LOG.info { "Verifying user is authenticated prior to download" }
            if (!isValidCodeTransformConnection(project)) {
                CodeModernizerManager.getInstance(project).handleResumableDownloadArtifactFailure(job)
                return DownloadArtifactResult.DownloadFailure(DownloadFailureReason.CREDENTIALS_EXPIRED(artifactType))
            }

            LOG.info { "About to download the export result archive" }
            // only notify if downloading client instructions (upgraded code)
            if (artifactType == TransformationDownloadArtifactType.CLIENT_INSTRUCTIONS) {
                notifyDownloadStart()
            }
            val downloadResultsResponse = if (artifactType == TransformationDownloadArtifactType.LOGS) {
                clientAdaptor.downloadExportResultArchive(job, null, TransformationDownloadArtifactType.LOGS)
            } else {
                clientAdaptor.downloadExportResultArchive(job)
            }

            // 3. Convert to zip
            LOG.info { "Downloaded the export result archive, about to transform to zip" }
            val path: Path
            val totalDownloadBytes: Int
            val zipPath: String
            try {
                val result = unzipToPath(downloadResultsResponse)
                path = result.first
                totalDownloadBytes = result.second
                zipPath = path.toAbsolutePath().toString()
                LOG.info { "Successfully converted the download to a zip at $zipPath." }
            } catch (e: Exception) {
                LOG.error { e.message.toString() }
                return DownloadArtifactResult.UnzipFailure(UnzipFailureReason(artifactType, e.message.orEmpty()))
            }

            // 4. Deserialize zip
            var telemetryErrorMessage: String? = null
            return try {
                val output = if (artifactType == TransformationDownloadArtifactType.LOGS) {
                    DownloadArtifactResult.Success(CodeTransformFailureBuildLog.create(zipPath), zipPath)
                } else {
                    DownloadArtifactResult.Success(CodeModernizerArtifact.create(zipPath), zipPath)
                }
                if (artifactType == TransformationDownloadArtifactType.LOGS) {
                    downloadedBuildLogPath[job] = path
                } else {
                    downloadedArtifacts[job] = path
                    if (output.artifact is CodeModernizerArtifact && output.artifact.metrics != null) {
                        output.artifact.metrics.linesOfCodeSubmitted = CodeModernizerSessionState.getInstance(project).getLinesOfCodeSubmitted()
                        output.artifact.metrics.programmingLanguage = CodeModernizerSessionState.getInstance(project).getTransformationLanguage()
                        try {
                            clientAdaptor.sendTransformTelemetryEvent(job, output.artifact.metrics)
                        } catch (e: Exception) {
                            // log error, but can still show diff.patch and summary.md
                            LOG.error { e.message.toString() }
                            telemetryErrorMessage = "Unexpected error when sending telemetry with metrics ${e.localizedMessage}"
                        }
                    }
                }
                output
            } catch (e: RuntimeException) {
                LOG.error { e.message.toString() }
                LOG.error { "Unable to find patch for file: $zipPath" }
                telemetryErrorMessage = "Unexpected error when downloading result ${e.localizedMessage}"
                DownloadArtifactResult.ParseZipFailure(ParseZipFailureReason(artifactType, e.message.orEmpty()))
            } finally {
                telemetry.downloadArtifact(mapArtifactTypes(artifactType), downloadStartTime, job, totalDownloadBytes, telemetryErrorMessage)
            }
        } catch (e: Exception) {
            if (isPreFetch) return DownloadArtifactResult.Skipped
            return when {
                e is SsoOidcException || e is NoTokenInitializedException -> {
                    CodeModernizerManager.getInstance(project).handleResumableDownloadArtifactFailure(job)
                    DownloadArtifactResult.DownloadFailure(DownloadFailureReason.CREDENTIALS_EXPIRED(artifactType))
                }
                e.message.toString().contains(DOWNLOAD_PROXY_WILDCARD_ERROR) ->
                    DownloadArtifactResult.DownloadFailure(DownloadFailureReason.PROXY_WILDCARD_ERROR(artifactType))
                e.message.toString().contains(DOWNLOAD_SSL_HANDSHAKE_ERROR) ->
                    DownloadArtifactResult.DownloadFailure(DownloadFailureReason.SSL_HANDSHAKE_ERROR(artifactType))

                e.message.toString().contains(INVALID_ARTIFACT_ERROR) ->
                    DownloadArtifactResult.DownloadFailure(DownloadFailureReason.INVALID_ARTIFACT(artifactType))

                else -> DownloadArtifactResult.DownloadFailure(DownloadFailureReason.OTHER(artifactType, e.message.orEmpty()))
            }
        } finally {
            isCurrentlyDownloading.set(false)
        }
    }