Create resource for repositories of one namespace

This commit is contained in:
René Pfeuffer
2020-09-03 07:53:56 +02:00
parent edc6088296
commit 4d09f71d40
4 changed files with 158 additions and 2 deletions

View File

@@ -0,0 +1,112 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.api.v2.resources;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryManager;
import sonia.scm.search.SearchRequest;
import sonia.scm.search.SearchUtil;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.util.function.Predicate;
import static com.google.common.base.Strings.isNullOrEmpty;
public class RepositoryNamespaceResource {
private static final int DEFAULT_PAGE_SIZE = 10;
private final CollectionResourceManagerAdapter<Repository, RepositoryDto> adapter;
private final RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper;
@Inject
public RepositoryNamespaceResource(RepositoryManager manager, RepositoryCollectionToDtoMapper repositoryCollectionToDtoMapper) {
this.adapter = new CollectionResourceManagerAdapter<>(manager, Repository.class);
this.repositoryCollectionToDtoMapper = repositoryCollectionToDtoMapper;
}
/**
* Returns all repositories from a namespace for a given page number with a given page size (default page size is {@value DEFAULT_PAGE_SIZE}).
*
* <strong>Note:</strong> This method requires "repository" privilege.
*
* @param page the number of the requested page
* @param pageSize the page size (default page size is {@value DEFAULT_PAGE_SIZE})
* @param sortBy sort parameter (if empty - undefined sorting)
* @param desc sort direction desc or asc
*/
@GET
@Path("")
@Produces(VndMediaType.REPOSITORY_COLLECTION)
@Operation(summary = "List of repositories from a namespace", description = "Returns all repositories from a namespace for a given page number with a given page size.", tags = "Repository")
@ApiResponse(
responseCode = "200",
description = "success",
content = @Content(
mediaType = VndMediaType.REPOSITORY_COLLECTION,
schema = @Schema(implementation = CollectionDto.class)
)
)
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
@ApiResponse(responseCode = "403", description = "not authorized, the current user does not have the \"repository\" privilege")
@ApiResponse(
responseCode = "500",
description = "internal server error",
content = @Content(
mediaType = VndMediaType.ERROR_TYPE,
schema = @Schema(implementation = ErrorDto.class)
))
public Response getByNamespace(@PathParam("namespace") String namespace,
@DefaultValue("0") @QueryParam("page") int page,
@DefaultValue("" + DEFAULT_PAGE_SIZE) @QueryParam("pageSize") int pageSize,
@QueryParam("sortBy") String sortBy,
@DefaultValue("false") @QueryParam("desc") boolean desc,
@DefaultValue("") @QueryParam("q") String search
) {
return adapter.getAll(page, pageSize, createSearchPredicate(namespace, search), sortBy, desc,
pageResult -> repositoryCollectionToDtoMapper.map(page, pageSize, pageResult));
}
private Predicate<Repository> createSearchPredicate(String namespace, String search) {
if (isNullOrEmpty(search)) {
return repository -> repository.getNamespace().equals(namespace);
}
SearchRequest searchRequest = new SearchRequest(search, true);
return repository -> repository.getNamespace().equals(namespace)
&& SearchUtil.matchesOne(searchRequest, repository.getName(), repository.getNamespace(), repository.getDescription());
}
}

View File

@@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.api.v2.resources;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
@@ -44,11 +44,13 @@ public class RepositoryRootResource {
static final String REPOSITORIES_PATH_V2 = "v2/repositories/";
private final Provider<RepositoryResource> repositoryResource;
private final Provider<RepositoryNamespaceResource> repositoryNamespaceResource;
private final Provider<RepositoryCollectionResource> repositoryCollectionResource;
@Inject
public RepositoryRootResource(Provider<RepositoryResource> repositoryResource, Provider<RepositoryCollectionResource> repositoryCollectionResource) {
public RepositoryRootResource(Provider<RepositoryResource> repositoryResource, Provider<RepositoryNamespaceResource> repositoryNamespaceResource, Provider<RepositoryCollectionResource> repositoryCollectionResource) {
this.repositoryResource = repositoryResource;
this.repositoryNamespaceResource = repositoryNamespaceResource;
this.repositoryCollectionResource = repositoryCollectionResource;
}
@@ -57,6 +59,11 @@ public class RepositoryRootResource {
return repositoryResource.get();
}
@Path("{namespace}")
public RepositoryNamespaceResource getNamespaceResource() {
return repositoryNamespaceResource.get();
}
@Path("")
public RepositoryCollectionResource getRepositoryCollectionResource() {
return repositoryCollectionResource.get();