diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java index 9b0c00c82f..d9f900a3a6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateRenderer.java @@ -10,12 +10,12 @@ import static net.sourceforge.pmd.renderers.CodeClimateRule.CODECLIMATE_REMEDIAT import java.io.IOException; import java.io.Writer; import java.util.Iterator; -import java.util.List; import org.apache.commons.lang3.StringUtils; import com.google.gson.Gson; +import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RuleViolation; @@ -24,10 +24,10 @@ import net.sourceforge.pmd.RuleViolation; */ public class CodeClimateRenderer extends AbstractIncrementingRenderer { public static final String NAME = "codeclimate"; + public static final String BODY_PLACEHOLDER = "REPLACE_THIS_WITH_MARKDOWN"; public static final int REMEDIATION_POINTS_DEFAULT = 50000; public static final String[] CODECLIMATE_DEFAULT_CATEGORIES = new String[]{ "Style" }; - protected static final String EOL = System.getProperty("line.separator", "\n"); // Note: required by https://github.com/codeclimate/spec/blob/master/SPEC.md protected static final String NULL_CHARACTER = "\u0000"; @@ -45,7 +45,9 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { while (violations.hasNext()) { RuleViolation rv = violations.next(); - writer.write(gson.toJson(asIssue(rv)) + NULL_CHARACTER + EOL); + String json = gson.toJson(asIssue(rv)); + json = json.replaceAll(BODY_PLACEHOLDER, getBody(rv)); + writer.write(json + NULL_CHARACTER + PMD.EOL); } } @@ -60,7 +62,7 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { CodeClimateIssue issue = new CodeClimateIssue(); issue.check_name = rule.getName(); issue.description = cleaned(rv.getDescription()); - issue.content = getContent(rv); + issue.content = new CodeClimateIssue.Content(BODY_PLACEHOLDER); issue.location = getLocation(rv); switch(rule.getPriority()) { @@ -117,20 +119,22 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { return result; } - private CodeClimateIssue.Content getContent(RuleViolation rv) { - String body = "### Description /n/n" + cleaned( rv.getRule().getDescription() ); + private String getBody(RuleViolation rv) { + Rule rule = rv.getRule(); - List examples = rv.getRule().getExamples(); + String result = "### Description" + PMD.EOL + + cleaned( rule.getDescription() ); - if(!examples.isEmpty()) { - body += "\n" + - "### Example\n"; + if(!rule.getExamples().isEmpty()) { + result += PMD.EOL + "### Example" + PMD.EOL; - for(String snippet : examples) { - body += "\n" +"```" + snippet + "```"; + for(String snippet : rule.getExamples()) { + result += "```java " + PMD.EOL + + snippet + PMD.EOL + + "``` "; } } - return new CodeClimateIssue.Content(body); + return result; } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/renderers/CodeClimateRendererTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/renderers/CodeClimateRendererTest.java index bd3eaea679..b3b8c36fe7 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/renderers/CodeClimateRendererTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/renderers/CodeClimateRendererTest.java @@ -14,7 +14,7 @@ public class CodeClimateRendererTest extends AbstractRendererTst { @Override public String getExpected() { - return "{\"type\":\"issue\",\"check_name\":\"Foo\",\"description\":\"blah\",\"content\":{\"body\":\"### Description /n/ndesc\"},\"categories\":[\"Style\"],\"location\":{\"path\":\"n/a\",\"lines\":{\"begin\":1,\"end\":1}},\"severity\":\"info\",\"remediation_points\":50000}" + "\u0000" + PMD.EOL; + return "{\"type\":\"issue\",\"check_name\":\"Foo\",\"description\":\"blah\",\"content\":{\"body\":\"### Description" + PMD.EOL + "desc\"},\"categories\":[\"Style\"],\"location\":{\"path\":\"n/a\",\"lines\":{\"begin\":1,\"end\":1}},\"severity\":\"info\",\"remediation_points\":50000}" + "\u0000" + PMD.EOL; } @Override @@ -24,8 +24,8 @@ public class CodeClimateRendererTest extends AbstractRendererTst { @Override public String getExpectedMultiple() { - return "{\"type\":\"issue\",\"check_name\":\"Foo\",\"description\":\"blah\",\"content\":{\"body\":\"### Description /n/ndesc\"},\"categories\":[\"Style\"],\"location\":{\"path\":\"n/a\",\"lines\":{\"begin\":1,\"end\":1}},\"severity\":\"info\",\"remediation_points\":50000}" + "\u0000" + PMD.EOL + - "{\"type\":\"issue\",\"check_name\":\"Foo\",\"description\":\"blah\",\"content\":{\"body\":\"### Description /n/ndesc\"},\"categories\":[\"Style\"],\"location\":{\"path\":\"n/a\",\"lines\":{\"begin\":1,\"end\":1}},\"severity\":\"info\",\"remediation_points\":50000}" + "\u0000" + PMD.EOL; + return "{\"type\":\"issue\",\"check_name\":\"Foo\",\"description\":\"blah\",\"content\":{\"body\":\"### Description" + PMD.EOL + "desc\"},\"categories\":[\"Style\"],\"location\":{\"path\":\"n/a\",\"lines\":{\"begin\":1,\"end\":1}},\"severity\":\"info\",\"remediation_points\":50000}" + "\u0000" + PMD.EOL + + "{\"type\":\"issue\",\"check_name\":\"Foo\",\"description\":\"blah\",\"content\":{\"body\":\"### Description" + PMD.EOL + "desc\"},\"categories\":[\"Style\"],\"location\":{\"path\":\"n/a\",\"lines\":{\"begin\":1,\"end\":1}},\"severity\":\"info\",\"remediation_points\":50000}" + "\u0000" + PMD.EOL; } public static junit.framework.Test suite() {