diff --git a/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt index 6223704..ab6402f 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt @@ -2,6 +2,7 @@ package net.leanix.githubagent.services import net.leanix.githubagent.client.GitHubClient import net.leanix.githubagent.dto.Installation +import net.leanix.githubagent.dto.Organization import net.leanix.githubagent.dto.OrganizationDto import net.leanix.githubagent.exceptions.JwtTokenNotFound import org.slf4j.LoggerFactory @@ -45,6 +46,11 @@ class GitHubScanningService( private fun fetchAndSendOrganisationsData( installations: List ) { + if (installations.isEmpty()) { + logger.warn("No installations found, please install the GitHub App on an organization") + webSocketService.sendMessage("${cachingService.get("runId")}/organizations", emptyList()) + return + } val installationToken = cachingService.get("installationToken:${installations.first().id}") val organizations = gitHubClient.getOrganizations("Bearer $installationToken") .map { organization -> diff --git a/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt index e9134ed..6253db6 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt @@ -59,6 +59,18 @@ class GitHubScanningServiceTest { verify { webSocketService.sendMessage(eq("$runId/organizations"), any()) } } + @Test + fun `scanGitHubResources should handle empty installations`() { + val runId = UUID.randomUUID() + + every { cachingService.get("runId") } returns runId + every { gitHubClient.getInstallations(any()) } returns emptyList() + + gitHubScanningService.scanGitHubResources() + + verify { webSocketService.sendMessage(eq("$runId/organizations"), eq(emptyList())) } + } + @Test fun `scanGitHubResources should throw JwtTokenNotFound when jwtToken is expired`() { every { cachingService.get("jwtToken") } returns null