Skip to content

The Unified and Autonomous Development Platform

Consider file additions/deletions when suggest reviewer

robin shen17608ea7d196
diff --git a/core/src/main/java/com/gitplex/server/git/command/FileChange.java b/core/src/main/java/com/gitplex/server/git/command/FileChange.java
index 03c714c1e6..bcfa169f6e 100644
--- a/core/src/main/java/com/gitplex/server/git/command/FileChange.java
+++ b/core/src/main/java/com/gitplex/server/git/command/FileChange.java
@@ -13,11 +13,17 @@ public class FileChange implements Serializable {
private final Action action;
- public FileChange(Action action, String oldPath, String newPath) {
+ private final int additions;
+
+ private final int deletions;
+
+ public FileChange(Action action, String oldPath, String newPath, int additions, int deletions) {
this.action = action;
this.oldPath = oldPath;
this.newPath = newPath;
+ this.additions = additions;
+ this.deletions = deletions;
}
public String getOldPath() {
@@ -32,6 +38,14 @@ public class FileChange implements Serializable {
return action;
}
+ public int getAdditions() {
+ return additions;
+ }
+
+ public int getDeletions() {
+ return deletions;
+ }
+
@Override
public String toString() {
if (action == Action.COPY || action == Action.RENAME)
diff --git a/core/src/main/java/com/gitplex/server/git/command/ListFileChangesCommand.java b/core/src/main/java/com/gitplex/server/git/command/ListFileChangesCommand.java
new file mode 100644
index 0000000000..ea6c97ab5d
--- /dev/null
+++ b/core/src/main/java/com/gitplex/server/git/command/ListFileChangesCommand.java
@@ -0,0 +1,99 @@
+package com.gitplex.server.git.command;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.StringTokenizer;
+
+import org.eclipse.jgit.util.QuotedString;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.gitplex.server.git.command.FileChange.Action;
+import com.gitplex.utils.command.Commandline;
+import com.gitplex.utils.command.LineConsumer;
+import com.google.common.base.Preconditions;
+
+public class ListFileChangesCommand extends GitCommand<Collection<FileChange>> {
+
+ private static final Logger logger = LoggerFactory.getLogger(ListFileChangesCommand.class);
+
+ private String fromRev;
+
+ private String toRev;
+
+ private String path;
+
+ public ListFileChangesCommand(File gitDir) {
+ super(gitDir);
+ }
+
+ public ListFileChangesCommand fromRev(String fromRev) {
+ this.fromRev = fromRev;
+ return this;
+ }
+
+ public ListFileChangesCommand toRev(String toRev) {
+ this.toRev = toRev;
+ return this;
+ }
+
+ public ListFileChangesCommand path(String path) {
+ this.path = path;
+ return this;
+ }
+
+ @Override
+ public Collection<FileChange> call() {
+ Preconditions.checkNotNull(toRev, "toRev has to be specified.");
+ Preconditions.checkNotNull(fromRev, "fromRev has to be specified.");
+
+ final Collection<FileChange> fileChanges = new ArrayList<>();
+
+ Commandline cmd = cmd();
+
+ cmd.addArgs("diff", "--numstat", "--no-renames", fromRev + ".." + toRev);
+
+ if (path != null)
+ cmd.addArgs("--", path);
+
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ if (line.contains("\t")) {
+ StringTokenizer tokenizer = new StringTokenizer(line, "\t");
+
+ int additions;
+ String field = tokenizer.nextToken();
+ if (field.equals("-"))
+ additions = -1;
+ else
+ additions = Integer.parseInt(field);
+
+ int deletions;
+ field = tokenizer.nextToken();
+ if (field.equals("-"))
+ deletions = -1;
+ else
+ deletions = Integer.parseInt(field);
+
+ String file = QuotedString.GIT_PATH.dequote(tokenizer.nextToken());
+
+ fileChanges.add(new FileChange(Action.MODIFY, file, file, additions, deletions));
+ }
+ }
+
+ }, new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ }).checkReturnCode();
+
+ return fileChanges;
+ }
+
+}
diff --git a/core/src/main/java/com/gitplex/server/git/command/LogCommand.java b/core/src/main/java/com/gitplex/server/git/command/LogCommand.java
index 49d8a33842..ecc681b4d6 100644
--- a/core/src/main/java/com/gitplex/server/git/command/LogCommand.java
+++ b/core/src/main/java/com/gitplex/server/git/command/LogCommand.java
@@ -88,7 +88,7 @@ public abstract class LogCommand extends GitCommand<Void> {
} else {
path1 = path2 = QuotedString.GIT_PATH.dequote(path);
}
- FileChange fileChange = new FileChange(action, path1, path2);
+ FileChange fileChange = new FileChange(action, path1, path2, -1, -1);
commitBuilderRef.get().fileChanges.add(fileChange);
}
} else if (line.startsWith("hash:")) {
diff --git a/core/src/main/java/com/gitplex/server/model/PullRequestUpdate.java b/core/src/main/java/com/gitplex/server/model/PullRequestUpdate.java
index 1368a17afb..cacca9c1ce 100644
--- a/core/src/main/java/com/gitplex/server/model/PullRequestUpdate.java
+++ b/core/src/main/java/com/gitplex/server/model/PullRequestUpdate.java
@@ -29,6 +29,8 @@ import org.eclipse.jgit.treewalk.filter.TreeFilter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.gitplex.server.git.GitUtils;
+import com.gitplex.server.git.command.FileChange;
+import com.gitplex.server.git.command.ListFileChangesCommand;
import com.gitplex.server.manager.impl.DefaultCodeCommentRelationManager;
import com.google.common.base.Preconditions;
@@ -59,6 +61,8 @@ public class PullRequestUpdate extends AbstractEntity {
private transient List<RevCommit> commits;
private transient Collection<String> changedFiles;
+
+ private transient Collection<FileChange> fileChanges;
public PullRequest getRequest() {
return request;
@@ -102,6 +106,16 @@ public class PullRequestUpdate extends AbstractEntity {
GitUtils.deleteRef(GitUtils.getRefUpdate(getRequest().getTargetProject().getRepository(), getHeadRef()));
}
+ public Collection<FileChange> getFileChanges() {
+ if (fileChanges == null) {
+ ListFileChangesCommand cmd = new ListFileChangesCommand(getRequest().getTargetProject().getGitDir());
+ cmd.fromRev(getBaseCommitHash());
+ cmd.toRev(getHeadCommitHash());
+ fileChanges = cmd.call();
+ }
+ return fileChanges;
+ }
+
/**
* Get changed files of this update since previous update. This calculation
* excludes changes introduced by commits from target branch (this will
diff --git a/core/src/main/java/com/gitplex/server/util/QualityCheckStatus.java b/core/src/main/java/com/gitplex/server/util/QualityCheckStatus.java
index 551b4a90c4..d9985082b0 100644
--- a/core/src/main/java/com/gitplex/server/util/QualityCheckStatus.java
+++ b/core/src/main/java/com/gitplex/server/util/QualityCheckStatus.java
@@ -2,6 +2,7 @@ package com.gitplex.server.util;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
@@ -15,6 +16,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.Permission;
import com.gitplex.server.GitPlex;
+import com.gitplex.server.git.command.FileChange;
import com.gitplex.server.manager.CommitInfoManager;
import com.gitplex.server.manager.UserManager;
import com.gitplex.server.manager.VerificationManager;
@@ -274,20 +276,29 @@ public class QualityCheckStatus {
}
private void sortByContributions(List<UserFacade> users, PullRequestUpdate update) {
+ if (users.size() <= 1)
+ return;
+
CommitInfoManager commitInfoManager = GitPlex.getInstance(CommitInfoManager.class);
- Map<UserFacade, Integer> contributions = new HashMap<>();
+ Map<UserFacade, Long> contributions = new HashMap<>();
for (UserFacade user: users)
- contributions.put(user, 0);
+ contributions.put(user, 0L);
int count = 0;
- for (String file: update.getChangedFiles()) {
- int addedContributions = addContributions(contributions, commitInfoManager, file, update);
+ for (FileChange change: update.getFileChanges()) {
+ String path = change.getNewPath();
+ int edits = change.getAdditions() + change.getDeletions();
+ if (edits < 0)
+ edits = 100;
+ else if (edits == 0)
+ edits = 1;
+ int addedContributions = addContributions(contributions, commitInfoManager, path, edits, update);
while (addedContributions == 0) {
- if (file.contains("/")) {
- file = StringUtils.substringBeforeLast(file, "/");
- addedContributions = addContributions(contributions, commitInfoManager, file, update);
+ if (path.contains("/")) {
+ path = StringUtils.substringBeforeLast(path, "/");
+ addedContributions = addContributions(contributions, commitInfoManager, path, edits, update);
} else {
- addContributions(contributions, commitInfoManager, "", update);
+ addContributions(contributions, commitInfoManager, "", edits, update);
break;
}
}
@@ -295,18 +306,28 @@ public class QualityCheckStatus {
break;
}
- users.sort((user1, user2)-> contributions.get(user2) - contributions.get(user1));
+ Collections.sort(users, new Comparator<UserFacade>() {
+
+ @Override
+ public int compare(UserFacade o1, UserFacade o2) {
+ if (contributions.get(o1) < contributions.get(o2))
+ return 1;
+ else
+ return -1;
+ }
+
+ });
}
- private int addContributions(Map<UserFacade, Integer> contributions, CommitInfoManager commitInfoManager,
- String file, PullRequestUpdate update) {
+ private int addContributions(Map<UserFacade, Long> contributions, CommitInfoManager commitInfoManager,
+ String path, int edits, PullRequestUpdate update) {
int addedContributions = 0;
- for (Map.Entry<UserFacade, Integer> entry: contributions.entrySet()) {
+ for (Map.Entry<UserFacade, Long> entry: contributions.entrySet()) {
UserFacade user = entry.getKey();
- int fileContribution = commitInfoManager.getContributions(
- update.getRequest().getTargetProject().getFacade(), user, file);
- entry.setValue(entry.getValue() + fileContribution);
- addedContributions += fileContribution;
+ int pathContribution = commitInfoManager.getContributions(
+ update.getRequest().getTargetProject().getFacade(), user, path);
+ entry.setValue(entry.getValue() + edits*pathContribution);
+ addedContributions += pathContribution;
}
return addedContributions;
}