Skip to content

Commit

Permalink
Use a Configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
kasunsiyambalapitiya committed Apr 3, 2017
1 parent 123494b commit 25560d7
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private ApiUtility() {
*
* @param httpGet Instance of the relevant httpGet
* @return String representation of the json response
* @throws CodeQualityMetricsException
* @throws CodeQualityMetricsException results
*/
public static String callApi(HttpGet httpGet) throws CodeQualityMetricsException {
BufferedReader bufferedReader = null;
Expand Down Expand Up @@ -84,21 +84,21 @@ public static String callApi(HttpGet httpGet) throws CodeQualityMetricsException
try {
bufferedReader.close();
} catch (IOException e) {
logger.debug("IOException occurred when closing the BufferedReader", e);
logger.error("IOException occurred when closing the BufferedReader", e);
}
}
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
logger.debug("IOException occurred when closing the HttpResponse", e);
logger.error("IOException occurred when closing the HttpResponse", e);
}
}
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
logger.debug("IOException occurred when closing the HttpClient", e);
logger.error("IOException occurred when closing the HttpClient", e);
}
}
}
Expand All @@ -110,7 +110,7 @@ public static String callApi(HttpGet httpGet) throws CodeQualityMetricsException
*
* @param httpPost relevant instance of the httpost
* @return String representation of the json response
* @throws CodeQualityMetricsException
* @throws CodeQualityMetricsException results
*/
public static String callGraphQlApi(HttpPost httpPost) throws CodeQualityMetricsException {
BufferedReader bufferedReader = null;
Expand Down Expand Up @@ -148,21 +148,21 @@ public static String callGraphQlApi(HttpPost httpPost) throws CodeQualityMetrics
try {
bufferedReader.close();
} catch (IOException e) {
logger.debug("IOException occurred when closing the buffered reader", e);
logger.error("IOException occurred when closing the buffered reader", e);
}
}
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
logger.debug("IOException occurred when closing the HttpResponse", e);
logger.error("IOException occurred when closing the HttpResponse", e);
}
}
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
logger.debug("IOException occurred when closing the HttpClient", e);
logger.error("IOException occurred when closing the HttpClient", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void main(String[] args) {
gitHubToken);
codeQualityMetricsExecutor.execute();
} else {
logger.debug("Command line arguments were not given correctly to start the execution");
logger.error("Command line arguments were not given correctly to start the execution");
logger.debug("Please enter PMT Access token, patch id and github access token in order as command " +
"line arguments to initiate the program");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ public class ChangesFinder {
*/

public Set<String> obtainRepoNamesForCommitHashes(String gitHubToken, List<String> commitHashes) {

commitHashes.forEach(commitHash -> {
try {
String jsonText = githubApiCaller.callSearchCommitApi(commitHash, gitHubToken);
saveRepoNames(jsonText, commitHash, gitHubToken);
} catch (CodeQualityMetricsException e) {
logger.debug(e.getMessage(), e.getCause());
logger.error(e.getMessage(), e.getCause());
}
});
return authorCommits;
Expand Down Expand Up @@ -112,18 +111,15 @@ private void saveRepoNames(String jsonText, String commitHash, String gitHubToke
try {
fileNamesWithPatcheString = sdkGitHubClient.getFilesChanged(repositoryName, commitHash);
} catch (CodeQualityMetricsException e) {
logger.debug(e.getMessage(), e.getCause());
logger.error(e.getMessage(), e.getCause());
}
if (fileNamesWithPatcheString != null) {
fileNames = fileNamesWithPatcheString.get("fileNames");
patchString = fileNamesWithPatcheString.get("patchString");
}
saveRelaventEditLineNumbers(fileNames, patchString);
try {
findFileChanges(repositoryName, commitHash, gitHubToken);
} catch (CodeQualityMetricsException e) {
logger.debug(e.getMessage(), e.getCause());
}
findFileChanges(repositoryName, commitHash, gitHubToken);

});
if (logger.isDebugEnabled()) {
logger.debug("\n Author names :" + authorNames);
Expand Down Expand Up @@ -198,9 +194,7 @@ private void saveRelaventEditLineNumbers(List<String> fileNames, List<String> pa
* @param commitHash current selected Repository
* @param gitHubToken github token for accessing github GraphQL API
*/
private void findFileChanges(String repoLocation, String commitHash, String gitHubToken)
throws CodeQualityMetricsException {

private void findFileChanges(String repoLocation, String commitHash, String gitHubToken) {
// filtering the owner and the Repository name from the repoLocation
String owner = StringUtils.substringBefore(repoLocation, "/");
String repositoryName = StringUtils.substringAfter(repoLocation, "/");
Expand All @@ -221,7 +215,7 @@ private void findFileChanges(String repoLocation, String commitHash, String gitH
// calling the graphql API for getting blame information for the current file.
jsonText = githubApiCaller.callGraphqlApi(jsonStructure, gitHubToken);
} catch (CodeQualityMetricsException e) {
logger.debug(e.getMessage(), e.getCause());
logger.error(e.getMessage(), e.getCause());
}
//reading the above saved output for the current selected file name
try {
Expand All @@ -230,7 +224,7 @@ private void findFileChanges(String repoLocation, String commitHash, String gitH
logger.debug("Parent commits are saved for the " + fileName + " for all the modified line ranges");
}
} catch (CodeQualityMetricsException e) {
logger.debug(e.getMessage(), e.getCause());
logger.error(e.getMessage(), e.getCause());
}
findAuthorCommits(owner, repositoryName, fileName, lineRangesForSelectedFile, gitHubToken);
logger.debug("Authors and author commits of bug lines of code which are being fixed from the given " +
Expand All @@ -247,7 +241,6 @@ private void findFileChanges(String repoLocation, String commitHash, String gitH
*/
private void readBlameForSelectedFile(String jsonText, List<String> changedRangesOfSelectedFile)
throws CodeQualityMetricsException {

GraphQlResponse graphQlResponse;
try {
graphQlResponse = gson.fromJson(jsonText, GraphQlResponse.class);
Expand Down Expand Up @@ -310,9 +303,9 @@ private void readBlameForSelectedFile(String jsonText, List<String> changedRange
*/
private void findAuthorCommits(String owner, String repositoryName, String fileName,
List<String> lineRangesForSelectedFile, String gitHubToken) {
for (Map.Entry entry : parentCommitHashes.entrySet()) {
String oldRange = (String) entry.getKey();
Set<String> commitHashes = (Set<String>) entry.getValue();
for (Map.Entry<String, Set<String>> entry : parentCommitHashes.entrySet()) {
String oldRange = entry.getKey();
Set<String> commitHashes = entry.getValue();
Graphql graphqlBean = new Graphql();
commitHashes.parallelStream()
.forEach(commitHash -> {
Expand All @@ -322,7 +315,7 @@ private void findAuthorCommits(String owner, String repositoryName, String fileN
try {
jsonText = githubApiCaller.callGraphqlApi(jsonStructure, gitHubToken);
} catch (CodeQualityMetricsException e) {
logger.debug(e.getMessage(), e.getCause());
logger.error(e.getMessage(), e.getCause());
}
saveAuthorCommits(jsonText, oldRange, lineRangesForSelectedFile);
});
Expand All @@ -342,7 +335,6 @@ private void findAuthorCommits(String owner, String repositoryName, String fileN
* @param lineRangesForSelectedFile arraylist containing the changed line ranges of the current selected file
*/
private void saveAuthorCommits(String jsonText, String oldRange, List<String> lineRangesForSelectedFile) {

GraphQlResponse graphQlResponse = gson.fromJson(jsonText, GraphQlResponse.class);
lineRangesForSelectedFile.forEach(lineRange -> {
int startingLineNo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void execute() {
reviewAnalyser.printReviewUsers();
logger.debug("The application executed successfully");
} catch (CodeQualityMetricsException e) {
logger.debug(e.getMessage(), e.getCause());
logger.error(e.getMessage(), e.getCause());
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ private List<String> findCommitHashesInPatch() throws CodeQualityMetricsExceptio
}
}
if (logger.isDebugEnabled()) {
logger.debug("The commit hashes are: " + commitHashes);
logger.error("The commit hashes are: " + commitHashes);
}
} else {
throw new CodeQualityMetricsException("The returned jsonText from PMT API is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import static com.wso2.code.quality.metrics.model.Constants.ACCEPT;
import static com.wso2.code.quality.metrics.model.Constants.AUTHORIZATION;
Expand All @@ -36,25 +39,33 @@
*/
public class GithubApiCaller {
private HttpGet httpGet;
private final Properties defaultProperties = new Properties();
private final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
private final InputStream inputStream = classLoader.getResourceAsStream("url.properties");


/**
* This is used for calling the github search REST API.
*
* @param commitHash commit hash to be searched
* @param githubAccessToken Github access token for accessing github API
* @return String representation of the json response
* @throws CodeQualityMetricsException
* @throws CodeQualityMetricsException results
*/
public String callSearchCommitApi(String commitHash, String githubAccessToken) throws CodeQualityMetricsException {
String url = "https://api.github.com/search/commits?q=hash%3A" + commitHash;
try {
defaultProperties.load(inputStream);
String url = defaultProperties.getProperty("searchCommitApiUrl") + commitHash;
httpGet = new HttpGet(url);
httpGet.addHeader(AUTHORIZATION, BEARER + githubAccessToken);
//as the accept header is needed for accessing commit search API which is still in preview mode
httpGet.addHeader(ACCEPT, "application/vnd.github.cloak-preview");
httpGet.addHeader(ACCEPT, defaultProperties.getProperty("searchCommitApiHeader"));
} catch (IllegalArgumentException e) {
throw new CodeQualityMetricsException("The url provided for accessing the Github Search Commit API is " +
"invalid ", e);
} catch (IOException e) {
throw new CodeQualityMetricsException("IO exception occurred when loading the inputstream to the " +
"properties object", e);
}
return ApiUtility.callApi(httpGet);
}
Expand All @@ -66,18 +77,24 @@ public String callSearchCommitApi(String commitHash, String githubAccessToken) t
* @param pullRequestNumber pull request number to be queried for
* @param githubAccessToken Github access token for accessing github API
* @return String representation of the json response
* @throws CodeQualityMetricsException
* @throws CodeQualityMetricsException results
*/
public String callReviewApi(String repoLocation, int pullRequestNumber, String githubAccessToken) throws
CodeQualityMetricsException {
String url = "https://api.github.com/repos/" + repoLocation + "/pulls/" + pullRequestNumber + "/reviews";
try {
defaultProperties.load(inputStream);
String tempUrl = defaultProperties.getProperty("reviewApiUrl");
String url = tempUrl.replaceFirst("REPO_LOCATION", repoLocation).replaceFirst("PULL_REQUEST_NUMBER",
String.valueOf(pullRequestNumber));
httpGet = new HttpGet(url);
httpGet.addHeader(ACCEPT, "application/vnd.github.black-cat-preview+json");
httpGet.addHeader(ACCEPT, defaultProperties.getProperty("reviewApiUrlHeader"));
httpGet.addHeader(AUTHORIZATION, BEARER + githubAccessToken);
} catch (IllegalArgumentException e) {
throw new CodeQualityMetricsException("The url provided for accessing the Github Review Commit API is " +
"invalid ", e);
} catch (IOException e) {
throw new CodeQualityMetricsException("IO exception occurred when loading the inputstream to the " +
"properties object", e);
}
return ApiUtility.callApi(httpGet);
}
Expand All @@ -88,18 +105,22 @@ public String callReviewApi(String repoLocation, int pullRequestNumber, String g
* @param commitHashToBeSearched commit hash to be searched for issues
* @param githubAccessToken Github access token for accessing github API
* @return String representation of the json response
* @throws CodeQualityMetricsException
* @throws CodeQualityMetricsException results
*/
public String callSearchIssueApi(String commitHashToBeSearched, String githubAccessToken) throws
CodeQualityMetricsException {
String url = "https://api.github.com/search/issues?q=" + commitHashToBeSearched;
try {
defaultProperties.load(inputStream);
String url = defaultProperties.getProperty("searchIssueApiUrl") + commitHashToBeSearched;
httpGet = new HttpGet(url);
httpGet.addHeader(ACCEPT, "application/vnd.github.mercy-preview+json");
httpGet.addHeader(ACCEPT, defaultProperties.getProperty("searchIssueApiUrlHeader"));
httpGet.addHeader(AUTHORIZATION, BEARER + githubAccessToken);
} catch (IllegalArgumentException e) {
throw new CodeQualityMetricsException("The url provided for accessing the Github Search Issue API is " +
"invalid ", e);
} catch (IOException e) {
throw new CodeQualityMetricsException("IO exception occurred when loading the inputstream to the " +
"properties object", e);
}
return ApiUtility.callApi(httpGet);
}
Expand All @@ -110,16 +131,17 @@ public String callSearchIssueApi(String commitHashToBeSearched, String githubAcc
* @param graphqlJsonStructure JSON input structure for calling the graphql API
* @param githubToken Github access token for accessing github API
* @return String representation of the json response
* @throws CodeQualityMetricsException
* @throws CodeQualityMetricsException results
*/
public String callGraphqlApi(JSONObject graphqlJsonStructure, String githubToken) throws
CodeQualityMetricsException {
String url = "https://api.github.com/graphql";
HttpPost httpPost;
try {
defaultProperties.load(inputStream);
String url = defaultProperties.getProperty("githubGraphqlUrl");
httpPost = new HttpPost(url);
httpPost.addHeader(AUTHORIZATION, BEARER + githubToken);
httpPost.addHeader(ACCEPT, "application/json");
httpPost.addHeader(ACCEPT, defaultProperties.getProperty("githubGraphqlUrlHeader"));
StringEntity entity = new StringEntity(graphqlJsonStructure.toString());
httpPost.setEntity(entity);
} catch (IllegalArgumentException e) {
Expand All @@ -128,6 +150,9 @@ public String callGraphqlApi(JSONObject graphqlJsonStructure, String githubToken
} catch (UnsupportedEncodingException e) {
throw new CodeQualityMetricsException("An error occurred when creating the String entity from Json " +
"Structure", e);
} catch (IOException e) {
throw new CodeQualityMetricsException("IO exception occurred when loading the inputstream to the " +
"properties object", e);
}
return ApiUtility.callGraphQlApi(httpPost);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

import org.apache.http.client.methods.HttpGet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import static com.wso2.code.quality.metrics.model.Constants.AUTHORIZATION;
import static com.wso2.code.quality.metrics.model.Constants.BEARER;

Expand All @@ -38,17 +42,26 @@ public PmtApiCaller() {
* @param accessToken WSO2 PMT access token
* @param patchId Patch Id
* @return String representation of the json response
* @throws CodeQualityMetricsException
* @throws CodeQualityMetricsException results
*/
public String callApi(String accessToken, String patchId) throws CodeQualityMetricsException {
String pmtUrl = "http://umt.private.wso2.com:9765/codequalitymatricesapi/1.0.0//properties?path=/_system/" +
"governance/patchs/" + patchId;
HttpGet httpGet;
try {
Properties defaultProperties = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("url.properties");
defaultProperties.load(inputStream);
String pmtApiUrl = defaultProperties.getProperty("pmtApiUrl");
String pmtUrl = pmtApiUrl + patchId;
httpGet = new HttpGet(pmtUrl);
httpGet.addHeader(AUTHORIZATION, BEARER + accessToken);
} catch (IllegalArgumentException e) {
throw new CodeQualityMetricsException("The url provided for accessing the PMT API is invalid ", e);
} catch (SecurityException e) {
throw new CodeQualityMetricsException("The url properties file is not found", e);
} catch (IOException e) {
throw new CodeQualityMetricsException("IO exception occurred when loading the inputstream to the " +
"properties object", e);
}
return ApiUtility.callApi(httpGet);
}
Expand Down
Loading

0 comments on commit 25560d7

Please sign in to comment.