generated from leanix/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
334 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/net/leanix/githubagent/services/GitHubAPIService.kt
This file contains 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,42 @@ | ||
package net.leanix.githubagent.services | ||
|
||
import net.leanix.githubagent.client.GitHubClient | ||
import net.leanix.githubagent.dto.Installation | ||
import net.leanix.githubagent.dto.Organization | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class GitHubAPIService( | ||
private val gitHubClient: GitHubClient, | ||
) { | ||
|
||
companion object { | ||
private const val PAGE_SIZE = 30 // Maximum allowed by GitHub API is 100 | ||
} | ||
|
||
fun getPaginatedInstallations(jwtToken: String): List<Installation> { | ||
val installations = mutableListOf<Installation>() | ||
var page = 1 | ||
var currentInstallations: List<Installation> | ||
|
||
do { | ||
currentInstallations = gitHubClient.getInstallations("Bearer $jwtToken", PAGE_SIZE, page) | ||
if (currentInstallations.isNotEmpty()) installations.addAll(currentInstallations) else break | ||
page++ | ||
} while (currentInstallations.size == PAGE_SIZE) | ||
return installations | ||
} | ||
|
||
fun getPaginatedOrganizations(installationToken: String): List<Organization> { | ||
val organizations = mutableListOf<Organization>() | ||
var since = 1 | ||
var currentOrganizations: List<Organization> | ||
|
||
do { | ||
currentOrganizations = gitHubClient.getOrganizations("Bearer $installationToken", PAGE_SIZE, since) | ||
if (currentOrganizations.isNotEmpty()) organizations.addAll(currentOrganizations) else break | ||
since = currentOrganizations.last().id | ||
} while (currentOrganizations.size == PAGE_SIZE) | ||
return organizations | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
81 changes: 81 additions & 0 deletions
81
src/test/kotlin/net/leanix/githubagent/services/GitHubAPIServiceTest.kt
This file contains 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,81 @@ | ||
package net.leanix.githubagent.services | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
import net.leanix.githubagent.client.GitHubClient | ||
import net.leanix.githubagent.dto.Account | ||
import net.leanix.githubagent.dto.Installation | ||
import net.leanix.githubagent.dto.Organization | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class GitHubAPIServiceTest { | ||
|
||
private val gitHubClient = mockk<GitHubClient>() | ||
private val gitHubAPIService = GitHubAPIService(gitHubClient) | ||
|
||
private val permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read") | ||
private val events = listOf("label", "public", "repository", "push") | ||
|
||
@Test | ||
fun `test getPaginatedInstallations with one page`() { | ||
val jwtToken = "test-jwt-token" | ||
val installationsPage1 = listOf( | ||
Installation(1, Account("test-account"), permissions, events), | ||
Installation(2, Account("test-account"), permissions, events) | ||
) | ||
|
||
every { gitHubClient.getInstallations(any(), any(), any()) } returns installationsPage1 | ||
|
||
val installations = gitHubAPIService.getPaginatedInstallations(jwtToken) | ||
assertEquals(2, installations.size) | ||
assertEquals(installationsPage1, installations) | ||
} | ||
|
||
@Test | ||
fun `test getPaginatedInstallations with multiple pages`() { | ||
val jwtToken = "test-jwt-token" | ||
val perPage = 30 | ||
val totalInstallations = 100 | ||
val installations = (1..totalInstallations).map { | ||
Installation(it.toLong(), Account("test-account-$it"), permissions, events) | ||
} | ||
val pages = installations.chunked(perPage) | ||
|
||
every { gitHubClient.getInstallations(any(), any(), any()) } returnsMany pages + listOf(emptyList()) | ||
|
||
val result = gitHubAPIService.getPaginatedInstallations(jwtToken) | ||
assertEquals(totalInstallations, result.size) | ||
assertEquals(installations, result) | ||
} | ||
|
||
@Test | ||
fun `test getPaginatedOrganizations with one page`() { | ||
val installationToken = "test-installation-token" | ||
val organizationsPage1 = listOf( | ||
Organization("org-1", 1), | ||
Organization("org-2", 2) | ||
) | ||
|
||
every { gitHubClient.getOrganizations(any(), any(), any()) } returns organizationsPage1 | ||
|
||
val organizations = gitHubAPIService.getPaginatedOrganizations(installationToken) | ||
assertEquals(2, organizations.size) | ||
assertEquals(organizationsPage1, organizations) | ||
} | ||
|
||
@Test | ||
fun `test getPaginatedOrganizations with multiple pages`() { | ||
val installationToken = "test-installation-token" | ||
val perPage = 30 | ||
val totalOrganizations = 100 | ||
val organizations = (1..totalOrganizations).map { Organization("org-$it", it) } | ||
val pages = organizations.chunked(perPage) | ||
|
||
every { gitHubClient.getOrganizations(any(), any(), any()) } returnsMany pages + listOf(emptyList()) | ||
|
||
val result = gitHubAPIService.getPaginatedOrganizations(installationToken) | ||
assertEquals(totalOrganizations, result.size) | ||
assertEquals(organizations, result) | ||
} | ||
} |
Oops, something went wrong.