Skip to content

Commit

Permalink
Merge pull request #1119 from daniel-parasoft/vale-integration
Browse files Browse the repository at this point in the history
Vale integration
  • Loading branch information
uhafner authored Dec 27, 2024
2 parents 0c2a702 + c4b3236 commit d0bd472
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/edu/hm/hafner/analysis/Severity.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static Severity guessFromString(@CheckForNull final String severity) {
if (StringUtils.containsAnyIgnoreCase(severity, "error", "severe", "critical", "fatal")) {
return ERROR;
}
if (StringUtils.containsAnyIgnoreCase(severity, "info", "note", "low")) {
if (StringUtils.containsAnyIgnoreCase(severity, "info", "note", "low", "suggestion")) {
return WARNING_LOW;
}
if (StringUtils.containsAnyIgnoreCase(severity, "warning", "medium")) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/edu/hm/hafner/analysis/parser/ValeParser.java
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) {

Check warning on line 32 in src/main/java/edu/hm/hafner/analysis/parser/ValeParser.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 32 is only partially covered, one branch is missing
JSONArray jsonArray = jsonReport.getJSONArray(f);
for (Object data : jsonArray) {
if (data instanceof JSONObject dataObject) {

Check warning on line 35 in src/main/java/edu/hm/hafner/analysis/parser/ValeParser.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Partially covered line

Line 35 is only partially covered, one branch is missing
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public class ParserRegistry {
new TnsdlDescriptor(),
new TrivyDescriptor(),
new TsLintDescriptor(),
new ValeDescriptor(),
new ValgrindDescriptor(),
new VeraCodePipelineScannerDescriptor(),
new XlcDescriptor(),
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/edu/hm/hafner/analysis/registry/ValeDescriptor.java
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";

Check warning on line 24 in src/main/java/edu/hm/hafner/analysis/registry/ValeDescriptor.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 24 is not covered by tests
}

@Override
public String getUrl() {
return "https://vale.sh/";
}

@Override
public String getHelp() {
return "Reads vale report files. Use the flag --output=JSON";

Check warning on line 34 in src/main/java/edu/hm/hafner/analysis/registry/ValeDescriptor.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 34 is not covered by tests
}
}
47 changes: 47 additions & 0 deletions src/test/java/edu/hm/hafner/analysis/parser/ValeParserTest.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class ParserRegistryTest extends ResourceTest {
// Note for parser developers: if you add a new parser,
// please check if you are using the correct type and increment the corresponding count
private static final long WARNING_PARSERS_COUNT = 127L;
private static final long WARNING_PARSERS_COUNT = 128L;
private static final long BUG_PARSERS_COUNT = 3L;
private static final long VULNERABILITY_PARSERS_COUNT = 7L;
private static final long DUPLICATION_PARSERS_COUNT = 3L;
Expand Down
64 changes: 64 additions & 0 deletions src/test/resources/edu/hm/hafner/analysis/parser/vale-report.json
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
}
]
}

0 comments on commit d0bd472

Please sign in to comment.