@@ -3,13 +3,21 @@ package net.leanix.githubagent.services
33import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
44import com.fasterxml.jackson.module.kotlin.readValue
55import net.leanix.githubagent.client.GitHubClient
6+ import net.leanix.githubagent.dto.Installation
67import net.leanix.githubagent.dto.InstallationEventPayload
8+ import net.leanix.githubagent.dto.InstallationRepositoriesEventPayload
9+ import net.leanix.githubagent.dto.InstallationRepositoriesRepository
710import net.leanix.githubagent.dto.ManifestFileAction
811import net.leanix.githubagent.dto.ManifestFileUpdateDto
912import net.leanix.githubagent.dto.PushEventCommit
1013import net.leanix.githubagent.dto.PushEventPayload
14+ import net.leanix.githubagent.dto.RateLimitType
15+ import net.leanix.githubagent.dto.RepositoryDto
16+ import net.leanix.githubagent.dto.toInstallation
1117import net.leanix.githubagent.exceptions.JwtTokenNotFound
18+ import net.leanix.githubagent.handler.RateLimitHandler
1219import net.leanix.githubagent.shared.INSTALLATION_LABEL
20+ import net.leanix.githubagent.shared.INSTALLATION_REPOSITORIES
1321import net.leanix.githubagent.shared.MANIFEST_FILE_NAME
1422import net.leanix.githubagent.shared.WORKFLOW_RUN_EVENT
1523import net.leanix.githubagent.shared.fileNameMatchRegex
@@ -18,6 +26,7 @@ import org.slf4j.LoggerFactory
1826import org.springframework.beans.factory.annotation.Value
1927import org.springframework.stereotype.Service
2028
29+ @SuppressWarnings(" TooManyFunctions" )
2130@Service
2231class WebhookEventService (
2332 private val webSocketService : WebSocketService ,
@@ -29,7 +38,8 @@ class WebhookEventService(
2938 @Value(" \$ {webhookEventService.waitingTime}" ) private val waitingTime : Long ,
3039 private val gitHubClient : GitHubClient ,
3140 private val gitHubEnterpriseService : GitHubEnterpriseService ,
32- private val workflowRunService : WorkflowRunService
41+ private val workflowRunService : WorkflowRunService ,
42+ private val rateLimitHandler : RateLimitHandler ,
3343) {
3444
3545 private val logger = LoggerFactory .getLogger(WebhookEventService ::class .java)
@@ -39,6 +49,7 @@ class WebhookEventService(
3949 when (eventType.uppercase()) {
4050 " PUSH" -> handlePushEvent(payload)
4151 " INSTALLATION" -> handleInstallationEvent(payload)
52+ INSTALLATION_REPOSITORIES -> handleInstallationRepositories(payload)
4253 WORKFLOW_RUN_EVENT -> workflowRunService.consumeWebhookPayload(payload)
4354 else -> {
4455 logger.info(" Sending event of type: $eventType " )
@@ -204,4 +215,71 @@ class WebhookEventService(
204215 " root folder"
205216 }
206217 }
218+
219+ private val handleInstallationRepositories: (String ) -> Unit = { payload ->
220+ logger.info(" Handling installation repositories event" )
221+ val installationRepositoriesEventPayload: InstallationRepositoriesEventPayload = objectMapper.readValue(payload)
222+ val installation = installationRepositoriesEventPayload.installation
223+ val installationToken = gitHubAuthenticationService.getInstallationToken(installation.id)
224+
225+ installationRepositoriesEventPayload.repositoriesAdded
226+ .forEach { repositoryAdded ->
227+ logger.info(" Processing repository: ${repositoryAdded.fullName} " )
228+ processRepository(installation.toInstallation(), repositoryAdded, installationToken)
229+ }
230+ }
231+
232+ private fun processRepository (
233+ installation : Installation ,
234+ repositoryAdded : InstallationRepositoriesRepository ,
235+ installationToken : String
236+ ) {
237+ kotlin.runCatching {
238+ logger.info(" Fetching repository details for: ${repositoryAdded.fullName} " )
239+ val repository = rateLimitHandler.executeWithRateLimitHandler(RateLimitType .GRAPHQL ) {
240+ gitHubGraphQLService.getRepository(
241+ installation.account.login,
242+ repositoryAdded.name,
243+ installationToken
244+ )
245+ }
246+ if (repository == null ) {
247+ logger.error(" Failed to fetch repository details for: ${repositoryAdded.fullName} " )
248+ return
249+ }
250+ if (repository.archived) {
251+ logger.info(" Repository ${repository.fullName} is archived, skipping." )
252+ return
253+ }
254+ logger.info(" Sending repository details for: ${repository.fullName} " )
255+ webSocketService.sendMessage(" /events/repository" , repository)
256+ fetchAndSendManifestFiles(installation, repository)
257+ }.onFailure {
258+ logger.error(" Failed to process repository event: ${repositoryAdded.fullName} " , it)
259+ }
260+ }
261+
262+ private fun fetchAndSendManifestFiles (installation : Installation , repositoryAdded : RepositoryDto ) {
263+ logger.info(" Fetching manifest files for repository: ${repositoryAdded.fullName} " )
264+ val manifestFiles = gitHubScanningService.fetchManifestFiles(installation, repositoryAdded.name).getOrThrow()
265+ val manifestContents = gitHubScanningService.fetchManifestContents(
266+ installation,
267+ manifestFiles,
268+ repositoryAdded.name,
269+ repositoryAdded.defaultBranch
270+ ).getOrThrow()
271+
272+ manifestContents.forEach {
273+ logger.info(" Sending manifest file content for: ${it.path} in repository: ${repositoryAdded.fullName} " )
274+ webSocketService.sendMessage(
275+ " /events/manifestFile" ,
276+ ManifestFileUpdateDto (
277+ repositoryAdded.fullName,
278+ ManifestFileAction .ADDED ,
279+ it.content,
280+ generateFullPath(repositoryAdded.defaultBranch, fileNameMatchRegex.replace(it.path, " " ))
281+ )
282+ )
283+ }
284+ }
207285}
0 commit comments