-
Notifications
You must be signed in to change notification settings - Fork 186
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 #537 from iamabhishek-dubey/master
Added dockerlinter parser analysis
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/edu/hm/hafner/analysis/parser/OTDockerLintParser.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,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(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/edu/hm/hafner/analysis/parser/OTDockerLintParserTest.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,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); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/test/resources/edu/hm/hafner/analysis/parser/ot-docker-linter.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,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" | ||
} | ||
] |