generated from leanix/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
CID-3745: Handle Repository Archived & Unarchived Events #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mohamedlajmileanix
merged 4 commits into
main
from
feature/CID-3745/Handle-Repository-Archived-Unarchived-Events
Apr 16, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
df6006e
CID-3745: Handle Repository Archived & Unarchived Events - Initial co…
mohamedlajmileanix 22e427d
CID-3745: Code refactor
mohamedlajmileanix 63f7eb4
CID-3745: Code refactor
mohamedlajmileanix 3388717
CID-3745: Code refactor
mohamedlajmileanix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/net/leanix/githubagent/dto/RepositoryRequestDTO.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package net.leanix.githubagent.dto | ||
|
|
||
| data class RepositoryRequestDTO( | ||
| val installation: RepositoryRequestInstallationDTO, | ||
| val repositoryName: String, | ||
| val repositoryFullName: String, | ||
| ) | ||
|
|
||
| data class RepositoryRequestInstallationDTO( | ||
| val id: Long, | ||
| val account: Account | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/net/leanix/githubagent/handler/RepositoryGetHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package net.leanix.githubagent.handler | ||
|
|
||
| import net.leanix.githubagent.dto.Installation | ||
| import net.leanix.githubagent.dto.RepositoryRequestDTO | ||
| import net.leanix.githubagent.services.GitHubAuthenticationService | ||
| import net.leanix.githubagent.services.GitHubRepositoryService | ||
| import org.slf4j.LoggerFactory | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.context.annotation.Lazy | ||
| import org.springframework.messaging.simp.stomp.StompFrameHandler | ||
| import org.springframework.messaging.simp.stomp.StompHeaders | ||
| import org.springframework.stereotype.Component | ||
| import java.lang.reflect.Type | ||
|
|
||
| @Component | ||
| class RepositoryGetHandler( | ||
| @Lazy @Autowired | ||
| private val gitHubAuthenticationService: GitHubAuthenticationService, | ||
| @Lazy @Autowired | ||
| private val repositoryGetService: GitHubRepositoryService | ||
| ) : StompFrameHandler { | ||
|
|
||
| private val logger = LoggerFactory.getLogger(RepositoryGetHandler::class.java) | ||
|
|
||
| override fun getPayloadType(headers: StompHeaders): Type { | ||
| return RepositoryRequestDTO::class.java | ||
| } | ||
|
|
||
| override fun handleFrame(headers: StompHeaders, payload: Any?) { | ||
| payload?.let { | ||
| val dto = payload as RepositoryRequestDTO | ||
| logger.info("Received repository get message from server for repo: ${dto.repositoryName}") | ||
| runCatching { | ||
| val installationToken = gitHubAuthenticationService.getInstallationToken(dto.installation.id.toInt()) | ||
| repositoryGetService.fetchAndSendRepositoryAndManifest( | ||
| Installation( | ||
| id = dto.installation.id, | ||
| account = dto.installation.account, | ||
| mapOf(), | ||
| emptyList(), | ||
| ), | ||
| dto.repositoryName, | ||
| dto.repositoryFullName, | ||
| installationToken | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } |
86 changes: 86 additions & 0 deletions
86
src/main/kotlin/net/leanix/githubagent/services/GitHubRepositoryService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package net.leanix.githubagent.services | ||
|
|
||
| import net.leanix.githubagent.dto.Installation | ||
| import net.leanix.githubagent.dto.ManifestFileAction | ||
| import net.leanix.githubagent.dto.ManifestFileUpdateDto | ||
| import net.leanix.githubagent.dto.RateLimitType | ||
| import net.leanix.githubagent.dto.RepositoryDto | ||
| import net.leanix.githubagent.handler.RateLimitHandler | ||
| import net.leanix.githubagent.shared.fileNameMatchRegex | ||
| import net.leanix.githubagent.shared.generateFullPath | ||
| import org.slf4j.LoggerFactory | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.context.annotation.Lazy | ||
| import org.springframework.stereotype.Service | ||
|
|
||
| @Service | ||
| class GitHubRepositoryService( | ||
| @Lazy @Autowired | ||
| private val webSocketService: WebSocketService, | ||
| private val gitHubGraphQLService: GitHubGraphQLService, | ||
| @Lazy @Autowired | ||
| private val gitHubScanningService: GitHubScanningService, | ||
| @Lazy @Autowired | ||
| private val rateLimitHandler: RateLimitHandler, | ||
| ) { | ||
| private val logger = LoggerFactory.getLogger(GitHubRepositoryService::class.java) | ||
|
|
||
| fun fetchAndSendRepositoryAndManifest( | ||
| installation: Installation, | ||
| repositoryName: String, | ||
| repositoryFullName: String, | ||
| installationToken: String | ||
| ) { | ||
| kotlin.runCatching { | ||
| logger.info("Fetching repository details for: $repositoryFullName") | ||
| val repository = rateLimitHandler.executeWithRateLimitHandler(RateLimitType.GRAPHQL) { | ||
| gitHubGraphQLService.getRepository( | ||
| installation.account.login, | ||
| repositoryName, | ||
| installationToken | ||
| ) | ||
| } | ||
| if (repository == null) { | ||
| logger.error("Failed to fetch repository details for: $repositoryFullName") | ||
| return | ||
| } | ||
| if (repository.archived) { | ||
| logger.info("Repository ${repository.fullName} is archived, skipping.") | ||
| return | ||
| } | ||
| logger.info("Sending repository details for: ${repository.fullName}") | ||
| webSocketService.sendMessage("/events/repository", repository) | ||
| fetchAndSendManifestFiles(installation, repository) | ||
| }.onFailure { | ||
| logger.error("Failed to process repository event: $repositoryFullName", it) | ||
| } | ||
| } | ||
|
|
||
| private fun fetchAndSendManifestFiles(installation: Installation, repositoryAdded: RepositoryDto) { | ||
| logger.info("Fetching manifest files for repository: ${repositoryAdded.fullName}") | ||
| val manifestFiles = gitHubScanningService.fetchManifestFiles(installation, repositoryAdded.name).getOrThrow() | ||
| val manifestContents = gitHubScanningService.fetchManifestContents( | ||
| installation, | ||
| manifestFiles, | ||
| repositoryAdded.name, | ||
| repositoryAdded.defaultBranch | ||
| ).getOrThrow() | ||
|
|
||
| if (manifestContents.isEmpty()) { | ||
| logger.info("No manifest files found for repository: ${repositoryAdded.fullName}") | ||
| return | ||
| } | ||
| manifestContents.forEach { | ||
| logger.info("Sending manifest file content for: ${it.path} in repository: ${repositoryAdded.fullName}") | ||
| webSocketService.sendMessage( | ||
| "/events/manifestFile", | ||
| ManifestFileUpdateDto( | ||
| repositoryAdded.fullName, | ||
| ManifestFileAction.ADDED, | ||
| it.content, | ||
| generateFullPath(repositoryAdded.defaultBranch, fileNameMatchRegex.replace(it.path, "")) | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/kotlin/net/leanix/githubagent/handler/RepositoryGetHandlerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package net.leanix.githubagent.handler | ||
|
|
||
| import io.mockk.MockKAnnotations | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import io.mockk.verify | ||
| import net.leanix.githubagent.dto.Account | ||
| import net.leanix.githubagent.dto.Installation | ||
| import net.leanix.githubagent.dto.RepositoryRequestDTO | ||
| import net.leanix.githubagent.dto.RepositoryRequestInstallationDTO | ||
| import net.leanix.githubagent.services.GitHubAuthenticationService | ||
| import net.leanix.githubagent.services.GitHubRepositoryService | ||
| import org.junit.jupiter.api.BeforeEach | ||
| import org.junit.jupiter.api.Test | ||
| import org.springframework.messaging.simp.stomp.StompHeaders | ||
|
|
||
| class RepositoryGetHandlerTest { | ||
|
|
||
| private val gitHubAuthenticationService = mockk<GitHubAuthenticationService>() | ||
|
|
||
| private val gitHubRepositoryService = mockk<GitHubRepositoryService>() | ||
|
|
||
| private val repositoryGetHandler = RepositoryGetHandler( | ||
| gitHubAuthenticationService, | ||
| gitHubRepositoryService | ||
| ) | ||
|
|
||
| @BeforeEach | ||
| fun setup() { | ||
| MockKAnnotations.init(repositoryGetHandler) | ||
| } | ||
|
|
||
| @Test | ||
| fun `it should receive message from server and send repository data and manifest files`() { | ||
| // given | ||
| every { gitHubAuthenticationService.getInstallationToken(any()) } returns "token" | ||
|
|
||
| // when | ||
| repositoryGetHandler.handleFrame( | ||
| StompHeaders(), | ||
| RepositoryRequestDTO( | ||
| installation = RepositoryRequestInstallationDTO(1, Account("account")), | ||
| repositoryName = "repoName", | ||
| repositoryFullName = "repoFullName" | ||
| ) | ||
| ) | ||
|
|
||
| // then | ||
| verify { | ||
| gitHubRepositoryService.fetchAndSendRepositoryAndManifest( | ||
| installation = Installation(1, Account("account"), mapOf(), listOf()), | ||
| repositoryName = "repoName", | ||
| repositoryFullName = "repoFullName", | ||
| installationToken = "token" | ||
| ) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.