|
| 1 | +package net.leanix.githubagent.handler |
| 2 | + |
| 3 | +import net.leanix.githubagent.client.GitHubClient |
| 4 | +import net.leanix.githubagent.dto.Artifact |
| 5 | +import net.leanix.githubagent.dto.ArtifactDTO |
| 6 | +import net.leanix.githubagent.dto.ArtifactDownloadDTO |
| 7 | +import net.leanix.githubagent.services.GitHubAuthenticationService |
| 8 | +import net.leanix.githubagent.services.WebSocketService |
| 9 | +import org.slf4j.LoggerFactory |
| 10 | +import org.springframework.beans.factory.annotation.Autowired |
| 11 | +import org.springframework.context.annotation.Lazy |
| 12 | +import org.springframework.messaging.simp.stomp.StompFrameHandler |
| 13 | +import org.springframework.messaging.simp.stomp.StompHeaders |
| 14 | +import org.springframework.stereotype.Component |
| 15 | +import java.lang.reflect.Type |
| 16 | +import java.util.* |
| 17 | + |
| 18 | +@Component |
| 19 | +class ArtifactDownloadHandler( |
| 20 | + private val gitHubClient: GitHubClient, |
| 21 | + @Lazy @Autowired |
| 22 | + private val webSocketService: WebSocketService, |
| 23 | + @Lazy @Autowired |
| 24 | + private val gitHubAuthenticationService: GitHubAuthenticationService |
| 25 | +) : StompFrameHandler { |
| 26 | + |
| 27 | + private val logger = LoggerFactory.getLogger(ArtifactDownloadHandler::class.java) |
| 28 | + |
| 29 | + override fun getPayloadType(headers: StompHeaders): Type { |
| 30 | + return ArtifactDownloadDTO::class.java |
| 31 | + } |
| 32 | + |
| 33 | + override fun handleFrame(headers: StompHeaders, payload: Any?) { |
| 34 | + payload?.let { |
| 35 | + val dto = payload as ArtifactDownloadDTO |
| 36 | + logger.info("Received artifact download message from server for repo: ${dto.repositoryName}") |
| 37 | + runCatching { |
| 38 | + val installationToken = |
| 39 | + "Bearer ${gitHubAuthenticationService.getInstallationToken(dto.installationId)}" |
| 40 | + |
| 41 | + getValidArtifacts(dto, installationToken) |
| 42 | + .takeIf { it.isNotEmpty() } |
| 43 | + ?.let { artifacts -> |
| 44 | + logger.info("Found ${artifacts.size} artifact(s).") |
| 45 | + fetchAndProcessArtifacts(artifacts, dto, installationToken) |
| 46 | + } ?: logger.info("No artifacts found for this repository: ${dto.repositoryName}") |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + private fun getValidArtifacts(dto: ArtifactDownloadDTO, token: String): List<Artifact> { |
| 51 | + return gitHubClient.getRunArtifacts(dto.repositoryOwner, dto.repositoryName, dto.runId, token) |
| 52 | + .artifacts |
| 53 | + .filter { |
| 54 | + if (dto.artifactName != null) { |
| 55 | + it.name.contains(dto.artifactName) |
| 56 | + } else { |
| 57 | + true |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private fun fetchAndProcessArtifacts( |
| 63 | + artifacts: List<Artifact>, |
| 64 | + dto: ArtifactDownloadDTO, |
| 65 | + installationToken: String |
| 66 | + ) { |
| 67 | + artifacts.forEach { artifact -> |
| 68 | + logger.info("Processing artifact: ${artifact.name}") |
| 69 | + downloadAndSendArtifact(dto, artifact, installationToken) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private fun downloadAndSendArtifact(dto: ArtifactDownloadDTO, artifact: Artifact, token: String) = runCatching { |
| 74 | + val owner = dto.repositoryOwner |
| 75 | + val repo = dto.repositoryOwner |
| 76 | + gitHubClient.downloadArtifact(owner, repo, artifact.id, token).body()?.use { body -> |
| 77 | + val artifactContent = Base64.getEncoder().encodeToString(body.asInputStream().readAllBytes()) |
| 78 | + sendArtifactEvent(dto, artifact.name, artifactContent) |
| 79 | + } ?: logger.error("Failed to download artifact: ${artifact.name}") |
| 80 | + }.onFailure { |
| 81 | + logger.error("Error processing artifact: ${artifact.name}", it) |
| 82 | + } |
| 83 | + |
| 84 | + private fun sendArtifactEvent(dto: ArtifactDownloadDTO, artifactName: String, artifactContent: String) { |
| 85 | + logger.info("Sending artifacts file: ${dto.repositoryName} - $artifactName") |
| 86 | + webSocketService.sendMessage( |
| 87 | + "/artifact", |
| 88 | + ArtifactDTO( |
| 89 | + repositoryFullName = "${dto.repositoryOwner}/${dto.repositoryName}", |
| 90 | + artifactFileName = artifactName, |
| 91 | + artifactFileContent = artifactContent, |
| 92 | + ) |
| 93 | + ) |
| 94 | + } |
| 95 | +} |
0 commit comments