Skip to content

Commit 0b4e2e2

Browse files
Merge pull request #88 from leanix/feature/cid-3656-simplify-sbom-ingestion
Feature/cid 3656 simplify SBOM ingestion
2 parents aacc4bf + 2f12f47 commit 0b4e2e2

File tree

12 files changed

+246
-314
lines changed

12 files changed

+246
-314
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.leanix.githubagent.dto
2+
3+
data class ArtifactDTO(
4+
val repositoryFullName: String,
5+
val artifactFileName: String,
6+
val artifactFileContent: String,
7+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.leanix.githubagent.dto
2+
3+
data class ArtifactDownloadDTO(
4+
val repositoryName: String,
5+
val repositoryOwner: String,
6+
val runId: Long,
7+
val installationId: Int,
8+
val artifactName: String? = null,
9+
)

src/main/kotlin/net/leanix/githubagent/dto/SbomConfig.kt

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/kotlin/net/leanix/githubagent/dto/SbomEventDTO.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/kotlin/net/leanix/githubagent/dto/WorkflowRunEventDto.kt

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import org.springframework.stereotype.Component
1212
import java.util.concurrent.CountDownLatch
1313

1414
@Component
15-
class BrokerStompSessionHandler : StompSessionHandlerAdapter() {
15+
class BrokerStompSessionHandler(
16+
private val artifactDownloadHandler: ArtifactDownloadHandler
17+
) : StompSessionHandlerAdapter() {
1618
@Lazy
1719
@Autowired
1820
private lateinit var webSocketService: WebSocketService
@@ -27,7 +29,7 @@ class BrokerStompSessionHandler : StompSessionHandlerAdapter() {
2729
logger.info("connected to the server: ${session.sessionId}")
2830
isConnected = true
2931
latch.countDown()
30-
session.subscribe("/user/queue/repositories-string", this)
32+
session.subscribe("/user/queue/message/artifact", artifactDownloadHandler)
3133
}
3234

3335
override fun handleException(

src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import net.leanix.githubagent.handler.RateLimitHandler
1919
import net.leanix.githubagent.shared.INSTALLATION_LABEL
2020
import net.leanix.githubagent.shared.INSTALLATION_REPOSITORIES
2121
import net.leanix.githubagent.shared.MANIFEST_FILE_NAME
22-
import net.leanix.githubagent.shared.WORKFLOW_RUN_EVENT
2322
import net.leanix.githubagent.shared.fileNameMatchRegex
2423
import net.leanix.githubagent.shared.generateFullPath
2524
import org.slf4j.LoggerFactory
@@ -38,7 +37,6 @@ class WebhookEventService(
3837
@Value("\${webhookEventService.waitingTime}") private val waitingTime: Long,
3938
private val gitHubClient: GitHubClient,
4039
private val gitHubEnterpriseService: GitHubEnterpriseService,
41-
private val workflowRunService: WorkflowRunService,
4240
private val rateLimitHandler: RateLimitHandler,
4341
) {
4442

@@ -50,10 +48,9 @@ class WebhookEventService(
5048
"PUSH" -> handlePushEvent(payload)
5149
"INSTALLATION" -> handleInstallationEvent(payload)
5250
INSTALLATION_REPOSITORIES -> handleInstallationRepositories(payload)
53-
WORKFLOW_RUN_EVENT -> workflowRunService.consumeWebhookPayload(payload)
5451
else -> {
5552
logger.info("Sending event of type: $eventType")
56-
webSocketService.sendMessage("/events/other", payload)
53+
webSocketService.sendMessage("/events/$eventType", payload)
5754
}
5855
}
5956
}

src/main/kotlin/net/leanix/githubagent/services/WorkflowRunService.kt

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)