Skip to content
Permalink
Browse files

JENKINS-31006 refactoring

  • Loading branch information...
aquarellian committed Oct 18, 2015
1 parent c18bba6 commit abe662928017438497f99b8bf04cb3614d6e5fbf
@@ -228,7 +228,8 @@ public boolean apply(@Nullable String input) {
}

// Step 6 - Send review to Gerrit
ReviewInput reviewInput = getReviewResult(file2issues, gerritServerName);
Collection<String> categoryNames = getCategoryNames(gerritConfig.getCategories());
ReviewInput reviewInput = getReviewResult(file2issues, categoryNames);

// Step 7 - Post review
revision.review(reviewInput);
@@ -271,7 +272,7 @@ private int parseNumber(String number, int deflt) {
}

@VisibleForTesting
ReviewInput getReviewResult(Multimap<String, Issue> finalIssues, String gerritServerName) {
ReviewInput getReviewResult(Multimap<String, Issue> finalIssues, Collection<String> existingCategories) {
String reviewMessage = getReviewMessage(finalIssues);
ReviewInput reviewInput = new ReviewInput().message(reviewMessage);

@@ -299,20 +300,23 @@ ReviewInput getReviewResult(Multimap<String, Issue> finalIssues, String gerritSe
);
}
if (postScore) {
String realCategory = getRealCategory(gerritServerName);
String realCategory = getRealCategory(existingCategories);
reviewInput.label(realCategory, getReviewMark(finalIssues));
}
return reviewInput;
}

protected String getRealCategory(String gerritServerName) {
IGerritHudsonTriggerConfig config = GerritManagement.getConfig(gerritServerName);
private Collection<String> getCategoryNames(List<VerdictCategory> categories){
Set<String> availableCategories = new HashSet<String>();
for (VerdictCategory verdictCategory : config.getCategories()) {
for (VerdictCategory verdictCategory : categories) {
availableCategories.add(verdictCategory.getVerdictDescription());
}
// todo notify user about switching category
return availableCategories.contains(category) ? category : DEFAULT_CATEGORY;
return availableCategories;
}

protected String getRealCategory(Collection<String> categories) {
// todo notify user about switching category to default?
return categories.contains(category) ? category : DEFAULT_CATEGORY;
}

@VisibleForTesting
@@ -10,6 +10,7 @@
import com.google.gerrit.extensions.common.MergeableInfo;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.sonyericsson.hudson.plugins.gerrit.trigger.VerdictCategory;
import hudson.FilePath;
import junit.framework.Assert;
import org.jenkinsci.plugins.sonargerrit.data.SonarReportBuilder;
@@ -166,49 +167,60 @@ public void testFilterIssuesByChangedLines() throws InterruptedException, IOExce
@Test
public void getReviewResultTest() throws InterruptedException, IOException, URISyntaxException, RestApiException {
Multimap<String, Issue> finalIssues = LinkedListMultimap.create();
List<String> categories = Collections.singletonList("Test");

finalIssues.put("guice-bootstrap/src/main/java/com/magenta/guice/bootstrap/plugins/PluginsManager.java", new DummyIssue());
finalIssues.put("guice-bootstrap/src/main/java/com/magenta/guice/bootstrap/plugins/PluginsManager.java", new DummyIssue());
SonarToGerritBuilder builder = new DummySonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
SonarToGerritBuilder builder = new SonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
"No Issues Header", "Some Issues Header", "Issue Comment", true, "Test", "+1", "-1");
ReviewInput reviewResult = builder.getReviewResult(finalIssues, "");
ReviewInput reviewResult = builder.getReviewResult(finalIssues, categories);
Assert.assertEquals("Some Issues Header", reviewResult.message);
Assert.assertEquals(1, reviewResult.comments.size());
Assert.assertEquals(1, reviewResult.labels.size());
Assert.assertEquals(-1, reviewResult.labels.get("Test").intValue());

builder = new DummySonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
builder = new SonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
"No Issues Header", "Some Issues Header", "Issue Comment", false, "Test", "1", "-1");
reviewResult = builder.getReviewResult(finalIssues, "");
reviewResult = builder.getReviewResult(finalIssues, categories);
Assert.assertEquals("Some Issues Header", reviewResult.message);
Assert.assertEquals(1, reviewResult.comments.size());
Assert.assertEquals(null, reviewResult.labels);

builder = new DummySonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
builder = new SonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
"No Issues Header", "Some Issues Header", "Issue Comment", true, "Test", "0", "0");
reviewResult = builder.getReviewResult(finalIssues, "");
reviewResult = builder.getReviewResult(finalIssues, categories);
Assert.assertEquals("Some Issues Header", reviewResult.message);
Assert.assertEquals(1, reviewResult.comments.size());
Assert.assertEquals(1, reviewResult.labels.size());
Assert.assertEquals(0, reviewResult.labels.get("Test").intValue());

builder = new DummySonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
builder = new SonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
"No Issues Header", "Some Issues Header", "Issue Comment", true, "Test", "1test", "-1test");
reviewResult = builder.getReviewResult(finalIssues, "");
reviewResult = builder.getReviewResult(finalIssues, categories);
Assert.assertEquals("Some Issues Header", reviewResult.message);
Assert.assertEquals(1, reviewResult.comments.size());
Assert.assertEquals(1, reviewResult.labels.size());
Assert.assertEquals(0, reviewResult.labels.get("Test").intValue());

builder = new DummySonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
builder = new SonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
"No Issues Header", "Some Issues Header", "Issue Comment", true, "Test", "1", "-1");
finalIssues = LinkedListMultimap.create();
reviewResult = builder.getReviewResult(finalIssues, "");
reviewResult = builder.getReviewResult(finalIssues, categories);
Assert.assertEquals("No Issues Header", reviewResult.message);
Assert.assertEquals(0, reviewResult.comments.size());
Assert.assertEquals(1, reviewResult.labels.size());
Assert.assertEquals(+1, reviewResult.labels.get("Test").intValue());

categories = new ArrayList<String>();
builder = new SonarToGerritBuilder("", "", "", Severity.INFO.name(), true, false,
"No Issues Header", "Some Issues Header", "Issue Comment", true, "Test", "1", "-1");
finalIssues = LinkedListMultimap.create();
reviewResult = builder.getReviewResult(finalIssues, categories);
Assert.assertEquals("No Issues Header", reviewResult.message);
Assert.assertEquals(0, reviewResult.comments.size());
Assert.assertEquals(1, reviewResult.labels.size());
Assert.assertEquals(+1, reviewResult.labels.get("Code-Review").intValue());

}

private Report readreport() throws IOException, InterruptedException, URISyntaxException {
@@ -220,18 +232,6 @@ private Report readreport() throws IOException, InterruptedException, URISyntaxE
return new SonarReportBuilder().fromJson(json);
}


private class DummySonarToGerritBuilder extends SonarToGerritBuilder {
public DummySonarToGerritBuilder(String projectPath, String sonarURL, String path, String severity, boolean changedLinesOnly, boolean newIssuesOnly, String noIssuesToPostText, String someIssuesToPostText, String issueComment, boolean postScore, String category, String noIssuesScore, String issuesScore) {
super(projectPath, sonarURL, path, severity, changedLinesOnly, newIssuesOnly, noIssuesToPostText, someIssuesToPostText, issueComment, postScore, category, noIssuesScore, issuesScore);
}

@Override
protected String getRealCategory(String gerritServerName) {
return getCategory();
}
}

private class DummyIssue extends Issue {
@Override
public Severity getSeverity() {

0 comments on commit abe6629

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.