Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify to identify only the changed lines from a given patch #2

Open
wants to merge 64 commits into
base: master
Choose a base branch
from

Conversation

kasunsiyambalapitiya
Copy link
Contributor

Previous program considered a line range containing the modified lines from the given patch, for finding the authors, author commits and approved reviewers. As asked in the second code review the code is changed to detect exact lines been modified from the given patch and find authors, author commits and approved reviewers of those lines. This PR also contains all the modifications asked in PR1 https://github.com/wso2-incubator/code-quality-matrices/pull/1

Note:
At present the program works well for all WSO2 patches except the patches containing octopus merge commits ( commits having more than 2 parents) which may rarely available in WSO2 repositories

kasunsiyambalapitiya and others added 30 commits March 10, 2017 09:33
Add log messages for all classes
added streams for saveReviewersToList method

added streams for savePrNumberAndRepoName method

replaced for loop with for each loop in readTheReviewOutJSON method
add constants for Reviewers.java
Handle the response code recieved from Graphql API

Correct the message thrown in Exception
try {
String jsonText = githubApiCaller.callReviewApi(repositoryName, prNumber, githubToken);
if (jsonText != null) {
// Type listType = new TypeToken<List<ReviewApiResponse>>() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

public class Author {

@SerializedName("date")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

private String commitHash;

@SerializedName("commit")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

@Test
public void testSaveRepoNames() throws CodeQualityMetricsException {
Map<String, List<String>> commitHashWithRepoNames = new HashMap<>();
commitHashWithRepoNames.put("eaa45529cbabc5f30a2ffaa4781821ad0a5223ab",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These hashes seems to be used in multiple places. So might be able to use a constant.

/**
* This is a utility class for calling APIs.
*
* @since 1.0.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doc line is not used AFAIR. Better to check for consistency with other repositories.

Copy link
Contributor Author

@kasunsiyambalapitiya kasunsiyambalapitiya Apr 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked with few WSO2 repos, some contain @SInCE tag some doesn't, since this is a class level doc comment I think it is better to have it, What is your opinion?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kasunsiyambalapitiya : I think what chamila meant was don't use "This" word in the doc section. We all know that the doc is related to "This". So we can just say "Utility class for calling APIs".

BufferedReader bufferedReader = null;
CloseableHttpClient httpClient;
CloseableHttpResponse httpResponse = null;
httpClient = HttpClients.createDefault();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to instantiate at the same line where this is declared?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

String jsonText;
try {
httpResponse = httpClient.execute(httpGet);
int responseCode = httpResponse.getStatusLine().getStatusCode();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for httpResponse.getStatusLine() to return null? If so this might be a possible NPE producer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handled NPE in 698c71e

int responseCode = httpResponse.getStatusLine().getStatusCode();
if (responseCode == 200) {
//success
bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with httpResponse.getEntity(). When chaining calls, better to verify for possible NPEs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

if (args.length == 3) {
String pmtToken = args[0];
String patchId = args[1];
String gitHubToken = args[2];
Copy link

@chamilad chamilad Apr 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, it might be better to offload tokens to a config file and only accept the patch ID as an argument, as tokens tend to be reused again and would be cumbersome to enter in the command line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

*
* @since 1.0.0
*/
public class GithubApiCaller {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a util class and could be final and methods could be static as there doesn't seem to be any state to be preserved between calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted to a Util class in 698c71e

* @since 1.0.0
*/
public class PmtApiCaller {
public PmtApiCaller() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this constructor declared? Leftover from any refactoring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) Removed in 698c71e

* @return a map of pull requests againt their repository name
* @throws CodeQualityMetricsException results
*/
Map<String, Set<Integer>> savePrNumberAndRepoName(String jsonText) throws CodeQualityMetricsException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be default access? Can it be private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this method is called from a test case private access cannot be given

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kasunsiyambalapitiya : You can give private access even it is used in a test case. AFAIK, you can use java reflection to temporary change the access modifier before calling this method from the test case.

* under the License.
*/

package com.wso2.code.quality.metrics.model;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this class contains all the constants used in the project, then it might belong at the parent level of the package hierarchy, or under a separate util package.

private String graphqlInputWithoutHistory;
private static final long serialVersionUID = 42L;

public Graphql() {
Copy link

@chamilad chamilad Apr 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary declaration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it maven build fails giving this error com.wso2.code.quality.metrics.model.Graphql is Serializable; consider declaring a serialVersionUID [com.wso2.code.quality.metrics.model.Graphql] At Graphql.java:[lines 32-44] SE_NO_SERIALVERSIONID. I fixed it as mentioned in the answer by "Jon Skeet" in http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it or else we need to avoid implementing Serializable interface. What is the best way to handle it?

public class GraphqlCommit {
@SerializedName("author")
private GraphqlAuthor author;
@SerializedName("history")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to keep a new line between properties to improve readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

log4j.appender.CONSOLE= org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd}-5p--10c:%m%n



Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary new lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

* @since 1.0.0
*/
public class Token {
final String pmtToken = "tQU5vxzrGeBpLMQuwOsJW_fyYLYa";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be extracted out to the properties file? Looks like a hard coded config value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

Copy link

@chamilad chamilad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you able to run any code coverage runs for the project? Do the unit tests cover important logic?

int responseCode = httpResponse.getStatusLine().getStatusCode();
if (responseCode == 200) {
//success
bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use try with resource style for readers. Then you don't need to close them explicitly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

* @throws CodeQualityMetricsException results
*/
public static String callGraphQlApi(HttpPost httpPost) throws CodeQualityMetricsException {
BufferedReader bufferedReader = null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible use java7 try with resource style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

}
return ApiUtility.callGraphQlApi(httpPost);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add call to al the methods? can we think of something like search, getHistory etc..

try {
Properties defaultProperties = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("url.properties");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to close the InputStreams

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

IssueApiResponse issueApiResponse = gson.fromJson(jsonText, IssueApiResponse.class);
issueApiResponse.getIssue().parallelStream()
.filter(searchItem -> GITHUB_REVIEW_API_CLOSED_STATE.equals(searchItem.getStateOfThePr()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

result = IOUtils.toString(classLoader.getResourceAsStream(path));
} catch (IOException e) {
logger.debug(e.getMessage(), e.getCause());
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why exception is ignored here. better throw the exception. if we need to ignore, log it as an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

*/
public class SearchItem {
@SerializedName("state")
private String stateOfThePr;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better if we can change the variable name to prState

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 698c71e

@kasunsiyambalapitiya kasunsiyambalapitiya changed the title Modified to identify only the changed lines from a given patch Modify to identify only the changed lines from a given patch Apr 19, 2017
close resources with try with resources

Seperate tokens to a property file

Fix bug from history simplificatio

If two commits in the history of a file cancel each other out (the changes are reversed), then Git detect this and simplifies the git log output of the file by leaving out those two commits.
README.md Outdated
## Installation
### Prerequisites
* [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed.
* [Java](http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html) installed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing . at the end.

@Shan1024
Copy link

@kasunsiyambalapitiya : Please remove the org.wso2.codequalitymatrices directory from the project root as well(it would be better to have src, pom.xml, LICENSE and README in the project root). Please refer [1] for more details about the directory layout of a maven project.

[1] Introduction to the Standard Directory Layout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants