Search in sources :

Example 1 with Comment

use of com.cloudogu.scm.review.comment.service.Comment in project scm-review-plugin by scm-manager.

the class CommentResource method getReply.

@GET
@Path("replies/{replyId}")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Pull request comment reply", description = "Returns a single pull request comment reply.", tags = "Pull Request Comment")
@ApiResponse(responseCode = "200", description = "success", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = ReplyDto.class)))
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
@ApiResponse(responseCode = "403", description = "not authorized, the current user does not have the \"commentPullRequest\" privilege")
@ApiResponse(responseCode = "404", description = "not found, a reply with the given id is not available for this pull request")
@ApiResponse(responseCode = "500", description = "internal server error", content = @Content(mediaType = VndMediaType.ERROR_TYPE, schema = @Schema(implementation = ErrorDto.class)))
public ReplyDto getReply(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("pullRequestId") String pullRequestId, @PathParam("commentId") String commentId, @PathParam("replyId") String replyId, @QueryParam("sourceRevision") String expectedSourceRevision, @QueryParam("targetRevision") String expectedTargetRevision) {
    checkRevision(branchRevisionResolver, namespace, name, pullRequestId, expectedSourceRevision, expectedTargetRevision);
    Repository repository = repositoryResolver.resolve(new NamespaceAndName(namespace, name));
    BranchRevisionResolver.RevisionResult revisions = branchRevisionResolver.getRevisions(namespace, name, pullRequestId);
    Comment comment = service.get(namespace, name, pullRequestId, commentId);
    Reply reply = service.getReply(namespace, name, pullRequestId, commentId, replyId);
    return replyMapper.map(reply, repository, pullRequestId, comment, revisions);
}
Also used : Comment(com.cloudogu.scm.review.comment.service.Comment) Repository(sonia.scm.repository.Repository) NamespaceAndName(sonia.scm.repository.NamespaceAndName) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Reply(com.cloudogu.scm.review.comment.service.Reply) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 2 with Comment

use of com.cloudogu.scm.review.comment.service.Comment in project scm-review-plugin by scm-manager.

the class CommentResource method getExecutedTransition.

@GET
@Path("transitions/{transitionId}")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Get pull request comment transition", description = "Returns a single pull request comment transition.", tags = "Pull Request Comment")
@ApiResponse(responseCode = "200", description = "success")
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
@ApiResponse(responseCode = "403", description = "not authorized, the current user does not have the \"commentPullRequest\" privilege")
@ApiResponse(responseCode = "404", description = "not found, no comment transition with the specified id is available")
@ApiResponse(responseCode = "500", description = "internal server error", content = @Content(mediaType = VndMediaType.ERROR_TYPE, schema = @Schema(implementation = ErrorDto.class)))
public ExecutedTransitionDto getExecutedTransition(@Context UriInfo uriInfo, @PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("pullRequestId") String pullRequestId, @PathParam("commentId") String commentId, @PathParam("transitionId") String transitionId, @QueryParam("sourceRevision") String expectedSourceRevision, @QueryParam("targetRevision") String expectedTargetRevision) {
    checkRevision(branchRevisionResolver, namespace, name, pullRequestId, expectedSourceRevision, expectedTargetRevision);
    Comment comment = service.get(namespace, name, pullRequestId, commentId);
    ExecutedTransition<?> executedTransition = comment.getExecutedTransitions().stream().filter(t -> transitionId.equals(t.getId())).findFirst().orElseThrow(() -> notFound(entity("transition", transitionId).in(Comment.class, commentId).in(PullRequest.class, pullRequestId).in(new NamespaceAndName(namespace, name))));
    return executedTransitionMapper.map(executedTransition, new NamespaceAndName(namespace, name), pullRequestId, comment);
}
Also used : Comment(com.cloudogu.scm.review.comment.service.Comment) NamespaceAndName(sonia.scm.repository.NamespaceAndName) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 3 with Comment

use of com.cloudogu.scm.review.comment.service.Comment in project scm-review-plugin by scm-manager.

the class CommentRootResource method create.

@POST
@Path("")
@Consumes(MediaType.APPLICATION_JSON)
@Operation(summary = "Create pull request comment", description = "Creates a new pull request comment.", tags = "Pull Request Comment", operationId = "review_create_comment")
@ApiResponse(responseCode = "201", description = "create success")
@ApiResponse(responseCode = "400", description = "Invalid body, e.g. illegal change of namespace or name")
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
@ApiResponse(responseCode = "403", description = "not authorized, the current user does not have the \"commentPullRequest\" privilege")
@ApiResponse(responseCode = "500", description = "internal server error", content = @Content(mediaType = VndMediaType.ERROR_TYPE, schema = @Schema(implementation = ErrorDto.class)))
public Response create(@PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("pullRequestId") String pullRequestId, @QueryParam("sourceRevision") String expectedSourceRevision, @QueryParam("targetRevision") String expectedTargetRevision, @Valid @NotNull CommentDto commentDto) {
    if (commentDto.isSystemComment()) {
        throw new AuthorizationException("Is is Forbidden to create a system comment.");
    }
    checkRevision(branchRevisionResolver, namespace, name, pullRequestId, expectedSourceRevision, expectedTargetRevision);
    Comment comment = mapper.map(commentDto);
    String id = service.add(namespace, name, pullRequestId, comment);
    URI location = URI.create(commentPathBuilder.createCommentSelfUri(namespace, name, pullRequestId, id));
    return Response.created(location).build();
}
Also used : Comment(com.cloudogu.scm.review.comment.service.Comment) AuthorizationException(org.apache.shiro.authz.AuthorizationException) URI(java.net.URI) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 4 with Comment

use of com.cloudogu.scm.review.comment.service.Comment in project scm-review-plugin by scm-manager.

the class CommentRootResource method getAll.

@GET
@Path("")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Get all pull request comments", description = "Returns all pull request comments.", tags = "Pull Request Comment", operationId = "review_get_comments")
@ApiResponse(responseCode = "200", description = "success", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = HalRepresentation.class)))
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
@ApiResponse(responseCode = "403", description = "not authorized, the current user does not have the \"commentPullRequest\" privilege")
@ApiResponse(responseCode = "500", description = "internal server error", content = @Content(mediaType = VndMediaType.ERROR_TYPE, schema = @Schema(implementation = ErrorDto.class)))
public HalRepresentation getAll(@Context UriInfo uriInfo, @PathParam("namespace") String namespace, @PathParam("name") String name, @PathParam("pullRequestId") String pullRequestId) {
    PullRequestResourceLinks resourceLinks = new PullRequestResourceLinks(uriInfo::getBaseUri);
    Repository repository = repositoryResolver.resolve(new NamespaceAndName(namespace, name));
    PullRequest pullRequest = pullRequestService.get(namespace, name, pullRequestId);
    BranchRevisionResolver.RevisionResult revisions = branchRevisionResolver.getRevisions(new NamespaceAndName(namespace, name), pullRequest);
    List<Comment> list = service.getAll(namespace, name, pullRequestId);
    List<CommentDto> dtoList = list.stream().map(comment -> mapper.map(comment, repository, pullRequestId, service.possibleTransitions(namespace, name, pullRequestId, comment.getId()), revisions)).collect(Collectors.toList());
    boolean permission = PermissionCheck.mayComment(repository);
    return createCollection(permission, resourceLinks.pullRequestComments().all(namespace, name, pullRequestId), resourceLinks.pullRequestComments().create(namespace, name, pullRequestId, revisions), dtoList, "pullRequestComments");
}
Also used : PullRequestMediaType(com.cloudogu.scm.review.PullRequestMediaType) PathParam(javax.ws.rs.PathParam) Provider(javax.inject.Provider) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Repository(sonia.scm.repository.Repository) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) Inject(javax.inject.Inject) Valid(javax.validation.Valid) Content(io.swagger.v3.oas.annotations.media.Content) Operation(io.swagger.v3.oas.annotations.Operation) MediaType(javax.ws.rs.core.MediaType) VndMediaType(sonia.scm.web.VndMediaType) QueryParam(javax.ws.rs.QueryParam) HalRepresentation(de.otto.edison.hal.HalRepresentation) Consumes(javax.ws.rs.Consumes) Comment(com.cloudogu.scm.review.comment.service.Comment) ErrorDto(sonia.scm.api.v2.resources.ErrorDto) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse) PermissionCheck(com.cloudogu.scm.review.PermissionCheck) PullRequestService(com.cloudogu.scm.review.pullrequest.service.PullRequestService) URI(java.net.URI) Schema(io.swagger.v3.oas.annotations.media.Schema) RepositoryResolver(com.cloudogu.scm.review.RepositoryResolver) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) HalRepresentations.createCollection(com.cloudogu.scm.review.HalRepresentations.createCollection) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) AuthorizationException(org.apache.shiro.authz.AuthorizationException) List(java.util.List) Response(javax.ws.rs.core.Response) PullRequestResourceLinks(com.cloudogu.scm.review.PullRequestResourceLinks) CommentService(com.cloudogu.scm.review.comment.service.CommentService) RevisionChecker.checkRevision(com.cloudogu.scm.review.comment.api.RevisionChecker.checkRevision) UriInfo(javax.ws.rs.core.UriInfo) NamespaceAndName(sonia.scm.repository.NamespaceAndName) Comment(com.cloudogu.scm.review.comment.service.Comment) NamespaceAndName(sonia.scm.repository.NamespaceAndName) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Repository(sonia.scm.repository.Repository) PullRequestResourceLinks(com.cloudogu.scm.review.PullRequestResourceLinks) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Example 5 with Comment

use of com.cloudogu.scm.review.comment.service.Comment in project scm-review-plugin by scm-manager.

the class CommentRootResourceTest method shouldCreateNewComment.

@Test
@SubjectAware(username = "slarti", password = "secret")
public void shouldCreateNewComment() throws URISyntaxException {
    when(pullRequestService.get(any(), any(), any())).thenReturn(PULL_REQUEST);
    when(service.add(eq(REPOSITORY_NAMESPACE), eq(REPOSITORY_NAME), eq("1"), argThat(t -> t.getComment().equals("this is my comment")))).thenReturn("1");
    byte[] commentJson = "{\"comment\" : \"this is my comment\"}".getBytes();
    MockHttpRequest request = MockHttpRequest.post("/" + PullRequestRootResource.PULL_REQUESTS_PATH_V2 + "/space/name/1/comments?sourceRevision=source&targetRevision=target").content(commentJson).contentType(MediaType.APPLICATION_JSON);
    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
    assertThat(response.getOutputHeaders().getFirst("Location")).hasToString("/v2/pull-requests/space/name/1/comments/1");
}
Also used : ContextLine.copy(com.cloudogu.scm.review.comment.service.ContextLine.copy) Provider(javax.inject.Provider) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ShiroRule(com.github.sdorra.shiro.ShiroRule) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RestDispatcher(sonia.scm.web.RestDispatcher) URISyntaxException(java.net.URISyntaxException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BranchRevisionResolver(com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver) Repository(sonia.scm.repository.Repository) PullRequest(com.cloudogu.scm.review.pullrequest.service.PullRequest) PullRequestRootResource(com.cloudogu.scm.review.pullrequest.api.PullRequestRootResource) PullRequestMapperImpl(com.cloudogu.scm.review.pullrequest.dto.PullRequestMapperImpl) MediaType(javax.ws.rs.core.MediaType) Comment(com.cloudogu.scm.review.comment.service.Comment) Arrays.asList(java.util.Arrays.asList) JsonNode(com.fasterxml.jackson.databind.JsonNode) UriBuilder(javax.ws.rs.core.UriBuilder) PullRequestService(com.cloudogu.scm.review.pullrequest.service.PullRequestService) RepositoryResolver(com.cloudogu.scm.review.RepositoryResolver) PullRequestResource(com.cloudogu.scm.review.pullrequest.api.PullRequestResource) ExecutedTransition(com.cloudogu.scm.review.comment.service.ExecutedTransition) ChannelRegistry(sonia.scm.sse.ChannelRegistry) MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) CommentService(com.cloudogu.scm.review.comment.service.CommentService) UserDisplayManager(sonia.scm.user.UserDisplayManager) InlineContext(com.cloudogu.scm.review.comment.service.InlineContext) Providers(com.google.inject.util.Providers) UriInfo(javax.ws.rs.core.UriInfo) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockHttpResponse(org.jboss.resteasy.mock.MockHttpResponse) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) ArrayList(java.util.ArrayList) Reply(com.cloudogu.scm.review.comment.service.Reply) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Reply.createReply(com.cloudogu.scm.review.comment.service.Reply.createReply) RepositoryServiceFactory(sonia.scm.repository.api.RepositoryServiceFactory) MockedDiffLine(com.cloudogu.scm.review.comment.service.MockedDiffLine) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) TestData(com.cloudogu.scm.review.TestData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) CommentTransition(com.cloudogu.scm.review.comment.service.CommentTransition) SubjectAware(com.github.sdorra.shiro.SubjectAware) Rule(org.junit.Rule) Location(com.cloudogu.scm.review.comment.service.Location) Comment.createComment(com.cloudogu.scm.review.comment.service.Comment.createComment) Assert.assertEquals(org.junit.Assert.assertEquals) MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) Test(org.junit.Test) SubjectAware(com.github.sdorra.shiro.SubjectAware)

Aggregations

Comment (com.cloudogu.scm.review.comment.service.Comment)18 Repository (sonia.scm.repository.Repository)7 Comment.createComment (com.cloudogu.scm.review.comment.service.Comment.createComment)6 BranchRevisionResolver (com.cloudogu.scm.review.pullrequest.dto.BranchRevisionResolver)6 MockHttpRequest (org.jboss.resteasy.mock.MockHttpRequest)6 Test (org.junit.Test)6 Location (com.cloudogu.scm.review.comment.service.Location)5 Reply (com.cloudogu.scm.review.comment.service.Reply)5 PullRequest (com.cloudogu.scm.review.pullrequest.service.PullRequest)5 Operation (io.swagger.v3.oas.annotations.Operation)5 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)5 Path (javax.ws.rs.Path)5 NamespaceAndName (sonia.scm.repository.NamespaceAndName)5 RepositoryResolver (com.cloudogu.scm.review.RepositoryResolver)4 CommentService (com.cloudogu.scm.review.comment.service.CommentService)4 PullRequestService (com.cloudogu.scm.review.pullrequest.service.PullRequestService)4 GET (javax.ws.rs.GET)4 Produces (javax.ws.rs.Produces)4 MediaType (javax.ws.rs.core.MediaType)4 UriBuilder (javax.ws.rs.core.UriBuilder)4