From 5bfc5d3448ab978510e5ca31067ab0ac80f37450 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sat, 5 Mar 2016 22:03:49 +0100 Subject: [PATCH] Resharper support #3 --- CHANGELOG.md | 45 +++++++++++- README.md | 1 + build.gradle | 4 ++ .../lib/parsers/ResharperParser.java | 70 +++++++++++++++++++ .../violations/lib/reports/Reporter.java | 4 +- .../bjurr/violations/lib/ResharperTest.java | 64 +++++++++++++++++ src/test/resources/resharper/main.xml | 21 ++++++ 7 files changed, 207 insertions(+), 2 deletions(-) create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/ResharperParser.java create mode 100644 src/test/java/se/bjurr/violations/lib/ResharperTest.java create mode 100644 src/test/resources/resharper/main.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 4381ab61..8bc0f780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,55 @@ Changelog of Git Changelog. ## Unreleased +### GitHub [#1](https://github.com/tomasbjerre/violations-lib/issues/#1) + +**Resharper support** + + +[4b00706b1d9cbc4](https://github.com/tomasbjerre/git-changelog-lib/commit/4b00706b1d9cbc4) Tomas Bjerre *2016-03-05 21:14:33* + +**CPPCheck support** + + +[d92013ebb6f4111](https://github.com/tomasbjerre/git-changelog-lib/commit/d92013ebb6f4111) Tomas Bjerre *2016-03-05 20:28:19* + + +## 1.4 +### No issue + +**Adding possibility to set findbugs messages** + + +[3a8c7089d924848](https://github.com/tomasbjerre/git-changelog-lib/commit/3a8c7089d924848) Tomas Bjerre *2016-03-05 09:34:51* + + +## 1.3 +### No issue + +**Only most specific FindBugs line** + + * Also SLF4J and some debug logging. + +[b50c549861119cf](https://github.com/tomasbjerre/git-changelog-lib/commit/b50c549861119cf) Tomas Bjerre *2016-03-04 17:16:52* + + +## 1.2 ### No issue +**Adding reporter to Violation** + + +[ddddaefba1f1aee](https://github.com/tomasbjerre/git-changelog-lib/commit/ddddaefba1f1aee) Tomas Bjerre *2016-03-04 05:34:23* + +**Adding links to projects using this lib** + + +[b6b4b74339b5008](https://github.com/tomasbjerre/git-changelog-lib/commit/b6b4b74339b5008) Tomas Bjerre *2016-03-03 21:05:58* + **Moving example reports to its own repo** -[7a2e967de70bc6f](https://github.com/tomasbjerre/git-changelog-lib/commit/7a2e967de70bc6f) Tomas Bjerre *2016-03-03 18:36:49* +[344567418ac1167](https://github.com/tomasbjerre/git-changelog-lib/commit/344567418ac1167) Tomas Bjerre *2016-03-03 18:53:24* ## 1.1 diff --git a/README.md b/README.md index 995e81b8..ead13272 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ It supports: * [_CPPCheck_](http://cppcheck.sourceforge.net/) * [_CSSLint_](https://github.com/CSSLint/csslint) * [_JSHint_](http://jshint.com/) + * [_Resharper_](https://www.jetbrains.com/resharper/) Very easy to use with a nice builder pattern ``` diff --git a/build.gradle b/build.gradle index 8a3e1484..0714ed11 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,10 @@ apply plugin: "se.bjurr.gitchangelog.git-changelog-gradle-plugin" task gitChangelogTask(type: se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask) { filePath = "CHANGELOG.md"; templateContent = new File('changelog.mustache').getText('UTF-8'); + removeIssueFromMessage = true + customIssues = [ + [ "GitHub", "#([0-9]+)", "https://github.com/tomasbjerre/violations-lib/issues/\${PATTERN_GROUP}" ] + ] } group = 'se.bjurr.violations' diff --git a/src/main/java/se/bjurr/violations/lib/parsers/ResharperParser.java b/src/main/java/se/bjurr/violations/lib/parsers/ResharperParser.java new file mode 100644 index 00000000..c0925f27 --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/ResharperParser.java @@ -0,0 +1,70 @@ +package se.bjurr.violations.lib.parsers; + +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Maps.newHashMap; +import static se.bjurr.violations.lib.model.SEVERITY.ERROR; +import static se.bjurr.violations.lib.model.SEVERITY.INFO; +import static se.bjurr.violations.lib.model.SEVERITY.WARN; +import static se.bjurr.violations.lib.model.Violation.violationBuilder; +import static se.bjurr.violations.lib.reports.Reporter.RESHARPER; + +import java.io.File; +import java.util.List; +import java.util.Map; + +import se.bjurr.violations.lib.model.SEVERITY; +import se.bjurr.violations.lib.model.Violation; + +import com.google.common.base.Charsets; +import com.google.common.io.Files; + +public class ResharperParser extends ViolationsParser { + + @Override + public List parseFile(File file) throws Exception { + String string = Files.toString(file, Charsets.UTF_8); + List violations = newArrayList(); + List issueTypeChunks = getChunks(string, ""); + Map> issueTypesPerTypeId = newHashMap(); + for (String issueTypesChunk : issueTypeChunks) { + Map issueType = newHashMap(); + String id = getAttribute(issueTypesChunk, "Id"); + issueType.put("category", getAttribute(issueTypesChunk, "Category")); + issueType.put("description", getAttribute(issueTypesChunk, "Description")); + issueType.put("severity", getAttribute(issueTypesChunk, "Severity")); + issueTypesPerTypeId.put(id, issueType); + } + + List issueChunks = getChunks(string, ""); + for (String issueChunk : issueChunks) { + String typeId = getAttribute(issueChunk, "TypeId"); + String filename = getAttribute(issueChunk, "File"); + String message = getAttribute(issueChunk, "Message") + ". " + issueTypesPerTypeId.get(typeId).get("category") + ". " + + issueTypesPerTypeId.get(typeId).get("description"); + Integer line = findIntegerAttribute(issueChunk, "Line").or(0); + String severity = issueTypesPerTypeId.get(typeId).get("severity"); + violations.add(// + violationBuilder()// + .setReporter(RESHARPER)// + .setStartLine(line)// + .setFile(filename)// + .setSeverity(toSeverity(severity))// + .setMessage(message)// + .setRule(typeId)// + .build()// + ); + } + return violations; + } + + public SEVERITY toSeverity(String severity) { + if (severity.equalsIgnoreCase("ERROR")) { + return ERROR; + } + if (severity.equalsIgnoreCase("WARNING")) { + return WARN; + } + return INFO; + } + +} diff --git a/src/main/java/se/bjurr/violations/lib/reports/Reporter.java b/src/main/java/se/bjurr/violations/lib/reports/Reporter.java index 14c7cfaf..8303d8a6 100644 --- a/src/main/java/se/bjurr/violations/lib/reports/Reporter.java +++ b/src/main/java/se/bjurr/violations/lib/reports/Reporter.java @@ -14,6 +14,7 @@ import se.bjurr.violations.lib.parsers.FindbugsParser; import se.bjurr.violations.lib.parsers.JSHintParser; import se.bjurr.violations.lib.parsers.PMDParser; +import se.bjurr.violations.lib.parsers.ResharperParser; import se.bjurr.violations.lib.parsers.ViolationsParser; public enum Reporter { @@ -22,7 +23,8 @@ public enum Reporter { FINDBUGS(new FindbugsParser()), // JSHINT(new JSHintParser()), // PMD(new PMDParser()), // - CPPCHECK(new CPPCheckParser()); + CPPCHECK(new CPPCheckParser()), // + RESHARPER(new ResharperParser()); private static Logger LOG = Logger.getLogger(Reporter.class.getSimpleName()); private ViolationsParser violationsParser; diff --git a/src/test/java/se/bjurr/violations/lib/ResharperTest.java b/src/test/java/se/bjurr/violations/lib/ResharperTest.java new file mode 100644 index 00000000..f4f3ab39 --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/ResharperTest.java @@ -0,0 +1,64 @@ +package se.bjurr.violations.lib; + +import static org.assertj.core.api.Assertions.assertThat; +import static se.bjurr.violations.lib.TestUtils.getRootFolder; +import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi; +import static se.bjurr.violations.lib.model.SEVERITY.INFO; +import static se.bjurr.violations.lib.model.SEVERITY.WARN; +import static se.bjurr.violations.lib.reports.Reporter.RESHARPER; + +import java.util.List; + +import org.junit.Test; + +import se.bjurr.violations.lib.model.Violation; + +public class ResharperTest { + + @Test + public void testThatViolationsCanBeParsed() { + String rootFolder = getRootFolder(); + + List actual = violationsReporterApi() // + .withPattern(".*/resharper/.*\\.xml$") // + .inFolder(rootFolder) // + .findAll(RESHARPER) // + .violations(); + + assertThat(actual)// + .hasSize(3); + + assertThat(actual.get(0).getReporter())// + .isEqualTo(RESHARPER); + + assertThat(actual.get(0).getMessage())// + .isEqualTo( + "Using directive is not required by the code and can be safely removed. Redundancies in Code. Redundant using directive"); + assertThat(actual.get(0).getRule().get())// + .isEqualTo("RedundantUsingDirective"); + assertThat(actual.get(0).getFile())// + .isEqualTo("MyLibrary/Class1.cs"); + assertThat(actual.get(0).getSeverity())// + .isEqualTo(WARN); + + assertThat(actual.get(1).getMessage())// + .isEqualTo( + "Join declaration and assignment. Common Practices and Code Improvements. Join local variable declaration and assignment"); + assertThat(actual.get(1).getRule().get())// + .isEqualTo("JoinDeclarationAndInitializer"); + assertThat(actual.get(1).getFile())// + .isEqualTo("MyLibrary/Class1.cs"); + assertThat(actual.get(1).getSeverity())// + .isEqualTo(INFO); + + assertThat(actual.get(2).getMessage())// + .isEqualTo( + "Using directive is not required by the code and can be safely removed. Redundancies in Code. Redundant using directive"); + assertThat(actual.get(2).getRule().get())// + .isEqualTo("RedundantUsingDirective"); + assertThat(actual.get(2).getFile())// + .isEqualTo("MyLibrary/Properties/AssemblyInfo.cs"); + assertThat(actual.get(2).getSeverity())// + .isEqualTo(WARN); + } +} diff --git a/src/test/resources/resharper/main.xml b/src/test/resources/resharper/main.xml new file mode 100644 index 00000000..b906df44 --- /dev/null +++ b/src/test/resources/resharper/main.xml @@ -0,0 +1,21 @@ + + + + + CSharpPlayground.sln + + 9B2650A2-C7C6-435F-80D6-D6C7B522FFF9 + + + + + + + + + + + + + + \ No newline at end of file