Skip to content

Commit b275f1f

Browse files
CID-3774: Addressing PR comments
1 parent 349f8ce commit b275f1f

File tree

4 files changed

+96
-62
lines changed

4 files changed

+96
-62
lines changed

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package net.leanix.githubagent.handler
22

3+
import net.leanix.githubagent.client.GitHubClient
34
import net.leanix.githubagent.dto.InstallationRequestDTO
4-
import net.leanix.githubagent.services.WebhookEventService
5+
import net.leanix.githubagent.exceptions.JwtTokenNotFound
6+
import net.leanix.githubagent.services.CachingService
7+
import net.leanix.githubagent.services.GitHubAuthenticationService
8+
import net.leanix.githubagent.services.GitHubEnterpriseService
9+
import net.leanix.githubagent.services.GitHubScanningService
10+
import net.leanix.githubagent.services.SyncLogService
11+
import net.leanix.githubagent.shared.INSTALLATION_LABEL
512
import org.slf4j.LoggerFactory
613
import org.springframework.beans.factory.annotation.Autowired
14+
import org.springframework.beans.factory.annotation.Value
715
import org.springframework.context.annotation.Lazy
816
import org.springframework.messaging.simp.stomp.StompFrameHandler
917
import org.springframework.messaging.simp.stomp.StompHeaders
@@ -13,7 +21,18 @@ import java.lang.reflect.Type
1321
@Component
1422
class InstallationGetHandler(
1523
@Lazy @Autowired
16-
private val webhookEventService: WebhookEventService
24+
private val gitHubAuthenticationService: GitHubAuthenticationService,
25+
@Lazy @Autowired
26+
private val gitHubScanningService: GitHubScanningService,
27+
@Lazy @Autowired
28+
private val gitHubEnterpriseService: GitHubEnterpriseService,
29+
@Lazy @Autowired
30+
private val cachingService: CachingService,
31+
@Lazy @Autowired
32+
private val syncLogService: SyncLogService,
33+
@Lazy @Autowired
34+
private val gitHubClient: GitHubClient,
35+
@Value("\${webhookEventService.waitingTime}") private val waitingTime: Long,
1736
) : StompFrameHandler {
1837

1938
private val logger = LoggerFactory.getLogger(InstallationGetHandler::class.java)
@@ -27,8 +46,37 @@ class InstallationGetHandler(
2746
val dto = payload as InstallationRequestDTO
2847
logger.info("Received installation get message from server for organisation: ${dto.account.login}")
2948
runCatching {
30-
webhookEventService.handleInstallationCreated(dto)
49+
fetchAndSendOrganisationData(dto)
50+
}
51+
}
52+
}
53+
54+
fun fetchAndSendOrganisationData(installationRequestDTO: InstallationRequestDTO) {
55+
while (cachingService.get("runId") != null) {
56+
logger.info("A full scan is already in progress, waiting for it to finish.")
57+
Thread.sleep(waitingTime)
58+
}
59+
syncLogService.sendFullScanStart(installationRequestDTO.account.login)
60+
kotlin.runCatching {
61+
val jwtToken = cachingService.get("jwtToken") ?: throw JwtTokenNotFound()
62+
val installation = gitHubClient.getInstallation(
63+
installationRequestDTO.id,
64+
"Bearer $jwtToken"
65+
)
66+
gitHubEnterpriseService.validateEnabledPermissionsAndEvents(
67+
INSTALLATION_LABEL,
68+
installation.permissions,
69+
installation.events
70+
)
71+
gitHubAuthenticationService.refreshTokens()
72+
gitHubScanningService.fetchAndSendOrganisationsData(listOf(installation))
73+
gitHubScanningService.fetchAndSendRepositoriesData(installation).forEach { repository ->
74+
gitHubScanningService.fetchManifestFilesAndSend(installation, repository)
3175
}
76+
}.onSuccess {
77+
syncLogService.sendFullScanSuccess()
78+
}.onFailure {
79+
syncLogService.sendFullScanFailure(it.message)
3280
}
3381
}
3482
}

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

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,22 @@ package net.leanix.githubagent.services
22

33
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
44
import com.fasterxml.jackson.module.kotlin.readValue
5-
import net.leanix.githubagent.client.GitHubClient
6-
import net.leanix.githubagent.dto.InstallationRequestDTO
75
import net.leanix.githubagent.dto.ManifestFileAction
86
import net.leanix.githubagent.dto.ManifestFileUpdateDto
97
import net.leanix.githubagent.dto.PushEventCommit
108
import net.leanix.githubagent.dto.PushEventPayload
11-
import net.leanix.githubagent.exceptions.JwtTokenNotFound
12-
import net.leanix.githubagent.shared.INSTALLATION_LABEL
139
import net.leanix.githubagent.shared.MANIFEST_FILE_NAME
1410
import net.leanix.githubagent.shared.fileNameMatchRegex
1511
import net.leanix.githubagent.shared.generateFullPath
1612
import org.slf4j.LoggerFactory
17-
import org.springframework.beans.factory.annotation.Value
1813
import org.springframework.stereotype.Service
1914

2015
@SuppressWarnings("TooManyFunctions")
2116
@Service
2217
class WebhookEventService(
2318
private val webSocketService: WebSocketService,
2419
private val gitHubGraphQLService: GitHubGraphQLService,
25-
private val cachingService: CachingService,
2620
private val gitHubAuthenticationService: GitHubAuthenticationService,
27-
private val gitHubScanningService: GitHubScanningService,
28-
private val syncLogService: SyncLogService,
29-
@Value("\${webhookEventService.waitingTime}") private val waitingTime: Long,
30-
private val gitHubClient: GitHubClient,
31-
private val gitHubEnterpriseService: GitHubEnterpriseService
3221
) {
3322

3423
private val logger = LoggerFactory.getLogger(WebhookEventService::class.java)
@@ -66,35 +55,6 @@ class WebhookEventService(
6655
}
6756
}
6857

69-
fun handleInstallationCreated(installationRequestDTO: InstallationRequestDTO) {
70-
while (cachingService.get("runId") != null) {
71-
logger.info("A full scan is already in progress, waiting for it to finish.")
72-
Thread.sleep(waitingTime)
73-
}
74-
syncLogService.sendFullScanStart(installationRequestDTO.account.login)
75-
kotlin.runCatching {
76-
val jwtToken = cachingService.get("jwtToken") ?: throw JwtTokenNotFound()
77-
val installation = gitHubClient.getInstallation(
78-
installationRequestDTO.id,
79-
"Bearer $jwtToken"
80-
)
81-
gitHubEnterpriseService.validateEnabledPermissionsAndEvents(
82-
INSTALLATION_LABEL,
83-
installation.permissions,
84-
installation.events
85-
)
86-
gitHubAuthenticationService.refreshTokens()
87-
gitHubScanningService.fetchAndSendOrganisationsData(listOf(installation))
88-
gitHubScanningService.fetchAndSendRepositoriesData(installation).forEach { repository ->
89-
gitHubScanningService.fetchManifestFilesAndSend(installation, repository)
90-
}
91-
}.onSuccess {
92-
syncLogService.sendFullScanSuccess()
93-
}.onFailure {
94-
syncLogService.sendFullScanFailure(it.message)
95-
}
96-
}
97-
9858
private fun handleManifestFileChanges(
9959
defaultBranch: String,
10060
headCommit: PushEventCommit,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package net.leanix.githubagent.services
2+
3+
import com.ninjasquad.springmockk.MockkBean
4+
import com.ninjasquad.springmockk.SpykBean
5+
import io.mockk.every
6+
import io.mockk.just
7+
import io.mockk.runs
8+
import io.mockk.verify
9+
import net.leanix.githubagent.dto.Account
10+
import net.leanix.githubagent.dto.InstallationRequestDTO
11+
import net.leanix.githubagent.handler.InstallationGetHandler
12+
import org.junit.jupiter.api.Test
13+
import org.springframework.boot.test.context.SpringBootTest
14+
import org.springframework.test.context.ActiveProfiles
15+
16+
@SpringBootTest
17+
@ActiveProfiles("test")
18+
class InstallationGetHandlerTest {
19+
20+
@MockkBean
21+
private lateinit var webSocketService: WebSocketService
22+
23+
@MockkBean
24+
private lateinit var cachingService: CachingService
25+
26+
@SpykBean
27+
private lateinit var installationGetHandler: InstallationGetHandler
28+
29+
@Test
30+
fun `should wait for active scan to finish before starting scanning new org`() {
31+
every { cachingService.get("runId") } returnsMany listOf("value", "value", "value", null)
32+
every { cachingService.set("runId", any(), any()) } just runs
33+
every { cachingService.remove("runId") } just runs
34+
every { webSocketService.sendMessage(any(), any()) } returns Unit
35+
36+
val installationRequestDTO = InstallationRequestDTO(
37+
30,
38+
Account("test-org")
39+
)
40+
41+
installationGetHandler.fetchAndSendOrganisationData(installationRequestDTO)
42+
43+
verify(exactly = 6) { cachingService.get("runId") }
44+
}
45+
}

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ package net.leanix.githubagent.services
22

33
import com.ninjasquad.springmockk.MockkBean
44
import io.mockk.every
5-
import io.mockk.just
6-
import io.mockk.runs
75
import io.mockk.verify
86
import net.leanix.githubagent.client.GitHubClient
97
import net.leanix.githubagent.dto.Account
108
import net.leanix.githubagent.dto.Installation
11-
import net.leanix.githubagent.dto.InstallationRequestDTO
129
import net.leanix.githubagent.dto.ItemResponse
1310
import net.leanix.githubagent.dto.ManifestFileAction
1411
import net.leanix.githubagent.dto.ManifestFileUpdateDto
@@ -412,22 +409,6 @@ class WebhookEventServiceTest {
412409
}
413410
}
414411

415-
@Test
416-
fun `should wait for active scan to finish before starting scanning new org`() {
417-
every { cachingService.get("runId") } returnsMany listOf("value", "value", "value", null)
418-
every { cachingService.set("runId", any(), any()) } just runs
419-
every { cachingService.remove("runId") } just runs
420-
421-
val installationRequestDTO = InstallationRequestDTO(
422-
30,
423-
Account("test-org")
424-
)
425-
426-
webhookEventService.handleInstallationCreated(installationRequestDTO)
427-
428-
verify(exactly = 6) { cachingService.get("runId") }
429-
}
430-
431412
@Test
432413
fun `should ignore push events without a head commit`() {
433414
val payload = """{

0 commit comments

Comments
 (0)