Skip to content

The Unified and Autonomous Development Platform

Improve hibernate Jackson support to be able to deal with JsonView.

robin shine3b19981912bb
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.java
index 9465148b53..51ad2f1dc1 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java
@@ -345,11 +345,12 @@ public class Git implements Serializable {
* <tt>true</tt> if success, or <tt>false</tt> if the reference
* can not be locked due to old revision not matching
*/
- public boolean updateRef(String refName, String revision,
+ public Git updateRef(String refName, String revision,
@Nullable String oldRevision, @Nullable String reason) {
- return new UpdateRefCommand(repoDir).refName(refName)
+ new UpdateRefCommand(repoDir).refName(refName)
.revision(revision).oldRevision(oldRevision)
.reason(reason).call();
+ return this;
}
public Git deleteRef(String refName, @Nullable String oldRevision, @Nullable String reason) {
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/UpdateRefCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/UpdateRefCommand.java
index 6fbaa2ec5e..eb79f9381c 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/UpdateRefCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/UpdateRefCommand.java
@@ -1,17 +1,15 @@
package com.pmease.commons.git.command;
import java.io.File;
-import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
import com.pmease.commons.util.execution.Commandline;
-import com.pmease.commons.util.execution.ExecuteResult;
import com.pmease.commons.util.execution.LineConsumer;
-public class UpdateRefCommand extends GitCommand<Boolean> {
+public class UpdateRefCommand extends GitCommand<Void> {
private static final Logger logger = LoggerFactory.getLogger(UpdateRefCommand.class);
@@ -48,7 +46,7 @@ public class UpdateRefCommand extends GitCommand<Boolean> {
}
@Override
- public Boolean call() {
+ public Void call() {
Preconditions.checkNotNull(refName, "refName has to be specified.");
Preconditions.checkNotNull(revision, "revision has to be specified.");
@@ -59,8 +57,7 @@ public class UpdateRefCommand extends GitCommand<Boolean> {
if (reason != null)
cmd.addArgs("-m", reason);
- final AtomicBoolean refLockError = new AtomicBoolean(false);
- ExecuteResult result = cmd.execute(new LineConsumer() {
+ cmd.execute(new LineConsumer() {
@Override
public void consume(String line) {
@@ -75,19 +72,11 @@ public class UpdateRefCommand extends GitCommand<Boolean> {
logger.info(line);
else
logger.error(line);
-
- if (line.startsWith("fatal: Cannot lock the ref"))
- refLockError.set(true);
}
- });
+ }).checkReturnCode();
- if (refLockError.get())
- return false;
-
- result.checkReturnCode();
-
- return true;
+ return null;
}
}
diff --git a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/EntitySerializer.java b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/EntitySerializer.java
deleted file mode 100644
index e0502196a9..0000000000
--- a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/EntitySerializer.java
+++ /dev/null
@@ -1,27 +0,0 @@
-package com.pmease.commons.hibernate.jackson;
-
-import java.io.IOException;
-
-import org.hibernate.proxy.HibernateProxy;
-
-import com.fasterxml.jackson.core.JsonGenerationException;
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.databind.SerializerProvider;
-import com.fasterxml.jackson.databind.ser.BeanSerializer;
-
-public class EntitySerializer extends BeanSerializer {
-
- public EntitySerializer(BeanSerializer defaultSerializer) {
- super(defaultSerializer);
- }
-
- @Override
- protected void serializeFields(Object bean, JsonGenerator jgen, SerializerProvider provider)
- throws IOException, JsonGenerationException {
- if (bean instanceof HibernateProxy)
- bean = ((HibernateProxy) bean).getHibernateLazyInitializer().getImplementation();
-
- super.serializeFields(bean, jgen, provider);
- }
-
-}
diff --git a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/HibernateObjectMapperModule.java b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/HibernateObjectMapperModule.java
index 536d809369..d2eba264a6 100644
--- a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/HibernateObjectMapperModule.java
+++ b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/HibernateObjectMapperModule.java
@@ -3,17 +3,19 @@ package com.pmease.commons.hibernate.jackson;
import javax.inject.Inject;
import javax.inject.Singleton;
+import org.hibernate.proxy.HibernateProxy;
+
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
+import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
-import com.fasterxml.jackson.databind.ser.BeanSerializer;
-import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
+import com.fasterxml.jackson.databind.ser.Serializers;
import com.pmease.commons.hibernate.AbstractEntity;
import com.pmease.commons.hibernate.dao.Dao;
@@ -33,21 +35,19 @@ public class HibernateObjectMapperModule extends Module {
context.appendAnnotationIntrospector(new HibernateAnnotationIntrospector());
- context.addBeanSerializerModifier(new BeanSerializerModifier() {
+ context.addSerializers(new Serializers.Base(){
@Override
- public JsonSerializer<?> modifySerializer(
- SerializationConfig config, BeanDescription beanDesc,
- JsonSerializer<?> serializer) {
- if (AbstractEntity.class.isAssignableFrom(beanDesc.getBeanClass())) {
- return new EntitySerializer((BeanSerializer) serializer);
- } else {
- return super.modifySerializer(config, beanDesc, serializer);
- }
- }
+ public JsonSerializer<?> findSerializer(SerializationConfig config, JavaType type, BeanDescription beanDesc) {
+ Class<?> raw = type.getRawClass();
+ if (HibernateProxy.class.isAssignableFrom(raw))
+ return new HibernateProxySerializer();
+ else
+ return null;
+ }
});
-
+
context.addBeanDeserializerModifier(new BeanDeserializerModifier() {
@Override
diff --git a/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/HibernateProxySerializer.java b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/HibernateProxySerializer.java
new file mode 100644
index 0000000000..656009f1e6
--- /dev/null
+++ b/commons.hibernate/src/main/java/com/pmease/commons/hibernate/jackson/HibernateProxySerializer.java
@@ -0,0 +1,105 @@
+package com.pmease.commons.hibernate.jackson;
+
+import java.io.IOException;
+
+import org.hibernate.proxy.HibernateProxy;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
+import com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap;
+
+/**
+ * Serializer to use for values proxied using {@link HibernateProxy}.
+ * <p>
+ * TODO: should try to make this work more like Jackson
+ * <code>BeanPropertyWriter</code>, possibly sub-classing it -- it handles much
+ * of functionality we need, and has access to more information than value
+ * serializers (like this one) have.
+ */
+public class HibernateProxySerializer extends JsonSerializer<HibernateProxy> {
+
+ protected PropertySerializerMap _dynamicSerializers = PropertySerializerMap.emptyMap();
+
+ // since 2.3
+ @Override
+ public boolean isEmpty(HibernateProxy value) {
+ return (value == null) || (findProxied(value) == null);
+ }
+
+ @Override
+ public void serialize(HibernateProxy value, JsonGenerator jgen,
+ SerializerProvider provider) throws IOException,
+ JsonProcessingException {
+ Object proxiedValue = findProxied(value);
+ // TODO: figure out how to suppress nulls, if necessary? (too late for
+ // that here)
+ if (proxiedValue == null) {
+ provider.defaultSerializeNull(jgen);
+ return;
+ }
+ findSerializer(provider, proxiedValue).serialize(proxiedValue, jgen,
+ provider);
+ }
+
+ @Override
+ public void serializeWithType(HibernateProxy value, JsonGenerator jgen,
+ SerializerProvider provider, TypeSerializer typeSer)
+ throws IOException, JsonProcessingException {
+ Object proxiedValue = findProxied(value);
+ if (proxiedValue == null) {
+ provider.defaultSerializeNull(jgen);
+ return;
+ }
+ /*
+ * This isn't exactly right, since type serializer really refers to
+ * proxy object, not value. And we really don't either know static type
+ * (necessary to know how to apply additional type info) or other
+ * things; so it's not going to work well. But... we'll do out best.
+ */
+ findSerializer(provider, proxiedValue).serializeWithType(proxiedValue,
+ jgen, provider, typeSer);
+ }
+
+ /*
+ * /**********************************************************************
+ * /* Helper methods
+ * /**********************************************************************
+ */
+
+ protected JsonSerializer<Object> findSerializer(
+ SerializerProvider provider, Object value) throws IOException,
+ JsonProcessingException {
+ /*
+ * TODO: if Hibernate did use generics, or we wanted to allow use of
+ * Jackson annotations to indicate type, should take that into account.
+ */
+ Class<?> type = value.getClass();
+ /*
+ * we will use a map to contain serializers found so far, keyed by type:
+ * this avoids potentially costly lookup from global caches and/or
+ * construction of new serializers
+ */
+ /*
+ * 18-Oct-2013, tatu: Whether this is for the primary property or
+ * secondary is really anyone's guess at this point; proxies can exist
+ * at any level?
+ */
+ PropertySerializerMap.SerializerAndMapResult result = _dynamicSerializers
+ .findAndAddPrimarySerializer(type, provider, null);
+ if (_dynamicSerializers != result.map) {
+ _dynamicSerializers = result.map;
+ }
+ return result.serializer;
+ }
+
+ /**
+ * Helper method for finding value being proxied, if it is available or if
+ * it is to be forced to be loaded.
+ */
+ protected Object findProxied(HibernateProxy proxy) {
+ return proxy.getHibernateLazyInitializer().getImplementation();
+ }
+}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/IfVerifiedByBuilds.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/IfVerifiedByBuilds.java
index a4e550307f..7263da299e 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/IfVerifiedByBuilds.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/gatekeeper/IfVerifiedByBuilds.java
@@ -8,14 +8,13 @@ import com.pmease.commons.editable.annotation.Editable;
import com.pmease.gitplex.core.GitPlex;
import com.pmease.gitplex.core.gatekeeper.checkresult.CheckResult;
import com.pmease.gitplex.core.gatekeeper.voteeligibility.NoneCanVote;
-import com.pmease.gitplex.core.manager.PullRequestManager;
import com.pmease.gitplex.core.manager.VerificationManager;
import com.pmease.gitplex.core.model.Branch;
import com.pmease.gitplex.core.model.IntegrationPreview;
import com.pmease.gitplex.core.model.PullRequest;
+import com.pmease.gitplex.core.model.PullRequestVerification;
import com.pmease.gitplex.core.model.Repository;
import com.pmease.gitplex.core.model.User;
-import com.pmease.gitplex.core.model.PullRequestVerification;
@Editable(icon="fa-checkbox-checked", order=1000,
description="This gate keeper will be satisfied if commit is verified successfully "
@@ -73,9 +72,7 @@ public class IfVerifiedByBuilds extends AbstractGateKeeper {
String commit;
if (isCheckIntegrated()) {
- PullRequestManager pullRequestManager = GitPlex.getInstance(PullRequestManager.class);
-
- IntegrationPreview preview = pullRequestManager.previewIntegration(request);
+ IntegrationPreview preview = request.getIntegrationPreview();
if (preview == null) {
if (blockMode)
return pendingAndBlock("To be verified by build", new NoneCanVote());
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java
index 9dec27f759..9bd9bd292e 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestManager.java
@@ -157,7 +157,7 @@ public class DefaultPullRequestManager implements PullRequestManager {
if (request.getStatus() != PullRequest.Status.PENDING_INTEGRATE)
throw new IllegalStateException("Gate keeper disallows integration right now.");
- IntegrationPreview preview = previewIntegration(request);
+ IntegrationPreview preview = request.getIntegrationPreview();
if (preview == null)
throw new IllegalStateException("Integration preview has not been calculated yet.");
@@ -186,64 +186,55 @@ public class DefaultPullRequestManager implements PullRequestManager {
}
if (strategy == REBASE_SOURCE_ONTO_TARGET || strategy == MERGE_WITH_SQUASH) {
Git sourceGit = request.getSource().getRepository().git();
- if (sourceGit.updateRef(request.getSource().getHeadRef(),
- integrated, preview.getRequestHead(),
- "Pull request #" + request.getId())) {
- request.getSource().setHeadCommitHash(integrated);
- branchManager.save(request.getSource());
- } else {
- throw new RuntimeException(String.format(
- "Unable to target branch '%s' due to lock failure.", request.getTarget()));
- }
+ sourceGit.updateRef(request.getSource().getHeadRef(), integrated, preview.getRequestHead(),
+ "Pull request #" + request.getId());
+ request.getSource().setHeadCommitHash(integrated);
+ branchManager.save(request.getSource());
}
- if (git.updateRef(request.getTarget().getHeadRef(), integrated,
- preview.getTargetHead(), "Pull request #" + request.getId())) {
- request.getTarget().setHeadCommitHash(integrated);
- branchManager.save(request.getTarget());
-
- PullRequestAudit audit = new PullRequestAudit();
- audit.setRequest(request);
- audit.setDate(new Date());
- audit.setOperation(PullRequestOperation.INTEGRATE);
- audit.setUser(user);
-
- dao.persist(audit);
+ git.updateRef(request.getTarget().getHeadRef(), integrated,
+ preview.getTargetHead(), "Pull request #" + request.getId());
+ request.getTarget().setHeadCommitHash(integrated);
+ branchManager.save(request.getTarget());
+
+ PullRequestAudit audit = new PullRequestAudit();
+ audit.setRequest(request);
+ audit.setDate(new Date());
+ audit.setOperation(PullRequestOperation.INTEGRATE);
+ audit.setUser(user);
+
+ dao.persist(audit);
- if (comment != null) {
- PullRequestComment requestComment = new PullRequestComment();
- requestComment.setContent(comment);
- requestComment.setDate(audit.getDate());
- requestComment.setRequest(request);
- requestComment.setUser(user);
- dao.persist(requestComment);
- }
+ if (comment != null) {
+ PullRequestComment requestComment = new PullRequestComment();
+ requestComment.setContent(comment);
+ requestComment.setDate(audit.getDate());
+ requestComment.setRequest(request);
+ requestComment.setUser(user);
+ dao.persist(requestComment);
+ }
- request.setCloseStatus(CloseStatus.INTEGRATED);
- request.setUpdateDate(new Date());
+ request.setCloseStatus(CloseStatus.INTEGRATED);
+ request.setUpdateDate(new Date());
- dao.persist(request);
+ dao.persist(request);
- final Long requestId = request.getId();
-
- dao.afterCommit(new Runnable() {
+ final Long requestId = request.getId();
+
+ dao.afterCommit(new Runnable() {
- @Override
- public void run() {
- pullRequestListeners.asyncCall(requestId, new PullRequestListeners.Callback() {
-
- @Override
- protected void call(PullRequestListener listener, PullRequest request) {
- listener.onIntegrated(request);
- }
-
- });
- }
-
- });
- } else {
- throw new RuntimeException(String.format(
- "Unable to target branch '%s' due to lock failure.", request.getTarget()));
- }
+ @Override
+ public void run() {
+ pullRequestListeners.asyncCall(requestId, new PullRequestListeners.Callback() {
+
+ @Override
+ protected void call(PullRequestListener listener, PullRequest request) {
+ listener.onIntegrated(request);
+ }
+
+ });
+ }
+
+ });
}
@Sessional
@@ -314,7 +305,7 @@ public class DefaultPullRequestManager implements PullRequestManager {
audit.setDate(new Date());
dao.persist(audit);
- request.setIntegrationPreview(null);
+ request.setLastIntegrationPreview(null);
request.setCloseStatus(CloseStatus.INTEGRATED);
request.setUpdateDate(new Date());
@@ -364,7 +355,7 @@ public class DefaultPullRequestManager implements PullRequestManager {
@Override
public IntegrationPreview previewIntegration(PullRequest request) {
- IntegrationPreview preview = request.getIntegrationPreview();
+ IntegrationPreview preview = request.getLastIntegrationPreview();
if (!request.isOpen() || preview != null
&& preview.getRequestHead().equals(request.getLatestUpdate().getHeadCommitHash())
&& preview.getTargetHead().equals(request.getTarget().getHeadCommitHash())
@@ -385,7 +376,7 @@ public class DefaultPullRequestManager implements PullRequestManager {
Git git = request.getTarget().getRepository().git();
IntegrationPreview preview = new IntegrationPreview(request.getTarget().getHeadCommitHash(),
request.getLatestUpdate().getHeadCommitHash(), request.getIntegrationStrategy(), null);
- request.setIntegrationPreview(preview);
+ request.setLastIntegrationPreview(preview);
String integrateRef = request.getIntegrateRef();
if (preview.getIntegrationStrategy() == MERGE_IF_NECESSARY && git.isAncestor(targetHead, requestHead)
|| preview.getIntegrationStrategy() == MERGE_WITH_SQUASH && git.isAncestor(targetHead, requestHead)
@@ -471,7 +462,7 @@ public class DefaultPullRequestManager implements PullRequestManager {
if (request.getStatus() != PullRequest.Status.PENDING_INTEGRATE) {
return false;
} else {
- IntegrationPreview integrationPreview = previewIntegration(request);
+ IntegrationPreview integrationPreview = request.getIntegrationPreview();
return integrationPreview != null && integrationPreview.getIntegrated() != null;
}
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/IntegrationPreview.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/IntegrationPreview.java
index e2fda88e38..e1ecd4295f 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/IntegrationPreview.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/IntegrationPreview.java
@@ -28,12 +28,12 @@ public class IntegrationPreview implements Serializable {
private IntegrationPreview() {
}
- public IntegrationPreview(String targetHead, String requestHead, IntegrationStrategy integrationStrategy,
- @Nullable String previewIntegrated) {
+ public IntegrationPreview(String targetHead, String requestHead,
+ IntegrationStrategy integrationStrategy, @Nullable String integrated) {
this.targetHead = targetHead;
this.requestHead = requestHead;
this.integrationStrategy = integrationStrategy;
- this.integrated = previewIntegrated;
+ this.integrated = integrated;
}
public String getTargetHead() {
@@ -60,6 +60,13 @@ public class IntegrationPreview implements Serializable {
this.integrationStrategy = integrationStrategy;
}
+ /**
+ * Integrated commit hash
+ *
+ * @return
+ * null if there are conflicts
+ */
+ @Nullable
public String getIntegrated() {
return integrated;
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequest.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequest.java
index 186ff104d5..d8a8ae525a 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequest.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/model/PullRequest.java
@@ -43,6 +43,7 @@ import com.pmease.gitplex.core.gatekeeper.checkresult.CheckResult;
import com.pmease.gitplex.core.gatekeeper.checkresult.Disapproved;
import com.pmease.gitplex.core.gatekeeper.checkresult.Pending;
import com.pmease.gitplex.core.gatekeeper.checkresult.PendingAndBlock;
+import com.pmease.gitplex.core.manager.PullRequestManager;
@SuppressWarnings("serial")
@Entity
@@ -126,7 +127,7 @@ public class PullRequest extends AbstractEntity {
private Git sandbox;
@Embedded
- private IntegrationPreview integrationPreview;
+ private IntegrationPreview lastIntegrationPreview;
@Column(nullable=false)
private Date createDate = new Date();
@@ -386,18 +387,36 @@ public class PullRequest extends AbstractEntity {
}
/**
- * Get integration preview of this pull request.
+ * Get last integration preview of this pull request. Note that this method may return an
+ * out dated integration preview. Refer to {@link this#getIntegrationPreview()}
+ * if you'd like to get an update-to-date integration preview
*
* @return
* integration preview of this pull request, or <tt>null</tt> if integration
- * preview has not been calculated yet
+ * preview has not been calculated yet.
*/
- public @Nullable IntegrationPreview getIntegrationPreview() {
- return integrationPreview;
+ @Nullable
+ public IntegrationPreview getLastIntegrationPreview() {
+ return lastIntegrationPreview;
}
- public void setIntegrationPreview(IntegrationPreview integrationPreview) {
- this.integrationPreview = integrationPreview;
+ public void setLastIntegrationPreview(IntegrationPreview lastIntegrationPreview) {
+ this.lastIntegrationPreview = lastIntegrationPreview;
+ }
+
+ /**
+ * Get effective integration preview of this pull request.
+ *
+ * @return
+ * update to date integration preview of this pull request, or <tt>null</tt> if
+ * the integration preview has not been calculated or out dated. In both cases,
+ * it will trigger a re-calculation, and client should call this method later
+ * to get the calculated result
+ */
+ @JsonView(ExternalView.class)
+ @Nullable
+ public IntegrationPreview getIntegrationPreview() {
+ return GitPlex.getInstance(PullRequestManager.class).previewIntegration(this);
}
/**
@@ -448,6 +467,7 @@ public class PullRequest extends AbstractEntity {
this.integrationStrategy = integrationStrategy;
}
+ @JsonView(ExternalView.class)
public PullRequestUpdate getLatestUpdate() {
return getSortedUpdates().get(getSortedUpdates().size()-1);
}
diff --git a/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/BranchResource.java b/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/BranchResource.java
index 356a2669a1..bd81f71380 100644
--- a/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/BranchResource.java
+++ b/gitplex.rest/src/main/java/com/pmease/gitplex/rest/resource/BranchResource.java
@@ -1,5 +1,6 @@
package com.pmease.gitplex.rest.resource;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -21,9 +22,11 @@ import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.hibernate.criterion.Restrictions;
+import com.google.common.collect.Lists;
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.manager.BranchManager;
import com.pmease.gitplex.core.model.Branch;
import com.pmease.gitplex.core.permission.ObjectPermission;
@@ -35,9 +38,12 @@ public class BranchResource {
private final Dao dao;
+ private final BranchManager branchManager;
+
@Inject
- public BranchResource(Dao dao) {
+ public BranchResource(Dao dao, BranchManager branchManager) {
this.dao = dao;
+ this.branchManager = branchManager;
}
@GET
@@ -51,9 +57,16 @@ public class BranchResource {
@ValidQueryParams
@GET
- public Collection<Branch> query(
- @QueryParam("repository") Long repositoryId,
- @QueryParam("name") String name) {
+ public Collection<Branch> query(@QueryParam("repository") Long repositoryId, @QueryParam("name") String name,
+ @QueryParam("path") String path) {
+ if (path != null) {
+ Branch branch = branchManager.findBy(path);
+ if (branch != null)
+ return Lists.newArrayList(branch);
+ else
+ return new ArrayList<>();
+ }
+
EntityCriteria<Branch> criteria = EntityCriteria.of(Branch.class);
if (repositoryId != null)
criteria.add(Restrictions.eq("repository.id", repositoryId));
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
index 813dad24ec..123710b93a 100644
--- 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
@@ -1,5 +1,6 @@
package com.pmease.gitplex.rest.resource;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -7,7 +8,6 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
-import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@@ -22,11 +22,8 @@ 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.manager.BranchManager;
-import com.pmease.gitplex.core.manager.PullRequestManager;
import com.pmease.gitplex.core.model.Branch;
-import com.pmease.gitplex.core.model.IntegrationPreview;
import com.pmease.gitplex.core.model.PullRequest;
-import com.pmease.gitplex.core.model.PullRequestUpdate;
import com.pmease.gitplex.core.permission.ObjectPermission;
@Path("/pull_requests")
@@ -39,13 +36,10 @@ public class PullRequestResource {
private final BranchManager branchManager;
- private final PullRequestManager pullRequestManager;
-
@Inject
- public PullRequestResource(Dao dao, BranchManager branchManager, PullRequestManager pullRequestManager) {
+ public PullRequestResource(Dao dao, BranchManager branchManager) {
this.dao = dao;
this.branchManager = branchManager;
- this.pullRequestManager = pullRequestManager;
}
@GET
@@ -59,32 +53,6 @@ public class PullRequestResource {
return request;
}
- @GET
- @Path("/{id}/latest_update")
- public PullRequestUpdate getLatestUpdate(@PathParam("id") Long id) {
- PullRequest request = dao.load(PullRequest.class, id);
-
- if (!SecurityUtils.getSubject().isPermitted(ObjectPermission.ofRepositoryRead(request.getTarget().getRepository())))
- throw new UnauthorizedException();
-
- return request.getLatestUpdate();
- }
-
- @GET
- @Path("/{id}/integrated_commit")
- public String getIntegratedCommit(@PathParam("id") Long id) {
- PullRequest request = dao.load(PullRequest.class, id);
-
- if (!SecurityUtils.getSubject().isPermitted(ObjectPermission.ofRepositoryRead(request.getTarget().getRepository())))
- throw new UnauthorizedException();
-
- IntegrationPreview preview = pullRequestManager.previewIntegration(request);
- if (preview != null)
- return preview.getIntegrated();
- else
- return null;
- }
-
@ValidQueryParams
@GET
public Collection<PullRequest> query(
@@ -100,7 +68,7 @@ public class PullRequestResource {
if (target != null)
criteria.add(Restrictions.eq("target.id", target.getId()));
else
- throw new NotFoundException("Unable to find target branch: " + targetPath);
+ return new ArrayList<>();
}
if (sourceId != null) {
@@ -110,7 +78,7 @@ public class PullRequestResource {
if (source != null)
criteria.add(Restrictions.eq("source.id", source.getId()));
else
- throw new NotFoundException("Unable to find source branch: " + targetPath);
+ return new ArrayList<>();
}
if (submitterId != null)
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 2134437ebf..53daf446c2 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
@@ -1,5 +1,6 @@
package com.pmease.gitplex.rest.resource;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -22,9 +23,11 @@ import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.hibernate.criterion.Restrictions;
+import com.google.common.collect.Lists;
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.manager.RepositoryManager;
import com.pmease.gitplex.core.model.Repository;
import com.pmease.gitplex.core.permission.ObjectPermission;
@@ -36,9 +39,12 @@ public class RepositoryResource {
private final Dao dao;
+ private final RepositoryManager repositoryManager;
+
@Inject
- public RepositoryResource(Dao dao) {
+ public RepositoryResource(Dao dao, RepositoryManager repositoryManager) {
this.dao = dao;
+ this.repositoryManager = repositoryManager;
}
@Path("/{id}")
@@ -54,10 +60,17 @@ public class RepositoryResource {
@ValidQueryParams
@GET
- public Collection<Repository> query(
- @QueryParam("user") Long userId,
- @QueryParam("name") String name) {
+ public Collection<Repository> query(@QueryParam("user") Long userId, @QueryParam("name") String name,
+ @QueryParam("path") String path) {
EntityCriteria<Repository> criteria = EntityCriteria.of(Repository.class);
+ if (path != null) {
+ Repository repository = repositoryManager.findBy(path);
+ if (repository != null)
+ return Lists.newArrayList(repository);
+ else
+ return new ArrayList<>();
+ }
+
if (userId != null)
criteria.add(Restrictions.eq("owner.id", userId));
if (name != null)
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
index 11e5a41afd..bfd5ef5c05 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
@@ -6,7 +6,7 @@ import org.apache.wicket.request.mapper.parameter.PageParameters;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pmease.commons.hibernate.dao.Dao;
-import com.pmease.commons.jackson.ExternalView;
+import com.pmease.commons.jackson.DefaultView;
import com.pmease.gitplex.core.GitPlex;
import com.pmease.gitplex.core.model.PullRequest;
@@ -26,10 +26,10 @@ public class TestPage extends BasePage {
@Override
public void onClick() {
ObjectMapper mapper = GitPlex.getInstance(ObjectMapper.class);
- mapper.setConfig(mapper.getSerializationConfig().withView(ExternalView.class));
+ mapper.setConfig(mapper.getSerializationConfig().withView(DefaultView.class));
PullRequest request = GitPlex.getInstance(Dao.class).load(PullRequest.class, 11L);
try {
- mapper.writeValueAsString(request);
+ System.out.println(mapper.writeValueAsString(request));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java
index 7a40ccdfd4..9eb066299b 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestComparePage.java
@@ -49,7 +49,6 @@ import com.pmease.gitplex.core.GitPlex;
import com.pmease.gitplex.core.comment.InlineComment;
import com.pmease.gitplex.core.comment.InlineCommentSupport;
import com.pmease.gitplex.core.manager.PullRequestCommentManager;
-import com.pmease.gitplex.core.manager.PullRequestManager;
import com.pmease.gitplex.core.manager.UserManager;
import com.pmease.gitplex.core.model.IntegrationPreview;
import com.pmease.gitplex.core.model.PullRequest;
@@ -115,7 +114,7 @@ public class RequestComparePage extends RequestDetailPage {
}
if (request.isOpen()) {
- IntegrationPreview preview = GitPlex.getInstance(PullRequestManager.class).previewIntegration(request);
+ IntegrationPreview preview = request.getIntegrationPreview();
if (preview != null && preview.getIntegrated() != null && !preview.getIntegrated().equals(preview.getRequestHead())) {
Commit commit = getRepository().getCommit(preview.getIntegrated());
choices.put(commit.getHash(), new CommitDescription(INTEGRATION_PREVIEW, commit.getSubject()));
@@ -288,7 +287,7 @@ public class RequestComparePage extends RequestDetailPage {
PullRequest request = getPullRequest();
if (request.isOpen()) {
- final IntegrationPreview preview = GitPlex.getInstance(PullRequestManager.class).previewIntegration(request);
+ final IntegrationPreview preview = request.getIntegrationPreview();
if (preview != null && preview.getIntegrated() != null
&& !getRepository().getChanges(preview.getRequestHead(), preview.getIntegrated()).isEmpty()) {
items.add(new ComparisonChoiceItem("Target Branch", "Integration Preview") {
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.java
index 3a36f8c87a..5abf5a898b 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/RequestDetailPage.java
@@ -458,8 +458,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
if (operation != INTEGRATE) {
form.add(noteInput = new MarkdownInput("note", Model.of("")));
} else {
- PullRequestManager manager = GitPlex.getInstance(PullRequestManager.class);
- IntegrationPreview preview = manager.previewIntegration(getPullRequest());
+ IntegrationPreview preview = getPullRequest().getIntegrationPreview();
if (preview == null || preview.getIntegrated() == null) {
Session.get().warn("Unable to integrate now as integration preview has to be recalculated");
return new WebMarkupContainer(id).setVisible(false);
@@ -643,8 +642,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
@Override
protected void onConfigure() {
super.onConfigure();
- PullRequestManager manager = GitPlex.getInstance(PullRequestManager.class);
- setVisible(manager.previewIntegration(getPullRequest()) == null);
+ setVisible(getPullRequest().getIntegrationPreview() == null);
}
});
@@ -679,8 +677,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
@Override
protected void onConfigure() {
super.onConfigure();
- PullRequestManager manager = GitPlex.getInstance(PullRequestManager.class);
- IntegrationPreview preview = manager.previewIntegration(getPullRequest());
+ IntegrationPreview preview = getPullRequest().getIntegrationPreview();
setVisible(preview != null && preview.getIntegrated() == null);
}
@@ -692,8 +689,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
super.onInitialize();
PullRequest request = getPullRequest();
- PullRequestManager manager = GitPlex.getInstance(PullRequestManager.class);
- IntegrationPreview preview = manager.previewIntegration(request);
+ IntegrationPreview preview = request.getIntegrationPreview();
PageParameters params = RequestComparePage.paramsOf(
request, request.getTarget().getHeadCommitHash(),
@@ -706,8 +702,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
super.onConfigure();
PullRequest request = getPullRequest();
- PullRequestManager manager = GitPlex.getInstance(PullRequestManager.class);
- IntegrationPreview preview = manager.previewIntegration(request);
+ IntegrationPreview preview = request.getIntegrationPreview();
setVisible(!preview.getIntegrated().equals(preview.getRequestHead()));
}
@@ -719,8 +714,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
@Override
public String getObject() {
PullRequest request = getPullRequest();
- PullRequestManager manager = GitPlex.getInstance(PullRequestManager.class);
- IntegrationPreview preview = manager.previewIntegration(request);
+ IntegrationPreview preview = request.getIntegrationPreview();
if (preview != null)
return preview.getIntegrated();
else
@@ -775,8 +769,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
@Override
protected void onConfigure() {
super.onConfigure();
- PullRequestManager manager = GitPlex.getInstance(PullRequestManager.class);
- IntegrationPreview preview = manager.previewIntegration(getPullRequest());
+ IntegrationPreview preview = getPullRequest().getIntegrationPreview();
setVisible(preview != null && preview.getIntegrated() != null);
}
@@ -813,7 +806,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
super.onConfigure();
PullRequest request = getPullRequest();
- IntegrationPreview preview = request.getIntegrationPreview();
+ IntegrationPreview preview = request.getLastIntegrationPreview();
setVisible(request.getStatus() == INTEGRATED && preview != null
&& preview.getRequestHead().equals(preview.getIntegrated()));
}
@@ -826,7 +819,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
super.onConfigure();
PullRequest request = getPullRequest();
- IntegrationPreview preview = request.getIntegrationPreview();
+ IntegrationPreview preview = request.getLastIntegrationPreview();
setVisible(request.getStatus() == INTEGRATED && preview != null
&& !preview.getRequestHead().equals(preview.getIntegrated())
&& (preview.getIntegrationStrategy() == MERGE_ALWAYS || preview.getIntegrationStrategy() == MERGE_IF_NECESSARY));
@@ -840,7 +833,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
super.onConfigure();
PullRequest request = getPullRequest();
- IntegrationPreview preview = request.getIntegrationPreview();
+ IntegrationPreview preview = request.getLastIntegrationPreview();
setVisible(request.getStatus() == INTEGRATED && preview == null);
}
@@ -852,7 +845,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
super.onConfigure();
PullRequest request = getPullRequest();
- IntegrationPreview preview = request.getIntegrationPreview();
+ IntegrationPreview preview = request.getLastIntegrationPreview();
setVisible(request.getStatus() == INTEGRATED && preview != null
&& !preview.getRequestHead().equals(preview.getIntegrated())
&& preview.getIntegrationStrategy() == MERGE_WITH_SQUASH);
@@ -885,7 +878,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
super.onConfigure();
PullRequest request = getPullRequest();
- IntegrationPreview preview = request.getIntegrationPreview();
+ IntegrationPreview preview = request.getLastIntegrationPreview();
setVisible(request.getStatus() == INTEGRATED && preview != null
&& !preview.getRequestHead().equals(preview.getIntegrated())
&& preview.getIntegrationStrategy() == REBASE_SOURCE_ONTO_TARGET);
@@ -918,7 +911,7 @@ public abstract class RequestDetailPage extends RepositoryPage {
super.onConfigure();
PullRequest request = getPullRequest();
- IntegrationPreview preview = request.getIntegrationPreview();
+ IntegrationPreview preview = request.getLastIntegrationPreview();
setVisible(request.getStatus() == INTEGRATED && preview != null
&& !preview.getRequestHead().equals(preview.getIntegrated())
&& preview.getIntegrationStrategy() == REBASE_TARGET_ONTO_SOURCE);
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/ResolveConflictInstructionPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/ResolveConflictInstructionPanel.java
index cea4729826..b4ae62de42 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/ResolveConflictInstructionPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/pullrequest/ResolveConflictInstructionPanel.java
@@ -1,6 +1,9 @@
package com.pmease.gitplex.web.page.repository.pullrequest;
-import static com.pmease.gitplex.core.model.PullRequest.IntegrationStrategy.*;
+import static com.pmease.gitplex.core.model.PullRequest.IntegrationStrategy.MERGE_ALWAYS;
+import static com.pmease.gitplex.core.model.PullRequest.IntegrationStrategy.MERGE_WITH_SQUASH;
+import static com.pmease.gitplex.core.model.PullRequest.IntegrationStrategy.REBASE_SOURCE_ONTO_TARGET;
+import static com.pmease.gitplex.core.model.PullRequest.IntegrationStrategy.REBASE_TARGET_ONTO_SOURCE;
import org.apache.shiro.SecurityUtils;
import org.apache.wicket.markup.html.WebMarkupContainer;
@@ -10,7 +13,6 @@ import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import com.pmease.gitplex.core.GitPlex;
-import com.pmease.gitplex.core.manager.PullRequestManager;
import com.pmease.gitplex.core.manager.UserManager;
import com.pmease.gitplex.core.model.IntegrationPreview;
import com.pmease.gitplex.core.model.PullRequest;
@@ -34,7 +36,7 @@ class ResolveConflictInstructionPanel extends Panel {
Fragment fragment;
Repository targetRepo = request.getTarget().getRepository();
User user = GitPlex.getInstance(UserManager.class).getCurrent();
- IntegrationPreview preview = GitPlex.getInstance(PullRequestManager.class).previewIntegration(request);
+ IntegrationPreview preview = request.getIntegrationPreview();
String sourceHead = preview.getRequestHead();
IntegrationStrategy strategy = request.getIntegrationStrategy();
boolean sameRepo = request.getTarget().getRepository().equals(request.getSource().getRepository());