-
Jack MacLauchlan CS2015 authoredJack MacLauchlan CS2015 authored
IssueReader.java 4.54 KiB
package git;
import issueData.Comment;
import issueData.Issue;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.treewalk.TreeWalk;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class IssueReader {
private Repository repo;
private List<Issue> issues;
public IssueReader(Repository repo, RevTree commitTree) {
this.repo = repo;
this.issues = new ArrayList<>();
buildIssues(commitTree);
}
private void buildIssues(RevTree commitTree) {
try (TreeWalk treeWalk = new TreeWalk(repo)) {
treeWalk.addTree(commitTree);
treeWalk.setRecursive(true);
while (treeWalk.next()) {
ObjectId blob = treeWalk.getObjectId(0);
String content = getContent(blob);
String hash = split(treeWalk.getPathString(), 0);
if (issues.isEmpty() || getIssue(hash) == null) {
Issue issue = new Issue();
issue.setOriginalHash(hash);
issues.add(issue);
addFieldData(issue, treeWalk.getPathString(), content);
} else {
addFieldData(getIssue(hash), treeWalk.getPathString(), content);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private Issue getIssue(String hash) {
for (Issue issue : issues) {
if (issue.getOriginalHash().equals((hash))) {
return issue;
}
}
return null;
}
private String getContent(ObjectId blob) throws IOException {
try (ObjectReader objectReader = repo.newObjectReader()) {
ObjectLoader objectLoader = objectReader.open(blob);
byte[] bytes = objectLoader.getBytes();
return new String(bytes, StandardCharsets.UTF_8);
}
}
private void addFieldData(Issue issue, String path, String data) {
String field = split(path, 1);
switch (field) {
case "title":
issue.setTitle(data);
break;
case "description":
issue.setDescription(data);
break;
case "creator":
issue.setCreator(data);
break;
case "status":
issue.setStatus(Boolean.valueOf(data));
break;
case "creationTime":
issue.setCreationTime(Long.valueOf(data));
break;
case "editTime":
issue.setEdited(Long.valueOf(data));
break;
case "comments":
buildComment(issue, path, data);
break;
case "tags":
issue.addTag(data);
break;
default:
//ignore
break;
}
}
private void buildComment(Issue issue, String path, String data) {
String commentHash = split(path, 2);
String commentField = split(path, 3);
if (issue.getComments().isEmpty() || getComment(issue, commentHash) == null) {
Comment comment = new Comment();
comment.setHash(commentHash);
issue.addComment(comment);
addCommentFields(comment, commentField, data);
} else {
addCommentFields(getComment(issue, commentHash), commentField, data);
}
}
private void addCommentFields(Comment comment, String field, String data) {
switch (field) {
case "author":
comment.setAuthor(data);
break;
case "message":
comment.setMessage(data);
break;
case "commentTime":
comment.setCreationTime(Long.valueOf(data));
break;
default:
//ignore
break;
}
}
private Comment getComment(Issue issue, String hash) {
for (Comment comment : issue.getComments()) {
if (comment.getHash().equals((hash))) {
return comment;
}
}
return null;
}
public List<Issue> getIssues() {
return issues;
}
private String split(String path, int index) {
return path.split("/")[index];
}
}