|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 The ORT Server Authors (See <https://github.com/eclipse-apoapsis/ort-server/blob/main/NOTICE>) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package org.eclipse.apoapsis.ortserver.components.search.routes |
| 21 | + |
| 22 | +import io.github.smiley4.ktoropenapi.get |
| 23 | + |
| 24 | +import io.ktor.http.HttpStatusCode |
| 25 | +import io.ktor.server.response.respond |
| 26 | +import io.ktor.server.routing.Route |
| 27 | + |
| 28 | +import kotlinx.datetime.Clock |
| 29 | + |
| 30 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.permissions.OrganizationPermission |
| 31 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.permissions.ProductPermission |
| 32 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.permissions.RepositoryPermission |
| 33 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.requirePermission |
| 34 | +import org.eclipse.apoapsis.ortserver.components.authorization.keycloak.requireSuperuser |
| 35 | +import org.eclipse.apoapsis.ortserver.components.search.apimodel.RunWithPackage |
| 36 | +import org.eclipse.apoapsis.ortserver.components.search.backend.SearchService |
| 37 | +import org.eclipse.apoapsis.ortserver.shared.ktorutils.jsonBody |
| 38 | +import org.eclipse.apoapsis.ortserver.shared.ktorutils.requireParameter |
| 39 | + |
| 40 | +@Suppress("LongMethod") |
| 41 | +internal fun Route.getRunsWithPackage(searchService: SearchService) = |
| 42 | + get("/search/package", { |
| 43 | + operationId = "getRunsWithPackage" |
| 44 | + summary = "Return ORT runs containing a package, possibly scoped by organization, product, and repository" |
| 45 | + tags = listOf("Search") |
| 46 | + |
| 47 | + request { |
| 48 | + queryParameter<String>("identifier") { |
| 49 | + description = "The package identifier to search for. Also RegEx supported." |
| 50 | + required = true |
| 51 | + } |
| 52 | + queryParameter<Long?>("organizationId") { |
| 53 | + description = "Optional organization ID to filter the search." |
| 54 | + } |
| 55 | + queryParameter<Long?>("productId") { |
| 56 | + description = "Optional product ID to filter the search." |
| 57 | + } |
| 58 | + queryParameter<Long?>("repositoryId") { |
| 59 | + description = "Optional repository ID to filter the search." |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + response { |
| 64 | + HttpStatusCode.OK to { |
| 65 | + description = "Success" |
| 66 | + jsonBody<List<RunWithPackage>> { |
| 67 | + example("Package Search Result") { |
| 68 | + value = listOf( |
| 69 | + RunWithPackage( |
| 70 | + organizationId = 1L, |
| 71 | + productId = 2L, |
| 72 | + repositoryId = 3L, |
| 73 | + ortRunId = 42L, |
| 74 | + revision = "a1b2c3d4", |
| 75 | + createdAt = Clock.System.now(), |
| 76 | + packageId = "Maven:foo:bar:1.0.0" |
| 77 | + ), |
| 78 | + RunWithPackage( |
| 79 | + organizationId = 1L, |
| 80 | + productId = 3L, |
| 81 | + repositoryId = 7L, |
| 82 | + ortRunId = 120L, |
| 83 | + revision = "a1b2c3d4", |
| 84 | + createdAt = Clock.System.now(), |
| 85 | + packageId = "Maven:foo:bar:1.0.0" |
| 86 | + ) |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }) { |
| 93 | + val identifierParam = call.requireParameter("identifier") |
| 94 | + val organizationIdParam = call.request.queryParameters["organizationId"]?.toLongOrNull() |
| 95 | + val productIdParam = call.request.queryParameters["productId"]?.toLongOrNull() |
| 96 | + val repositoryIdParam = call.request.queryParameters["repositoryId"]?.toLongOrNull() |
| 97 | + |
| 98 | + if (repositoryIdParam != null && (productIdParam == null || organizationIdParam == null)) { |
| 99 | + return@get call.respond( |
| 100 | + HttpStatusCode.BadRequest, |
| 101 | + "A repository ID requires a product and an organization ID." |
| 102 | + ) |
| 103 | + } |
| 104 | + if (productIdParam != null && organizationIdParam == null) { |
| 105 | + return@get call.respond( |
| 106 | + HttpStatusCode.BadRequest, |
| 107 | + "A product ID requires an organization ID." |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + if (repositoryIdParam != null) { |
| 112 | + requirePermission(RepositoryPermission.READ.roleName(repositoryIdParam)) |
| 113 | + } else if (productIdParam != null) { |
| 114 | + requirePermission(ProductPermission.READ.roleName(productIdParam)) |
| 115 | + } else if (organizationIdParam != null) { |
| 116 | + requirePermission(OrganizationPermission.READ.roleName(organizationIdParam)) |
| 117 | + } else { |
| 118 | + requireSuperuser() |
| 119 | + } |
| 120 | + |
| 121 | + val ortRuns = searchService.findOrtRunsByPackage( |
| 122 | + identifier = identifierParam, |
| 123 | + organizationId = organizationIdParam, |
| 124 | + productId = productIdParam, |
| 125 | + repositoryId = repositoryIdParam |
| 126 | + ) |
| 127 | + call.respond(HttpStatusCode.OK, ortRuns) |
| 128 | + } |
0 commit comments