The Unified and Autonomous Development Platform
refactor: Remove PullRequestAssignmentResource and integrate assignment functionality into PullRequestResource
Robin Shen53225f4dd068
diff --git a/server-core/src/main/java/io/onedev/server/rest/resource/PullRequestAssignmentResource.java b/server-core/src/main/java/io/onedev/server/rest/resource/PullRequestAssignmentResource.javadeleted file mode 100644index fcdbe593c1..0000000000--- a/server-core/src/main/java/io/onedev/server/rest/resource/PullRequestAssignmentResource.java+++ /dev/null@@ -1,77 +0,0 @@-package io.onedev.server.rest.resource;--import javax.inject.Inject;-import javax.inject.Singleton;-import javax.ws.rs.Consumes;-import javax.ws.rs.DELETE;-import javax.ws.rs.GET;-import javax.ws.rs.POST;-import javax.ws.rs.Path;-import javax.ws.rs.PathParam;-import javax.ws.rs.Produces;-import javax.ws.rs.core.MediaType;-import javax.ws.rs.core.Response;--import org.apache.shiro.authz.UnauthorizedException;--import io.onedev.commons.utils.ExplicitException;-import io.onedev.server.service.PullRequestAssignmentService;-import io.onedev.server.model.PullRequest;-import io.onedev.server.model.PullRequestAssignment;-import io.onedev.server.rest.annotation.Api;-import io.onedev.server.security.SecurityUtils;--@Path("/pull-request-assignments")-@Consumes(MediaType.APPLICATION_JSON)-@Produces(MediaType.APPLICATION_JSON)-@Singleton-public class PullRequestAssignmentResource {-- private final PullRequestAssignmentService assignmentService;-- @Inject- public PullRequestAssignmentResource(PullRequestAssignmentService assignmentService) {- this.assignmentService = assignmentService;- }-- @Api(order=100)- @Path("/{assignmentId}")- @GET- public PullRequestAssignment get(@PathParam("assignmentId") Long assignmentId) {- PullRequestAssignment assignment = assignmentService.load(assignmentId);- if (!SecurityUtils.canReadCode(assignment.getRequest().getProject()))- throw new UnauthorizedException();- return assignment;- }-- @Api(order=200, description="Create new pull request assignment")- @POST- public Long create(PullRequestAssignment assignment) {- PullRequest pullRequest = assignment.getRequest();- if (!SecurityUtils.canModifyPullRequest(pullRequest)) {- throw new UnauthorizedException();- }-- if (pullRequest.isMerged())- throw new ExplicitException("Pull request is merged");- assignmentService.create(assignment);- return assignment.getId();- }-- @Api(order=300)- @Path("/{assignmentId}")- @DELETE- public Response delete(@PathParam("assignmentId") Long assignmentId) {- PullRequestAssignment assignment = assignmentService.load(assignmentId);- if (!SecurityUtils.canModifyPullRequest(assignment.getRequest())) {- throw new UnauthorizedException();- }- if (assignment.getRequest().isMerged())- throw new ExplicitException("Pull request is merged");-- assignmentService.delete(assignment);-- return Response.ok().build();- }--}diff --git a/server-core/src/main/java/io/onedev/server/rest/resource/PullRequestResource.java b/server-core/src/main/java/io/onedev/server/rest/resource/PullRequestResource.javaindex aaac02c282..2d8732cda9 100644--- a/server-core/src/main/java/io/onedev/server/rest/resource/PullRequestResource.java+++ b/server-core/src/main/java/io/onedev/server/rest/resource/PullRequestResource.java@@ -59,6 +59,7 @@ import io.onedev.server.rest.resource.support.RestConstants;import io.onedev.server.search.entity.pullrequest.PullRequestQuery;import io.onedev.server.security.SecurityUtils;import io.onedev.server.service.AuditService;+import io.onedev.server.service.PullRequestAssignmentService;import io.onedev.server.service.PullRequestChangeService;import io.onedev.server.service.PullRequestReviewService;import io.onedev.server.service.PullRequestService;@@ -83,6 +84,9 @@ public class PullRequestResource {@Injectprivate PullRequestReviewService pullRequestReviewService;++ @Inject+ private PullRequestAssignmentService pullRequestAssignmentService;@Injectprivate UserService userService;@@ -135,11 +139,18 @@ public class PullRequestResource {@Api(order=300)@Path("/{requestId}/assignments")@GET- public Collection<PullRequestAssignment> getAssignments(@PathParam("requestId") Long requestId) {+ public Collection<Map<String, Object>> getAssignments(@PathParam("requestId") Long requestId) {PullRequest pullRequest = pullRequestService.load(requestId);if (!SecurityUtils.canReadCode(pullRequest.getProject()))throw new UnauthorizedException();- return pullRequest.getAssignments();++ var typeReference = new TypeReference<HashMap<String, Object>>() {};+ return pullRequest.getAssignments().stream()+ .map(it-> {+ var map = objectMapper.convertValue(it, typeReference);+ map.remove("id");+ return map;+ }).collect(toList());}@Api(order=400)@@ -379,7 +390,7 @@ public class PullRequestResource {var request = pullRequestService.load(requestId);var user = SecurityUtils.getUser();note = StringUtils.trimToNull(note);-+if (user == null)throw new UnauthorizedException();@@ -413,6 +424,50 @@ public class PullRequestResource {return Response.ok().build();});}++ @Api(order=1480)+ @Path("/{requestId}/assignees/{userId}")+ @POST+ public Response addAssignee(@PathParam("requestId") Long requestId, @PathParam("userId") Long userId) {+ var request = pullRequestService.load(requestId);+ var user = userService.load(userId);++ if (!SecurityUtils.canModifyPullRequest(request))+ throw new UnauthorizedException();++ if (!SecurityUtils.canWriteCode(user.asSubject(), request.getProject()))+ throw new NotAcceptableException("Assignee needs to have write code permission to the project");++ if (request.getAssignees().contains(user))+ return Response.ok().build();++ var assignment = new PullRequestAssignment();+ assignment.setRequest(request);+ assignment.setUser(user);+ pullRequestAssignmentService.create(assignment);++ return Response.ok().build();+ }++ @Api(order=1490)+ @Path("/{requestId}/assignees/{userId}")+ @DELETE+ public Response removeAssignee(@PathParam("requestId") Long requestId, @PathParam("userId") Long userId) {+ var request = pullRequestService.load(requestId);+ var user = userService.load(userId);++ if (!SecurityUtils.canModifyPullRequest(request))+ throw new UnauthorizedException();++ var assignment = request.getAssignments().stream()+ .filter(it -> it.getUser().equals(user))+ .findFirst()+ .orElse(null);+ if (assignment != null)+ pullRequestAssignmentService.delete(assignment);++ return Response.ok().build();+ }@Api(order=1500)@Path("/{requestId}/merge-strategy")