Skip to content

Instantly share code, notes, and snippets.

@masud-technope
Last active December 6, 2020 22:19
Show Gist options
  • Save masud-technope/78bb7582fd83a8259f78f14b81662368 to your computer and use it in GitHub Desktop.
Save masud-technope/78bb7582fd83a8259f78f14b81662368 to your computer and use it in GitHub Desktop.
package developer.experience;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import json.loader.RepoPRJSONLoader;
import repo.commit.loader.RepoCommitMapLoader;
import config.StaticData;
import utility.CommentDateLoader;
import utility.ContentLoader;
public class ReviewCommitCounter {
String repoName;
String commentKey;
String authorLogin;
String commentPath;
public static HashMap<String, ArrayList<String>> commitFileMap = new HashMap();
public static HashMap<Integer, JSONObject> prMap = new HashMap<>();
public ReviewCommitCounter(String repoName, String commentKey,
String authorLogin, String commentPath) {
this.repoName = repoName;
this.commentKey = commentKey;
this.authorLogin = authorLogin;
this.commentPath = commentPath;
}
protected void loadRequiredLibs() {
if (commitFileMap.isEmpty()) {
RepoCommitMapLoader commitMapLoader = new RepoCommitMapLoader(
repoName);
commitFileMap = commitMapLoader.loadCommitFileMap();
}
if (prMap.isEmpty()) {
RepoPRJSONLoader prLoader = new RepoPRJSONLoader(repoName);
prMap = prLoader.loadPRJSON();
}
}
public static void clearRequiredLibs() {
commitFileMap.clear();
prMap.clear();
}
public void calculateCodeReviewExperience() {
// calculate code review experience of the reviewers
// HashMap<String, Date> commentDateMap = loadCommentDates();
// HashMap<Integer, String> commentAuthorMap = loadCommentAuthors();
// HashMap<Integer, String> commentFileMap = loadCommentFiles();
// loading the required libraries
loadRequiredLibs();
// for (String commentKey : commentDateMap.keySet()) {
String[] parts = commentKey.split("-");
// int commentID = Integer.parseInt(parts[0].trim());
int prNumber = Integer.parseInt(parts[1].trim());
// Date commentDate = commentDateMap.get(commentKey);
// String authorLogin = commentAuthorMap.get(commentID);
// String commentPath = commentFileMap.get(commentID);
// now lets calculate the PR that reviewer actually reviewed
ArrayList<Integer> reviewedPRs = new ArrayList<>();
ArrayList<String> reviewedCommits = new ArrayList<>();
for (int prKey : prMap.keySet()) {
if (prKey > prNumber)
continue;
JSONObject prObject = prMap.get(prKey);
JSONArray revs = (JSONArray) prObject.get("actualRevs");
boolean found = false;
for (int i = 0; i < revs.size(); i++) {
String revLogin = revs.get(i).toString().trim();
if (authorLogin.contains(revLogin)) {
// flagging as found
found = true;
break;
}
}
if (found) {
reviewedPRs.add(prKey);
JSONArray commits = (JSONArray) prObject.get("commitSHAs");
for (int i = 0; i < commits.size(); i++) {
reviewedCommits.add(commits.get(i).toString());
}
}
}
int allReviewedCommit = reviewedCommits.size();
int targetedReviewCommit = 0;
// now we have pr and commit SHAs
for (String commitSHA : reviewedCommits) {
if (commitFileMap.containsKey(commitSHA)) {
ArrayList<String> files = commitFileMap.get(commitSHA);
if (files.contains(commentPath)) {
targetedReviewCommit++;
}
}
}
String prefix = commentKey.endsWith("-u") ? "u" : "nu";
prefix=commentKey;
System.out.println(commentKey + "\t" + (targetedReviewCommit > 0 ? 1 : 0)
+ "\t" + targetedReviewCommit + "\t" + allReviewedCommit + "\t"
+ reviewedPRs.size());
// }
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String repoName = "SM";
String commentKey = "29441472-1332-nu";
String authorLogin = "dwalker-va";
String commentPath = "src/app/models/account.py";
new ReviewCommitCounter(repoName, commentKey, authorLogin, commentPath)
.calculateCodeReviewExperience();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment