Skip to content

Commit

Permalink
CID-2744: addressing PR comments, add runId in calls to backend, and …
Browse files Browse the repository at this point in the history
…update RepositoryDto
  • Loading branch information
mohamedlajmileanix committed Jul 23, 2024
1 parent 9dfedf6 commit 68f0dbc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/main/kotlin/net/leanix/githubagent/dto/RepositoryDto.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package net.leanix.githubagent.dto

import net.leanix.githubbroker.connector.adapter.graphql.data.enums.RepositoryVisibility

data class RepositoryDto(
val id: String,
val name: String,
val fullName: String,
val description: String?,
val url: String,
val organization: RepositoryOrganizationDto,
val isArchived: Boolean,
val visibility: RepositoryVisibility,
val updatedAt: String,
val languages: List<String>,
val topics: List<String>,
val manifest: String?,
)

class RepositoryOrganizationDto(
val id: String,
val name: String
val manifestFileContent: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class CachingService(
return cache.getIfPresent(key)?.value
}

fun remove(key: String) {
cache.invalidate(key)
}

@PostConstruct
private fun init() {
set("baseUrl", gitHubEnterpriseProperties.baseUrl, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import kotlinx.coroutines.runBlocking
import net.leanix.githubagent.config.GitHubEnterpriseProperties
import net.leanix.githubagent.dto.PagedRepositories
import net.leanix.githubagent.dto.RepositoryDto
import net.leanix.githubagent.dto.RepositoryOrganizationDto
import net.leanix.githubagent.exceptions.GraphQLApiException
import net.leanix.githubbroker.connector.adapter.graphql.data.GetRepositories
import net.leanix.githubbroker.connector.adapter.graphql.data.getrepositories.Blob
Expand Down Expand Up @@ -53,15 +52,15 @@ class GitHubGraphQLService(
RepositoryDto(
id = it!!.id,
name = it.name,
fullName = it.nameWithOwner,
description = it.description,
url = it.url,
organization = RepositoryOrganizationDto(
id = it.owner.id,
name = it.owner.login
),
isArchived = it.isArchived,
visibility = it.visibility,
updatedAt = it.updatedAt,
languages = it.languages!!.nodes!!.map { language -> language!!.name },
topics = it.repositoryTopics.nodes!!.map { topic -> topic!!.topic.name },
manifest = (it.`object` as Blob?)?.text
manifestFileContent = (it.`object` as Blob?)?.text
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.leanix.githubagent.dto.OrganizationDto
import net.leanix.githubagent.exceptions.JwtTokenNotFound
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.util.UUID

@Service
class GitHubScanningService(
Expand All @@ -14,13 +15,11 @@ class GitHubScanningService(
private val webSocketService: WebSocketService,
private val gitHubGraphQLService: GitHubGraphQLService
) {
companion object {
const val WS_ORGANIZATIONS_TOPIC = "/app/ghe/organizations"
const val WS_REPOSITORIES_TOPIC = "/app/ghe/repositories"
}

private val logger = LoggerFactory.getLogger(GitHubScanningService::class.java)

fun scanGitHubResources() {
cachingService.set("runId", UUID.randomUUID(), null)
runCatching {
val jwtToken = cachingService.get("jwtToken") ?: throw JwtTokenNotFound()
val installations = getInstallations(jwtToken.toString())
Expand All @@ -30,6 +29,7 @@ class GitHubScanningService(
fetchAndSendRepositoriesData(installation)
}
}.onFailure {
cachingService.remove("runId")
logger.error("Error while scanning GitHub resources")
throw it
}
Expand Down Expand Up @@ -64,7 +64,7 @@ class GitHubScanningService(
}
}
logger.info("Sending organizations data")
webSocketService.sendMessage(WS_ORGANIZATIONS_TOPIC, organizations)
webSocketService.sendMessage("/ghe/${cachingService.get("runId")}/organizations", organizations)
}

private fun fetchAndSendRepositoriesData(installation: Installation) {
Expand All @@ -78,7 +78,10 @@ class GitHubScanningService(
cursor = cursor
)
logger.info("Sending page $page of repositories")
webSocketService.sendMessage(WS_REPOSITORIES_TOPIC, repositoriesPage.repositories)
webSocketService.sendMessage(
"/ghe/${cachingService.get("runId")}/repositories",
repositoriesPage.repositories
)
cursor = repositoriesPage.cursor
totalRepos += repositoriesPage.repositories.size
page++
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/graphql/GetRepositories.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ query GetRepositories($pageCount: Int!, $cursor: String, $expression: String!) {
nodes {
id
name
nameWithOwner
description
url
owner{
id
login
}
isArchived
visibility
updatedAt
languages(first: 10) {
nodes {
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import net.leanix.githubagent.dto.InstallationTokenResponse
import net.leanix.githubagent.dto.Organization
import net.leanix.githubagent.dto.PagedRepositories
import net.leanix.githubagent.dto.RepositoryDto
import net.leanix.githubagent.dto.RepositoryOrganizationDto
import net.leanix.githubagent.exceptions.JwtTokenNotFound
import net.leanix.githubbroker.connector.adapter.graphql.data.enums.RepositoryVisibility
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
Expand Down Expand Up @@ -44,12 +44,13 @@ class GitHubScanningServiceTest {
hasNextPage = false,
cursor = null
)
every { cachingService.remove(any()) } returns Unit
}

@Test
fun `scanGitHubResources should send organizations over WebSocket`() {
gitHubScanningService.scanGitHubResources()
verify { webSocketService.sendMessage("/app/ghe/organizations", any()) }
verify { webSocketService.sendMessage(any(), any()) }
}

@Test
Expand All @@ -67,18 +68,21 @@ class GitHubScanningServiceTest {
RepositoryDto(
id = "repo1",
name = "TestRepo",
fullName = "testOrg/testRepo",
description = "A test repository",
url = "https://github.com/testRepo",
organization = RepositoryOrganizationDto(id = "org1", name = "TestOrg"),
isArchived = false,
visibility = RepositoryVisibility.PUBLIC,
updatedAt = "2024-01-01T00:00:00Z",
languages = listOf("Kotlin", "Java"),
topics = listOf("test", "example"),
manifest = "dependencies { implementation 'com.example:example-lib:1.0.0' }"
manifestFileContent = "dependencies { implementation 'com.example:example-lib:1.0.0' }"
)
),
hasNextPage = false,
cursor = null
)
gitHubScanningService.scanGitHubResources()
verify { webSocketService.sendMessage(eq("/app/ghe/repositories"), any()) }
verify { webSocketService.sendMessage(any(), any()) }
}
}

0 comments on commit 68f0dbc

Please sign in to comment.