Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CID-2896: Handle GitHub App with no installation #17

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading