Skip to content

Commit

Permalink
Merge pull request #31 from leanix/feature/CID-3089/enchance-admin-lo…
Browse files Browse the repository at this point in the history
…gging

Feature/cid 3089/enchance admin logging
  • Loading branch information
geoandri authored Oct 31, 2024
2 parents bfde0ee + cc09848 commit be85526
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/net/leanix/githubagent/dto/SyncLogDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ data class SyncLogDto(

enum class Trigger {
START_FULL_SYNC,
PROGRESS_FULL_SYNC,
FINISH_FULL_SYNC,
GENERIC
SYSTEM
}

enum class LogLevel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand All @@ -149,13 +160,17 @@ 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
)
} else {
throw ManifestFileNotFoundException()
}
}.also {
syncLogService.sendInfoLog("Found $numOfManifestFilesFound manifest files in repository $repositoryName")
}
}
}
16 changes: 12 additions & 4 deletions src/main/kotlin/net/leanix/githubagent/services/SyncLogService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GitHubAuthenticationServiceTest {

@BeforeEach
fun setUp() {
every { syncLogService.sendErrorLog(any()) } returns Unit
every { syncLogService.sendSystemErrorLog(any()) } returns Unit
}

@Test
Expand Down Expand Up @@ -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") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ 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
fun `scanGitHubResources should send organizations over WebSocket`() {
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
Expand Down Expand Up @@ -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") }
}
}

0 comments on commit be85526

Please sign in to comment.