mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Fix default revision for hg and fix encoded slashes in URLs
This commit is contained in:
@@ -70,6 +70,8 @@ public class RepositoryAccessITCase {
|
|||||||
@Test
|
@Test
|
||||||
public void shouldReadContent() throws IOException, InterruptedException {
|
public void shouldReadContent() throws IOException, InterruptedException {
|
||||||
repositoryUtil.createAndCommitFile("a.txt", "a");
|
repositoryUtil.createAndCommitFile("a.txt", "a");
|
||||||
|
tempFolder.newFolder("subfolder");
|
||||||
|
repositoryUtil.createAndCommitFile("subfolder/a.txt", "sub-a");
|
||||||
|
|
||||||
sleep(1000);
|
sleep(1000);
|
||||||
|
|
||||||
@@ -81,19 +83,40 @@ public class RepositoryAccessITCase {
|
|||||||
.extract()
|
.extract()
|
||||||
.path("_links.sources.href");
|
.path("_links.sources.href");
|
||||||
|
|
||||||
String contentUrl = given()
|
String rootContentUrl = given()
|
||||||
.when()
|
.when()
|
||||||
.get(sourcesUrl)
|
.get(sourcesUrl)
|
||||||
.then()
|
.then()
|
||||||
.statusCode(HttpStatus.SC_OK)
|
.statusCode(HttpStatus.SC_OK)
|
||||||
.extract()
|
.extract()
|
||||||
.path("files[0]._links.content.href");
|
.path("files.find{it.name=='a.txt'}._links.content.href");
|
||||||
|
|
||||||
given()
|
given()
|
||||||
.when()
|
.when()
|
||||||
.get(contentUrl)
|
.get(rootContentUrl)
|
||||||
.then()
|
.then()
|
||||||
.statusCode(HttpStatus.SC_OK)
|
.statusCode(HttpStatus.SC_OK)
|
||||||
.body(equalTo("a"));
|
.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"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,11 +45,26 @@ public class RepositoryUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void createAndCommitFile(String fileName, String content) throws IOException {
|
void createAndCommitFile(String fileName, String content) throws IOException {
|
||||||
Files.write(content, new File(folder, fileName), Charsets.UTF_8);
|
File file = new File(folder, fileName);
|
||||||
repositoryClient.getAddCommand().add(fileName);
|
Files.write(content, file, Charsets.UTF_8);
|
||||||
|
addWithParentDirectories(file);
|
||||||
commit("added " + fileName);
|
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 commit(String message) throws IOException {
|
||||||
Changeset changeset = repositoryClient.getCommitCommand().commit(
|
Changeset changeset = repositoryClient.getCommitCommand().commit(
|
||||||
new Person("scmadmin", "scmadmin@scm-manager.org"), message
|
new Person("scmadmin", "scmadmin@scm-manager.org"), message
|
||||||
|
|||||||
@@ -36,16 +36,14 @@ package sonia.scm.repository.spi;
|
|||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
|
|
||||||
import sonia.scm.repository.BrowserResult;
|
import sonia.scm.repository.BrowserResult;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.repository.RepositoryException;
|
|
||||||
import sonia.scm.repository.spi.javahg.HgFileviewCommand;
|
import sonia.scm.repository.spi.javahg.HgFileviewCommand;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
@@ -76,12 +74,9 @@ public class HgBrowseCommand extends AbstractCommand implements BrowseCommand
|
|||||||
* @return
|
* @return
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws RepositoryException
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public BrowserResult getBrowserResult(BrowseCommandRequest request)
|
public BrowserResult getBrowserResult(BrowseCommandRequest request) throws IOException {
|
||||||
throws IOException, RepositoryException
|
|
||||||
{
|
|
||||||
HgFileviewCommand cmd = HgFileviewCommand.on(open());
|
HgFileviewCommand cmd = HgFileviewCommand.on(open());
|
||||||
|
|
||||||
if (!Strings.isNullOrEmpty(request.getRevision()))
|
if (!Strings.isNullOrEmpty(request.getRevision()))
|
||||||
@@ -113,6 +108,12 @@ public class HgBrowseCommand extends AbstractCommand implements BrowseCommand
|
|||||||
|
|
||||||
result.setFiles(cmd.execute());
|
result.setFiles(cmd.execute());
|
||||||
|
|
||||||
|
if (!Strings.isNullOrEmpty(request.getRevision())) {
|
||||||
|
result.setRevision(request.getRevision());
|
||||||
|
} else {
|
||||||
|
result.setRevision("tip");
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,6 @@ public class BrowserResultMapper {
|
|||||||
@Inject
|
@Inject
|
||||||
private ResourceLinks resourceLinks;
|
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) {
|
public BrowserResultDto map(BrowserResult browserResult, NamespaceAndName namespaceAndName) {
|
||||||
BrowserResultDto browserResultDto = new BrowserResultDto();
|
BrowserResultDto browserResultDto = new BrowserResultDto();
|
||||||
|
|
||||||
@@ -38,6 +34,10 @@ public class BrowserResultMapper {
|
|||||||
return browserResultDto;
|
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) {
|
private void addLinks(BrowserResult browserResult, BrowserResultDto dto, NamespaceAndName namespaceAndName) {
|
||||||
if (browserResult.getRevision() == null) {
|
if (browserResult.getRevision() == null) {
|
||||||
dto.add(Links.linkingTo().self(resourceLinks.source().selfWithoutRevision(namespaceAndName.getNamespace(), namespaceAndName.getName())).build());
|
dto.add(Links.linkingTo().self(resourceLinks.source().selfWithoutRevision(namespaceAndName.getNamespace(), namespaceAndName.getName())).build());
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import sonia.scm.repository.NamespaceAndName;
|
|||||||
import sonia.scm.repository.SubRepository;
|
import sonia.scm.repository.SubRepository;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
import static de.otto.edison.hal.Link.link;
|
import static de.otto.edison.hal.Link.link;
|
||||||
|
|
||||||
@@ -25,16 +26,18 @@ public abstract class FileObjectMapper extends BaseMapper<FileObject, FileObject
|
|||||||
|
|
||||||
@AfterMapping
|
@AfterMapping
|
||||||
void addLinks(FileObject fileObject, @MappingTarget FileObjectDto dto, @Context NamespaceAndName namespaceAndName, @Context String revision) {
|
void addLinks(FileObject fileObject, @MappingTarget FileObjectDto dto, @Context NamespaceAndName namespaceAndName, @Context String revision) {
|
||||||
|
String path = fileObject.getPath();
|
||||||
Links.Builder links = Links.linkingTo()
|
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()) {
|
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());
|
dto.add(links.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
private String removeFirstSlash(String source) {
|
// we have to add the file path using URI, so that path separators (aka '/') will not be encoded as '%2F'
|
||||||
return source.startsWith("/") ? source.substring(1) : source;
|
private String addPath(String sourceWithPath, String path) {
|
||||||
|
return URI.create(sourceWithPath).resolve(path).toASCIIString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ public class FileObjectMapperTest {
|
|||||||
FileObject fileObject = createFileObject();
|
FileObject fileObject = createFileObject();
|
||||||
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
|
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
|
@Test
|
||||||
@@ -62,7 +62,7 @@ public class FileObjectMapperTest {
|
|||||||
FileObject fileObject = createFileObject();
|
FileObject fileObject = createFileObject();
|
||||||
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
|
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() {
|
private FileObject createFileObject() {
|
||||||
|
|||||||
Reference in New Issue
Block a user