Skip to content

Commit

Permalink
use stream API in Reviewers.java
Browse files Browse the repository at this point in the history
  • Loading branch information
kasunsiyambalapitiya committed Mar 22, 2017
1 parent 81db43d commit 8622a82
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void saveRelaventEditLineNumbers(ArrayList<String> fileNames, ArrayList<S
//filtering the lines ranges that existed in the previous file, that exists in the new file and saving them in to the same array
IntStream.range(0, lineChanges.length)
.forEach(j -> {
//@@ -22,7 +22,7 @@
//@@ -22,7 +22,7 @@ => -22,7 +22,7 => 22,28/22,28
String tempString = lineChanges[j];
String lineRangeInTheOldFileBeingModified = StringUtils.substringBetween(tempString, "-", " +"); // for taking the authors and commit hashes of the previous lines
String lineRangeInTheNewFileResultedFromModification = StringUtils.substringAfter(tempString, "+"); // for taking the parent commit
Expand Down Expand Up @@ -230,23 +230,22 @@ public void iterateOverFileChanges(String repoLocation, String commitHash, Strin
.forEach(fileName -> {
int index = fileNames.indexOf(fileName);
// the relevant arraylist of changed lines for that file
ArrayList<String> arrayListOfRelevantChangedLines = lineRangesChanged.get(index);
ArrayList<String> arrayListOfRelevantChangedLinesOfSelectedFile = lineRangesChanged.get(index);
commitHashesMapOfTheParent = new HashMap<>(); // for storing the parent commit hashes for all the changed line ranges of the relevant file
graphqlApiJsonObject.put("query", "{repository(owner:\"" + owner + "\",name:\"" + repositoryName + "\"){object(expression:\"" + commitHash + "\"){ ... on Commit{blame(path:\"" + fileName + "\"){ranges{startingLine endingLine age commit{history(first: 2) { edges { node { message url } } } author { name email } } } } } } } }");
JSONObject rootJsonObject = null;
try {
// calling the graphql API for getting blame information for the current file and saving it in a location.
// calling the graphql API for getting blame information for the current file.
rootJsonObject = (JSONObject) graphQlApiCaller.callGraphQlApi(graphqlApiJsonObject, gitHubToken);
} catch (CodeQualityMatricesException e) {
logger.error(e.getMessage(), e.getCause()); // as exceptions cannot be thrown inside lambda expression
System.exit(1);
}
// reading the above saved output for the current selected file name
readBlameReceivedForAFile(rootJsonObject, arrayListOfRelevantChangedLines, false, null);
readBlameReceivedForAFile(rootJsonObject, arrayListOfRelevantChangedLinesOfSelectedFile, false, null);
logger.info("Parent Commits hashes of the lines which are being fixed by the patch in file " + fileName + " are saved to commitHashesMapOfTheParent map successfully ");

// parent commit hashes are stored in the arraylist for the given file

iterateOverToFindAuthors(owner, repositoryName, fileName, arrayListOfRelevantChangedLines, gitHubToken);
iterateOverToFindAuthors(owner, repositoryName, fileName, arrayListOfRelevantChangedLinesOfSelectedFile, gitHubToken);
logger.info("Authors of the bug lines of code which are being fixed from the given patch are saved successfully to authorNames SET");
});
}
Expand All @@ -255,11 +254,11 @@ public void iterateOverFileChanges(String repoLocation, String commitHash, Strin
* Reading the blame received for a current selected file name and insert the parent commits of the changed lines,
* relevant authors and the relevant commits hashes to look for the reviewers of those line ranges
*
* @param rootJsonObject JSONObject containing blame information for current selected file
* @param arrayListOfRelevantChangedLines arraylist containing the changed line ranges of the current selected file
* @param gettingPr should be true if running this method for finding the authors of buggy lines which are being fixed from the patch
* @param rootJsonObject JSONObject containing blame information for current selected file
* @param arrayListOfRelevantChangedLinesOfSelectedFile arraylist containing the changed line ranges of the current selected file
* @param gettingPr should be true if running this method for finding the authors of buggy lines which are being fixed from the patch
*/
public void readBlameReceivedForAFile(JSONObject rootJsonObject, ArrayList<String> arrayListOfRelevantChangedLines, boolean gettingPr, String oldRange) {
public void readBlameReceivedForAFile(JSONObject rootJsonObject, ArrayList<String> arrayListOfRelevantChangedLinesOfSelectedFile, boolean gettingPr, String oldRange) {

//running a iterator for fileName arrayList to get the location of the above saved file
JSONObject dataJSONObject = (JSONObject) rootJsonObject.get(GITHUB_GRAPHQL_API_DATA_KEY_STRING);
Expand All @@ -270,7 +269,7 @@ public void readBlameReceivedForAFile(JSONObject rootJsonObject, ArrayList<Strin

//getting the starting line no of the range of lines that are modified from the patch
// parallel streams are not used in here as the order of the arraylist is important in the process
arrayListOfRelevantChangedLines.stream()
arrayListOfRelevantChangedLinesOfSelectedFile.stream()
.forEach(lineRanges -> {
int startingLineNo = 0;
int endLineNo = 0;
Expand All @@ -286,15 +285,14 @@ public void readBlameReceivedForAFile(JSONObject rootJsonObject, ArrayList<Strin
// need to consider the line range in the new file resulted from applying the commit, for finding parent commits
startingLineNo = Integer.parseInt(StringUtils.substringBefore(newFileRange, ","));
endLineNo = Integer.parseInt(StringUtils.substringAfter(newFileRange, ","));
}
else{
} else {
return; // to skip the to the next iteration if oldRange != oldFileRange when finding authornames and commits for obtaining PRs
}

// as it is required to create a new Map for finding the recent commit for each line range
// as a new mapForStoringAgeAndIndex map should be available for each line range to find the most recent change
Map<Integer, ArrayList<Integer>> mapForStoringAgeAndIndex = new HashMap<Integer, ArrayList<Integer>>();

//checking line by line by iterating the startinLineNo
//checking line by line by iterating the startingLineNo
while (endLineNo >= startingLineNo) {
// since the index value is required for later processing, without Java 8 features "for loop" is used for iteration
for (int i = 0; i < rangeJSONArray.length(); i++) {
Expand Down Expand Up @@ -338,7 +336,7 @@ public void readBlameReceivedForAFile(JSONObject rootJsonObject, ArrayList<Strin
//converting the map into a treeMap to get it ordered
TreeMap<Integer, ArrayList<Integer>> treeMap = new TreeMap<>(mapForStoringAgeAndIndex);
int minimumKeyOfMapForStoringAgeAndIndex = treeMap.firstKey(); // getting the minimum key
//getting the relevant JSONObject indexes which consists of the recent commit with in the relevant line range
//getting the relevant JSONObject indexes which consists of the recent change with in the relevant line range
ArrayList<Integer> indexesOfJsonObjectForRecentCommit = mapForStoringAgeAndIndex.get(minimumKeyOfMapForStoringAgeAndIndex);
// the order of the indexesOfJsonObjectForRecentCommit is not important as we only need to get the parent commit hashes
indexesOfJsonObjectForRecentCommit.parallelStream()
Expand All @@ -359,7 +357,6 @@ public void readBlameReceivedForAFile(JSONObject rootJsonObject, ArrayList<Strin
commitHashesMapOfTheParent.get(oldFileRange).add(commitHash);
}
});
logger.info("Parent Commits hashes of the lines which are being fixed by the patch are saved to commitHashesOfTheParent SET successfully ");
}

}
Expand All @@ -370,14 +367,13 @@ public void readBlameReceivedForAFile(JSONObject rootJsonObject, ArrayList<Strin
/**
* Finding the authors of the commits
*
* @param owner owner of the repository
* @param repositoryName repository name
* @param fileName name of the file which is required to get blame details
* @param arrayListOfRelevantChangedLines arraylist containing the changed line ranges of the current selected file
* @param gitHubToken github token for accessing github GraphQL API
* @param owner owner of the repository
* @param repositoryName repository name
* @param fileName name of the file which is required to get blame details
* @param arrayListOfRelevantChangedLinesOfSelectedFile arraylist containing the changed line ranges of the current selected file
* @param gitHubToken github token for accessing github GraphQL API
*/
public void iterateOverToFindAuthors(String owner, String repositoryName, String fileName, ArrayList<String> arrayListOfRelevantChangedLines, String gitHubToken) {

public void iterateOverToFindAuthors(String owner, String repositoryName, String fileName, ArrayList<String> arrayListOfRelevantChangedLinesOfSelectedFile, String gitHubToken) {

for (Map.Entry m : commitHashesMapOfTheParent.entrySet()) {
String oldRange = (String) m.getKey();
Expand All @@ -388,29 +384,13 @@ public void iterateOverToFindAuthors(String owner, String repositoryName, String
JSONObject rootJsonObject = null;
try {
rootJsonObject = (JSONObject) graphQlApiCaller.callGraphQlApi(graphqlApiJsonObject, gitHubToken);
readBlameReceivedForAFile(rootJsonObject, arrayListOfRelevantChangedLines, true, oldRange);
readBlameReceivedForAFile(rootJsonObject, arrayListOfRelevantChangedLinesOfSelectedFile, true, oldRange);
} catch (CodeQualityMatricesException e) {
logger.error(e.getMessage(), e.getCause());
System.exit(1);
}
});

}

// calling the graphql api to get the blame details of the current file for the parent commits (That is found by filtering in the graqhQL output)
//as the order is not important in here parallel streams are used
// commitHashesOfTheParent.parallelStream()
// .forEach(parentCommitHashForCallingGraphQl -> {
// graphqlApiJsonObject.put("query", "{repository(owner:\"" + owner + "\",name:\"" + repositoryName + "\"){object(expression:\"" + parentCommitHashForCallingGraphQl + "\"){ ... on Commit{blame(path:\"" + fileName + "\"){ranges{startingLine endingLine age commit{ url author { name email } } } } } } } }");
// JSONObject rootJsonObject = null;
// try {
// rootJsonObject = (JSONObject) graphQlApiCaller.callGraphQlApi(graphqlApiJsonObject, gitHubToken);
// readBlameReceivedForAFile(rootJsonObject, arrayListOfRelevantChangedLines, true);
// } catch (CodeQualityMatricesException e) {
// logger.error(e.getMessage(), e.getCause());
// System.exit(1);
// }
// });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ public void saveReviewersToList(String githubToken, RestApiCaller restApiCaller)

for (Map.Entry m : mapContainingPRNoAgainstRepoName.entrySet()) {
String productLocation = (String) m.getKey();

Set<Integer> prNumbers = (Set<Integer>) m.getValue();

prNumbers.stream()
.forEach(prNumber -> {
setPullRequestReviewAPIUrl(productLocation, prNumber);
Expand Down Expand Up @@ -175,15 +173,22 @@ public void saveReviewersToList(String githubToken, RestApiCaller restApiCaller)
public void readTheReviewOutJSON(JSONArray reviewJsonArray, String productLocation, int prNumber) {

if (reviewJsonArray.length() != 0) {
for (Object object : reviewJsonArray) {
if (object instanceof JSONObject) {
JSONObject reviewJsonObject = (JSONObject) object;
Pmt.arrayToStream(reviewJsonArray)
.map(JSONObject.class::cast)
.forEach(reviewJsonObject->{
addRelevantUsersToList(reviewJsonObject);
});

addRelevantUsersToList(reviewJsonObject);
}
}
// for (Object object : reviewJsonArray) {
// if (object instanceof JSONObject) {
// JSONObject reviewJsonObject = (JSONObject) object;
//
//
// }
// }
} else {
System.out.println("There are no records of reviews for pull request: " + prNumber + " on " + productLocation + " repository");
logger.info("There are no records of reviews for pull request: " + prNumber + " on " + productLocation + " repository");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,18 @@ public Map<String, ArrayList<String>> getFilesChanged(String repositoryName, Str
IRepositoryIdProvider iRepositoryIdProvider = () -> repositoryName;
RepositoryCommit repositoryCommit = commitService.getCommit(iRepositoryIdProvider, commitHash);
List<CommitFile> filesChanged = repositoryCommit.getFiles();
ArrayList<String> tempFileNames=new ArrayList<>();
ArrayList<String> tempPatchString= new ArrayList<>();

// this can be run parallely as patchString of a file will always be in the same index as the file
filesChanged.parallelStream()
.forEach(commitFile -> {
fileNames.add(commitFile.getFilename());
patchString.add(commitFile.getPatch());
tempFileNames.add(commitFile.getFilename());
tempPatchString.add(commitFile.getPatch());
});
logger.info("for" + commitHash + " on the " + repositoryName + " repository, files changed and their relevant changed line ranges added to the arraylists successfully");
mapWithFileNamesAndPatches.put("fileNames", fileNames);
mapWithFileNamesAndPatches.put("patchString", patchString);
mapWithFileNamesAndPatches.put("fileNames", tempFileNames);
mapWithFileNamesAndPatches.put("patchString", tempPatchString);
logger.info("map with the modified file names with their relevant modified line ranges are saved successfully");
} catch (IOException e) {
throw new CodeQualityMatricesException("IO Exception occurred when getting the commit with the given SHA form the given repository ", e);
Expand Down

0 comments on commit 8622a82

Please sign in to comment.