Fix default revision for hg and fix encoded slashes in URLs

This commit is contained in:
René Pfeuffer
2018-08-23 15:52:02 +02:00
parent 7177cbd3fe
commit ca563dd874
6 changed files with 66 additions and 24 deletions

View File

@@ -70,6 +70,8 @@ public class RepositoryAccessITCase {
@Test
public void shouldReadContent() throws IOException, InterruptedException {
repositoryUtil.createAndCommitFile("a.txt", "a");
tempFolder.newFolder("subfolder");
repositoryUtil.createAndCommitFile("subfolder/a.txt", "sub-a");
sleep(1000);
@@ -81,19 +83,40 @@ public class RepositoryAccessITCase {
.extract()
.path("_links.sources.href");
String contentUrl = given()
String rootContentUrl = given()
.when()
.get(sourcesUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("files[0]._links.content.href");
.path("files.find{it.name=='a.txt'}._links.content.href");
given()
.when()
.get(contentUrl)
.get(rootContentUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.body(equalTo("a"));
String subfolderSourceUrl = given()
.when()
.get(sourcesUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("files.find{it.name=='subfolder'}._links.self.href");
String subfolderContentUrl= given()
.when()
.get(subfolderSourceUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("files[0]._links.content.href");
System.out.println(subfolderContentUrl);
given()
.when()
.get(subfolderContentUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.body(equalTo("sub-a"));
}
}

View File

@@ -45,11 +45,26 @@ public class RepositoryUtil {
}
void createAndCommitFile(String fileName, String content) throws IOException {
Files.write(content, new File(folder, fileName), Charsets.UTF_8);
repositoryClient.getAddCommand().add(fileName);
File file = new File(folder, fileName);
Files.write(content, file, Charsets.UTF_8);
addWithParentDirectories(file);
commit("added " + fileName);
}
private String addWithParentDirectories(File file) throws IOException {
File parent = file.getParentFile();
String thisName = file.getName();
String path;
if (!folder.equals(parent)) {
addWithParentDirectories(parent);
path = addWithParentDirectories(parent) + File.separator + thisName;
} else {
path = thisName;
}
repositoryClient.getAddCommand().add(path);
return path;
}
Changeset commit(String message) throws IOException {
Changeset changeset = repositoryClient.getCommitCommand().commit(
new Person("scmadmin", "scmadmin@scm-manager.org"), message

View File

@@ -36,16 +36,14 @@ package sonia.scm.repository.spi;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Strings;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryException;
import sonia.scm.repository.spi.javahg.HgFileviewCommand;
//~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
//~--- JDK imports ------------------------------------------------------------
/**
*
* @author Sebastian Sdorra
@@ -76,12 +74,9 @@ public class HgBrowseCommand extends AbstractCommand implements BrowseCommand
* @return
*
* @throws IOException
* @throws RepositoryException
*/
@Override
public BrowserResult getBrowserResult(BrowseCommandRequest request)
throws IOException, RepositoryException
{
public BrowserResult getBrowserResult(BrowseCommandRequest request) throws IOException {
HgFileviewCommand cmd = HgFileviewCommand.on(open());
if (!Strings.isNullOrEmpty(request.getRevision()))
@@ -113,6 +108,12 @@ public class HgBrowseCommand extends AbstractCommand implements BrowseCommand
result.setFiles(cmd.execute());
if (!Strings.isNullOrEmpty(request.getRevision())) {
result.setRevision(request.getRevision());
} else {
result.setRevision("tip");
}
return result;
}
}

View File

@@ -17,10 +17,6 @@ public class BrowserResultMapper {
@Inject
private ResourceLinks resourceLinks;
private FileObjectDto mapFileObject(FileObject fileObject, NamespaceAndName namespaceAndName, String revision) {
return fileObjectMapper.map(fileObject, namespaceAndName, revision);
}
public BrowserResultDto map(BrowserResult browserResult, NamespaceAndName namespaceAndName) {
BrowserResultDto browserResultDto = new BrowserResultDto();
@@ -38,6 +34,10 @@ public class BrowserResultMapper {
return browserResultDto;
}
private FileObjectDto mapFileObject(FileObject fileObject, NamespaceAndName namespaceAndName, String revision) {
return fileObjectMapper.map(fileObject, namespaceAndName, revision);
}
private void addLinks(BrowserResult browserResult, BrowserResultDto dto, NamespaceAndName namespaceAndName) {
if (browserResult.getRevision() == null) {
dto.add(Links.linkingTo().self(resourceLinks.source().selfWithoutRevision(namespaceAndName.getNamespace(), namespaceAndName.getName())).build());

View File

@@ -10,6 +10,7 @@ import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.SubRepository;
import javax.inject.Inject;
import java.net.URI;
import static de.otto.edison.hal.Link.link;
@@ -25,16 +26,18 @@ public abstract class FileObjectMapper extends BaseMapper<FileObject, FileObject
@AfterMapping
void addLinks(FileObject fileObject, @MappingTarget FileObjectDto dto, @Context NamespaceAndName namespaceAndName, @Context String revision) {
String path = fileObject.getPath();
Links.Builder links = Links.linkingTo()
.self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, removeFirstSlash(fileObject.getPath())));
.self(addPath(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, ""), path));
if (!dto.isDirectory()) {
links.single(link("content", resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, removeFirstSlash(fileObject.getPath()))));
links.single(link("content", addPath(resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, ""), path)));
}
dto.add(links.build());
}
private String removeFirstSlash(String source) {
return source.startsWith("/") ? source.substring(1) : source;
// we have to add the file path using URI, so that path separators (aka '/') will not be encoded as '%2F'
private String addPath(String sourceWithPath, String path) {
return URI.create(sourceWithPath).resolve(path).toASCIIString();
}
}

View File

@@ -54,7 +54,7 @@ public class FileObjectMapperTest {
FileObject fileObject = createFileObject();
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
assertThat(dto.getLinks().getLinkBy("self").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/sources/revision/foo%2Fbar").toString());
assertThat(dto.getLinks().getLinkBy("self").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/sources/revision/foo/bar").toString());
}
@Test
@@ -62,7 +62,7 @@ public class FileObjectMapperTest {
FileObject fileObject = createFileObject();
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
assertThat(dto.getLinks().getLinkBy("content").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/content/revision/foo%2Fbar").toString());
assertThat(dto.getLinks().getLinkBy("content").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/content/revision/foo/bar").toString());
}
private FileObject createFileObject() {