-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1119 from daniel-parasoft/vale-integration
Vale integration
- Loading branch information
Showing
7 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/edu/hm/hafner/analysis/parser/ValeParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package edu.hm.hafner.analysis.parser; | ||
|
||
import java.io.Serial; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import edu.hm.hafner.analysis.Issue; | ||
import edu.hm.hafner.analysis.IssueBuilder; | ||
import edu.hm.hafner.analysis.Report; | ||
import edu.hm.hafner.analysis.Severity; | ||
|
||
/** | ||
* Parser for vale reports. | ||
*/ | ||
public class ValeParser extends JsonIssueParser { | ||
// Constants for JSON keys | ||
private static final String CHECK = "Check"; | ||
private static final String LINE_KEY = "Line"; | ||
private static final String LINK_KEY = "Link"; | ||
private static final String MESSAGE_KEY = "Message"; | ||
private static final String SPAN_KEY = "Span"; | ||
private static final String SEVERITY_KEY = "Severity"; | ||
|
||
@Serial | ||
private static final long serialVersionUID = -4034450901865555017L; | ||
|
||
@Override | ||
protected void parseJsonObject(final Report report, final JSONObject jsonReport, final IssueBuilder issueBuilder) { | ||
JSONArray fileNames = jsonReport.names(); | ||
for (Object o : fileNames) { | ||
if (o instanceof String f) { | ||
JSONArray jsonArray = jsonReport.getJSONArray(f); | ||
for (Object data : jsonArray) { | ||
if (data instanceof JSONObject dataObject) { | ||
report.add(createIssue(issueBuilder, f, dataObject)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private Issue createIssue(final IssueBuilder issueBuilder, final String fileName, final JSONObject data) { | ||
JSONArray span = data.getJSONArray(SPAN_KEY); | ||
int line = data.getInt(LINE_KEY); | ||
return issueBuilder.setFileName(fileName) | ||
.setDescription(data.getString(CHECK)) | ||
.setMessage(data.getString(MESSAGE_KEY)) | ||
.setSeverity(Severity.guessFromString(data.getString(SEVERITY_KEY))) | ||
.setReference(data.getString(LINK_KEY)) | ||
.setLineStart(line) | ||
.setLineEnd(line) | ||
.setColumnStart(span.getInt(0)) | ||
.setColumnEnd(span.getInt(1)) | ||
.buildAndClean(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/edu/hm/hafner/analysis/registry/ValeDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package edu.hm.hafner.analysis.registry; | ||
|
||
import edu.hm.hafner.analysis.IssueParser; | ||
import edu.hm.hafner.analysis.parser.ValeParser; | ||
|
||
/** | ||
* Descriptor for the vale prose linter. | ||
*/ | ||
public class ValeDescriptor extends ParserDescriptor { | ||
private static final String ID = "vale"; | ||
private static final String NAME = "Vale"; | ||
|
||
ValeDescriptor() { | ||
super(ID, NAME); | ||
} | ||
|
||
@Override | ||
public IssueParser createParser(Option... options) { | ||
return new ValeParser(); | ||
} | ||
|
||
@Override | ||
public String getPattern() { | ||
return "**/vale-report.json"; | ||
} | ||
|
||
@Override | ||
public String getUrl() { | ||
return "https://vale.sh/"; | ||
} | ||
|
||
@Override | ||
public String getHelp() { | ||
return "Reads vale report files. Use the flag --output=JSON"; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/edu/hm/hafner/analysis/parser/ValeParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package edu.hm.hafner.analysis.parser; | ||
|
||
import edu.hm.hafner.analysis.IssueParser; | ||
import edu.hm.hafner.analysis.Report; | ||
import edu.hm.hafner.analysis.Severity; | ||
import edu.hm.hafner.analysis.assertions.SoftAssertions; | ||
import edu.hm.hafner.analysis.registry.AbstractParserTest; | ||
|
||
class ValeParserTest extends AbstractParserTest { | ||
ValeParserTest() { | ||
super("vale-report.json"); | ||
} | ||
|
||
@Override | ||
protected void assertThatIssuesArePresent(final Report report, final SoftAssertions softly) { | ||
softly.assertThat(report).hasSize(3).hasDuplicatesSize(0); | ||
softly.assertThat(report.get(0)) | ||
.hasFileName("file1.adoc") | ||
.hasDescription("RedHat.SentenceLength") | ||
.hasMessage("Try to keep sentences to an average of 32 words or fewer.") | ||
.hasLineStart(10) | ||
.hasColumnStart(1) | ||
.hasColumnEnd(5) | ||
.hasSeverity(Severity.WARNING_LOW); | ||
softly.assertThat(report.get(1)) | ||
.hasFileName("file2.adoc") | ||
.hasDescription("RedHat.TermsWarnings") | ||
.hasMessage("Consider using 'might' or 'can' rather than 'may' unless updating existing content that uses the term.") | ||
.hasLineStart(39) | ||
.hasColumnStart(143) | ||
.hasColumnEnd(145) | ||
.hasSeverity(Severity.WARNING_NORMAL); | ||
softly.assertThat(report.get(2)) | ||
.hasFileName("file2.adoc") | ||
.hasDescription("RedHat.Using") | ||
.hasMessage("Use 'by using' instead of 'using' when it follows a noun for clarity and grammatical correctness.") | ||
.hasLineStart(51) | ||
.hasColumnStart(44) | ||
.hasColumnEnd(64) | ||
.hasSeverity(Severity.ERROR); | ||
} | ||
|
||
@Override | ||
protected IssueParser createParser() { | ||
return new ValeParser(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/resources/edu/hm/hafner/analysis/parser/vale-report.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{ | ||
"file1.adoc": [ | ||
{ | ||
"Action": { | ||
"Name": "", | ||
"Params": null | ||
}, | ||
"Span": [ | ||
1, | ||
5 | ||
], | ||
"Check": "RedHat.SentenceLength", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/sentencelength/", | ||
"Message": "Try to keep sentences to an average of 32 words or fewer.", | ||
"Severity": "suggestion", | ||
"Match": "While", | ||
"Line": 10 | ||
} | ||
], | ||
"file2.adoc": [ | ||
{ | ||
"Action": { | ||
"Name": "replace", | ||
"Params": [ | ||
"might", | ||
"can" | ||
] | ||
}, | ||
"Span": [ | ||
143, | ||
145 | ||
], | ||
"Check": "RedHat.TermsWarnings", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/termswarnings/", | ||
"Message": "Consider using 'might' or 'can' rather than 'may' unless updating existing content that uses the term.", | ||
"Severity": "warning", | ||
"Match": "may", | ||
"Line": 39 | ||
}, | ||
{ | ||
"Action": { | ||
"Name": "edit", | ||
"Params": [ | ||
"regex", | ||
"(\\w+)( using)", | ||
"$1 by using" | ||
] | ||
}, | ||
"Span": [ | ||
44, | ||
64 | ||
], | ||
"Check": "RedHat.Using", | ||
"Description": "", | ||
"Link": "https://redhat-documentation.github.io/vale-at-red-hat/docs/main/reference-guide/using/", | ||
"Message": "Use 'by using' instead of 'using' when it follows a noun for clarity and grammatical correctness.", | ||
"Severity": "error", | ||
"Match": "functionalities using", | ||
"Line": 51 | ||
} | ||
] | ||
} |