Skip to content

Commit

Permalink
Merge pull request #17 from leanix/feature/CID-2896/handle-github-app…
Browse files Browse the repository at this point in the history
…-with-no-installation

CID-2896: Handle GitHub App with no installation
  • Loading branch information
mohamedlajmileanix authored Aug 9, 2024
2 parents 649f246 + 0b1b804 commit 5b33c79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,6 +46,11 @@ class GitHubScanningService(
private fun fetchAndSendOrganisationsData(
installations: List<Installation>
) {
if (installations.isEmpty()) {
logger.warn("No installations found, please install the GitHub App on an organization")
webSocketService.sendMessage("${cachingService.get("runId")}/organizations", emptyList<Organization>())
return
}
val installationToken = cachingService.get("installationToken:${installations.first().id}")
val organizations = gitHubClient.getOrganizations("Bearer $installationToken")
.map { organization ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Organization>())) }
}

@Test
fun `scanGitHubResources should throw JwtTokenNotFound when jwtToken is expired`() {
every { cachingService.get("jwtToken") } returns null
Expand Down

0 comments on commit 5b33c79

Please sign in to comment.