Append self links to parsed diff results

This commit is contained in:
Rene Pfeuffer
2020-01-24 16:31:31 +01:00
parent 9fcaf5e69d
commit 02a9dafee4
8 changed files with 171 additions and 51 deletions

View File

@@ -3,6 +3,7 @@ package sonia.scm.api.v2.resources;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
import lombok.Data;
import java.util.List;
@@ -10,6 +11,10 @@ import java.util.List;
@Data
public class DiffResultDto extends HalRepresentation {
public DiffResultDto(Links links) {
super(links);
}
private List<FileDto> files;
@Data

View File

@@ -3,6 +3,8 @@ package sonia.scm.api.v2.resources;
import com.github.sdorra.spotter.ContentTypes;
import com.github.sdorra.spotter.Language;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.DiffFile;
import sonia.scm.repository.api.DiffLine;
import sonia.scm.repository.api.DiffResult;
@@ -13,24 +15,38 @@ import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import static de.otto.edison.hal.Links.linkingTo;
/**
* TODO conflicts, copy and rename
*/
final class DiffResultToDiffResultDtoMapper {
class DiffResultToDiffResultDtoMapper {
static final DiffResultToDiffResultDtoMapper INSTANCE = new DiffResultToDiffResultDtoMapper();
private final ResourceLinks resourceLinks;
private DiffResultToDiffResultDtoMapper() {
@Inject
DiffResultToDiffResultDtoMapper(ResourceLinks resourceLinks) {
this.resourceLinks = resourceLinks;
}
public DiffResultDto map(DiffResult result) {
public DiffResultDto mapForIncoming(Repository repository, DiffResult result, String source, String target) {
DiffResultDto dto = new DiffResultDto(linkingTo().self(resourceLinks.incoming().diffParsed(repository.getNamespace(), repository.getName(), source, target)).build());
setFiles(result, dto);
return dto;
}
public DiffResultDto mapForRevision(Repository repository, DiffResult result, String revision) {
DiffResultDto dto = new DiffResultDto(linkingTo().self(resourceLinks.diff().parsed(repository.getNamespace(), repository.getName(), revision)).build());
setFiles(result, dto);
return dto;
}
private void setFiles(DiffResult result, DiffResultDto dto) {
List<DiffResultDto.FileDto> files = new ArrayList<>();
for (DiffFile file : result) {
files.add(mapFile(file));
}
DiffResultDto dto = new DiffResultDto();
dto.setFiles(files);
return dto;
}
private DiffResultDto.FileDto mapFile(DiffFile file) {

View File

@@ -31,10 +31,12 @@ public class DiffRootResource {
static final String DIFF_FORMAT_VALUES_REGEX = "NATIVE|GIT|UNIFIED";
private final RepositoryServiceFactory serviceFactory;
private final DiffResultToDiffResultDtoMapper parsedDiffMapper;
@Inject
public DiffRootResource(RepositoryServiceFactory serviceFactory) {
public DiffRootResource(RepositoryServiceFactory serviceFactory, DiffResultToDiffResultDtoMapper parsedDiffMapper) {
this.serviceFactory = serviceFactory;
this.parsedDiffMapper = parsedDiffMapper;
}
@@ -83,11 +85,11 @@ public class DiffRootResource {
@ResponseCode(code = 404, condition = "not found, no revision with the specified param for the repository available or repository not found"),
@ResponseCode(code = 500, condition = "internal server error")
})
public Response getParsed(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision) throws IOException {
public DiffResultDto getParsed(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("revision") String revision) throws IOException {
HttpUtil.checkForCRLFInjection(revision);
try (RepositoryService repositoryService = serviceFactory.create(new NamespaceAndName(namespace, name))) {
DiffResult diffResult = repositoryService.getDiffResultCommand().setRevision(revision).getDiffResult();
return Response.ok(DiffResultToDiffResultDtoMapper.INSTANCE.map(diffResult)).build();
return parsedDiffMapper.mapForRevision(repositoryService.getRepository(), diffResult, revision);
}
}
}

View File

@@ -34,16 +34,16 @@ import static sonia.scm.api.v2.resources.DiffRootResource.HEADER_CONTENT_DISPOSI
public class IncomingRootResource {
private final RepositoryServiceFactory serviceFactory;
private final IncomingChangesetCollectionToDtoMapper mapper;
private final IncomingChangesetCollectionToDtoMapper changesetMapper;
private final DiffResultToDiffResultDtoMapper parsedDiffMapper;
@Inject
public IncomingRootResource(RepositoryServiceFactory serviceFactory, IncomingChangesetCollectionToDtoMapper incomingChangesetCollectionToDtoMapper) {
public IncomingRootResource(RepositoryServiceFactory serviceFactory, IncomingChangesetCollectionToDtoMapper incomingChangesetCollectionToDtoMapper, DiffResultToDiffResultDtoMapper parsedDiffMapper) {
this.serviceFactory = serviceFactory;
this.mapper = incomingChangesetCollectionToDtoMapper;
this.changesetMapper = incomingChangesetCollectionToDtoMapper;
this.parsedDiffMapper = parsedDiffMapper;
}
/**
@@ -110,7 +110,7 @@ public class IncomingRootResource {
.getChangesets();
if (changesets != null && changesets.getChangesets() != null) {
PageResult<Changeset> pageResult = new PageResult<>(changesets.getChangesets(), changesets.getTotal());
return Response.ok(mapper.map(page, pageSize, pageResult, repository, source, target)).build();
return Response.ok(changesetMapper.map(page, pageSize, pageResult, repository, source, target)).build();
} else {
return Response.ok().build();
}
@@ -160,7 +160,7 @@ public class IncomingRootResource {
@ResponseCode(code = 400, condition = "Bad Request"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user has no privileges to read the diff"),
@ResponseCode(code = 404, condition = "not found, no revision with the specified param for the repository available or repository not found"),
@ResponseCode(code = 404, condition = "not found, source or target branch for the repository not available or repository not found"),
@ResponseCode(code = 500, condition = "internal server error")
})
public Response incomingDiffParsed(@PathParam("namespace") String namespace,
@@ -174,7 +174,7 @@ public class IncomingRootResource {
.setRevision(source)
.setAncestorChangeset(target)
.getDiffResult();
return Response.ok(DiffResultToDiffResultDtoMapper.INSTANCE.map(diffResult)).build();
return Response.ok(parsedDiffMapper.mapForIncoming(repositoryService.getRepository(), diffResult, source, target)).build();
}
}
}

View File

@@ -419,7 +419,18 @@ class ResourceLinks {
}
public String diffParsed(String namespace, String name) {
return toTemplateParams(incomingLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("incoming").parameters().method("incomingDiffParsed").parameters("source", "target").href());
return toTemplateParams(diffParsed(namespace, name, "source", "target"));
}
public String diffParsed(String namespace, String name, String source, String target) {
return incomingLinkBuilder
.method("getRepositoryResource")
.parameters(namespace, name)
.method("incoming")
.parameters()
.method("incomingDiffParsed")
.parameters(source, target)
.href();
}
public String toTemplateParams(String href) {