From 0cb79873889c405848943a508b457eeebebef793 Mon Sep 17 00:00:00 2001 From: David Renz Date: Fri, 20 May 2016 12:46:06 +0200 Subject: [PATCH] Fixed #40 --- .../pmd/renderers/CodeClimateRenderer.java | 36 ++++++++++++------- .../renderers/CodeClimateRendererTest.java | 6 ++-- 2 files changed, 26 insertions(+), 16 deletions(-) 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 482f321ee9..1feac29a4c 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 @@ -15,6 +15,7 @@ import java.util.Iterator; import org.apache.commons.lang3.StringUtils; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import net.sourceforge.pmd.PMD; import net.sourceforge.pmd.PropertyDescriptor; @@ -43,12 +44,12 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { @Override public void renderFileViolations(Iterator violations) throws IOException { Writer writer = getWriter(); - Gson gson = new Gson(); + Gson gson = new GsonBuilder().disableHtmlEscaping().create(); while (violations.hasNext()) { RuleViolation rv = violations.next(); String json = gson.toJson(asIssue(rv)); - json = json.replaceAll(BODY_PLACEHOLDER, getBody(rv)); + json = json.replace(BODY_PLACEHOLDER, getBody(rv)); writer.write(json + NULL_CHARACTER + PMD.EOL); } } @@ -111,28 +112,37 @@ public class CodeClimateRenderer extends AbstractIncrementingRenderer { private String getBody(RuleViolation rv) { Rule rule = rv.getRule(); - String result = "## " + rule.getName() + PMD.EOL + PMD.EOL + - "Since: PMD " + rule.getSince() + PMD.EOL + PMD.EOL + - "Priority: " + rule.getPriority() + PMD.EOL + PMD.EOL + - "[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): " + Arrays.toString(getCategories(rule)).replaceAll("[\\[\\]]","") + PMD.EOL + PMD.EOL + - "[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): " + getRemediationPoints(rule) + PMD.EOL + PMD.EOL + + String result = "## " + rule.getName() + "\\n\\n" + + "Since: PMD " + rule.getSince() + "\\n\\n" + + "Priority: " + rule.getPriority() + "\\n\\n" + + "[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): " + Arrays.toString(getCategories(rule)).replaceAll("[\\[\\]]","") + "\\n\\n" + + "[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): " + getRemediationPoints(rule) + "\\n\\n" + cleaned(rule.getDescription()); if(!rule.getExamples().isEmpty()) { - result += PMD.EOL + PMD.EOL + "Example(s):" + PMD.EOL + PMD.EOL; + result += "\\n\\n### Example:\\n\\n"; for(String snippet : rule.getExamples()) { - result += "```java " + PMD.EOL + snippet + PMD.EOL + "``` "; + snippet = snippet.replaceAll("\\n", "\\\\n"); + snippet = snippet.replaceAll("\\t", "\\\\t"); + result += "```java\\n" + snippet + "\\n``` "; } } if(!rule.getPropertyDescriptors().isEmpty()) { - result += PMD.EOL + PMD.EOL + "This rule has the following properties:" + PMD.EOL + PMD.EOL; - result += "Name | Default Value | Description" + PMD.EOL; - result += "--- | --- | ---" + PMD.EOL; + result += "\\n\\n### [PMD properties](http://pmd.github.io/pmd-5.1.3/pmd-developer.html)\\n\\n"; + result += "Name | Default Value | Description\\n"; + result += "--- | --- | ---\\n"; for(PropertyDescriptor property : rule.getPropertyDescriptors()) { - result += property.name() + " | " + property.defaultValue() + " | " + property.description() + PMD.EOL; + String defaultValue; + try { + defaultValue = Arrays.toString((String[])property.defaultValue()).replaceAll("[\\[\\]]",""); + } + catch(Exception ignore) { + defaultValue = property.defaultValue().toString(); + } + result += property.name() + " | " + defaultValue + " | " + property.description() + "\\n"; } } 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 26c1e065ab..e30a541496 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\":\"## Foo" + PMD.EOL + PMD.EOL + "Since: PMD null" + PMD.EOL + PMD.EOL + "Priority: Low" + PMD.EOL + PMD.EOL + "[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): Style" + PMD.EOL + PMD.EOL + "[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): 50000" + PMD.EOL + PMD.EOL + "desc" + PMD.EOL + PMD.EOL + "This rule has the following properties:" + PMD.EOL + PMD.EOL + "Name | Default Value | Description" + PMD.EOL + "--- | --- | ---" + PMD.EOL + "violationSuppressRegex | null | Suppress violations with messages matching a regular expression" + PMD.EOL + "violationSuppressXPath | null | Suppress violations on nodes which match a given relative XPath expression." + PMD.EOL + "\"},\"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\":\"## Foo\\n\\nSince: PMD null\\n\\nPriority: Low\\n\\n[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): Style\\n\\n[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): 50000\\n\\ndesc\\n\\n### [PMD properties](http://pmd.github.io/pmd-5.1.3/pmd-developer.html)\\n\\nName | Default Value | Description\\n--- | --- | ---\\nviolationSuppressRegex | null | Suppress violations with messages matching a regular expression\\nviolationSuppressXPath | null | Suppress violations on nodes which match a given relative XPath expression.\\n\"},\"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\":\"## Foo" + PMD.EOL + PMD.EOL + "Since: PMD null" + PMD.EOL + PMD.EOL + "Priority: Low" + PMD.EOL + PMD.EOL + "[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): Style" + PMD.EOL + PMD.EOL + "[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): 50000" + PMD.EOL + PMD.EOL + "desc" + PMD.EOL + PMD.EOL + "This rule has the following properties:" + PMD.EOL + PMD.EOL + "Name | Default Value | Description" + PMD.EOL + "--- | --- | ---" + PMD.EOL + "violationSuppressRegex | null | Suppress violations with messages matching a regular expression" + PMD.EOL + "violationSuppressXPath | null | Suppress violations on nodes which match a given relative XPath expression." + PMD.EOL + "\"},\"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\":\"## Foo" + PMD.EOL + PMD.EOL + "Since: PMD null" + PMD.EOL + PMD.EOL + "Priority: Low" + PMD.EOL + PMD.EOL + "[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): Style" + PMD.EOL + PMD.EOL + "[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): 50000" + PMD.EOL + PMD.EOL + "desc" + PMD.EOL + PMD.EOL + "This rule has the following properties:" + PMD.EOL + PMD.EOL + "Name | Default Value | Description" + PMD.EOL + "--- | --- | ---" + PMD.EOL + "violationSuppressRegex | null | Suppress violations with messages matching a regular expression" + PMD.EOL + "violationSuppressXPath | null | Suppress violations on nodes which match a given relative XPath expression." + PMD.EOL + "\"},\"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\":\"## Foo\\n\\nSince: PMD null\\n\\nPriority: Low\\n\\n[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): Style\\n\\n[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): 50000\\n\\ndesc\\n\\n### [PMD properties](http://pmd.github.io/pmd-5.1.3/pmd-developer.html)\\n\\nName | Default Value | Description\\n--- | --- | ---\\nviolationSuppressRegex | null | Suppress violations with messages matching a regular expression\\nviolationSuppressXPath | null | Suppress violations on nodes which match a given relative XPath expression.\\n\"},\"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\":\"## Foo\\n\\nSince: PMD null\\n\\nPriority: Low\\n\\n[Categories](https://github.com/codeclimate/spec/blob/master/SPEC.md#categories): Style\\n\\n[Remediation Points](https://github.com/codeclimate/spec/blob/master/SPEC.md#remediation-points): 50000\\n\\ndesc\\n\\n### [PMD properties](http://pmd.github.io/pmd-5.1.3/pmd-developer.html)\\n\\nName | Default Value | Description\\n--- | --- | ---\\nviolationSuppressRegex | null | Suppress violations with messages matching a regular expression\\nviolationSuppressXPath | null | Suppress violations on nodes which match a given relative XPath expression.\\n\"},\"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() {