From 202c5927d6da636579c2c0aa2d9ed2f7d9829bd0 Mon Sep 17 00:00:00 2001 From: Johanna Lamppu Date: Thu, 2 Jan 2025 17:46:58 +0200 Subject: [PATCH] feat(services): Add function to get repository IDs for an organization Signed-off-by: Johanna Lamppu --- .../src/main/kotlin/OrganizationService.kt | 11 +++++++++++ .../src/test/kotlin/OrganizationServiceTest.kt | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/services/hierarchy/src/main/kotlin/OrganizationService.kt b/services/hierarchy/src/main/kotlin/OrganizationService.kt index 9f769a631..e365d01c4 100644 --- a/services/hierarchy/src/main/kotlin/OrganizationService.kt +++ b/services/hierarchy/src/main/kotlin/OrganizationService.kt @@ -21,6 +21,8 @@ package org.eclipse.apoapsis.ortserver.services import org.eclipse.apoapsis.ortserver.dao.dbQuery import org.eclipse.apoapsis.ortserver.dao.dbQueryCatching +import org.eclipse.apoapsis.ortserver.dao.repositories.product.ProductsTable +import org.eclipse.apoapsis.ortserver.dao.repositories.repository.RepositoriesTable import org.eclipse.apoapsis.ortserver.model.Organization import org.eclipse.apoapsis.ortserver.model.authorization.OrganizationRole import org.eclipse.apoapsis.ortserver.model.repositories.OrganizationRepository @@ -184,6 +186,15 @@ class OrganizationService( // and just let the exception propagate. authorizationService.removeUserFromGroup(username, groupName) } + + /** Get IDs for all repositories found in the products of the organization. */ + suspend fun getRepositoryIdsForOrganization(organizationId: Long): List = db.dbQuery { + RepositoriesTable + .innerJoin(ProductsTable) + .select(RepositoriesTable.id) + .where { ProductsTable.organizationId eq organizationId } + .map { it[RepositoriesTable.id].value } + } } class OrganizationNotEmptyException(message: String) : Exception(message) diff --git a/services/hierarchy/src/test/kotlin/OrganizationServiceTest.kt b/services/hierarchy/src/test/kotlin/OrganizationServiceTest.kt index d80396546..983053d64 100644 --- a/services/hierarchy/src/test/kotlin/OrganizationServiceTest.kt +++ b/services/hierarchy/src/test/kotlin/OrganizationServiceTest.kt @@ -21,6 +21,7 @@ package org.eclipse.apoapsis.ortserver.services import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.WordSpec +import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder import io.mockk.coEvery import io.mockk.coVerify @@ -220,4 +221,21 @@ class OrganizationServiceTest : WordSpec({ } } } + + "getRepositoryIdsForOrganization" should { + "return IDs for all repositories found in the products of the organization" { + val service = OrganizationService(db, organizationRepository, productRepository, mockk()) + + val orgId = fixtures.createOrganization().id + + val prod1Id = fixtures.createProduct(organizationId = orgId).id + val prod2Id = fixtures.createProduct("Prod2", organizationId = orgId).id + + val repo1Id = fixtures.createRepository(productId = prod1Id).id + val repo2Id = fixtures.createRepository(url = "https://example.com/repo2.git", productId = prod2Id).id + val repo3Id = fixtures.createRepository(url = "https://example.com/repo3.git", productId = prod2Id).id + + service.getRepositoryIdsForOrganization(orgId).shouldContainExactlyInAnyOrder(repo1Id, repo2Id, repo3Id) + } + } })