Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import net.leanix.githubagent.services.WebSocketService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Lazy
import org.springframework.http.HttpStatus
import org.springframework.messaging.simp.stomp.StompFrameHandler
import org.springframework.messaging.simp.stomp.StompHeaders
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -72,11 +73,16 @@ class ArtifactDownloadHandler(

private fun downloadAndSendArtifact(dto: ArtifactDownloadDTO, artifact: Artifact, token: String) = runCatching {
val owner = dto.repositoryOwner
val repo = dto.repositoryOwner
gitHubClient.downloadArtifact(owner, repo, artifact.id, token).body()?.use { body ->
val artifactContent = Base64.getEncoder().encodeToString(body.asInputStream().readAllBytes())
sendArtifactEvent(dto, artifact.name, artifactContent)
} ?: logger.error("Failed to download artifact: ${artifact.name}")
val repo = dto.repositoryName
val response = gitHubClient.downloadArtifact(owner, repo, artifact.id, token)
if (response.status() == HttpStatus.OK.value()) {
response.body()?.use { body ->
val artifactContent = Base64.getEncoder().encodeToString(body.asInputStream().readAllBytes())
sendArtifactEvent(dto, artifact.name, artifactContent)
}
} else {
logger.error("Failed to download artifact: ${artifact.name}")
}
}.onFailure {
logger.error("Error processing artifact: ${artifact.name}", it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class ArtifactDownloadHandlerTest {
every { gitHubClient.downloadArtifact(any(), any(), any(), any()) } returns Response
.builder()
.request(request)
.status(200)
.body(readSbomFile()).build()

// when
Expand Down Expand Up @@ -104,7 +105,9 @@ class ArtifactDownloadHandlerTest {
every { gitHubClient.downloadArtifact(any(), any(), any(), any()) } returns Response
.builder()
.request(request)
.body(readSbomFile()).build()
.body(readSbomFile())
.status(200)
.build()

// when
artifactDownloadHandler.handleFrame(
Expand All @@ -122,6 +125,52 @@ class ArtifactDownloadHandlerTest {
webSocketService.sendMessage("/artifact", any())
}
}

@Test
fun `it should receive message from server and not send empty artifact`() {
// given
every { gitHubAuthenticationService.getInstallationToken(any()) } returns "token"
every { gitHubClient.getRunArtifacts(any(), any(), any(), any()) } returns ArtifactsListResponse(
totalCount = 2,
artifacts = listOf(
Artifact(
id = 1,
name = "leanix-sbom-test-sbom",
url = "http://download.url",
archiveDownloadUrl = "http://download.url"
),
Artifact(
id = 2,
name = "invalid-name",
url = "http://download.url",
archiveDownloadUrl = "http://download.url"
)
)
)
val request = mockk<Request>()
every { gitHubClient.downloadArtifact(any(), any(), any(), any()) } returns Response
.builder()
.request(request)
.status(404)
.build()

// when
artifactDownloadHandler.handleFrame(
StompHeaders(),
ArtifactDownloadDTO(
repositoryName = "repository",
repositoryOwner = "leanix",
runId = 1,
installationId = 1,
)
)

// then
verify(exactly = 0) {
webSocketService.sendMessage("/artifact", any())
}
}

private fun readSbomFile(): ByteArray {
val filePath = Paths.get("src/test/resources/sbom/leanix-sbom-test-sbom.zip")
return Files.readAllBytes(filePath)
Expand Down
Loading