Skip to content

The Unified and Autonomous Development Platform

add pull request resource

robin shineb18d02c85e75
diff --git a/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/PullRequestResource.java b/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/PullRequestResource.java
new file mode 100644
index 0000000000..056fef9290
--- /dev/null
+++ b/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/PullRequestResource.java
@@ -0,0 +1,99 @@
+package com.pmease.gitplex.rest.resource;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.authz.UnauthorizedException;
+import org.hibernate.criterion.Restrictions;
+
+import com.pmease.commons.hibernate.dao.Dao;
+import com.pmease.commons.hibernate.dao.EntityCriteria;
+import com.pmease.commons.jersey.ValidQueryParams;
+import com.pmease.gitplex.core.model.PullRequest;
+import com.pmease.gitplex.core.permission.ObjectPermission;
+
+@Path("/pull_requests")
+@Consumes(MediaType.WILDCARD)
+@Produces(MediaType.APPLICATION_JSON)
+@Singleton
+public class PullRequestResource {
+
+ private static final String OPEN = "open";
+
+ private static final String CLOSED = "closed";
+
+ private final Dao dao;
+
+ @Inject
+ public PullRequestResource(Dao dao) {
+ this.dao = dao;
+ }
+
+ @GET
+ @Path("/{id}")
+ public PullRequest get(@PathParam("id") Long id) {
+ PullRequest request = dao.load(PullRequest.class, id);
+
+ if (!SecurityUtils.getSubject().isPermitted(ObjectPermission.ofRepoPull(request.getTargetRepo())))
+ throw new UnauthorizedException();
+
+ return request;
+ }
+
+ @ValidQueryParams
+ @GET
+ public Collection<PullRequest> query(
+ @QueryParam("targetRepoId") Long targetRepoId, @QueryParam("targetBranch") String targetBranch,
+ @QueryParam("sourceRepoId") Long sourceRepoId, @QueryParam("sourceBranch") String sourceBranch,
+ @QueryParam("submitterId") Long submitterId, @QueryParam("status") String status,
+ @QueryParam("assigneeId") Long assigneeId, @QueryParam("beginDate") Date beginDate,
+ @QueryParam("endDate") Date endDate) {
+
+ EntityCriteria<PullRequest> criteria = EntityCriteria.of(PullRequest.class);
+
+ if (targetRepoId != null)
+ criteria.add(Restrictions.eq("targetRepo.id", targetRepoId));
+ if (targetBranch != null)
+ criteria.add(Restrictions.eq("targetBranch", targetBranch));
+
+ if (sourceRepoId != null)
+ criteria.add(Restrictions.eq("sourceRepo.id", targetRepoId));
+ if (sourceBranch != null)
+ criteria.add(Restrictions.eq("sourceBranch", sourceBranch));
+
+ if (OPEN.equalsIgnoreCase(status))
+ criteria.add(PullRequest.CriterionHelper.ofOpen());
+ else if (CLOSED.equalsIgnoreCase(status))
+ criteria.add(PullRequest.CriterionHelper.ofClosed());
+
+ if (submitterId != null)
+ criteria.add(Restrictions.eq("submitter.id", submitterId));
+ if (assigneeId != null)
+ criteria.add(Restrictions.eq("assignee.id", assigneeId));
+ if (beginDate != null)
+ criteria.add(Restrictions.ge("createDate", beginDate));
+ if (endDate != null)
+ criteria.add(Restrictions.le("createDate", endDate));
+
+ List<PullRequest> requests = dao.query(criteria);
+
+ for (PullRequest request: requests) {
+ if (!SecurityUtils.getSubject().isPermitted(ObjectPermission.ofRepoPull(request.getTarget().getRepository())))
+ throw new UnauthorizedException("Unauthorized access to pull request " + request.getTarget() + "/" + request.getId());
+ }
+ return requests;
+ }
+
+}
\ No newline at end of file
diff --git a/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/RepositoryResource.java b/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/RepositoryResource.java
index 2c91c30918..1806b22821 100644
--- a/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/RepositoryResource.java
+++ b/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/RepositoryResource.java
@@ -60,7 +60,7 @@ public class RepositoryResource {
@ValidQueryParams
@GET
- public Collection<Repository> query(@QueryParam("user") Long userId, @QueryParam("name") String name,
+ public Collection<Repository> query(@QueryParam("userId") Long userId, @QueryParam("name") String name,
@QueryParam("path") String path) {
EntityCriteria<Repository> criteria = EntityCriteria.of(Repository.class);
if (path != null) {
diff --git a/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/VerificationResource.java b/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/VerificationResource.java
index ebbc63f74f..8bddf08b8d 100644
--- a/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/VerificationResource.java
+++ b/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/VerificationResource.java
@@ -56,7 +56,7 @@ public class VerificationResource {
@ValidQueryParams
@GET
public Collection<PullRequestVerification> query(
- @QueryParam("request") Long requestId,
+ @QueryParam("requestId") Long requestId,
@QueryParam("configuration") String configuration,
@QueryParam("commit") String commit) {
EntityCriteria<PullRequestVerification> criteria = EntityCriteria.of(PullRequestVerification.class);