Skip to content

Commit 349f8ce

Browse files
CID-3774: Move logic of processing installation event to backend
1 parent f2b4b82 commit 349f8ce

File tree

6 files changed

+52
-78
lines changed

6 files changed

+52
-78
lines changed

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

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.leanix.githubagent.dto
2+
3+
data class InstallationRequestDTO(
4+
val id: Long,
5+
val account: Account,
6+
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.util.concurrent.CountDownLatch
1515
class BrokerStompSessionHandler(
1616
private val artifactDownloadHandler: ArtifactDownloadHandler,
1717
private val repositoryGetHandler: RepositoryGetHandler,
18+
private val installationGetHandler: InstallationGetHandler,
1819
) : StompSessionHandlerAdapter() {
1920
@Lazy
2021
@Autowired
@@ -32,6 +33,7 @@ class BrokerStompSessionHandler(
3233
latch.countDown()
3334
session.subscribe("/user/queue/message/artifact", artifactDownloadHandler)
3435
session.subscribe("/user/queue/message/repository", repositoryGetHandler)
36+
session.subscribe("/user/queue/message/installation", installationGetHandler)
3537
}
3638

3739
override fun handleException(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.leanix.githubagent.handler
2+
3+
import net.leanix.githubagent.dto.InstallationRequestDTO
4+
import net.leanix.githubagent.services.WebhookEventService
5+
import org.slf4j.LoggerFactory
6+
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.context.annotation.Lazy
8+
import org.springframework.messaging.simp.stomp.StompFrameHandler
9+
import org.springframework.messaging.simp.stomp.StompHeaders
10+
import org.springframework.stereotype.Component
11+
import java.lang.reflect.Type
12+
13+
@Component
14+
class InstallationGetHandler(
15+
@Lazy @Autowired
16+
private val webhookEventService: WebhookEventService
17+
) : StompFrameHandler {
18+
19+
private val logger = LoggerFactory.getLogger(InstallationGetHandler::class.java)
20+
21+
override fun getPayloadType(headers: StompHeaders): Type {
22+
return InstallationRequestDTO::class.java
23+
}
24+
25+
override fun handleFrame(headers: StompHeaders, payload: Any?) {
26+
payload?.let {
27+
val dto = payload as InstallationRequestDTO
28+
logger.info("Received installation get message from server for organisation: ${dto.account.login}")
29+
runCatching {
30+
webhookEventService.handleInstallationCreated(dto)
31+
}
32+
}
33+
}
34+
}

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package net.leanix.githubagent.services
33
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
44
import com.fasterxml.jackson.module.kotlin.readValue
55
import net.leanix.githubagent.client.GitHubClient
6-
import net.leanix.githubagent.dto.InstallationEventPayload
6+
import net.leanix.githubagent.dto.InstallationRequestDTO
77
import net.leanix.githubagent.dto.ManifestFileAction
88
import net.leanix.githubagent.dto.ManifestFileUpdateDto
99
import net.leanix.githubagent.dto.PushEventCommit
@@ -37,7 +37,6 @@ class WebhookEventService(
3737
fun consumeWebhookEvent(eventType: String, payload: String) {
3838
when (eventType.uppercase()) {
3939
"PUSH" -> handlePushEvent(payload)
40-
"INSTALLATION" -> handleInstallationEvent(payload)
4140
else -> {
4241
logger.info("Sending event of type: $eventType")
4342
webSocketService.sendMessage("/events/other/$eventType", payload)
@@ -67,23 +66,16 @@ class WebhookEventService(
6766
}
6867
}
6968

70-
private fun handleInstallationEvent(payload: String) {
71-
val installationEventPayload: InstallationEventPayload = objectMapper.readValue(payload)
72-
if (installationEventPayload.action == "created") {
73-
handleInstallationCreated(installationEventPayload)
74-
}
75-
}
76-
77-
private fun handleInstallationCreated(installationEventPayload: InstallationEventPayload) {
69+
fun handleInstallationCreated(installationRequestDTO: InstallationRequestDTO) {
7870
while (cachingService.get("runId") != null) {
7971
logger.info("A full scan is already in progress, waiting for it to finish.")
8072
Thread.sleep(waitingTime)
8173
}
82-
syncLogService.sendFullScanStart(installationEventPayload.installation.account.login)
74+
syncLogService.sendFullScanStart(installationRequestDTO.account.login)
8375
kotlin.runCatching {
8476
val jwtToken = cachingService.get("jwtToken") ?: throw JwtTokenNotFound()
8577
val installation = gitHubClient.getInstallation(
86-
installationEventPayload.installation.id.toLong(),
78+
installationRequestDTO.id,
8779
"Bearer $jwtToken"
8880
)
8981
gitHubEnterpriseService.validateEnabledPermissionsAndEvents(

src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ import io.mockk.verify
88
import net.leanix.githubagent.client.GitHubClient
99
import net.leanix.githubagent.dto.Account
1010
import net.leanix.githubagent.dto.Installation
11+
import net.leanix.githubagent.dto.InstallationRequestDTO
1112
import net.leanix.githubagent.dto.ItemResponse
1213
import net.leanix.githubagent.dto.ManifestFileAction
1314
import net.leanix.githubagent.dto.ManifestFileUpdateDto
14-
import net.leanix.githubagent.dto.Organization
1515
import net.leanix.githubagent.dto.RepositoryItemResponse
1616
import net.leanix.githubagent.shared.MANIFEST_FILE_NAME
1717
import org.junit.jupiter.api.BeforeEach
1818
import org.junit.jupiter.api.Test
1919
import org.springframework.beans.factory.annotation.Autowired
2020
import org.springframework.boot.test.context.SpringBootTest
2121
import org.springframework.test.context.ActiveProfiles
22-
import java.util.UUID
2322

2423
const val UNSUPPORTED_MANIFEST_EXTENSION = "leanix.yml"
2524

@@ -419,19 +418,12 @@ class WebhookEventServiceTest {
419418
every { cachingService.set("runId", any(), any()) } just runs
420419
every { cachingService.remove("runId") } just runs
421420

422-
val eventType = "INSTALLATION"
423-
val payload = """{
424-
"action": "created",
425-
"installation": {
426-
"id": 30,
427-
"account": {
428-
"login": "test-org",
429-
"id": 20
430-
}
431-
}
432-
}"""
421+
val installationRequestDTO = InstallationRequestDTO(
422+
30,
423+
Account("test-org")
424+
)
433425

434-
webhookEventService.consumeWebhookEvent(eventType, payload)
426+
webhookEventService.handleInstallationCreated(installationRequestDTO)
435427

436428
verify(exactly = 6) { cachingService.get("runId") }
437429
}
@@ -456,37 +448,6 @@ class WebhookEventServiceTest {
456448
}
457449
}
458450

459-
@Test
460-
fun `should send the org to the backend when an new installation is created`() {
461-
val runId = UUID.randomUUID()
462-
every { cachingService.get("runId") } returnsMany listOf("value", null, runId)
463-
every { cachingService.set("runId", any(), any()) } just runs
464-
every { cachingService.remove("runId") } just runs
465-
every { gitHubAPIService.getPaginatedOrganizations(any()) } returns
466-
listOf(Organization("testOrganization", 1))
467-
468-
val eventType = "INSTALLATION"
469-
val payload = """{
470-
"action": "created",
471-
"installation": {
472-
"id": 30,
473-
"account": {
474-
"login": "test-org",
475-
"id": 20
476-
}
477-
}
478-
}"""
479-
480-
webhookEventService.consumeWebhookEvent(eventType, payload)
481-
482-
verify {
483-
webSocketService.sendMessage(
484-
"$runId/organizations",
485-
any()
486-
)
487-
}
488-
}
489-
490451
fun createItemResponse(repoName: String, organization: String): ItemResponse {
491452
return ItemResponse(
492453
MANIFEST_FILE_NAME,

0 commit comments

Comments
 (0)