Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Feature/workdir fix master #171

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.sonar</groupId>
<artifactId>sonar-stash-plugin</artifactId>
<version>1.3.0-SNAPSHOT</version>
<version>1.4.0-SNAPSHOT</version>
<packaging>sonar-plugin</packaging>
<description>Integration between Atlassian Stash (BitBucket) and SonarQube</description>
<name>Stash</name>
Expand Down Expand Up @@ -110,7 +110,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>[1.7.21,)</version>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ private void updateStashWithSonarInfo(StashClient stashClient,
int issueThreshold = stashRequestFacade.getIssueThreshold();
PullRequestRef pr = stashRequestFacade.getPullRequest();

// SonarQube objects
List<PostJobIssue> issueReport = stashRequestFacade.extractIssueReport(issues);

StashUser stashUser = stashRequestFacade
.getSonarQubeReviewer(stashCredentials.getUserSlug(), stashClient);

Expand All @@ -92,6 +89,9 @@ private void updateStashWithSonarInfo(StashClient stashClient,
"No Stash differential report available to process the SQ analysis");
}

// SonarQube objects
List<PostJobIssue> issueReport = stashRequestFacade.extractIssueReport(issues, diffReport);

// if requested, reset all comments linked to the pull-request
if (config.resetComments()) {
stashRequestFacade.resetComments(pr, diffReport, stashUser, stashClient);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package org.sonar.plugins.stash;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.bootstrap.ProjectBuilder;

import java.io.File;

public class StashProjectBuilder extends ProjectBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(StashProjectBuilder.class);

private File projectBaseDir;

@Override
public void build(Context context) {
projectBaseDir = context.projectReactor().getRoot().getBaseDir();
LOGGER.debug("Project base dir is {}", projectBaseDir);
}

public File getProjectBaseDir() {
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/org/sonar/plugins/stash/StashRequestFacade.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.sonar.plugins.stash;

import java.io.File;
import java.util.Collection;
import java.util.Optional;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.BatchSide;
Expand Down Expand Up @@ -47,9 +48,9 @@ public StashRequestFacade(StashPluginConfiguration stashPluginConfiguration,
this.projectBaseDir = projectBuilder.getProjectBaseDir();
}

public List<PostJobIssue> extractIssueReport(Iterable<PostJobIssue> issues) {
public List<PostJobIssue> extractIssueReport(Iterable<PostJobIssue> issues, StashDiffReport diffReport) {
return SonarQubeCollector.extractIssueReport(
issues, this, config.includeExistingIssues(), config.excludedRules()
issues, this, diffReport, config.includeExistingIssues(), config.excludedRules(), config.issueVicinityRange()
);
}

Expand Down Expand Up @@ -437,6 +438,9 @@ public void resetComments(PullRequestRef pr,
@Override
public String getIssuePath(PostJobIssue issue) {
InputComponent ip = issue.inputComponent();

LOGGER.debug("Input component {}", ip);

if (ip == null || !ip.isFile()) {
return null;
}
Expand All @@ -446,6 +450,11 @@ public String getIssuePath(PostJobIssue issue) {
.getRepositoryRoot()
.orElse(projectBaseDir);

LOGGER.debug("Base dir is {}", baseDir);
LOGGER.debug("ip file {}", inputFile.file());
LOGGER.debug("ip abs path {}", inputFile.absolutePath());
LOGGER.debug("ip path {}", inputFile.path());
LOGGER.debug("ip relative path {}", inputFile.relativePath());

return new PathResolver().relativePath(baseDir, inputFile.file());
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/sonar/plugins/stash/issue/StashDiff.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.sonar.plugins.stash.issue;

import org.sonar.plugins.stash.StashPlugin;

import java.util.ArrayList;
import java.util.List;

import org.sonar.plugins.stash.StashPlugin.IssueType;

public class StashDiff {
Expand Down Expand Up @@ -49,4 +48,10 @@ public List<StashComment> 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 + "]";
}
}
20 changes: 15 additions & 5 deletions src/main/java/org/sonar/plugins/stash/issue/StashDiffReport.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
Expand Down Expand Up @@ -104,4 +104,14 @@ public List<StashComment> getComments() {
}
return result;
}
}

public boolean hasPath(String path) {
for (StashDiff diff : diffs) {
if (Objects.equals(diff.getPath(), path)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -24,44 +26,79 @@ private SonarQubeCollector() {
/**
* Create issue report according to issue list generated during SonarQube
* analysis.
* @param diffReport
* @param i
*/
public static List<PostJobIssue> extractIssueReport(
Iterable<PostJobIssue> issues, IssuePathResolver issuePathResolver,
boolean includeExistingIssues, Set<RuleKey> excludedRules) {
StashDiffReport diffReport, boolean includeExistingIssues, Set<RuleKey> 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<RuleKey> excludedRules
StashDiffReport diffReport, boolean includeExistingIssues, Set<RuleKey> 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;
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -130,7 +131,7 @@ public void testExecuteOnWithReachedThreshold() throws Exception {

List<PostJobIssue> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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<StashComment> comments = report1.getComments();
Expand All @@ -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);
}

}
Loading