Filepath search (#1568)

Add search for files to the sources view. The search is only for finding file paths. It does not search any file metadata nor the content. Results get a rating, where file names are rated higher than file paths. The results are sorted by the score and the first 50 results are displayed.

Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
Sebastian Sdorra
2021-03-04 10:39:58 +01:00
committed by GitHub
parent bafe84b79a
commit 89548d45bd
36 changed files with 1112 additions and 30 deletions

View File

@@ -0,0 +1,106 @@
/*
* 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 com.fasterxml.jackson.databind.ObjectMapper;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.RepositoryPathCollector;
import sonia.scm.repository.RepositoryPaths;
import sonia.scm.web.RestDispatcher;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class RepositoryPathsResourceTest extends RepositoryTestBase {
private final RestDispatcher dispatcher = new RestDispatcher();
private final NamespaceAndName PUZZLE_42 = new NamespaceAndName("hitchhiker", "puzzle-42");
private final ObjectMapper mapper = new ObjectMapper();
@Mock
private RepositoryPathCollector collector;
@InjectMocks
private RepositoryPathsResource resource;
@BeforeEach
public void prepareEnvironment() {
super.repositoryPathsResource = resource;
dispatcher.addSingletonResource(getRepositoryRootResource());
}
@Test
void shouldReturnCollectedPaths() throws IOException, URISyntaxException {
mockCollector("21", "a.txt", "b/c.txt");
MockHttpResponse response = request("21");
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
RepositoryPathsDto dto = mapper.readValue(response.getContentAsString(), RepositoryPathsDto.class);
assertThat(dto.getRevision()).isEqualTo("21");
assertThat(dto.getPaths()).containsExactly("a.txt", "b/c.txt");
}
@Test
void shouldAppendSelfLink() throws IOException, URISyntaxException {
mockCollector("42");
MockHttpResponse response = request("42");
RepositoryPathsDto dto = mapper.readValue(response.getContentAsString(), RepositoryPathsDto.class);
assertThat(dto.getLinks().getLinkBy("self")).isPresent().hasValueSatisfying(link ->
assertThat(link.getHref()).isEqualTo("/v2/repositories/hitchhiker/puzzle-42/paths/42")
);
}
private MockHttpResponse request(String revision) throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "hitchhiker/puzzle-42/paths/" + revision);
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
return response;
}
private void mockCollector(String revision, String... paths) throws IOException {
RepositoryPaths result = new RepositoryPaths(revision, Arrays.asList(paths));
when(collector.collect(PUZZLE_42, revision)).thenReturn(result);
}
}

View File

@@ -47,6 +47,7 @@ abstract class RepositoryTestBase {
AnnotateResource annotateResource;
RepositoryImportResource repositoryImportResource;
RepositoryExportResource repositoryExportResource;
RepositoryPathsResource repositoryPathsResource;
RepositoryRootResource getRepositoryRootResource() {
RepositoryBasedResourceProvider repositoryBasedResourceProvider = new RepositoryBasedResourceProvider(
@@ -61,7 +62,9 @@ abstract class RepositoryTestBase {
of(fileHistoryRootResource),
of(incomingRootResource),
of(annotateResource),
of(repositoryExportResource));
of(repositoryExportResource),
of(repositoryPathsResource)
);
return new RepositoryRootResource(
of(new RepositoryResource(
repositoryToDtoMapper,

View File

@@ -47,9 +47,9 @@ import sonia.scm.repository.api.ScmProtocol;
import java.net.URI;
import java.util.Set;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static java.util.stream.Stream.of;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -300,6 +300,17 @@ public class RepositoryToRepositoryDtoMapperTest {
dto.getLinks().getLinkBy("exportInfo").get().getHref());
}
@Test
public void shouldCreatePathsLink() {
RepositoryDto dto = mapper.map(createTestRepository());
assertThat(dto.getLinks().getLinkBy("paths"))
.isPresent()
.hasValueSatisfying(link -> {
assertThat(link.getHref()).isEqualTo("http://example.com/base/v2/repositories/testspace/test/paths/{revision}");
assertThat(link.isTemplated()).isTrue();
});
}
private ScmProtocol mockProtocol(String type, String protocol) {
return new MockScmProtocol(type, protocol);
}

View File

@@ -0,0 +1,115 @@
/*
* 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.repository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.repository.api.BrowseCommandBuilder;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import java.io.IOException;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class RepositoryPathCollectorTest {
private final NamespaceAndName HEART_OF_GOLD = new NamespaceAndName("hitchhiker", "heart-of-gold");
@Mock
private RepositoryServiceFactory factory;
@Mock
private RepositoryService service;
@Mock(answer = Answers.RETURNS_SELF)
private BrowseCommandBuilder browseCommand;
@InjectMocks
private RepositoryPathCollector collector;
@BeforeEach
void setUpMocks() {
when(factory.create(HEART_OF_GOLD)).thenReturn(service);
when(service.getBrowseCommand()).thenReturn(browseCommand);
}
@Test
void shouldDisableComputeHeavySettings() throws IOException {
BrowserResult result = new BrowserResult("42", new FileObject());
when(browseCommand.getBrowserResult()).thenReturn(result);
collector.collect(HEART_OF_GOLD, "42");
verify(browseCommand).setDisablePreProcessors(true);
verify(browseCommand).setDisableSubRepositoryDetection(true);
verify(browseCommand).setDisableLastCommit(true);
}
@Test
void shouldCollectFiles() throws IOException {
FileObject root = dir(
"a",
file("a/b.txt"),
dir("a/c",
file("a/c/d.txt"),
file("a/c/e.txt")
)
);
BrowserResult result = new BrowserResult("21", root);
when(browseCommand.getBrowserResult()).thenReturn(result);
RepositoryPaths paths = collector.collect(HEART_OF_GOLD, "develop");
assertThat(paths.getRevision()).isEqualTo("21");
assertThat(paths.getPaths()).containsExactlyInAnyOrder(
"a/b.txt",
"a/c/d.txt",
"a/c/e.txt"
);
}
FileObject dir(String path, FileObject... children) {
FileObject file = file(path);
file.setDirectory(true);
file.setChildren(Arrays.asList(children));
return file;
}
FileObject file(String path) {
FileObject file = new FileObject();
file.setPath(path);
file.setName(path);
return file;
}
}