package git; import issueData.Comment; import issueData.Issue; import issueData.User; 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.List; class IssueReader { private final Repository repo; private final List<Issue> issues; 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": String title = data.replace("\n", "").replace("\r", ""); issue.setTitle(title); break; case "description": issue.setDescription(data); break; case "creator": String name = data.split("\\r?\\n")[0]; String email = data.split("\\r?\\n")[1]; issue.setCreator(new User(name, email)); break; case "status": issue.setStatus(Boolean.valueOf(data)); break; case "creationTime": issue.setCreationTime(Long.valueOf(data)); break; case "editTime": String stringTime = data.replace("\n", "").replace("\r", ""); issue.setEdited(Long.valueOf(stringTime)); 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": String name = data.split("\\r?\\n")[0]; String email = data.split("\\r?\\n")[1]; comment.setAuthor(new User(name, email)); 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; } List<Issue> getIssues() { return issues; } private String split(String path, int index) { return path.split("/")[index]; } }