Skip to content

The Unified and Autonomous Development Platform

A performant approach (hopefully) to calculate related code comments of pull requests.

robin shine55052aae5d87
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/CoreModule.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/CoreModule.java
index b804c7672b..0d2f9f2641 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/CoreModule.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/CoreModule.java
@@ -30,11 +30,12 @@ import com.pmease.gitplex.core.listener.PullRequestUpdateListener;
import com.pmease.gitplex.core.listener.RefListener;
import com.pmease.gitplex.core.manager.AccountManager;
import com.pmease.gitplex.core.manager.AttachmentManager;
-import com.pmease.gitplex.core.manager.CommitInfoManager;
import com.pmease.gitplex.core.manager.BranchWatchManager;
+import com.pmease.gitplex.core.manager.CodeCommentInfoManager;
import com.pmease.gitplex.core.manager.CodeCommentManager;
import com.pmease.gitplex.core.manager.CodeCommentRelationManager;
import com.pmease.gitplex.core.manager.CodeCommentReplyManager;
+import com.pmease.gitplex.core.manager.CommitInfoManager;
import com.pmease.gitplex.core.manager.ConfigManager;
import com.pmease.gitplex.core.manager.DataManager;
import com.pmease.gitplex.core.manager.DepotManager;
@@ -60,11 +61,12 @@ import com.pmease.gitplex.core.manager.VerificationManager;
import com.pmease.gitplex.core.manager.WorkManager;
import com.pmease.gitplex.core.manager.impl.DefaultAccountManager;
import com.pmease.gitplex.core.manager.impl.DefaultAttachmentManager;
-import com.pmease.gitplex.core.manager.impl.DefaultCommitInfoManager;
import com.pmease.gitplex.core.manager.impl.DefaultBranchWatchManager;
+import com.pmease.gitplex.core.manager.impl.DefaultCodeCommentInfoManager;
import com.pmease.gitplex.core.manager.impl.DefaultCodeCommentManager;
import com.pmease.gitplex.core.manager.impl.DefaultCodeCommentRelationManager;
import com.pmease.gitplex.core.manager.impl.DefaultCodeCommentReplyManager;
+import com.pmease.gitplex.core.manager.impl.DefaultCommitInfoManager;
import com.pmease.gitplex.core.manager.impl.DefaultConfigManager;
import com.pmease.gitplex.core.manager.impl.DefaultDataManager;
import com.pmease.gitplex.core.manager.impl.DefaultDepotManager;
@@ -182,6 +184,7 @@ public class CoreModule extends AbstractPluginModule {
bind(NotificationManager.class).to(DefaultNotificationManager.class);
bind(CommitInfoManager.class).to(DefaultCommitInfoManager.class);
bind(PullRequestInfoManager.class).to(DefaultPullRequestInfoManager.class);
+ bind(CodeCommentInfoManager.class).to(DefaultCodeCommentInfoManager.class);
bind(WorkManager.class).to(DefaultWorkManager.class);
bind(SequentialWorkManager.class).to(DefaultSequentialWorkManager.class);
bind(TeamManager.class).to(DefaultTeamManager.class);
@@ -198,10 +201,12 @@ public class CoreModule extends AbstractPluginModule {
bind(AbstractRealm.class).to(SecurityRealm.class);
contribute(CodeCommentListener.class, DefaultAttachmentManager.class);
+ contribute(CodeCommentListener.class, DefaultCodeCommentInfoManager.class);
contribute(DepotListener.class, DefaultPullRequestManager.class);
contribute(DepotListener.class, DefaultCommitInfoManager.class);
contribute(DepotListener.class, DefaultPullRequestInfoManager.class);
+ contribute(DepotListener.class, DefaultCodeCommentInfoManager.class);
contribute(DepotListener.class, DefaultTeamAuthorizationManager.class);
contribute(RefListener.class, DefaultPullRequestManager.class);
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/entity/CodeComment.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/entity/CodeComment.java
index 39befd44eb..45ddab93c4 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/entity/CodeComment.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/entity/CodeComment.java
@@ -56,7 +56,10 @@ public class CodeComment extends AbstractEntity {
private String content;
@Column(nullable=false)
- private Date date = new Date();
+ private Date createDate = new Date();
+
+ @Column(nullable=false)
+ private Date updateDate = new Date();
@OptimisticLock(excluded=true)
@Column(nullable=false)
@@ -72,6 +75,8 @@ public class CodeComment extends AbstractEntity {
@Embedded
private CompareContext compareContext;
+ private boolean resolved;
+
@OneToMany(mappedBy="comment", cascade=CascadeType.REMOVE)
private Collection<CodeCommentReply> replies = new ArrayList<>();
@@ -110,8 +115,20 @@ public class CodeComment extends AbstractEntity {
this.content = content;
}
- public Date getDate() {
- return date;
+ public Date getCreateDate() {
+ return createDate;
+ }
+
+ public Date getUpdateDate() {
+ return updateDate;
+ }
+
+ public void setUpdateDate(Date updateDate) {
+ this.updateDate = updateDate;
+ }
+
+ public void setCreateDate(Date createDate) {
+ this.createDate = createDate;
}
public void delete() {
@@ -135,7 +152,7 @@ public class CodeComment extends AbstractEntity {
}
public void setDate(Date date) {
- this.date = date;
+ this.createDate = date;
}
public Mark getMark() {
@@ -178,4 +195,12 @@ public class CodeComment extends AbstractEntity {
this.uuid = uuid;
}
+ public boolean isResolved() {
+ return resolved;
+ }
+
+ public void setResolved(boolean resolved) {
+ this.resolved = resolved;
+ }
+
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/entity/PullRequest.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/entity/PullRequest.java
index 3bca07e339..e2965babdb 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/entity/PullRequest.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/entity/PullRequest.java
@@ -65,6 +65,8 @@ public class PullRequest extends AbstractEntity {
private static final long serialVersionUID = 1L;
+ public static final int MAX_CODE_COMMENTS = 1000;
+
public enum Status {
PENDING_APPROVAL("Pending Approval"),
PENDING_UPDATE("Pending Update"), PENDING_INTEGRATE("Pending Integration"),
@@ -217,7 +219,7 @@ public class PullRequest extends AbstractEntity {
private Collection<PullRequestUpdate> updates = new ArrayList<>();
@OneToMany(mappedBy="request", cascade=CascadeType.REMOVE)
- private Collection<CodeCommentRelation> commentRelations = new ArrayList<>();
+ private Collection<CodeCommentRelation> codeCommentRelations = new ArrayList<>();
@OneToMany(mappedBy="request", cascade=CascadeType.REMOVE)
private Collection<ReviewInvitation> reviewInvitations = new ArrayList<>();
@@ -413,12 +415,12 @@ public class PullRequest extends AbstractEntity {
effectiveUpdates = null;
}
- public Collection<CodeCommentRelation> getCommentRelations() {
- return commentRelations;
+ public Collection<CodeCommentRelation> getCodeCommentRelations() {
+ return codeCommentRelations;
}
- public void setCommentRelations(Collection<CodeCommentRelation> commentRelations) {
- this.commentRelations = commentRelations;
+ public void setCodeCommentRelations(Collection<CodeCommentRelation> codeCommentRelations) {
+ this.codeCommentRelations = codeCommentRelations;
}
public Collection<ReviewInvitation> getReviewInvitations() {
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/listener/CodeCommentReplyListener.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/listener/CodeCommentReplyListener.java
new file mode 100644
index 0000000000..b063ac3dbf
--- /dev/null
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/listener/CodeCommentReplyListener.java
@@ -0,0 +1,11 @@
+package com.pmease.gitplex.core.listener;
+
+import com.pmease.gitplex.core.entity.CodeCommentReply;
+
+public interface CodeCommentReplyListener {
+
+ void onSaveReply(CodeCommentReply reply);
+
+ void onDeleteReply(CodeCommentReply reply);
+
+}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentInfoManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentInfoManager.java
new file mode 100644
index 0000000000..a9f041c071
--- /dev/null
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentInfoManager.java
@@ -0,0 +1,23 @@
+package com.pmease.gitplex.core.manager;
+
+import java.util.Map;
+
+import org.eclipse.jgit.lib.ObjectId;
+
+import com.pmease.gitplex.core.entity.Depot;
+
+public interface CodeCommentInfoManager {
+
+ void collect(Depot depot);
+
+ /**
+ * Get comments over specified depot and commit
+ *
+ * @return
+ * map of comment uuid to compare commit hash
+ */
+ Map<String, String> getComments(Depot depot, ObjectId commit);
+
+ void removeComment(Depot depot, ObjectId commit, String comment);
+
+}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentManager.java
index e4a46e668d..de4b50c78e 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentManager.java
@@ -1,6 +1,7 @@
package com.pmease.gitplex.core.manager;
import java.util.Collection;
+import java.util.List;
import javax.annotation.Nullable;
@@ -20,4 +21,8 @@ public interface CodeCommentManager extends EntityDao<CodeComment> {
void delete(CodeComment comment);
+ @Nullable
+ CodeComment find(String uuid);
+
+ List<CodeComment> queryAfter(Depot depot, @Nullable String commentUUID);
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentRelationManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentRelationManager.java
index 1b6cf9c6bf..8df2d95127 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentRelationManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentRelationManager.java
@@ -1,8 +1,14 @@
package com.pmease.gitplex.core.manager;
+import java.util.List;
+
import com.pmease.commons.hibernate.dao.EntityDao;
+import com.pmease.gitplex.core.entity.CodeComment;
import com.pmease.gitplex.core.entity.CodeCommentRelation;
+import com.pmease.gitplex.core.entity.PullRequest;
public interface CodeCommentRelationManager extends EntityDao<CodeCommentRelation> {
-
+
+ List<CodeComment> queryCodeComments(PullRequest request);
+
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentReplyManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentReplyManager.java
index 05c47b2698..ff587712ff 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentReplyManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/CodeCommentReplyManager.java
@@ -5,4 +5,7 @@ import com.pmease.gitplex.core.entity.CodeCommentReply;
public interface CodeCommentReplyManager extends EntityDao<CodeCommentReply> {
+ void save(CodeCommentReply reply);
+
+ void delete(CodeCommentReply reply);
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/PullRequestInfoManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/PullRequestInfoManager.java
index 6dc635f6f5..e0f0c09998 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/PullRequestInfoManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/PullRequestInfoManager.java
@@ -12,4 +12,6 @@ public interface PullRequestInfoManager {
Collection<String> getRequests(Depot depot, ObjectId commit);
+ void removeRequest(Depot depot, Collection<ObjectId> commits, String request);
+
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentInfoManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentInfoManager.java
new file mode 100644
index 0000000000..b8cce3a639
--- /dev/null
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentInfoManager.java
@@ -0,0 +1,321 @@
+package com.pmease.gitplex.core.manager.impl;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.annotation.Nullable;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.apache.commons.lang3.SerializationUtils;
+import org.eclipse.jgit.lib.ObjectId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.pmease.commons.hibernate.Sessional;
+import com.pmease.commons.hibernate.UnitOfWork;
+import com.pmease.commons.hibernate.dao.Dao;
+import com.pmease.commons.util.FileUtils;
+import com.pmease.commons.util.concurrent.PrioritizedRunnable;
+import com.pmease.gitplex.core.entity.Account;
+import com.pmease.gitplex.core.entity.CodeComment;
+import com.pmease.gitplex.core.entity.Depot;
+import com.pmease.gitplex.core.listener.CodeCommentListener;
+import com.pmease.gitplex.core.listener.DepotListener;
+import com.pmease.gitplex.core.listener.LifecycleListener;
+import com.pmease.gitplex.core.manager.CodeCommentInfoManager;
+import com.pmease.gitplex.core.manager.CodeCommentManager;
+import com.pmease.gitplex.core.manager.DepotManager;
+import com.pmease.gitplex.core.manager.SequentialWorkManager;
+import com.pmease.gitplex.core.manager.StorageManager;
+import com.pmease.gitplex.core.manager.WorkManager;
+
+import jetbrains.exodus.ArrayByteIterable;
+import jetbrains.exodus.ByteIterable;
+import jetbrains.exodus.env.Environment;
+import jetbrains.exodus.env.EnvironmentConfig;
+import jetbrains.exodus.env.Environments;
+import jetbrains.exodus.env.Store;
+import jetbrains.exodus.env.StoreConfig;
+import jetbrains.exodus.env.Transaction;
+import jetbrains.exodus.env.TransactionalComputable;
+import jetbrains.exodus.env.TransactionalExecutable;
+
+@Singleton
+public class DefaultCodeCommentInfoManager implements CodeCommentInfoManager, DepotListener,
+ LifecycleListener, CodeCommentListener {
+
+ private static final Logger logger = LoggerFactory.getLogger(DefaultCodeCommentInfoManager.class);
+
+ private static final String INFO_DIR = "codeCommentInfo";
+
+ private static final String DEFAULT_STORE = "default";
+
+ private static final ByteIterable LAST_COMMENT_KEY = new StringByteIterable("lastComment");
+
+ private static final int PRIORITY = 100;
+
+ private final StorageManager storageManager;
+
+ private final WorkManager workManager;
+
+ private final SequentialWorkManager sequentialWorkManager;
+
+ private final DepotManager depotManager;
+
+ private final CodeCommentManager codeCommentManager;
+
+ private final UnitOfWork unitOfWork;
+
+ private final Dao dao;
+
+ private final Map<Long, Environment> envs = new HashMap<>();
+
+ @Inject
+ public DefaultCodeCommentInfoManager(Dao dao, DepotManager depotManager, StorageManager storageManager,
+ CodeCommentManager codeCommentManager, WorkManager workManager,
+ SequentialWorkManager sequentialWorkManager, UnitOfWork unitOfWork) {
+ this.dao = dao;
+ this.depotManager = depotManager;
+ this.storageManager = storageManager;
+ this.codeCommentManager = codeCommentManager;
+ this.workManager = workManager;
+ this.sequentialWorkManager = sequentialWorkManager;
+ this.unitOfWork = unitOfWork;
+ }
+
+ private String getSequentialExecutorKey(Depot depot) {
+ return "repository-" + depot.getId() + "-collectCodeCommentInfo";
+ }
+
+ private synchronized Environment getEnv(Depot depot) {
+ Environment env = envs.get(depot.getId());
+ if (env == null) {
+ EnvironmentConfig config = new EnvironmentConfig();
+ config.setLogCacheShared(false);
+ config.setMemoryUsage(1024*1024*64);
+ config.setLogFileSize(64*1024);
+ env = Environments.newInstance(getInfoDir(depot), config);
+ envs.put(depot.getId(), env);
+ }
+ return env;
+ }
+
+ private File getInfoDir(Depot depot) {
+ File infoDir = new File(storageManager.getCacheDir(depot), INFO_DIR);
+ if (!infoDir.exists())
+ FileUtils.createDir(infoDir);
+ return infoDir;
+ }
+
+ private Store getStore(final Environment env, final String storeName) {
+ return env.computeInTransaction(new TransactionalComputable<Store>() {
+ @Override
+ public Store compute(Transaction txn) {
+ return env.openStore(storeName, StoreConfig.WITHOUT_DUPLICATES, txn);
+ }
+ });
+ }
+
+ @Override
+ public synchronized void onDeleteDepot(Depot depot) {
+ sequentialWorkManager.removeExecutor(getSequentialExecutorKey(depot));
+ Environment env = envs.remove(depot.getId());
+ if (env != null)
+ env.close();
+ FileUtils.deleteDir(getInfoDir(depot));
+ }
+
+ @Override
+ public void onRenameDepot(Depot renamedDepot, String oldName) {
+ }
+
+ private byte[] getBytes(@Nullable ByteIterable byteIterable) {
+ if (byteIterable != null)
+ return Arrays.copyOf(byteIterable.getBytesUnsafe(), byteIterable.getLength());
+ else
+ return null;
+ }
+
+ static class StringByteIterable extends ArrayByteIterable {
+ StringByteIterable(String value) {
+ super(value.getBytes());
+ }
+ }
+
+ @Override
+ public void systemStarting() {
+ }
+
+ @Override
+ public void systemStarted() {
+ }
+
+ @Override
+ public void collect(Depot depot) {
+ Long depotId = depot.getId();
+ sequentialWorkManager.execute(getSequentialExecutorKey(depot), new PrioritizedRunnable(PRIORITY) {
+
+ @Override
+ public void run() {
+ try {
+ workManager.submit(new PrioritizedRunnable(PRIORITY) {
+
+ @Override
+ public void run() {
+ unitOfWork.call(new Callable<Void>() {
+
+ @Override
+ public Void call() throws Exception {
+ doCollect(depotManager.load(depotId));
+ return null;
+ }
+
+ });
+ }
+
+ }).get();
+ } catch (InterruptedException | ExecutionException e) {
+ logger.error("Error collecting comment information", e);
+ }
+ }
+
+ });
+ }
+
+ private void doCollect(Depot depot) {
+ Environment env = getEnv(depot);
+ Store store = getStore(env, DEFAULT_STORE);
+
+ AtomicReference<String> lastComment = new AtomicReference<>();
+ env.executeInTransaction(new TransactionalExecutable() {
+
+ @Override
+ public void execute(final Transaction txn) {
+ byte[] value = getBytes(store.get(txn, LAST_COMMENT_KEY));
+ lastComment.set(value!=null?new String(value):null);
+ }
+
+ });
+
+ for (CodeComment comment: codeCommentManager.queryAfter(depot, lastComment.get())) {
+ env.executeInTransaction(new TransactionalExecutable() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void execute(Transaction txn) {
+ ByteIterable key = new StringByteIterable(comment.getUUID());
+
+ byte[] keyBytes = new byte[20];
+ ObjectId.fromString(comment.getCommit()).copyRawTo(keyBytes, 0);
+ ByteIterable commitKey = new ArrayByteIterable(keyBytes);
+ byte[] valueBytes = getBytes(store.get(txn, commitKey));
+ Map<String, String> comments;
+ if (valueBytes != null)
+ comments = (Map<String, String>) SerializationUtils.deserialize(valueBytes);
+ else
+ comments = new HashMap<>();
+ if (comment.getCompareContext() != null)
+ comments.put(comment.getUUID(), comment.getCompareContext().getCompareCommit());
+ else
+ comments.put(comment.getUUID(), null);
+ store.add(txn, commitKey, new ArrayByteIterable(SerializationUtils.serialize((Serializable) comments)));
+ store.add(txn, LAST_COMMENT_KEY, key);
+ }
+
+ });
+ }
+ }
+
+ @Override
+ public synchronized void systemStopping() {
+ for (Environment env: envs.values())
+ env.close();
+ }
+
+ @Override
+ public void systemStopped() {
+ }
+
+ @Override
+ public void onTransferDepot(Depot depot, Account oldAccount) {
+ }
+
+ @Override
+ public void onSaveDepot(Depot depot) {
+ }
+
+ @Override
+ public Map<String, String> getComments(Depot depot, ObjectId commit) {
+ Environment env = getEnv(depot);
+ Store store = getStore(env, DEFAULT_STORE);
+
+ return env.computeInReadonlyTransaction(new TransactionalComputable<Map<String, String>>() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public Map<String, String> compute(Transaction txn) {
+ byte[] keyBytes = new byte[20];
+ commit.copyRawTo(keyBytes, 0);
+ ByteIterable commitKey = new ArrayByteIterable(keyBytes);
+ byte[] valueBytes = getBytes(store.get(txn, commitKey));
+ if (valueBytes != null) {
+ return (Map<String, String>) SerializationUtils.deserialize(valueBytes);
+ } else {
+ return new HashMap<>();
+ }
+ }
+ });
+ }
+
+ @Sessional
+ @Override
+ public void onSaveComment(CodeComment comment) {
+ dao.afterCommit(new Runnable() {
+
+ @Override
+ public void run() {
+ collect(comment.getDepot());
+ }
+
+ });
+ }
+
+ @Override
+ public void onDeleteComment(CodeComment comment) {
+ }
+
+ @Override
+ public void removeComment(Depot depot, ObjectId commit, String comment) {
+ Environment env = getEnv(depot);
+ Store store = getStore(env, DEFAULT_STORE);
+ env.executeInTransaction(new TransactionalExecutable() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void execute(Transaction txn) {
+ byte[] keyBytes = new byte[20];
+ commit.copyRawTo(keyBytes, 0);
+ ByteIterable commitKey = new ArrayByteIterable(keyBytes);
+ byte[] valueBytes = getBytes(store.get(txn, commitKey));
+ Collection<String> storedComments;
+ if (valueBytes != null)
+ storedComments = (Collection<String>) SerializationUtils.deserialize(valueBytes);
+ else
+ storedComments = new HashSet<>();
+ storedComments.remove(comment);
+ store.add(txn, commitKey, new ArrayByteIterable(SerializationUtils.serialize((Serializable) storedComments)));
+ }
+
+ });
+ }
+
+}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentManager.java
index ceaae3b99e..3310acfccd 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentManager.java
@@ -11,6 +11,7 @@ import javax.inject.Singleton;
import org.eclipse.jgit.lib.ObjectId;
import org.hibernate.criterion.Criterion;
+import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.google.common.base.Preconditions;
@@ -25,8 +26,7 @@ import com.pmease.gitplex.core.listener.CodeCommentListener;
import com.pmease.gitplex.core.manager.CodeCommentManager;
@Singleton
-public class DefaultCodeCommentManager extends AbstractEntityDao<CodeComment>
- implements CodeCommentManager {
+public class DefaultCodeCommentManager extends AbstractEntityDao<CodeComment> implements CodeCommentManager {
private final Provider<Set<CodeCommentListener>> listenersProvider;
@@ -78,4 +78,27 @@ public class DefaultCodeCommentManager extends AbstractEntityDao<CodeComment>
dao.remove(comment);
}
+ @Sessional
+ @Override
+ public CodeComment find(String uuid) {
+ EntityCriteria<CodeComment> criteria = newCriteria();
+ criteria.add(Restrictions.eq("uuid", uuid));
+ return find(criteria);
+ }
+
+ @Sessional
+ @Override
+ public List<CodeComment> queryAfter(Depot depot, String commentUUID) {
+ EntityCriteria<CodeComment> criteria = newCriteria();
+ criteria.add(Restrictions.eq("depot", depot));
+ criteria.addOrder(Order.asc("id"));
+ if (commentUUID != null) {
+ CodeComment comment = find(commentUUID);
+ if (comment != null) {
+ criteria.add(Restrictions.gt("id", comment.getId()));
+ }
+ }
+ return query(criteria);
+ }
+
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentRelationManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentRelationManager.java
index d4ee479ccf..558af83a9a 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentRelationManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentRelationManager.java
@@ -1,34 +1,124 @@
package com.pmease.gitplex.core.manager.impl;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+
import javax.inject.Inject;
import javax.inject.Singleton;
+import org.eclipse.jgit.lib.ObjectId;
+
+import com.pmease.commons.git.BriefCommit;
+import com.pmease.commons.hibernate.Transactional;
import com.pmease.commons.hibernate.dao.AbstractEntityDao;
import com.pmease.commons.hibernate.dao.Dao;
import com.pmease.gitplex.core.entity.CodeComment;
import com.pmease.gitplex.core.entity.CodeCommentRelation;
-import com.pmease.gitplex.core.listener.CodeCommentListener;
+import com.pmease.gitplex.core.entity.PullRequest;
+import com.pmease.gitplex.core.entity.PullRequestUpdate;
+import com.pmease.gitplex.core.manager.CodeCommentInfoManager;
+import com.pmease.gitplex.core.manager.CodeCommentManager;
import com.pmease.gitplex.core.manager.CodeCommentRelationManager;
-import com.pmease.gitplex.core.manager.PullRequestInfoManager;
@Singleton
public class DefaultCodeCommentRelationManager extends AbstractEntityDao<CodeCommentRelation>
- implements CodeCommentRelationManager, CodeCommentListener {
+ implements CodeCommentRelationManager {
- private final PullRequestInfoManager pullRequestInfoManager;
+ private final CodeCommentInfoManager codeCommentInfoManager;
+
+ private final CodeCommentManager codeCommentManager;
@Inject
- public DefaultCodeCommentRelationManager(Dao dao, PullRequestInfoManager pullRequestInfoManager) {
+ public DefaultCodeCommentRelationManager(Dao dao, CodeCommentInfoManager codeCommentInfoManager,
+ CodeCommentManager codeCommentManager) {
super(dao);
- this.pullRequestInfoManager = pullRequestInfoManager;
+ this.codeCommentInfoManager = codeCommentInfoManager;
+ this.codeCommentManager = codeCommentManager;
}
+ /*
+ * Find out code comments belong to a pull request. A code comment is considered to be
+ * belong to pull request if its commit and compare commit is covered by commits of
+ * the pull request. The simplest approach to determine this would be to query the
+ * database for each commit of the pull request. However that might issue a lot of
+ * sql queries if there are many commits. To address this issue, at the time of
+ * querying related comments, we query the comment information store first (which is
+ * fast) for each commit to see if there are new comments relevant to the pull request.
+ */
+ @Transactional
@Override
- public void onSaveComment(CodeComment comment) {
- }
+ public List<CodeComment> queryCodeComments(PullRequest request) {
+ if (request.getCodeCommentRelations().size() < PullRequest.MAX_CODE_COMMENTS) {
+ Collection<String> relatedComments = new HashSet<>();
+ for (CodeCommentRelation relation: request.getCodeCommentRelations()) {
+ relatedComments.add(relation.getComment().getUUID());
+ }
+
+ Collection<String> involvedCommits = new HashSet<>();
+ for (PullRequestUpdate update: request.getUpdates()) {
+ for (BriefCommit commit: update.getCommits())
+ involvedCommits.add(commit.getHash());
+ }
+ involvedCommits.add(request.getBaseCommitHash());
- @Override
- public void onDeleteComment(CodeComment comment) {
+ Map<String, CommitInfo> commitInfos = new HashMap<>();
+ for (String commit: involvedCommits) {
+ Map<String, String> commentsOfCommit = codeCommentInfoManager.getComments(
+ request.getTargetDepot(), ObjectId.fromString(commit));
+ for (Map.Entry<String, String> entry: commentsOfCommit.entrySet()) {
+ CommitInfo commitInfo = new CommitInfo();
+ commitInfo.commit = commit;
+ commitInfo.compareCommit = entry.getValue();
+ commitInfos.put(entry.getKey(), commitInfo);
+ }
+ }
+
+ commitInfos.keySet().removeAll(relatedComments);
+
+ String baseCommit = request.getBaseCommitHash();
+ for (Map.Entry<String, CommitInfo> entry: commitInfos.entrySet()) {
+ String uuid = entry.getKey();
+ String commit = entry.getValue().commit;
+ String compareCommit = entry.getValue().compareCommit;
+
+ /*
+ * Comment is relevant if both commit and compare commit relates to pull request,
+ * and also they are not equals to the base commit in the same time
+ */
+ if (involvedCommits.contains(commit)
+ && involvedCommits.contains(compareCommit)
+ && !(commit.equals(baseCommit) && compareCommit.equals(baseCommit))) {
+ CodeComment comment = codeCommentManager.find(uuid);
+ if (comment == null) {
+ codeCommentInfoManager.removeComment(request.getTargetDepot(), ObjectId.fromString(commit), uuid);
+ } else if (request.getCodeCommentRelations().size() < PullRequest.MAX_CODE_COMMENTS) {
+ CodeCommentRelation relation = new CodeCommentRelation();
+ relation.setComment(comment);
+ relation.setRequest(request);
+ persist(relation);
+ request.getCodeCommentRelations().add(relation);
+ }
+ }
+ }
+
+ }
+
+ List<CodeComment> comments = new ArrayList<>();
+ for (CodeCommentRelation relation: request.getCodeCommentRelations()) {
+ comments.add(relation.getComment());
+ }
+
+ return comments;
}
+ private static class CommitInfo {
+ String commit;
+
+ String compareCommit;
+ }
+
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentReplyManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentReplyManager.java
index d6d9d530c2..25e5ae0469 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentReplyManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCodeCommentReplyManager.java
@@ -1,20 +1,50 @@
package com.pmease.gitplex.core.manager.impl;
+import java.util.Set;
+
import javax.inject.Inject;
+import javax.inject.Provider;
import javax.inject.Singleton;
+import com.pmease.commons.hibernate.Transactional;
import com.pmease.commons.hibernate.dao.AbstractEntityDao;
import com.pmease.commons.hibernate.dao.Dao;
import com.pmease.gitplex.core.entity.CodeCommentReply;
+import com.pmease.gitplex.core.listener.CodeCommentReplyListener;
+import com.pmease.gitplex.core.manager.CodeCommentManager;
import com.pmease.gitplex.core.manager.CodeCommentReplyManager;
@Singleton
public class DefaultCodeCommentReplyManager extends AbstractEntityDao<CodeCommentReply>
implements CodeCommentReplyManager {
+ private final CodeCommentManager codeCommentManager;
+
+ private final Provider<Set<CodeCommentReplyListener>> listenersProvider;
+
@Inject
- public DefaultCodeCommentReplyManager(Dao dao) {
+ public DefaultCodeCommentReplyManager(Dao dao, CodeCommentManager codeCommentManager,
+ Provider<Set<CodeCommentReplyListener>> listenersProvider) {
super(dao);
+ this.codeCommentManager = codeCommentManager;
+ this.listenersProvider = listenersProvider;
+ }
+
+ @Transactional
+ @Override
+ public void save(CodeCommentReply reply) {
+ for (CodeCommentReplyListener listener: listenersProvider.get())
+ listener.onSaveReply(reply);
+ persist(reply);
+ reply.getComment().setUpdateDate(reply.getDate());
+ codeCommentManager.save(reply.getComment());
}
+ @Transactional
+ @Override
+ public void delete(CodeCommentReply reply) {
+ for (CodeCommentReplyListener listener: listenersProvider.get())
+ listener.onDeleteReply(reply);
+ remove(reply);
+ }
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCommitInfoManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCommitInfoManager.java
index 849948277f..2f37a68cd1 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCommitInfoManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultCommitInfoManager.java
@@ -379,7 +379,7 @@ public class DefaultCommitInfoManager implements CommitInfoManager, DepotListene
}).get();
} catch (InterruptedException | ExecutionException e) {
- logger.error("Error caching commit information", e);
+ logger.error("Error collecting commit information", e);
}
}
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultDepotManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultDepotManager.java
index 04aa57ec26..8575d407da 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultDepotManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultDepotManager.java
@@ -48,6 +48,7 @@ import com.pmease.gitplex.core.listener.DepotListener;
import com.pmease.gitplex.core.listener.LifecycleListener;
import com.pmease.gitplex.core.listener.RefListener;
import com.pmease.gitplex.core.manager.AccountManager;
+import com.pmease.gitplex.core.manager.CodeCommentInfoManager;
import com.pmease.gitplex.core.manager.CommitInfoManager;
import com.pmease.gitplex.core.manager.DepotManager;
import com.pmease.gitplex.core.manager.PullRequestInfoManager;
@@ -70,6 +71,8 @@ public class DefaultDepotManager extends AbstractEntityDao<Depot> implements Dep
private final CommitInfoManager commitInfoManager;
private final PullRequestInfoManager pullRequestInfoManager;
+
+ private final CodeCommentInfoManager codeCommentInfoManager;
private final PullRequestManager pullRequestManager;
@@ -86,7 +89,7 @@ public class DefaultDepotManager extends AbstractEntityDao<Depot> implements Dep
private final Map<Long, Repository> repositoryCache = new ConcurrentHashMap<>();
@Inject
- public DefaultDepotManager(Dao dao, AccountManager userManager,
+ public DefaultDepotManager(Dao dao, AccountManager userManager, CodeCommentInfoManager codeCommentInfoManager,
TeamAuthorizationManager teamAuthorizationManager, PullRequestInfoManager pullRequestInfoManager,
StorageManager storageManager, CommitInfoManager commitInfoManager,
PullRequestManager pullRequestManager, Provider<Set<DepotListener>> listenersProvider) {
@@ -97,6 +100,7 @@ public class DefaultDepotManager extends AbstractEntityDao<Depot> implements Dep
this.pullRequestInfoManager = pullRequestInfoManager;
this.teamAuthorizationManager = teamAuthorizationManager;
this.commitInfoManager = commitInfoManager;
+ this.codeCommentInfoManager = codeCommentInfoManager;
this.pullRequestManager = pullRequestManager;
this.listenersProvider = listenersProvider;
@@ -357,6 +361,7 @@ public class DefaultDepotManager extends AbstractEntityDao<Depot> implements Dep
checkSanity(depot);
commitInfoManager.collect(depot);
pullRequestInfoManager.collect(depot);
+ codeCommentInfoManager.collect(depot);
}
pullRequestManager.checkSanity();
diff --git a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestInfoManager.java b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestInfoManager.java
index dd91e24357..4959afc485 100644
--- a/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestInfoManager.java
+++ b/gitplex.core/src/main/java/com/pmease/gitplex/core/manager/impl/DefaultPullRequestInfoManager.java
@@ -188,7 +188,7 @@ public class DefaultPullRequestInfoManager implements PullRequestInfoManager, De
}).get();
} catch (InterruptedException | ExecutionException e) {
- logger.error("Error caching commit information", e);
+ logger.error("Error collecting pull request information", e);
}
}
@@ -299,4 +299,31 @@ public class DefaultPullRequestInfoManager implements PullRequestInfoManager, De
});
}
+ @Override
+ public void removeRequest(Depot depot, Collection<ObjectId> commits, String request) {
+ Environment env = getEnv(depot);
+ Store store = getStore(env, DEFAULT_STORE);
+ env.executeInTransaction(new TransactionalExecutable() {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void execute(Transaction txn) {
+ for (ObjectId commit: commits) {
+ byte[] keyBytes = new byte[20];
+ commit.copyRawTo(keyBytes, 0);
+ ByteIterable commitKey = new ArrayByteIterable(keyBytes);
+ byte[] valueBytes = getBytes(store.get(txn, commitKey));
+ Collection<String> storedRequests;
+ if (valueBytes != null)
+ storedRequests = (Collection<String>) SerializationUtils.deserialize(valueBytes);
+ else
+ storedRequests = new HashSet<>();
+ storedRequests.remove(request);
+ store.add(txn, commitKey, new ArrayByteIterable(SerializationUtils.serialize((Serializable) storedRequests)));
+ }
+ }
+
+ });
+ }
+
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/comment/CodeCommentPanel.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/comment/CodeCommentPanel.java
index 2aedfebb9b..020c4b7acb 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/comment/CodeCommentPanel.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/comment/CodeCommentPanel.java
@@ -62,7 +62,7 @@ public abstract class CodeCommentPanel extends GenericPanel<CodeComment> {
commentContainer.add(new AvatarLink("authorAvatar", getComment().getUser()));
commentContainer.add(new AccountLink("authorName", getComment().getUser()));
- commentContainer.add(new Label("authorDate", DateUtils.formatAge(getComment().getDate())));
+ commentContainer.add(new Label("authorDate", DateUtils.formatAge(getComment().getCreateDate())));
commentContainer.add(new Link<Void>("compareContext") {
@Override
@@ -119,7 +119,7 @@ public abstract class CodeCommentPanel extends GenericPanel<CodeComment> {
if (comment.getVersion() != lastVersionRef.get())
throw new StaleObjectStateException(CodeComment.class.getName(), comment.getId());
comment.setContent(object);
- GitPlex.getInstance(CodeCommentManager.class).persist(comment);
+ GitPlex.getInstance(CodeCommentManager.class).save(comment);
target.add(feedback); // clear the feedback
} catch (StaleObjectStateException e) {
commentContainer.warn("Some one changed the content you are editing. "
@@ -201,7 +201,7 @@ public abstract class CodeCommentPanel extends GenericPanel<CodeComment> {
if (comment.getVersion() != lastVersion)
throw new StaleObjectStateException(CodeComment.class.getName(), comment.getId());
comment.setContent(input.getModelObject());
- GitPlex.getInstance(CodeCommentManager.class).persist(comment);
+ GitPlex.getInstance(CodeCommentManager.class).save(comment);
WebMarkupContainer commentContainer = newCommentContainer();
fragment.replaceWith(commentContainer);
target.add(commentContainer);
@@ -317,7 +317,7 @@ public abstract class CodeCommentPanel extends GenericPanel<CodeComment> {
if (reply.getVersion() != lastVersionRef.get())
throw new StaleObjectStateException(CodeCommentReply.class.getName(), reply.getId());
reply.setContent(object);
- GitPlex.getInstance(CodeCommentReplyManager.class).persist(reply);
+ GitPlex.getInstance(CodeCommentReplyManager.class).save(reply);
target.add(feedback); // clear the feedback
} catch (StaleObjectStateException e) {
replyContainer.warn("Some one changed the content you are editing. The content has now been reloaded, "
@@ -399,7 +399,7 @@ public abstract class CodeCommentPanel extends GenericPanel<CodeComment> {
if (reply.getVersion() != lastVersion)
throw new StaleObjectStateException(CodeComment.class.getName(), reply.getId());
reply.setContent(input.getModelObject());
- GitPlex.getInstance(CodeCommentReplyManager.class).persist(reply);
+ GitPlex.getInstance(CodeCommentReplyManager.class).save(reply);
WebMarkupContainer replyContainer = newReplyContainer(componentId, replyId);
fragment.replaceWith(replyContainer);
target.add(replyContainer);
@@ -434,7 +434,7 @@ public abstract class CodeCommentPanel extends GenericPanel<CodeComment> {
@Override
public void onClick(AjaxRequestTarget target) {
- GitPlex.getInstance(CodeCommentReplyManager.class).remove(getReply(replyId));
+ GitPlex.getInstance(CodeCommentReplyManager.class).delete(getReply(replyId));
String script = String.format("$('#%s').remove();", replyContainer.getMarkupId());
target.appendJavaScript(script);
}
@@ -516,7 +516,7 @@ public abstract class CodeCommentPanel extends GenericPanel<CodeComment> {
reply.setUser(SecurityUtils.getAccount());
reply.setContent(input.getModelObject());
reply.setCompareContext(getCompareContext());
- GitPlex.getInstance(CodeCommentReplyManager.class).persist(reply);
+ GitPlex.getInstance(CodeCommentReplyManager.class).save(reply);
WebMarkupContainer replyContainer = newReplyContainer(repliesView.newChildId(), reply.getId());
repliesView.add(replyContainer);