diff --git a/src/main/kotlin/net/leanix/githubagent/dto/SyncLogDto.kt b/src/main/kotlin/net/leanix/githubagent/dto/SyncLogDto.kt index b608142..9b7224b 100644 --- a/src/main/kotlin/net/leanix/githubagent/dto/SyncLogDto.kt +++ b/src/main/kotlin/net/leanix/githubagent/dto/SyncLogDto.kt @@ -11,8 +11,9 @@ data class SyncLogDto( enum class Trigger { START_FULL_SYNC, + PROGRESS_FULL_SYNC, FINISH_FULL_SYNC, - GENERIC + SYSTEM } enum class LogLevel { diff --git a/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt index 6cd401e..8031c72 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubAuthenticationService.kt @@ -61,9 +61,9 @@ class GitHubAuthenticationService( }.onFailure { logger.error("Failed to generate/validate JWT token", it) if (it is GitHubAppInsufficientPermissionsException) { - syncLogService.sendErrorLog(it.message.toString()) + syncLogService.sendSystemErrorLog(it.message.toString()) } else { - syncLogService.sendErrorLog("Failed to generate/validate JWT token") + syncLogService.sendSystemErrorLog("Failed to generate/validate JWT token") } if (it is InvalidKeySpecException) { throw IllegalArgumentException("The provided private key is not in a valid PKCS8 format.", it) diff --git a/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt b/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt index 93bd6d0..a7765c8 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/GitHubScanningService.kt @@ -44,7 +44,11 @@ class GitHubScanningService( .forEach { repository -> fetchManifestFilesAndSend(installation, repository) } + syncLogService.sendInfoLog( + "Finished initial full scan for organization ${installation.account.login}" + ) } + syncLogService.sendInfoLog("Finished full scan for all available organizations") syncLogService.sendSyncLog( trigger = Trigger.FINISH_FULL_SYNC, logLevel = LogLevel.INFO, @@ -86,6 +90,11 @@ class GitHubScanningService( } } logger.info("Sending organizations data") + syncLogService.sendInfoLog( + "The connector found ${organizations.filter { it.installed }.size} " + + "organizations with GitHub application installed." + ) + syncLogService.sendInfoLog("The connector found ${organizations.size} available organizations.") webSocketService.sendMessage("${cachingService.get("runId")}/organizations", organizations) } @@ -141,6 +150,8 @@ class GitHubScanningService( repositoryName: String ) = runCatching { val installationToken = cachingService.get("installationToken:${installation.id}").toString() + syncLogService.sendInfoLog("Scanning repository $repositoryName for manifest files") + var numOfManifestFilesFound = 0 items.map { manifestFile -> val content = gitHubGraphQLService.getManifestFileContent( owner = installation.account.login, @@ -149,6 +160,8 @@ class GitHubScanningService( token = installationToken ) if (content != null) { + numOfManifestFilesFound++ + syncLogService.sendInfoLog("Fetched manifest file ${manifestFile.path} from repository $repositoryName") ManifestFileDTO( path = manifestFile.path.replace("/${ManifestFileName.YAML.fileName}", ""), content = content @@ -156,6 +169,8 @@ class GitHubScanningService( } else { throw ManifestFileNotFoundException() } + }.also { + syncLogService.sendInfoLog("Found $numOfManifestFilesFound manifest files in repository $repositoryName") } } } diff --git a/src/main/kotlin/net/leanix/githubagent/services/SyncLogService.kt b/src/main/kotlin/net/leanix/githubagent/services/SyncLogService.kt index bb3afa1..0a21696 100644 --- a/src/main/kotlin/net/leanix/githubagent/services/SyncLogService.kt +++ b/src/main/kotlin/net/leanix/githubagent/services/SyncLogService.kt @@ -13,14 +13,22 @@ class SyncLogService( private val cachingService: CachingService ) { fun sendErrorLog(message: String) { - sendSyncLog(message, LOGS_TOPIC, null, LogLevel.ERROR) + sendSyncLog(message, LOGS_TOPIC, Trigger.PROGRESS_FULL_SYNC, LogLevel.ERROR) } - fun sendSyncLog(message: String? = null, topic: String = LOGS_TOPIC, trigger: Trigger?, logLevel: LogLevel) { - val runId = cachingService.get("runId") as UUID + fun sendSystemErrorLog(message: String) { + sendSyncLog(message, LOGS_TOPIC, Trigger.SYSTEM, LogLevel.ERROR) + } + + fun sendInfoLog(message: String) { + sendSyncLog(message, LOGS_TOPIC, Trigger.PROGRESS_FULL_SYNC, LogLevel.INFO) + } + + fun sendSyncLog(message: String? = null, topic: String = LOGS_TOPIC, trigger: Trigger, logLevel: LogLevel) { + val runId = cachingService.get("runId")?.let { it as UUID } val syncLogDto = SyncLogDto( runId = runId, - trigger = trigger ?: Trigger.GENERIC, + trigger = trigger, logLevel = logLevel, message = message ) diff --git a/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt index 28ab72a..5a62e7f 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/GitHubAuthenticationServiceTest.kt @@ -33,7 +33,7 @@ class GitHubAuthenticationServiceTest { @BeforeEach fun setUp() { - every { syncLogService.sendErrorLog(any()) } returns Unit + every { syncLogService.sendSystemErrorLog(any()) } returns Unit } @Test @@ -68,6 +68,6 @@ class GitHubAuthenticationServiceTest { assertThrows(UnableToConnectToGitHubEnterpriseException::class.java) { githubAuthenticationService.generateAndCacheJwtToken() } - verify(exactly = 1) { syncLogService.sendErrorLog("Failed to generate/validate JWT token") } + verify(exactly = 1) { syncLogService.sendSystemErrorLog("Failed to generate/validate JWT token") } } } diff --git a/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt b/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt index e83c646..13a37ee 100644 --- a/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt +++ b/src/test/kotlin/net/leanix/githubagent/services/GitHubScanningServiceTest.kt @@ -56,6 +56,7 @@ class GitHubScanningServiceTest { every { cachingService.remove(any()) } returns Unit every { gitHubAuthenticationService.generateAndCacheInstallationTokens(any(), any()) } returns Unit every { syncLogService.sendErrorLog(any()) } returns Unit + every { syncLogService.sendInfoLog(any()) } returns Unit } @Test @@ -63,6 +64,8 @@ class GitHubScanningServiceTest { every { cachingService.get("runId") } returns runId gitHubScanningService.scanGitHubResources() verify { webSocketService.sendMessage(eq("$runId/organizations"), any()) } + verify { syncLogService.sendInfoLog("The connector found 0 organizations with GitHub application installed.") } + verify { syncLogService.sendInfoLog("The connector found 1 available organizations.") } } @Test @@ -154,5 +157,10 @@ class GitHubScanningServiceTest { // then verify { webSocketService.sendMessage(eq("$runId/manifestFiles"), any()) } + verify { syncLogService.sendInfoLog("Scanning repository TestRepo for manifest files") } + verify { syncLogService.sendInfoLog("Fetched manifest file dir/leanix.yaml from repository TestRepo") } + verify { syncLogService.sendInfoLog("Found 1 manifest files in repository TestRepo") } + verify { syncLogService.sendInfoLog("Finished initial full scan for organization testInstallation") } + verify { syncLogService.sendInfoLog("Finished full scan for all available organizations") } } }