Skip to content

Commit 2b909dc

Browse files
Merge pull request #90 from leanix/feature/cid-3656-fix-download-artifact
cid-3656: Fix download artifact
2 parents 0b4e2e2 + a5e94c2 commit 2b909dc

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/main/kotlin/net/leanix/githubagent/handler/ArtifactDownloadHandler.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import net.leanix.githubagent.services.WebSocketService
99
import org.slf4j.LoggerFactory
1010
import org.springframework.beans.factory.annotation.Autowired
1111
import org.springframework.context.annotation.Lazy
12+
import org.springframework.http.HttpStatus
1213
import org.springframework.messaging.simp.stomp.StompFrameHandler
1314
import org.springframework.messaging.simp.stomp.StompHeaders
1415
import org.springframework.stereotype.Component
@@ -72,11 +73,16 @@ class ArtifactDownloadHandler(
7273

7374
private fun downloadAndSendArtifact(dto: ArtifactDownloadDTO, artifact: Artifact, token: String) = runCatching {
7475
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}")
76+
val repo = dto.repositoryName
77+
val response = gitHubClient.downloadArtifact(owner, repo, artifact.id, token)
78+
if (response.status() == HttpStatus.OK.value()) {
79+
response.body()?.use { body ->
80+
val artifactContent = Base64.getEncoder().encodeToString(body.asInputStream().readAllBytes())
81+
sendArtifactEvent(dto, artifact.name, artifactContent)
82+
}
83+
} else {
84+
logger.error("Failed to download artifact: ${artifact.name}")
85+
}
8086
}.onFailure {
8187
logger.error("Error processing artifact: ${artifact.name}", it)
8288
}

src/test/kotlin/net/leanix/githubagent/handler/ArtifactDownloadHandlerTest.kt

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ArtifactDownloadHandlerTest {
5959
every { gitHubClient.downloadArtifact(any(), any(), any(), any()) } returns Response
6060
.builder()
6161
.request(request)
62+
.status(200)
6263
.body(readSbomFile()).build()
6364

6465
// when
@@ -104,7 +105,9 @@ class ArtifactDownloadHandlerTest {
104105
every { gitHubClient.downloadArtifact(any(), any(), any(), any()) } returns Response
105106
.builder()
106107
.request(request)
107-
.body(readSbomFile()).build()
108+
.body(readSbomFile())
109+
.status(200)
110+
.build()
108111

109112
// when
110113
artifactDownloadHandler.handleFrame(
@@ -122,6 +125,52 @@ class ArtifactDownloadHandlerTest {
122125
webSocketService.sendMessage("/artifact", any())
123126
}
124127
}
128+
129+
@Test
130+
fun `it should receive message from server and not send empty artifact`() {
131+
// given
132+
every { gitHubAuthenticationService.getInstallationToken(any()) } returns "token"
133+
every { gitHubClient.getRunArtifacts(any(), any(), any(), any()) } returns ArtifactsListResponse(
134+
totalCount = 2,
135+
artifacts = listOf(
136+
Artifact(
137+
id = 1,
138+
name = "leanix-sbom-test-sbom",
139+
url = "http://download.url",
140+
archiveDownloadUrl = "http://download.url"
141+
),
142+
Artifact(
143+
id = 2,
144+
name = "invalid-name",
145+
url = "http://download.url",
146+
archiveDownloadUrl = "http://download.url"
147+
)
148+
)
149+
)
150+
val request = mockk<Request>()
151+
every { gitHubClient.downloadArtifact(any(), any(), any(), any()) } returns Response
152+
.builder()
153+
.request(request)
154+
.status(404)
155+
.build()
156+
157+
// when
158+
artifactDownloadHandler.handleFrame(
159+
StompHeaders(),
160+
ArtifactDownloadDTO(
161+
repositoryName = "repository",
162+
repositoryOwner = "leanix",
163+
runId = 1,
164+
installationId = 1,
165+
)
166+
)
167+
168+
// then
169+
verify(exactly = 0) {
170+
webSocketService.sendMessage("/artifact", any())
171+
}
172+
}
173+
125174
private fun readSbomFile(): ByteArray {
126175
val filePath = Paths.get("src/test/resources/sbom/leanix-sbom-test-sbom.zip")
127176
return Files.readAllBytes(filePath)

0 commit comments

Comments
 (0)