From 68f0dbc3c7d1233666bdd05885a2100bd96f08ac Mon Sep 17 00:00:00 2001 From: mohamedlajmileanix Date: Tue, 23 Jul 2024 13:42:39 +0200 Subject: [PATCH] CID-2744: addressing PR comments, add runId in calls to backend, and update RepositoryDto --- .../net/leanix/githubagent/dto/RepositoryDto.kt | 14 +++++++------- .../leanix/githubagent/services/CachingService.kt | 4 ++++ .../githubagent/services/GitHubGraphQLService.kt | 11 +++++------ .../githubagent/services/GitHubScanningService.kt | 15 +++++++++------ .../resources/graphql/GetRepositories.graphql | 8 ++++---- .../services/GitHubScanningServiceTest.kt | 14 +++++++++----- 6 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/net/leanix/githubagent/dto/RepositoryDto.kt b/src/main/kotlin/net/leanix/githubagent/dto/RepositoryDto.kt index 98e3220..1d5725d 100644 --- a/src/main/kotlin/net/leanix/githubagent/dto/RepositoryDto.kt +++ b/src/main/kotlin/net/leanix/githubagent/dto/RepositoryDto.kt @@ -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, val topics: List, - val manifest: String?, -) - -class RepositoryOrganizationDto( - val id: String, - val name: String + val manifestFileContent: String?, ) diff --git a/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt b/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt index a1a3822..b9f7774 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/CachingService.kt @@ -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) diff --git a/src/main/kotlin/net/leanix/githubagent/services/GitHubGraphQLService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubGraphQLService.kt index e19c1f7..3ef36be 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GitHubGraphQLService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubGraphQLService.kt @@ -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 @@ -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 ) } ) diff --git a/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt index 623d2d2..9465ade 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt @@ -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( @@ -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()) @@ -30,6 +29,7 @@ class GitHubScanningService( fetchAndSendRepositoriesData(installation) } }.onFailure { + cachingService.remove("runId") logger.error("Error while scanning GitHub resources") throw it } @@ -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) { @@ -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++ diff --git a/src/main/resources/graphql/GetRepositories.graphql b/src/main/resources/graphql/GetRepositories.graphql index 58013f5..9a2183d 100644 --- a/src/main/resources/graphql/GetRepositories.graphql +++ b/src/main/resources/graphql/GetRepositories.graphql @@ -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 diff --git a/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt index 4d757d6..c315ab0 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt @@ -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 @@ -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 @@ -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()) } } }