diff --git a/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt index 266043a..5061a7e 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt @@ -50,7 +50,7 @@ class GitHubScanningService( return installations } - private fun fetchAndSendOrganisationsData( + fun fetchAndSendOrganisationsData( installations: List ) { if (installations.isEmpty()) { diff --git a/src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt b/src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt index 60f1377..74fd3bf 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/WebhookEventService.kt @@ -82,6 +82,7 @@ class WebhookEventService( Account(installationEventPayload.installation.account.login) ) gitHubAuthenticationService.refreshTokens() + gitHubScanningService.fetchAndSendOrganisationsData(listOf(installation)) gitHubScanningService.fetchAndSendRepositoriesData(installation).forEach { repository -> gitHubScanningService.fetchManifestFilesAndSend(installation, repository) } diff --git a/src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt index 39672c7..d07e8a6 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt @@ -5,14 +5,17 @@ import io.mockk.every import io.mockk.just import io.mockk.runs import io.mockk.verify +import net.leanix.githubagent.client.GitHubClient import net.leanix.githubagent.dto.ManifestFileAction import net.leanix.githubagent.dto.ManifestFileUpdateDto +import net.leanix.githubagent.dto.Organization import net.leanix.githubagent.shared.MANIFEST_FILE_NAME import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.ActiveProfiles +import java.util.UUID const val UNSUPPORTED_MANIFEST_EXTENSION = "leanix.yml" @@ -32,6 +35,9 @@ class WebhookEventServiceTest { @MockkBean private lateinit var gitHubAuthenticationService: GitHubAuthenticationService + @MockkBean + private lateinit var gitHubClient: GitHubClient + @Autowired private lateinit var webhookEventService: WebhookEventService @@ -338,4 +344,34 @@ class WebhookEventServiceTest { verify(exactly = 6) { cachingService.get("runId") } } + + @Test + fun `should send the org to the backend when an new installation is created`() { + val runId = UUID.randomUUID() + every { cachingService.get("runId") } returnsMany listOf("value", null, runId) + every { cachingService.set("runId", any(), any()) } just runs + every { cachingService.remove("runId") } just runs + every { gitHubClient.getOrganizations(any()) } returns listOf(Organization("testOrganization", 1)) + + val eventType = "INSTALLATION" + val payload = """{ + "action": "created", + "installation": { + "id": 30, + "account": { + "login": "test-org", + "id": 20 + } + } + }""" + + webhookEventService.consumeWebhookEvent(eventType, payload) + + verify { + webSocketService.sendMessage( + "$runId/organizations", + any() + ) + } + } }