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

Jenkins 52462: SonarQubeParser should read textRange #155

Merged
merged 5 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Added a parser for CMake warnings.
- [PR#137](https://github.com/jenkinsci/analysis-model/pull/137):
Added a parser for JSON output from Cargo.
- [PR#155](https://github.com/jenkinsci/analysis-model/pull/155):
Added textRange to SonarQubeParser.

### Fixed
- [JENKINS-56333](https://issues.jenkins-ci.org/browse/JENKINS-56333),
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>edu.hm.hafner</groupId>
<artifactId>analysis-model</artifactId>
<version>4.1.0-SNAPSHOT</version>
<version>5.0.0-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class SonarQubeDiffParser extends SonarQubeParser {

private static final String ISSUE_IS_NEW = "isNew";
private static final String COMPONENT_MODULE_KEY = "moduleKey";
private static final String ISSUE_START_LINE = "startLine";
private static final String ISSUE_END_LINE = "endLine";

@Override
protected boolean accepts(final JSONObject object) {
Expand All @@ -27,4 +29,14 @@ public boolean filterIssue(final JSONObject issue) {
protected String getModulePath(final JSONObject component, final JSONObject issue) {
return parseModulePath(component, COMPONENT_MODULE_KEY);
}

@Override
protected int parseStart(final JSONObject issue) {
return issue.optInt(ISSUE_START_LINE);
}

@Override
protected int parseEnd(final JSONObject issue) {
return issue.optInt(ISSUE_END_LINE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
*/
public class SonarQubeIssuesParser extends SonarQubeParser {
private static final long serialVersionUID = -8213765181968340929L;

private static final String ISSUE_SUB_PROJECT = "subProject";
private static final String ISSUE_TEXT_RANGE = "textRange";
private static final String ISSUE_TEXT_RANGE_START_LINE = "startLine";
private static final String ISSUE_TEXT_RANGE_END_LINE = "endLine";
private static final String ISSUE_LINE = "line";

@Override
protected boolean accepts(final JSONObject object) {
Expand All @@ -21,4 +25,20 @@ protected boolean accepts(final JSONObject object) {
protected String getModulePath(final JSONObject component, final JSONObject issue) {
return parseModulePath(issue, ISSUE_SUB_PROJECT);
}

@Override
protected int parseStart(final JSONObject issue) {
if (issue.has(ISSUE_TEXT_RANGE)) {
return issue.optJSONObject(ISSUE_TEXT_RANGE).optInt(ISSUE_TEXT_RANGE_START_LINE);
}
return issue.optInt(ISSUE_LINE);
}

@Override
protected int parseEnd(final JSONObject issue) {
if (issue.has(ISSUE_TEXT_RANGE)) {
return issue.optJSONObject(ISSUE_TEXT_RANGE).optInt(ISSUE_TEXT_RANGE_END_LINE);
}
return issue.optInt(ISSUE_LINE);
}
}
21 changes: 14 additions & 7 deletions src/main/java/edu/hm/hafner/analysis/parser/SonarQubeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public abstract class SonarQubeParser extends IssueParser {
/** issue.message attribute. */
private static final String ISSUE_MESSAGE = "message";
/** issue.line attribute. */
private static final String ISSUE_LINE = "line";
/** issue.line attribute. */
private static final String ISSUE_SEVERITY = "severity";
/** issue.type attribute. */
private static final String ISSUE_TYPE = "type";
Expand Down Expand Up @@ -108,7 +106,7 @@ private Report extractIssues(final JSONArray elements) {
if (object instanceof JSONObject) {
JSONObject issue = (JSONObject) object;
if (filterIssue(issue)) {
report.add(createIssueFormJsonObject(issue));
report.add(createIssueFromJsonObject(issue));
}
}
}
Expand Down Expand Up @@ -140,10 +138,11 @@ public boolean filterIssue(final JSONObject issue) {
return true; // Parse all issues by default
}

private Issue createIssueFormJsonObject(final JSONObject issue) {
private Issue createIssueFromJsonObject(final JSONObject issue) {
return new IssueBuilder()
.setFileName(parseFilename(issue))
.setLineStart(parseStart(issue))
.setLineEnd(parseEnd(issue))
.setType(parseType(issue))
.setCategory(CATEGORY_SONAR_QUBE)
.setMessage(parseMessage(issue))
Expand Down Expand Up @@ -198,9 +197,17 @@ private String parseFilename(final JSONObject issue) {
*
* @return the start.
*/
private int parseStart(final JSONObject issue) {
return issue.optInt(ISSUE_LINE, -1);
}
protected abstract int parseStart(JSONObject issue);

/**
* Default parse for end.
*
* @param issue
* the object to parse.
*
* @return the end.
*/
protected abstract int parseEnd(JSONObject issue);

/**
* Default parse for type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import edu.hm.hafner.analysis.AbstractParserTest;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.assertj.SoftAssertions;

import static edu.hm.hafner.analysis.assertj.SoftAssertions.*;
import static org.assertj.core.api.Assertions.*;

Expand All @@ -23,10 +24,11 @@ class SonarQubeIssuesParserTest extends AbstractParserTest {

@Override
protected void assertThatIssuesArePresent(final Report report, final SoftAssertions softly) {
softly.assertThat(report).hasSize(31);
softly.assertThat(report).hasSize(32).hasDuplicatesSize(0);
softly.assertThat(report.get(0))
.hasFileName("src/com/tsystems/sbs/jenkinslib/SbsBuild.groovy")
.hasLineStart(631);
.hasLineStart(631)
.hasLineEnd(631);
}

/**
Expand All @@ -38,11 +40,10 @@ void reportApiMultiModuleTest() {

assertThat(warnings).hasSize(106);

assertSoftly(softly -> {
softly.assertThat(warnings.get(0))
.hasFileName("cart-common-folder/src/main/java/com/example/sonarqube/CloseResource.java")
.hasLineStart(0);
});
assertSoftly(softly -> softly.assertThat(warnings.get(0))
.hasFileName("cart-common-folder/src/main/java/com/example/sonarqube/CloseResource.java")
.hasLineStart(0)
.hasLineEnd(0));
}

@Test
Expand Down