Add requested revision to browse result

This commit is contained in:
Rene Pfeuffer
2019-09-04 13:05:22 +02:00
parent a650ba54ca
commit 80b48b1dc4
5 changed files with 25 additions and 13 deletions

View File

@@ -53,13 +53,19 @@ import java.io.Serializable;
public class BrowserResult implements Serializable {
private String revision;
private String requestedRevision;
private FileObject file;
public BrowserResult() {
}
public BrowserResult(String revision, FileObject file) {
this(revision, revision, file);
}
public BrowserResult(String revision, String requestedRevision, FileObject file) {
this.revision = revision;
this.requestedRevision = requestedRevision;
this.file = file;
}
@@ -67,6 +73,10 @@ public class BrowserResult implements Serializable {
return revision;
}
public String getRequestedRevision() {
return requestedRevision;
}
public FileObject getFile() {
return file;
}

View File

@@ -124,7 +124,7 @@ public class GitBrowseCommand extends AbstractGitCommand
if (revId != null)
{
result = new BrowserResult(revId.getName(), getEntry(repo, request, revId));
result = new BrowserResult(revId.getName(), request.getRevision(), getEntry(repo, request, revId));
}
else
{
@@ -138,7 +138,7 @@ public class GitBrowseCommand extends AbstractGitCommand
logger.warn("could not find head of repository, empty?");
}
result = new BrowserResult(Constants.HEAD, createEmtpyRoot());
result = new BrowserResult(Constants.HEAD, request.getRevision(), createEmtpyRoot());
}
return result;

View File

@@ -15,7 +15,7 @@ public class BrowserResultToFileObjectDtoMapper {
}
public FileObjectDto map(BrowserResult browserResult, NamespaceAndName namespaceAndName) {
FileObjectDto fileObjectDto = fileObjectToFileObjectDtoMapper.map(browserResult.getFile(), namespaceAndName, browserResult.getRevision());
FileObjectDto fileObjectDto = fileObjectToFileObjectDtoMapper.map(browserResult.getFile(), namespaceAndName, browserResult);
fileObjectDto.setRevision( browserResult.getRevision() );
return fileObjectDto;
}

View File

@@ -6,6 +6,7 @@ import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ObjectFactory;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.SubRepository;
@@ -22,23 +23,23 @@ public abstract class FileObjectToFileObjectDtoMapper extends HalAppenderMapper
private ResourceLinks resourceLinks;
@Mapping(target = "attributes", ignore = true) // We do not map HAL attributes
protected abstract FileObjectDto map(FileObject fileObject, @Context NamespaceAndName namespaceAndName, @Context String revision);
protected abstract FileObjectDto map(FileObject fileObject, @Context NamespaceAndName namespaceAndName, @Context BrowserResult browserResult);
abstract SubRepositoryDto mapSubrepository(SubRepository subRepository);
@ObjectFactory
FileObjectDto createDto(@Context NamespaceAndName namespaceAndName, @Context String revision, FileObject fileObject) {
FileObjectDto createDto(@Context NamespaceAndName namespaceAndName, @Context BrowserResult browserResult, FileObject fileObject) {
String path = removeFirstSlash(fileObject.getPath());
Links.Builder links = Links.linkingTo();
if (fileObject.isDirectory()) {
links.self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, path));
links.self(resourceLinks.source().sourceWithPath(namespaceAndName.getNamespace(), namespaceAndName.getName(), browserResult.getRevision(), path));
} else {
links.self(resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, path));
links.single(link("history", resourceLinks.fileHistory().self(namespaceAndName.getNamespace(), namespaceAndName.getName(), revision, path)));
links.self(resourceLinks.source().content(namespaceAndName.getNamespace(), namespaceAndName.getName(), browserResult.getRevision(), path));
links.single(link("history", resourceLinks.fileHistory().self(namespaceAndName.getNamespace(), namespaceAndName.getName(), browserResult.getRevision(), path)));
}
Embedded.Builder embeddedBuilder = embeddedBuilder();
applyEnrichers(new EdisonHalAppender(links, embeddedBuilder), fileObject, namespaceAndName, revision);
applyEnrichers(new EdisonHalAppender(links, embeddedBuilder), fileObject, namespaceAndName, browserResult, browserResult.getRevision());
return new FileObjectDto(links.build(), embeddedBuilder.build());
}

View File

@@ -10,6 +10,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.repository.BrowserResult;
import sonia.scm.repository.FileObject;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.SubRepository;
@@ -49,7 +50,7 @@ public class FileObjectToFileObjectDtoMapperTest {
@Test
public void shouldMapAttributesCorrectly() {
FileObject fileObject = createFileObject();
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), new BrowserResult("revision", fileObject));
assertEqualAttributes(fileObject, dto);
}
@@ -57,7 +58,7 @@ public class FileObjectToFileObjectDtoMapperTest {
@Test
public void shouldHaveCorrectSelfLinkForDirectory() {
FileObject fileObject = createDirectoryObject();
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), new BrowserResult("revision", fileObject));
assertThat(dto.getLinks().getLinkBy("self").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/sources/revision/foo/bar").toString());
}
@@ -66,7 +67,7 @@ public class FileObjectToFileObjectDtoMapperTest {
public void shouldHaveCorrectContentLink() {
FileObject fileObject = createFileObject();
fileObject.setDirectory(false);
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), "revision");
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("namespace", "name"), new BrowserResult("revision", fileObject));
assertThat(dto.getLinks().getLinkBy("self").get().getHref()).isEqualTo(expectedBaseUri.resolve("namespace/name/content/revision/foo/bar").toString());
}
@@ -84,7 +85,7 @@ public class FileObjectToFileObjectDtoMapperTest {
mapper.setRegistry(registry);
FileObject fileObject = createFileObject();
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("hitchhiker", "hog"), "42");
FileObjectDto dto = mapper.map(fileObject, new NamespaceAndName("hitchhiker", "hog"), new BrowserResult("42", fileObject));
assertThat(dto.getLinks().getLinkBy("hog").get().getHref()).isEqualTo("http://hitchhiker/hog/foo/42");
}