getComments() {
public boolean containsComment(long commentId) {
return comments.stream().anyMatch(c -> c.getId() == commentId);
}
+
+ @Override
+ public String toString() {
+ return "StashDiff [type=" + type + ", path=" + path + ", source=" + source + ", destination=" + destination
+ + ", comments=" + comments + "]";
+ }
}
diff --git a/src/main/java/org/sonar/plugins/stash/issue/StashDiffReport.java b/src/main/java/org/sonar/plugins/stash/issue/StashDiffReport.java
index 471552df..7be790b2 100644
--- a/src/main/java/org/sonar/plugins/stash/issue/StashDiffReport.java
+++ b/src/main/java/org/sonar/plugins/stash/issue/StashDiffReport.java
@@ -1,13 +1,13 @@
package org.sonar.plugins.stash.issue;
-import com.google.common.collect.Range;
-import java.util.Objects;
-import org.sonar.plugins.stash.StashPlugin;
-
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
+
import org.sonar.plugins.stash.StashPlugin.IssueType;
+import com.google.common.collect.Range;
+
/**
* This class is a representation of the Stash Diff view.
*
@@ -104,4 +104,14 @@ public List getComments() {
}
return result;
}
-}
+
+ public boolean hasPath(String path) {
+ for (StashDiff diff : diffs) {
+ if (Objects.equals(diff.getPath(), path)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/sonar/plugins/stash/issue/collector/SonarQubeCollector.java b/src/main/java/org/sonar/plugins/stash/issue/collector/SonarQubeCollector.java
index 741c3620..462cf4e6 100644
--- a/src/main/java/org/sonar/plugins/stash/issue/collector/SonarQubeCollector.java
+++ b/src/main/java/org/sonar/plugins/stash/issue/collector/SonarQubeCollector.java
@@ -12,6 +12,8 @@
import org.sonar.api.batch.postjob.issue.PostJobIssue;
import org.sonar.api.rule.RuleKey;
import org.sonar.plugins.stash.IssuePathResolver;
+import org.sonar.plugins.stash.StashPlugin.IssueType;
+import org.sonar.plugins.stash.issue.StashDiffReport;
public final class SonarQubeCollector {
@@ -24,33 +26,40 @@ private SonarQubeCollector() {
/**
* Create issue report according to issue list generated during SonarQube
* analysis.
+ * @param diffReport
+ * @param i
*/
public static List extractIssueReport(
Iterable issues, IssuePathResolver issuePathResolver,
- boolean includeExistingIssues, Set excludedRules) {
+ StashDiffReport diffReport, boolean includeExistingIssues, Set excludedRules, int issueVicinityRange) {
return StreamSupport.stream(
issues.spliterator(), false)
.filter(issue -> shouldIncludeIssue(
issue, issuePathResolver,
- includeExistingIssues, excludedRules
+ diffReport,
+ includeExistingIssues, excludedRules, issueVicinityRange
))
.collect(Collectors.toList());
}
static boolean shouldIncludeIssue(
PostJobIssue issue, IssuePathResolver issuePathResolver,
- boolean includeExistingIssues, Set excludedRules
+ StashDiffReport diffReport, boolean includeExistingIssues, Set excludedRules, int issueVicinityRange
) {
if (!includeExistingIssues && !issue.isNew()) {
if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Issue {} is not a new issue and so, not added to the report", issue.key());
+ LOGGER.debug("Issue {} is not a new issue and so, NOT ADDED to the report"
+ + ", issue.componentKey = {}, issue.key = {}, issue.ruleKey = {}, issue.message = {}, issue.line = {}",
+ issue, issue.componentKey(), issue.key(), issue.ruleKey(), issue.message(), issue.line());
}
return false;
}
if (excludedRules.contains(issue.ruleKey())) {
if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Issue {} is ignored, not added to the report", issue.key());
+ LOGGER.debug("Issue {} is ignored, NOT ADDED to the report"
+ + ", issue.componentKey = {}, issue.key = {}, issue.ruleKey = {}, issue.message = {}, issue.line = {}",
+ issue, issue.componentKey(), issue.key(), issue.ruleKey(), issue.message(), issue.line());
}
return false;
}
@@ -58,10 +67,38 @@ static boolean shouldIncludeIssue(
String path = issuePathResolver.getIssuePath(issue);
if (!isProjectWide(issue) && path == null) {
if (LOGGER.isDebugEnabled()) {
- LOGGER.debug("Issue {} is not linked to a file, not added to the report", issue.key());
+ LOGGER.debug("Issue {} is not linked to a file, NOT ADDED to the report"
+ + ", issue.componentKey = {}, issue.key = {}, issue.ruleKey = {}, issue.message = {}, issue.line = {}",
+ issue, issue.componentKey(), issue.key(), issue.ruleKey(), issue.message(), issue.line());
}
return false;
}
+
+ if (!diffReport.hasPath(path)) {
+ LOGGER.debug("Issue {} is not linked to a diff by path, NOT ADDED to the report"
+ + ", issue.componentKey = {}, issue.key = {}, issue.ruleKey = {}, issue.message = {}, issue.line = {}",
+ issue, issue.componentKey(), issue.key(), issue.ruleKey(), issue.message(), issue.line());
+ return false;
+ }
+
+ Integer issueLine = issue.line();
+ if (issueLine == null) {
+ issueLine = 0;
+ }
+
+ // check if issue belongs to the Stash diff view
+ IssueType type = diffReport.getType(path, issueLine, issueVicinityRange);
+ if (type == null) {
+ LOGGER.debug("Issue {} is not linked to a diff, NOT ADDED to the report"
+ + ", issue.componentKey = {}, issue.key = {}, issue.ruleKey = {}, issue.message = {}, issue.line = {}", issue,
+ issue.componentKey(), issue.key(), issue.ruleKey(), issue.message(), issue.line());
+ return false;
+ }
+
+
+ LOGGER.debug("Issue {} is ADDED to the report"
+ + ", issue.componentKey = {}, issue.key = {}, issue.ruleKey = {}, issue.message = {}, issue.line = {}",
+ issue, issue.componentKey(), issue.key(), issue.ruleKey(), issue.message(), issue.line());
return true;
}
}
diff --git a/src/test/java/org/sonar/plugins/stash/StashIssueReportingPostJobTest.java b/src/test/java/org/sonar/plugins/stash/StashIssueReportingPostJobTest.java
index d7dd4b97..2e18ff3f 100755
--- a/src/test/java/org/sonar/plugins/stash/StashIssueReportingPostJobTest.java
+++ b/src/test/java/org/sonar/plugins/stash/StashIssueReportingPostJobTest.java
@@ -2,6 +2,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
@@ -84,7 +85,7 @@ public void setUp() throws Exception {
when(config.includeAnalysisOverview()).thenReturn(Boolean.TRUE);
when(report.size()).thenReturn(10);
- when(stashRequestFacade.extractIssueReport(eq(report)))
+ when(stashRequestFacade.extractIssueReport(eq(report), anyObject()))
.thenReturn(report);
when(context.issues()).thenReturn(report);
@@ -130,7 +131,7 @@ public void testExecuteOnWithReachedThreshold() throws Exception {
List report = mock(ArrayList.class);
when(report.size()).thenReturn(55);
- when(stashRequestFacade.extractIssueReport(eq(report)))
+ when(stashRequestFacade.extractIssueReport(eq(report), anyObject()))
.thenReturn(report);
myJob = new StashIssueReportingPostJob(config, stashRequestFacade, server);
diff --git a/src/test/java/org/sonar/plugins/stash/issue/StashDiffReportTest.java b/src/test/java/org/sonar/plugins/stash/issue/StashDiffReportTest.java
index 3274c39b..3bd3d32c 100755
--- a/src/test/java/org/sonar/plugins/stash/issue/StashDiffReportTest.java
+++ b/src/test/java/org/sonar/plugins/stash/issue/StashDiffReportTest.java
@@ -1,17 +1,15 @@
package org.sonar.plugins.stash.issue;
-import org.hamcrest.core.Is;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.plugins.stash.StashPlugin;
-
-import java.util.List;
-import org.sonar.plugins.stash.StashPlugin.IssueType;
-
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.plugins.stash.StashPlugin.IssueType;
+
public class StashDiffReportTest {
StashDiff diff1;
@@ -28,13 +26,13 @@ public void setUp() {
StashComment comment2 = mock(StashComment.class);
when(comment2.getId()).thenReturn((long)54321);
- diff1 = new StashDiff(IssueType.CONTEXT, "path/to/diff1", (long)10, (long)20);
+ diff1 = new StashDiff(IssueType.CONTEXT, "path/to/diff1", 10, 20);
diff1.addComment(comment1);
- diff2 = new StashDiff(IssueType.ADDED, "path/to/diff2", (long)20, (long)30);
+ diff2 = new StashDiff(IssueType.ADDED, "path/to/diff2", 20, 30);
diff2.addComment(comment2);
- diff3 = new StashDiff(IssueType.CONTEXT, "path/to/diff3", (long)30, (long)40);
+ diff3 = new StashDiff(IssueType.CONTEXT, "path/to/diff3", 30, 40);
report1.add(diff1);
report1.add(diff2);
@@ -131,13 +129,13 @@ public void testGetCommentsWithoutAnyIssues() {
@Test
public void testGetCommentsWithDuplicatedComments() {
- StashComment comment1 = new StashComment((long)12345, "message", "path", (long)1, mock(StashUser.class), (long)1);
+ StashComment comment1 = new StashComment(12345, "message", "path", (long)1, mock(StashUser.class), 1);
diff1.addComment(comment1);
- StashComment comment2 = new StashComment((long)12345, "message", "path", (long)1, mock(StashUser.class), (long)1);
+ StashComment comment2 = new StashComment(12345, "message", "path", (long)1, mock(StashUser.class), 1);
diff2.addComment(comment2);
- StashComment comment3 = new StashComment((long)54321, "message", "path", (long)1, mock(StashUser.class), (long)1);
+ StashComment comment3 = new StashComment(54321, "message", "path", (long)1, mock(StashUser.class), 1);
diff3.addComment(comment3);
List comments = report1.getComments();
@@ -147,4 +145,12 @@ public void testGetCommentsWithDuplicatedComments() {
assertEquals(54321, comments.get(1).getId());
}
+ @Test
+ public void testHasPath() {
+ assertEquals(report1.hasPath("path/to/diff1"), true);
+ assertEquals(report1.hasPath("path/to/diff2"), true);
+ assertEquals(report1.hasPath("path/to/diff3"), true);
+ assertEquals(report1.hasPath("path/to/diff4"), false);
+ }
+
}
diff --git a/src/test/java/org/sonar/plugins/stash/issue/collector/SonarQubeCollectorTest.java b/src/test/java/org/sonar/plugins/stash/issue/collector/SonarQubeCollectorTest.java
index 7f65f518..86af350c 100755
--- a/src/test/java/org/sonar/plugins/stash/issue/collector/SonarQubeCollectorTest.java
+++ b/src/test/java/org/sonar/plugins/stash/issue/collector/SonarQubeCollectorTest.java
@@ -1,25 +1,25 @@
package org.sonar.plugins.stash.issue.collector;
-import java.util.HashSet;
-import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.anyObject;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.plugins.stash.StashPluginUtils.countIssuesBySeverity;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
+
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
-import org.sonar.api.batch.fs.FilePredicate;
-import org.sonar.api.batch.fs.FilePredicates;
-import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
@@ -28,7 +28,9 @@
import org.sonar.api.rule.RuleKey;
import org.sonar.plugins.stash.DefaultIssue;
import org.sonar.plugins.stash.IssuePathResolver;
+import org.sonar.plugins.stash.StashPlugin.IssueType;
import org.sonar.plugins.stash.fixtures.DummyIssuePathResolver;
+import org.sonar.plugins.stash.issue.StashDiffReport;
@RunWith(MockitoJUnitRunner.class)
public class SonarQubeCollectorTest {
@@ -48,6 +50,9 @@ public class SonarQubeCollectorTest {
Set excludedRules;
IssuePathResolver ipr = new DummyIssuePathResolver();
+
+ @Mock
+ StashDiffReport stashDiffReport;
@Before
public void setUp() throws Exception {
@@ -90,7 +95,7 @@ public void setUp() throws Exception {
public void testExtractEmptyIssueReport() {
ArrayList issues = new ArrayList<>();
- List report = SonarQubeCollector.extractIssueReport(issues, ipr, false, excludedRules);
+ List report = SonarQubeCollector.extractIssueReport(issues, ipr, stashDiffReport, false, excludedRules, 0);
assertEquals(0, report.size());
}
@@ -100,7 +105,11 @@ public void testExtractIssueReport() {
issues.add(issue1);
issues.add(issue2);
- List report = SonarQubeCollector.extractIssueReport(issues, ipr, false, excludedRules);
+ when(stashDiffReport.hasPath("project/path1")).thenReturn(true);
+ when(stashDiffReport.hasPath("project/path2")).thenReturn(true);
+ when(stashDiffReport.getType(anyString(), anyLong(), anyInt())).thenReturn(IssueType.CONTEXT);
+
+ List report = SonarQubeCollector.extractIssueReport(issues, ipr, stashDiffReport, false, excludedRules, 0);
assertEquals(2, report.size());
assertEquals(1, countIssuesBySeverity(report, Severity.BLOCKER));
assertEquals(1, countIssuesBySeverity(report, Severity.CRITICAL));
@@ -116,6 +125,16 @@ public void testExtractIssueReport() {
assertEquals((Integer) 2, sqIssue2.line());
}
+
+ @Test
+ public void testExtractIssueReportIssuesNotInDiff() {
+ ArrayList issues = new ArrayList<>();
+ issues.add(issue1);
+ issues.add(issue2);
+
+ List report = SonarQubeCollector.extractIssueReport(issues, ipr, stashDiffReport, false, excludedRules, 0);
+ assertEquals(0, report.size());
+ }
@Test
public void testExtractIssueReportWithNoLine() {
@@ -123,8 +142,11 @@ public void testExtractIssueReportWithNoLine() {
ArrayList issues = new ArrayList<>();
issues.add(issue1);
+
+ when(stashDiffReport.hasPath("project/path1")).thenReturn(true);
+ when(stashDiffReport.getType(anyString(), anyLong(), anyInt())).thenReturn(IssueType.CONTEXT);
- List report = SonarQubeCollector.extractIssueReport(issues, ipr, false, excludedRules);
+ List report = SonarQubeCollector.extractIssueReport(issues, ipr, stashDiffReport, false, excludedRules, 0);
assertEquals(1, report.size());
assertEquals(1, countIssuesBySeverity(report, Severity.BLOCKER));
assertEquals(0, countIssuesBySeverity(report, Severity.CRITICAL));
@@ -143,8 +165,12 @@ public void testExtractIssueReportWithOldOption() {
ArrayList issues = new ArrayList<>();
issues.add(issue1);
issues.add(issue2);
+
+ when(stashDiffReport.hasPath("project/path1")).thenReturn(true);
+ when(stashDiffReport.hasPath("project/path2")).thenReturn(true);
+ when(stashDiffReport.getType(anyString(), anyLong(), anyInt())).thenReturn(IssueType.CONTEXT);
- List report = SonarQubeCollector.extractIssueReport(issues, ipr, false, excludedRules);
+ List report = SonarQubeCollector.extractIssueReport(issues, ipr, stashDiffReport, false, excludedRules, 0);
assertEquals(1, report.size());
assertEquals(0, countIssuesBySeverity(report, Severity.BLOCKER));
assertEquals(1, countIssuesBySeverity(report, Severity.CRITICAL));
@@ -157,8 +183,12 @@ public void testExtractIssueReportWithOneIssueWithoutInputFile() {
ArrayList issues = new ArrayList<>();
issues.add(issue1);
issues.add(issue2);
+
+ when(stashDiffReport.hasPath("project/path1")).thenReturn(true);
+ when(stashDiffReport.hasPath("project/path2")).thenReturn(true);
+ when(stashDiffReport.getType(anyString(), anyLong(), anyInt())).thenReturn(IssueType.CONTEXT);
- List report = SonarQubeCollector.extractIssueReport(issues, ipr, false, excludedRules);
+ List report = SonarQubeCollector.extractIssueReport(issues, ipr, stashDiffReport, false, excludedRules, 0);
assertEquals(1, report.size());
assertEquals(0, countIssuesBySeverity(report, Severity.BLOCKER));
assertEquals(1, countIssuesBySeverity(report, Severity.CRITICAL));
@@ -177,8 +207,12 @@ public void testExtractIssueReportWithIncludeExistingIssuesOption() {
ArrayList issues = new ArrayList<>();
issues.add(issue1);
issues.add(issue2);
+
+ when(stashDiffReport.hasPath("project/path1")).thenReturn(true);
+ when(stashDiffReport.hasPath("project/path2")).thenReturn(true);
+ when(stashDiffReport.getType(anyString(), anyLong(), anyInt())).thenReturn(IssueType.CONTEXT);
- List report = SonarQubeCollector.extractIssueReport(issues, ipr, true, excludedRules);
+ List report = SonarQubeCollector.extractIssueReport(issues, ipr, stashDiffReport, true, excludedRules, 0);
assertEquals(2, report.size());
}
@@ -190,10 +224,14 @@ public void testExtractIssueReportWithExcludedRules() {
ArrayList issues = new ArrayList<>();
issues.add(issue1);
issues.add(issue2);
+
+ when(stashDiffReport.hasPath("project/path1")).thenReturn(true);
+ when(stashDiffReport.hasPath("project/path2")).thenReturn(true);
+ when(stashDiffReport.getType(anyString(), anyLong(), anyInt())).thenReturn(IssueType.CONTEXT);
excludedRules.add(RuleKey.of("foo", "bar"));
- List report = SonarQubeCollector.extractIssueReport(issues, ipr, true, excludedRules);
+ List report = SonarQubeCollector.extractIssueReport(issues, ipr, stashDiffReport, true, excludedRules, 0);
assertEquals(1, report.size());
assertEquals("key2", report.get(0).key());
}
@@ -202,25 +240,28 @@ public void testExtractIssueReportWithExcludedRules() {
public void testShouldIncludeIssue() {
Set er = new HashSet<>();
InputComponent ic = new DefaultInputFile("module", "some/path");
+
+ when(stashDiffReport.hasPath("some/path")).thenReturn(true);
+ when(stashDiffReport.getType(anyString(), anyLong(), anyInt())).thenReturn(IssueType.CONTEXT);
assertFalse(
SonarQubeCollector.shouldIncludeIssue(
- new DefaultIssue().setNew(false).setInputComponent(ic), ipr, false, er
+ new DefaultIssue().setNew(false).setInputComponent(ic), ipr, stashDiffReport, false, er, 0
)
);
assertTrue(
SonarQubeCollector.shouldIncludeIssue(
- new DefaultIssue().setNew(false).setInputComponent(ic), ipr, true, er
+ new DefaultIssue().setNew(false).setInputComponent(ic), ipr, stashDiffReport, true, er, 0
)
);
assertTrue(
SonarQubeCollector.shouldIncludeIssue(
- new DefaultIssue().setNew(true).setInputComponent(ic), ipr, false, er
+ new DefaultIssue().setNew(true).setInputComponent(ic), ipr, stashDiffReport, false, er, 0
)
);
assertTrue(
SonarQubeCollector.shouldIncludeIssue(
- new DefaultIssue().setNew(true).setInputComponent(ic), ipr, true, er
+ new DefaultIssue().setNew(true).setInputComponent(ic), ipr, stashDiffReport, true, er, 0
)
);
}