Skip to content

The Unified and Autonomous Development Platform

Fix hibernate LazyLoadException issue when push to a branch ref.

robin shine7abf96c01cae
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/event/BranchRefUpdateEvent.java b/gitop.core/src/main/java/com/pmease/gitop/core/event/BranchRefUpdateEvent.java
deleted file mode 100644
index c56348a8d2..0000000000
--- a/gitop.core/src/main/java/com/pmease/gitop/core/event/BranchRefUpdateEvent.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.pmease.gitop.core.event;
-
-import com.pmease.gitop.model.Branch;
-
-public class BranchRefUpdateEvent {
-
- private final Branch branch;
-
- public BranchRefUpdateEvent(Branch branch) {
- this.branch = branch;
- }
-
- public Branch getBranch() {
- return branch;
- }
-
-}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/GitPostReceiveCallback.java b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/GitPostReceiveCallback.java
index 5007a81eb5..8caab4f6c3 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/GitPostReceiveCallback.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/hookcallback/GitPostReceiveCallback.java
@@ -4,6 +4,8 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -18,10 +20,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
-import com.google.common.eventbus.EventBus;
import com.pmease.commons.git.Commit;
+import com.pmease.commons.hibernate.UnitOfWork;
import com.pmease.commons.util.StringUtils;
-import com.pmease.gitop.core.event.BranchRefUpdateEvent;
import com.pmease.gitop.core.manager.BranchManager;
import com.pmease.gitop.core.manager.ProjectManager;
import com.pmease.gitop.core.manager.UserManager;
@@ -43,15 +44,19 @@ public class GitPostReceiveCallback extends HttpServlet {
private final UserManager userManager;
- private final EventBus eventBus;
+ private final UnitOfWork unitOfWork;
+
+ private final Executor executor;
@Inject
- public GitPostReceiveCallback(ProjectManager projectManager, BranchManager branchManager,
- UserManager userManager, EventBus eventBus) {
+ public GitPostReceiveCallback(ProjectManager projectManager,
+ BranchManager branchManager, UserManager userManager,
+ UnitOfWork unitOfWork, Executor executor) {
this.projectManager = projectManager;
this.branchManager = branchManager;
this.userManager = userManager;
- this.eventBus = eventBus;
+ this.unitOfWork = unitOfWork;
+ this.executor = executor;
}
@Override
@@ -126,9 +131,27 @@ public class GitPostReceiveCallback extends HttpServlet {
} else {
logger.debug("Executing post-receive hook against branch {}...", branchName);
- Branch branch = branchManager.findBy(project, branchName);
+ final Branch branch = branchManager.findBy(project, branchName);
Preconditions.checkNotNull(branch);
- eventBus.post(new BranchRefUpdateEvent(branch));
+
+ final Long branchId = branch.getId();
+ executor.execute(new Runnable() {
+
+ @Override
+ public void run() {
+ unitOfWork.call(new Callable<Void>() {
+
+ @Override
+ public Void call() throws Exception {
+ Branch branch = branchManager.load(branchId);
+ branchManager.onBranchRefUpdate(branch);
+ return null;
+ }
+
+ });
+ }
+
+ });
}
}
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
index 442049ba62..ee6adcf724 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/BranchManager.java
@@ -86,6 +86,8 @@ public interface BranchManager extends GenericDao<Branch> {
*/
public void rename(Branch branch, String newName);
+ public void onBranchRefUpdate(Branch branch);
+
public void trim(Collection<Long> branchIds);
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/PullRequestManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/PullRequestManager.java
index da945a885f..884e51b25d 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/PullRequestManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/PullRequestManager.java
@@ -18,9 +18,9 @@ public interface PullRequestManager extends GenericDao<PullRequest> {
void refresh(PullRequest request);
- boolean merge(PullRequest request);
+ boolean merge(PullRequest request, @Nullable User user, @Nullable String comment);
- void discard(PullRequest request);
+ void discard(PullRequest request, User user, @Nullable String comment);
/**
* Find pull requests whose head commit or merge commit equals to specified commit.
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/PullRequestUpdateManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/PullRequestUpdateManager.java
index cf9980187d..829c60a96a 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/PullRequestUpdateManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/PullRequestUpdateManager.java
@@ -10,4 +10,5 @@ import com.pmease.gitop.model.PullRequestUpdate;
public interface PullRequestUpdateManager extends GenericDao<PullRequestUpdate> {
void update(PullRequest request);
+
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/VoteManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/VoteManager.java
index 565531f7dc..123bb259c9 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/VoteManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/VoteManager.java
@@ -1,8 +1,11 @@
package com.pmease.gitop.core.manager;
+import javax.annotation.Nullable;
+
import com.google.inject.ImplementedBy;
import com.pmease.commons.hibernate.dao.GenericDao;
import com.pmease.gitop.core.manager.impl.DefaultVoteManager;
+import com.pmease.gitop.model.PullRequest;
import com.pmease.gitop.model.PullRequestUpdate;
import com.pmease.gitop.model.User;
import com.pmease.gitop.model.Vote;
@@ -12,4 +15,5 @@ public interface VoteManager extends GenericDao<Vote> {
Vote find(User reviewer, PullRequestUpdate update);
+ void vote(PullRequest request, User user, Vote.Result result, @Nullable String comment);
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
index b9ffd5f67b..ca874c90b5 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultBranchManager.java
@@ -1,7 +1,9 @@
package com.pmease.gitop.core.manager.impl;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -17,6 +19,7 @@ import com.pmease.commons.hibernate.dao.AbstractGenericDao;
import com.pmease.commons.hibernate.dao.GeneralDao;
import com.pmease.gitop.core.manager.BranchManager;
import com.pmease.gitop.core.manager.PullRequestManager;
+import com.pmease.gitop.core.manager.PullRequestUpdateManager;
import com.pmease.gitop.model.Branch;
import com.pmease.gitop.model.Project;
import com.pmease.gitop.model.PullRequest;
@@ -26,10 +29,15 @@ public class DefaultBranchManager extends AbstractGenericDao<Branch> implements
private final PullRequestManager pullRequestManager;
+ private final PullRequestUpdateManager pullRequestUpdateManager;
+
@Inject
- public DefaultBranchManager(GeneralDao generalDao, PullRequestManager pullRequestManager) {
+ public DefaultBranchManager(GeneralDao generalDao,
+ PullRequestManager pullRequestManager,
+ PullRequestUpdateManager pullRequestUpdateManager) {
super(generalDao);
this.pullRequestManager = pullRequestManager;
+ this.pullRequestUpdateManager = pullRequestUpdateManager;
}
@Sessional
@@ -117,4 +125,33 @@ public class DefaultBranchManager extends AbstractGenericDao<Branch> implements
}
}
+ @Transactional
+ @Override
+ public void onBranchRefUpdate(Branch branch) {
+ for (PullRequest request: branch.getIncomingRequests()) {
+ if (request.isOpen())
+ pullRequestManager.refresh(request);
+ }
+
+ // Calculates most recent open pull request for each branch. Note that although
+ // we do not allow multiple opened requests for a single branch, there still
+ // exist some chances multiple requests are opening for same branch, so we need
+ // to handle this case here.
+ Map<Branch, PullRequest> branchRequests = new HashMap<>();
+ for (PullRequest request: branch.getOutgoingRequests()) {
+ if (request.isOpen()) {
+ PullRequest branchRequest = branchRequests.get(request.getTarget());
+ if (branchRequest == null)
+ branchRequests.put(request.getTarget(), request);
+ else if (request.getId() > branchRequest.getId())
+ branchRequests.put(request.getTarget(), request);
+ }
+ }
+
+ for (PullRequest request: branchRequests.values()) {
+ pullRequestUpdateManager.update(request);
+ pullRequestManager.refresh(request);
+ }
+ }
+
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestManager.java
index 4beec36ea0..8d15305b04 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestManager.java
@@ -5,8 +5,10 @@ import static com.pmease.gitop.model.PullRequest.CriterionHelper.ofSource;
import static com.pmease.gitop.model.PullRequest.CriterionHelper.ofTarget;
import java.io.File;
+import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
+import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import javax.inject.Inject;
@@ -17,16 +19,16 @@ import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.google.common.base.Preconditions;
-import com.google.common.eventbus.EventBus;
-import com.google.common.eventbus.Subscribe;
import com.pmease.commons.git.Git;
import com.pmease.commons.hibernate.Sessional;
import com.pmease.commons.hibernate.Transactional;
+import com.pmease.commons.hibernate.UnitOfWork;
import com.pmease.commons.hibernate.dao.AbstractGenericDao;
import com.pmease.commons.hibernate.dao.GeneralDao;
import com.pmease.commons.util.FileUtils;
import com.pmease.commons.util.LockUtils;
-import com.pmease.gitop.core.event.BranchRefUpdateEvent;
+import com.pmease.gitop.core.manager.BranchManager;
+import com.pmease.gitop.core.manager.PullRequestCommentManager;
import com.pmease.gitop.core.manager.PullRequestManager;
import com.pmease.gitop.core.manager.PullRequestUpdateManager;
import com.pmease.gitop.core.manager.VoteInvitationManager;
@@ -34,6 +36,7 @@ import com.pmease.gitop.model.Branch;
import com.pmease.gitop.model.MergeResult;
import com.pmease.gitop.model.PullRequest;
import com.pmease.gitop.model.PullRequest.Status;
+import com.pmease.gitop.model.PullRequestComment;
import com.pmease.gitop.model.PullRequestUpdate;
import com.pmease.gitop.model.User;
import com.pmease.gitop.model.VoteInvitation;
@@ -43,23 +46,35 @@ import com.pmease.gitop.model.gatekeeper.checkresult.Pending;
import com.pmease.gitop.model.gatekeeper.checkresult.PendingAndBlock;
@Singleton
-public class DefaultPullRequestManager extends AbstractGenericDao<PullRequest> implements
- PullRequestManager {
+public class DefaultPullRequestManager extends AbstractGenericDao<PullRequest>
+ implements PullRequestManager {
private final VoteInvitationManager voteInvitationManager;
private final PullRequestUpdateManager pullRequestUpdateManager;
- private final EventBus eventBus;
+ private final PullRequestCommentManager pullRequestCommentManager;
+
+ private final BranchManager branchManager;
+
+ private final UnitOfWork unitOfWork;
+
+ private final Executor executor;
@Inject
- public DefaultPullRequestManager(GeneralDao generalDao, VoteInvitationManager voteInvitationManager,
- PullRequestUpdateManager pullRequestUpdateManager, EventBus eventBus) {
+ public DefaultPullRequestManager(GeneralDao generalDao,
+ VoteInvitationManager voteInvitationManager,
+ PullRequestUpdateManager pullRequestUpdateManager,
+ PullRequestCommentManager pullRequestCommentManager,
+ BranchManager branchManager,
+ UnitOfWork unitOfWork, Executor executor) {
super(generalDao);
this.voteInvitationManager = voteInvitationManager;
this.pullRequestUpdateManager = pullRequestUpdateManager;
- this.eventBus = eventBus;
- eventBus.register(this);
+ this.pullRequestCommentManager = pullRequestCommentManager;
+ this.branchManager = branchManager;
+ this.unitOfWork = unitOfWork;
+ this.executor = executor;
}
@Sessional
@@ -226,7 +241,7 @@ public class DefaultPullRequestManager extends AbstractGenericDao<PullRequest> i
save(request);
if (request.isAutoMerge())
- merge(request);
+ merge(request, null, null);
return null;
}
@@ -235,14 +250,25 @@ public class DefaultPullRequestManager extends AbstractGenericDao<PullRequest> i
}
}
- @Sessional
- public void discard(final PullRequest request) {
+ @Transactional
+ public void discard(final PullRequest request, final User user, final String comment) {
LockUtils.call(request.getLockName(), new Callable<Void>() {
@Override
public Void call() throws Exception {
request.setStatus(Status.DISCARDED);
save(request);
+
+ if (comment != null) {
+ PullRequestComment requestComment = new PullRequestComment();
+ requestComment.setRequest(request);
+ requestComment.setUser(user);
+ requestComment.setDate(new Date());
+ requestComment.setContent(comment);
+
+ pullRequestCommentManager.save(requestComment);
+ }
+
return null;
}
@@ -261,21 +287,53 @@ public class DefaultPullRequestManager extends AbstractGenericDao<PullRequest> i
* <li> branch ref has just been updated in some other threads and this thread
* is unable to lock the reference.
*/
- @Sessional
- public boolean merge(final PullRequest request) {
+ @Transactional
+ public boolean merge(final PullRequest request, final User user,
+ final String comment) {
return LockUtils.call(request.getLockName(), new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
if (request.getStatus() == Status.PENDING_INTEGRATE) {
Git git = request.getTarget().getProject().code();
- if (git.updateRef(request.getTarget().getHeadRef(), request.getMergeResult().getMergeHead(),
- request.getMergeResult().getBranchHead(), "merge pull request")) {
+ if (git.updateRef(request.getTarget().getHeadRef(),
+ request.getMergeResult().getMergeHead(),
+ request.getMergeResult().getBranchHead(),
+ comment!=null?comment:"merge pull request")) {
request.setStatus(Status.INTEGRATED);
save(request);
- eventBus.post(new BranchRefUpdateEvent(request.getTarget()));
+
+ final Long branchId = request.getTarget().getId();
+ executor.execute(new Runnable() {
+
+ @Override
+ public void run() {
+ unitOfWork.call(new Callable<Void>() {
+
+ @Override
+ public Void call() throws Exception {
+ Branch branch = branchManager.load(branchId);
+ branchManager.onBranchRefUpdate(branch);
+ return null;
+ }
+
+ });
+ }
+
+ });
return true;
}
+
+ if (user != null && comment != null) {
+ PullRequestComment requestComment = new PullRequestComment();
+ requestComment.setRequest(request);
+ requestComment.setUser(user);
+ requestComment.setDate(new Date());
+ requestComment.setContent(comment);
+
+ pullRequestCommentManager.save(requestComment);
+ }
+
}
return false;
}
@@ -283,15 +341,6 @@ public class DefaultPullRequestManager extends AbstractGenericDao<PullRequest> i
});
}
- @Sessional
- @Subscribe
- public void refreshUpon(BranchRefUpdateEvent event) {
- for (PullRequest request: event.getBranch().getIncomingRequests()) {
- if (request.isOpen())
- refresh(load(request.getId()));
- }
- }
-
@Sessional
@Override
public List<PullRequest> findByCommit(String commit) {
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestUpdateManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestUpdateManager.java
index 4b571de982..e8e0375149 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestUpdateManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultPullRequestUpdateManager.java
@@ -1,29 +1,16 @@
package com.pmease.gitop.core.manager.impl;
import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.Callable;
-import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Singleton;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.eventbus.Subscribe;
import com.pmease.commons.git.Git;
-import com.pmease.commons.hibernate.Sessional;
import com.pmease.commons.hibernate.Transactional;
-import com.pmease.commons.hibernate.UnitOfWork;
import com.pmease.commons.hibernate.dao.AbstractGenericDao;
import com.pmease.commons.hibernate.dao.GeneralDao;
-import com.pmease.commons.util.ExceptionUtils;
-import com.pmease.gitop.core.event.BranchRefUpdateEvent;
import com.pmease.gitop.core.manager.PullRequestManager;
import com.pmease.gitop.core.manager.PullRequestUpdateManager;
-import com.pmease.gitop.model.Branch;
import com.pmease.gitop.model.PullRequest;
import com.pmease.gitop.model.PullRequestUpdate;
@@ -31,21 +18,13 @@ import com.pmease.gitop.model.PullRequestUpdate;
public class DefaultPullRequestUpdateManager extends AbstractGenericDao<PullRequestUpdate>
implements PullRequestUpdateManager {
- private final Logger logger = LoggerFactory.getLogger(DefaultPullRequestUpdateManager.class);
-
private final PullRequestManager pullRequestManager;
- private final Executor executor;
-
- private final UnitOfWork unitOfWork;
-
@Inject
- public DefaultPullRequestUpdateManager(GeneralDao generalDao, PullRequestManager pullRequestManager,
- Executor executor, UnitOfWork unitOfWork) {
+ public DefaultPullRequestUpdateManager(GeneralDao generalDao,
+ PullRequestManager pullRequestManager) {
super(generalDao);
this.pullRequestManager = pullRequestManager;
- this.executor = executor;
- this.unitOfWork = unitOfWork;
}
@Transactional
@@ -87,50 +66,4 @@ public class DefaultPullRequestUpdateManager extends AbstractGenericDao<PullRequ
}
}
- @Sessional
- @Subscribe
- public void updateUpon(BranchRefUpdateEvent event) {
- // Calculates most recent open pull request for each branch. Note that although
- // we do not allow multiple opened requests for a single branch, there still
- // exist some chances multiple requests are opening for same branch, so we need
- // to handle this case here.
- Map<Branch, PullRequest> branchRequests = new HashMap<>();
- for (PullRequest request: event.getBranch().getOutgoingRequests()) {
- if (request.isOpen()) {
- PullRequest branchRequest = branchRequests.get(request.getTarget());
- if (branchRequest == null)
- branchRequests.put(request.getTarget(), request);
- else if (request.getId() > branchRequest.getId())
- branchRequests.put(request.getTarget(), request);
- }
- }
-
- for (final PullRequest request: branchRequests.values()) {
- executor.execute(new Runnable() {
-
- @Override
- public void run() {
- try {
- unitOfWork.call(new Callable<Void>() {
-
- @Override
- public Void call() throws Exception {
- // Reload request to avoid Hibernate LazyInitializationException
- update(pullRequestManager.load(request.getId()));
- pullRequestManager.refresh(request);
- return null;
- }
-
- });
- } catch (Exception e) {
- logger.error("Error updating pull request.", e);
- throw ExceptionUtils.unchecked(e);
- }
- }
-
- });
- }
-
- }
-
}
diff --git a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultVoteManager.java b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultVoteManager.java
index 33ec6a968d..5377060ae1 100644
--- a/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultVoteManager.java
+++ b/gitop.core/src/main/java/com/pmease/gitop/core/manager/impl/DefaultVoteManager.java
@@ -1,8 +1,11 @@
package com.pmease.gitop.core.manager.impl;
+import java.util.Date;
+
import javax.inject.Inject;
import javax.inject.Singleton;
+import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
@@ -10,48 +13,60 @@ import com.pmease.commons.hibernate.Sessional;
import com.pmease.commons.hibernate.Transactional;
import com.pmease.commons.hibernate.dao.AbstractGenericDao;
import com.pmease.commons.hibernate.dao.GeneralDao;
+import com.pmease.gitop.core.manager.PullRequestCommentManager;
import com.pmease.gitop.core.manager.PullRequestManager;
import com.pmease.gitop.core.manager.VoteManager;
+import com.pmease.gitop.model.PullRequest;
+import com.pmease.gitop.model.PullRequestComment;
import com.pmease.gitop.model.PullRequestUpdate;
import com.pmease.gitop.model.User;
import com.pmease.gitop.model.Vote;
+import com.pmease.gitop.model.Vote.Result;
@Singleton
public class DefaultVoteManager extends AbstractGenericDao<Vote> implements VoteManager {
private final PullRequestManager pullRequestManager;
+ private final PullRequestCommentManager pullRequestCommentManager;
+
@Inject
- public DefaultVoteManager(GeneralDao generalDao, PullRequestManager pullRequestManager) {
+ public DefaultVoteManager(GeneralDao generalDao, PullRequestManager pullRequestManager,
+ PullRequestCommentManager pullRequestCommentManager) {
super(generalDao);
this.pullRequestManager = pullRequestManager;
+ this.pullRequestCommentManager = pullRequestCommentManager;
}
- @Transactional
+ @Sessional
@Override
- public void save(Vote vote) {
- super.save(vote);
-
- if (vote.getUpdate().getRequest().isOpen())
- pullRequestManager.refresh(vote.getUpdate().getRequest());
+ public Vote find(User reviewer, PullRequestUpdate update) {
+ return find(new Criterion[]{
+ Restrictions.eq("voter", reviewer),
+ Restrictions.eq("update", update)});
}
@Transactional
@Override
- public void delete(Vote vote) {
- super.delete(vote);
-
- vote.getVoter().getVotes().remove(vote);
- vote.getUpdate().getVotes().remove(vote);
-
- if (vote.getUpdate().getRequest().isOpen())
- pullRequestManager.refresh(vote.getUpdate().getRequest());
- }
-
- @Sessional
- @Override
- public Vote find(User reviewer, PullRequestUpdate update) {
- return find(new Criterion[]{Restrictions.eq("reviewer", reviewer), Restrictions.eq("update", update)});
+ public void vote(PullRequest request, User user, Result result, String comment) {
+ Vote vote = new Vote();
+ vote.setResult(result);
+ vote.setUpdate(request.getLatestUpdate());
+ vote.setVoter(user);
+ vote.getVoter().getVotes().add(vote);
+ vote.getUpdate().getVotes().add(vote);
+ save(vote);
+
+ if (StringUtils.isNotBlank(comment)) {
+ PullRequestComment requestComment = new PullRequestComment();
+ requestComment.setRequest(request);
+ requestComment.setUser(user);
+ requestComment.setDate(new Date());
+ requestComment.setContent(comment);
+
+ pullRequestCommentManager.save(requestComment);
+ }
+ pullRequestManager.refresh(request);
}
}
diff --git a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestComment.java b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestComment.java
index 101ea77a35..883825699a 100644
--- a/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestComment.java
+++ b/gitop.model/src/main/java/com/pmease/gitop/model/PullRequestComment.java
@@ -22,7 +22,7 @@ public class PullRequestComment extends AbstractEntity {
private User user;
@Column(nullable=false)
- private String comment;
+ private String content;
@Column(nullable=false)
private Date date = new Date();
@@ -43,12 +43,12 @@ public class PullRequestComment extends AbstractEntity {
this.user = user;
}
- public String getComment() {
- return comment;
+ public String getContent() {
+ return content;
}
- public void setComment(String comment) {
- this.comment = comment;
+ public void setContent(String content) {
+ this.content = content;
}
public Date getDate() {
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/assets/css/page.css b/gitop.web/src/main/java/com/pmease/gitop/web/assets/css/page.css
index 79c60055e1..6d4b249434 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/assets/css/page.css
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/assets/css/page.css
@@ -801,5 +801,6 @@ ul.status-help li:last-child .description {border-bottom: none;}
.request-detail>.status td.icons {width: 1%;}
.request-detail>.status td.merge.warning .icon-warning-sign {display: block; color: #8A6D3B;}
.request-detail>.status td.merge.success .icon-ok-circle {display: block; color: #3C763D;}
-.request-detail>.status td.actions {width: 100px;}
-
+.request-detail>.status td.actions {width: 120px;}
+.request-detail>.status textarea {resize: vertical; min-height: 64px;}
+.request-detail>.status>form>table {border-spacing: 3px;}
\ No newline at end of file
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/pullrequest/RequestDetailPanel.html b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/pullrequest/RequestDetailPanel.html
index 768bc6811d..ce8ad12b3f 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/pullrequest/RequestDetailPanel.html
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/pullrequest/RequestDetailPanel.html
@@ -66,6 +66,19 @@
</td>
</tr>
</table>
+ <form wicket:id="commentEditor">
+ <table>
+ <tr>
+ <td class="comment">
+ <textarea wicket:id="comment" rows="8" class="form-control" placeholder="Leave a comment"></textarea>
+ </td>
+ <td class="actions">
+ <input wicket:id="confirm" type="submit" value="Confirm" class="btn btn-primary btn-sm btn-block"></input>
+ <input wicket:id="cancel" type="button" value="Cancel" class="btn btn-default btn-sm btn-block"></input>
+ </td>
+ </tr>
+ </table>
+ </form>
</div>
</div>
<wicket:fragment wicket:id="conflictHelpFrag">
diff --git a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/pullrequest/RequestDetailPanel.java b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/pullrequest/RequestDetailPanel.java
index 0c72610056..09ffaba1cc 100644
--- a/gitop.web/src/main/java/com/pmease/gitop/web/page/project/pullrequest/RequestDetailPanel.java
+++ b/gitop.web/src/main/java/com/pmease/gitop/web/page/project/pullrequest/RequestDetailPanel.java
@@ -5,6 +5,7 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
+import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
@@ -12,7 +13,9 @@ import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
@@ -22,6 +25,7 @@ import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
+import org.apache.wicket.model.PropertyModel;
import com.pmease.commons.wicket.behavior.dropdown.DropdownBehavior;
import com.pmease.commons.wicket.behavior.dropdown.DropdownPanel;
@@ -47,6 +51,10 @@ public class RequestDetailPanel extends Panel {
private boolean editingTitle;
+ private Action action;
+
+ private String comment;
+
public RequestDetailPanel(String id, IModel<PullRequest> model) {
super(id, model);
}
@@ -395,7 +403,7 @@ public class RequestDetailPanel extends Panel {
@Override
public void onClick() {
- vote(Vote.Result.APPROVE);
+ action = Action.Approve;
}
@Override
@@ -406,14 +414,19 @@ public class RequestDetailPanel extends Panel {
if (request.getStatus() == Status.PENDING_APPROVAL) {
User currentUser = Gitop.getInstance(UserManager.class).getCurrent();
if (currentUser != null) {
- boolean canVote = false;
- for (VoteEligibility each: request.getCheckResult().getVoteEligibilities()) {
- if (each.canVote(currentUser, request)) {
- canVote = true;
- break;
+ if (Gitop.getInstance(VoteManager.class).find(
+ currentUser, request.getLatestUpdate()) != null) {
+ setVisible(false);
+ } else {
+ boolean canVote = false;
+ for (VoteEligibility each: request.getCheckResult().getVoteEligibilities()) {
+ if (each.canVote(currentUser, request)) {
+ canVote = true;
+ break;
+ }
}
+ setVisible(canVote);
}
- setVisible(canVote);
} else {
setVisible(false);
}
@@ -428,7 +441,7 @@ public class RequestDetailPanel extends Panel {
@Override
public void onClick() {
- vote(Vote.Result.DISAPPROVE);
+ action = Action.Disapprove;
}
@Override
@@ -457,7 +470,7 @@ public class RequestDetailPanel extends Panel {
@Override
public void onClick() {
- Gitop.getInstance(PullRequestManager.class).merge(getPullRequest());
+ action = Action.Integrate;
}
});
@@ -478,23 +491,87 @@ public class RequestDetailPanel extends Panel {
@Override
public void onClick() {
- Gitop.getInstance(PullRequestManager.class).discard(getPullRequest());
+ action = Action.Discard;
+ }
+
+ });
+
+ Form<?> commentEditor = new Form<Void>("commentEditor") {
+
+ @Override
+ protected void onConfigure() {
+ super.onConfigure();
+ setVisible(action != null);
+ }
+
+ };
+ statusContainer.add(commentEditor);
+
+ commentEditor.add(new TextArea<String>("comment",
+ new PropertyModel<String>(this, "comment")));
+
+ commentEditor.add(new Button("confirm") {
+
+ @Override
+ public void onSubmit() {
+ super.onSubmit();
+
+ if (StringUtils.isBlank(comment))
+ comment = null;
+ User currentUser = Gitop.getInstance(UserManager.class).getCurrent();
+ if (action == Action.Approve) {
+ Gitop.getInstance(VoteManager.class).vote(getPullRequest(),
+ currentUser, Vote.Result.APPROVE, comment);
+ } else if (action == Action.Disapprove) {
+ Gitop.getInstance(VoteManager.class).vote(getPullRequest(),
+ currentUser, Vote.Result.DISAPPROVE, comment);
+ } else if (action == Action.Integrate) {
+ Gitop.getInstance(PullRequestManager.class).merge(
+ getPullRequest(), currentUser, comment);
+ } else {
+ Gitop.getInstance(PullRequestManager.class).discard(
+ getPullRequest(), currentUser, comment);
+ }
+ action = null;
+ comment = null;
+ }
+
+ }.add(AttributeModifier.replace("value", new AbstractReadOnlyModel<String>() {
+
+ @Override
+ public String getObject() {
+ return "Confirm " + action.name();
+ }
+
+ })).add(AttributeAppender.append("class", new AbstractReadOnlyModel<String>() {
+
+ @Override
+ public String getObject() {
+ if (action == Action.Approve)
+ return "btn-primary";
+ else if (action == Action.Disapprove)
+ return "btn-warning";
+ else if (action == Action.Integrate)
+ return "btn-success";
+ else
+ return "btn-danger";
+ }
+
+ })));
+ commentEditor.add(new Link<Void>("cancel") {
+
+ @Override
+ public void onClick() {
+ action = null;
+ comment = null;
}
});
}
- private void vote(Vote.Result result) {
- Vote vote = new Vote();
- vote.setResult(Vote.Result.APPROVE);
- vote.setUpdate(getPullRequest().getLatestUpdate());
- vote.setVoter(Gitop.getInstance(UserManager.class).getCurrent());
- Gitop.getInstance(VoteManager.class).save(vote);
- Gitop.getInstance(PullRequestManager.class).refresh(getPullRequest());
- }
-
public PullRequest getPullRequest() {
return (PullRequest) getDefaultModelObject();
}
+ private static enum Action {Approve, Disapprove, Integrate, Discard}
}
\ No newline at end of file