Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parser categories #1095

Merged
merged 5 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public String getHelp() {
public String getUrl() {
return "https://github.com/arminc/clair-scanner";
}

@Override
public Type getType() {
return Type.VULNERABILITY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import edu.hm.hafner.analysis.parser.CodeCheckerParser;

/**
* A descriptor for the Codechecker parser.
* A descriptor for the CodeChecker parser.
*
*/
class CodeCheckerDescriptor extends ParserDescriptor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ private static String getCodeFragment(final DuplicationGroup duplicationGroup) {
public String getDescription(final Issue issue) {
return getDuplicateCode(issue.getAdditionalProperties());
}

@Override
public Type getType() {
return Type.DUPLICATION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ protected Collection<? extends IssueParser> createParsers() {
public String getUrl() {
return "https://errorprone.info";
}

@Override
public Type getType() {
return Type.BUG;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public String getPattern() {
public String getDescription(final Issue issue) {
return messages.get().getMessage(issue.getType());
}

@Override
public Type getType() {
return Type.BUG;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public String getHelp() {
public String getUrl() {
return "https://dwheeler.com/flawfinder/";
}

@Override
public Type getType() {
return Type.VULNERABILITY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public String getUrl() {
public String getIconUrl() {
return "https://raw.githubusercontent.com/jeremylong/DependencyCheck/main/src/site/resources/images/logo.svg";
}

@Override
public Type getType() {
return Type.VULNERABILITY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public String getName() {
return name;
}

/**
* Returns the type of the parser. The type is used to customize parsers in the UI.
* This default implementation returns {@link Type#WARNING}.
* Override this method if your parser is of a different type.
*
* @return the type of the parser
*/
public Type getType() {
return Type.WARNING;
}

/**
* Creates a new {@link IssueParser} instance.
*
Expand Down Expand Up @@ -129,6 +140,20 @@ public String getDescription(final Issue issue) {
return issue.getDescription();
}

/**
* Returns the type of the parser. The type is used to customize parsers in the UI.
*/
public enum Type {
/** A parser that scans the output of a build tool to find warnings. */
WARNING,
/** A parser that scans the output of a build tool to find bugs. */
BUG,
/** A parser that scans the output of a build tool to find vulnerabilities. */
VULNERABILITY,
/** A parser that scans the output of a build tool to find vulnerabilities. */
DUPLICATION
}

/**
* A parser configuration option. Basically an immutable key and value pair.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ public static void main(final String... unused) throws IOException {
+ "\n"
+ "The static analysis model supports the following report formats.\n"
+ "\n"
+ "If your tool is not yet supported you can\n"
+ "If your tool is not yet supported, you can\n"
+ "1. export the issues of your tool to the native XML or JSON format (or any other format).\n"
+ "2. provide a [pull request](https://github.com/jenkinsci/analysis-model/pulls) with a new parser.\n"
+ "\n"
+ "If your tool is supported, but some properties are missing (icon, URL, etc.), please file a\n"
+ "If your tool is supported, but some properties are missing (icon, URL, etc.), please file a "
+ "[pull request](https://github.com/jenkinsci/analysis-model/pulls).\n"
+ "\n");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public String getUrl() {
public String getIconUrl() {
return "https://pnpm.io/img/pnpm-no-name-with-frame.svg";
}

@Override
public Type getType() {
return Type.VULNERABILITY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public String getUrl() {
public String getIconUrl() {
return "https://raw.githubusercontent.com/returntocorp/semgrep/develop/semgrep.svg";
}

@Override
public Type getType() {
return Type.VULNERABILITY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public String getUrl() {
public String getIconUrl() {
return "https://raw.githubusercontent.com/spotbugs/spotbugs.github.io/master/images/logos/spotbugs_icon_only_zoom_256px.png";
}

@Override
public Type getType() {
return Type.BUG;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public String getUrl() {
public String getIconUrl() {
return "https://github.com/aquasecurity/trivy/blob/main/docs/imgs/logo.png?raw=true";
}

@Override
public Type getType() {
return Type.VULNERABILITY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public String getUrl() {
public String getIconUrl() {
return "https://upload.wikimedia.org/wikipedia/commons/0/00/Yocto_Project_logo.svg";
}

@Override
public Type getType() {
return Type.VULNERABILITY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import edu.hm.hafner.analysis.parser.violations.ZptLintAdapter;

/**
* A descriptor for the Yui Compressor parser.
* A descriptor for the ZPT Lint parser.
*
* @author Lorenz Munsch
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

Expand All @@ -20,6 +21,13 @@
* @author Ullrich Hafner
*/
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 BUG_PARSERS_COUNT = 3L;
private static final long VULNERABILITY_PARSERS_COUNT = 7L;
private static final long DUPLICATION_PARSERS_COUNT = 3L;

public static final String SPOTBUGS = "spotbugs";
public static final String CHECKSTYLE = "checkstyle";
public static final String PMD = "pmd";
Expand All @@ -32,12 +40,28 @@ void shouldThrowExceptionIfParserNotFound() {
.isThrownBy(() -> parserRegistry.get("-"));
}

/**
* Ensures that new parsers have the correct type assigned.
*/
@Test
void shouldAssignCorrectParserType() {
var parserRegistry = new ParserRegistry();
var typeCountMap = parserRegistry.getAllDescriptors().stream()
.collect(Collectors.groupingBy(ParserDescriptor::getType, Collectors.counting()));
assertThat(typeCountMap)
.containsEntry(ParserDescriptor.Type.WARNING, WARNING_PARSERS_COUNT)
.containsEntry(ParserDescriptor.Type.BUG, BUG_PARSERS_COUNT)
.containsEntry(ParserDescriptor.Type.VULNERABILITY, VULNERABILITY_PARSERS_COUNT)
.containsEntry(ParserDescriptor.Type.DUPLICATION, DUPLICATION_PARSERS_COUNT);
}

@Test
void shouldFindSomeParsers() {
var parserRegistry = new ParserRegistry();

assertThat(parserRegistry).hasIds(SPOTBUGS, CHECKSTYLE, PMD).hasNames("SpotBugs", "CheckStyle", "PMD");
assertThat(parserRegistry.get(SPOTBUGS)).hasId(SPOTBUGS).hasName("SpotBugs");
assertThat(parserRegistry.get(SPOTBUGS)).hasId(SPOTBUGS).hasName("SpotBugs").hasType(ParserDescriptor.Type.BUG);
assertThat(parserRegistry.get("owasp-dependency-check")).hasName("OWASP Dependency Check").hasType(ParserDescriptor.Type.VULNERABILITY);
assertThat(parserRegistry.contains(SPOTBUGS)).isTrue();
assertThat(parserRegistry.contains("nothing")).isFalse();
List<ParserDescriptor> descriptors = parserRegistry.getAllDescriptors();
Expand All @@ -49,6 +73,7 @@ void shouldFindSomeParsers() {
void shouldConfigureCpdParser() {
var parserRegistry = new ParserRegistry();
var cpdDescriptor = parserRegistry.get("cpd");
assertThat(cpdDescriptor).hasType(ParserDescriptor.Type.DUPLICATION).hasName("CPD");

IssueParser parser = cpdDescriptor.createParser();

Expand Down
Loading