Skip to content

Commit

Permalink
Merge pull request #537 from iamabhishek-dubey/master
Browse files Browse the repository at this point in the history
Added dockerlinter parser analysis
  • Loading branch information
uhafner authored Nov 26, 2020
2 parents 92c802e + e3e0f83 commit 2d0e2eb
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package edu.hm.hafner.analysis.parser;

import java.io.IOException;
import java.io.Reader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;

/**
* A parser for ot-docker-linter json output.
* <p>
* See <a href='https://github.com/opstree/OT-Dockerlinter'>ot-docker-linter</a> for project details.
*
* @author Abhishek Dubey
*/
public class OTDockerLintParser extends IssueParser {
private static final long serialVersionUID = 42L;

@Override
public boolean accepts(final ReaderFactory readerFactory) {
return readerFactory.getFileName().endsWith(".json");
}

@Override
public Report parse(final ReaderFactory readerFactory) throws ParsingException {
try (Reader reader = readerFactory.create()) {
JSONArray jsonReport = (JSONArray) new JSONTokener(reader).nextValue();

Report report = new Report();
for (Object issue : jsonReport) {
if (issue instanceof JSONObject) {
report.add(convertToIssue((JSONObject) issue));
}
}
return report;
}
catch (IOException | JSONException | ClassCastException e) {
throw new ParsingException(e);
}
}

Issue convertToIssue(final JSONObject jsonIssue) {
IssueBuilder builder = new IssueBuilder();
if (jsonIssue.has("code")) {
builder.setCategory(jsonIssue.getString("code"));
}
if (jsonIssue.has("severity")) {
builder.setSeverity(Severity.guessFromString(jsonIssue.getString("severity")));
}
if (jsonIssue.has("line")) {
builder.setLineStart(jsonIssue.getInt("line_number"));
}
if (jsonIssue.has("line")) {
builder.setModuleName(jsonIssue.getString("line"));
}
if (jsonIssue.has("description")) {
builder.setDescription(jsonIssue.getString("description"));
}
if (jsonIssue.has("message")) {
builder.setMessage(jsonIssue.getString("message"));
}
if (jsonIssue.has("file")) {
builder.setFileName(jsonIssue.getString("file"));
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package edu.hm.hafner.analysis.parser;

import java.nio.file.FileSystems;

import org.junit.jupiter.api.Test;

import edu.hm.hafner.analysis.AbstractParserTest;
import edu.hm.hafner.analysis.FileReaderFactory;
import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.assertions.SoftAssertions;

import static edu.hm.hafner.analysis.assertions.Assertions.*;
import static org.assertj.core.api.Assertions.*;

/**
* Tests the class {@link OTDockerLintParser}.
*
* @author Abhishek Dubey
*/
class OTDockerLintParserTest extends AbstractParserTest {
OTDockerLintParserTest() {
super("ot-docker-linter.json");
}

@Override
protected void assertThatIssuesArePresent(final Report report, final SoftAssertions softly) {
assertThat(report).hasSize(3);
softly.assertThat(report.get(0))
.hasModuleName("WORKDIR spsp/")
.hasCategory("DL3000")
.hasDescription("Use absolute WORKDIR.")
.hasLineStart(3)
.hasFileName("testing/Dockerfile.testing")
.hasSeverity(Severity.ERROR);
softly.assertThat(report.get(2))
.hasSeverity(Severity.WARNING_LOW);
softly.assertThat(report.get(1))
.hasSeverity(Severity.WARNING_NORMAL);
}

@Override
protected IssueParser createParser() {
return new OTDockerLintParser();
}

@Test
void accepts() {
assertThat(new OTDockerLintParser().accepts(
new FileReaderFactory(FileSystems.getDefault().getPath("lint.json")))).isTrue();
assertThat(new OTDockerLintParser().accepts(
new FileReaderFactory(FileSystems.getDefault().getPath("foo.txt")))).isFalse();
}

@Test
void brokenInput() {
assertThatThrownBy(() -> parse("eclipse.txt"))
.isInstanceOf(ParsingException.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"line_number": 3,
"line": "WORKDIR spsp/",
"code": "DL3000",
"description": "Use absolute WORKDIR.",
"message": "",
"severity": "Error",
"file": "testing/Dockerfile.testing"
},
{
"line_number": 5,
"line": "RUN sudo apt-get update && \\",
"code": "DL3008",
"description": "Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`.",
"message": "",
"severity": "Warning",
"file": "testing/Dockerfile.testing"
},
{
"line_number": 5,
"line": "RUN sudo apt-get update && \\",
"code": "DL3009",
"description": "Delete the apt-get lists after installing something.",
"message": "",
"severity": "Info",
"file": "testing/Dockerfile.testing"
}
]

0 comments on commit 2d0e2eb

Please sign in to comment.