Skip to content

Commit

Permalink
feat(services): Add function to get repository IDs for an organization
Browse files Browse the repository at this point in the history
Signed-off-by: Johanna Lamppu <johanna.lamppu@doubleopen.org>
  • Loading branch information
lamppu authored and sschuberth committed Jan 3, 2025
1 parent ed1fd21 commit 202c592
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions services/hierarchy/src/main/kotlin/OrganizationService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Long> = db.dbQuery {
RepositoriesTable
.innerJoin(ProductsTable)
.select(RepositoriesTable.id)
.where { ProductsTable.organizationId eq organizationId }
.map { it[RepositoriesTable.id].value }
}
}

class OrganizationNotEmptyException(message: String) : Exception(message)
18 changes: 18 additions & 0 deletions services/hierarchy/src/test/kotlin/OrganizationServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
})

0 comments on commit 202c592

Please sign in to comment.