-
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 #817 from jenkinsci/revApi
Add support for Revapi
- Loading branch information
Showing
15 changed files
with
532 additions
and
69 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
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
58 changes: 58 additions & 0 deletions
58
src/main/java/edu/hm/hafner/analysis/RevApiInfoExtension.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,58 @@ | ||
package edu.hm.hafner.analysis; | ||
|
||
import java.io.Serializable; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
|
||
/** | ||
* Stores additional information of the Revapi analysis (severities, issue name, old file, and new file). | ||
*/ | ||
@SuppressWarnings("PMD.DataClass") | ||
public final class RevApiInfoExtension implements Serializable { | ||
private static final long serialVersionUID = 6058160289391492934L; | ||
private final Map<String, String> severities = new HashMap<>(); | ||
private final String issueName; | ||
private final String oldFile; | ||
private final String newFile; | ||
|
||
/** | ||
* Creates an object to store additional information of the Revapi analysis. | ||
* | ||
* @param code | ||
* of the parsed issue | ||
* @param oldFile | ||
* the oldFile where something was changed | ||
* @param newFile | ||
* the newFile where something was changed | ||
* @param severities | ||
* the severities of Binary and source | ||
*/ | ||
public RevApiInfoExtension(@CheckForNull final String code, final String oldFile, | ||
final String newFile, final Map<String, String> severities) { | ||
this.issueName = StringUtils.defaultString(code, "-"); | ||
this.oldFile = oldFile; | ||
this.newFile = newFile; | ||
this.severities.putAll(severities); | ||
} | ||
|
||
public Map<String, String> getSeverities() { | ||
return Collections.unmodifiableMap(severities); | ||
} | ||
|
||
public String getIssueName() { | ||
return issueName; | ||
} | ||
|
||
public String getOldFile() { | ||
return oldFile; | ||
} | ||
|
||
public String getNewFile() { | ||
return newFile; | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/main/java/edu/hm/hafner/analysis/parser/RevApiParser.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,124 @@ | ||
package edu.hm.hafner.analysis.parser; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
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.RevApiInfoExtension; | ||
import edu.hm.hafner.analysis.Severity; | ||
|
||
/** | ||
* Parser for Revapi reports. | ||
*/ | ||
public class RevApiParser extends JsonIssueParser { | ||
private static final long serialVersionUID = -2452699725595063377L; | ||
|
||
@Override | ||
protected void parseJsonArray(final Report report, final JSONArray jsonReport, final IssueBuilder issueBuilder) { | ||
for (Object issue : jsonReport) { | ||
if (issue instanceof JSONObject) { | ||
report.add(convertToIssue((JSONObject) issue, issueBuilder)); | ||
} | ||
else { | ||
report.logError("RevApi issues no instance of JSON"); | ||
} | ||
} | ||
} | ||
|
||
private Issue convertToIssue(final JSONObject jsonIssue, final IssueBuilder builder) { | ||
builder.setSeverity(evaluateSeverity(jsonIssue.getJSONArray("classification"))); | ||
builder.setDescription(getDescription(jsonIssue)); | ||
addAttachments(jsonIssue.getJSONArray("attachments"), builder); | ||
builder.setAdditionalProperties(convertToGroup(jsonIssue)); | ||
return builder.build(); | ||
} | ||
|
||
private RevApiInfoExtension convertToGroup(final JSONObject jsonIssue) { | ||
return new RevApiInfoExtension( | ||
jsonIssue.getString("code"), | ||
extractChange(jsonIssue, "old"), | ||
extractChange(jsonIssue, "new"), | ||
extractSeverities(jsonIssue)); | ||
} | ||
|
||
private static Map<String, String> extractSeverities(final JSONObject jsonIssue) { | ||
Map<String, String> allSeverities = new HashMap<>(); | ||
for (Object severity : jsonIssue.getJSONArray("classification")) { | ||
if (severity instanceof JSONObject) { | ||
allSeverities.put(((JSONObject) severity).getString("compatibility"), ((JSONObject) severity).getString("severity")); | ||
} | ||
} | ||
return allSeverities; | ||
} | ||
|
||
private static String extractChange(final JSONObject jsonIssue, final String key) { | ||
String value = jsonIssue.get(key).toString(); | ||
return "null".equals(value) ? "-" : value; | ||
} | ||
|
||
private void addAttachments(final JSONArray attachments, final IssueBuilder builder) { | ||
String packageName = attachments.getJSONObject(0).getString("value"); | ||
String classSimpleName = attachments.getJSONObject(2).getString("value"); | ||
String elementKind = attachments.getJSONObject(3).getString("value"); | ||
builder.setFileName(classSimpleName); | ||
builder.setPackageName(packageName); | ||
builder.setCategory(elementKind); | ||
} | ||
|
||
private Severity evaluateSeverity(final JSONArray classification) { | ||
Set<Severity> allSeverities = new HashSet<>(); | ||
for (Object severity : classification) { | ||
if (severity instanceof JSONObject) { | ||
allSeverities.add(toSeverity(((JSONObject) severity).getString("severity"))); | ||
} | ||
} | ||
if (allSeverities.contains(Severity.WARNING_HIGH)) { | ||
return Severity.WARNING_HIGH; | ||
} | ||
if (allSeverities.contains(Severity.WARNING_NORMAL)) { | ||
return Severity.WARNING_NORMAL; | ||
} | ||
return Severity.WARNING_LOW; | ||
} | ||
|
||
private Severity toSeverity(final String level) { | ||
switch (level) { | ||
case "NON_BREAKING": | ||
return Severity.WARNING_LOW; | ||
case "BREAKING": | ||
return Severity.WARNING_HIGH; | ||
case "POTENTIALLY_BREAKING": | ||
default: | ||
return Severity.WARNING_NORMAL; | ||
} | ||
} | ||
|
||
private String getDescription(final JSONObject jsonIssue) { | ||
StringBuilder severityDescription = new StringBuilder(); | ||
for (Object severity : jsonIssue.getJSONArray("classification")) { | ||
if (severity instanceof JSONObject) { | ||
severityDescription.append("<p>Compatibility: ") | ||
.append(((JSONObject) severity).getString("compatibility")) | ||
.append(" Severity: ") | ||
.append(((JSONObject) severity).getString("severity")) | ||
.append("</p>"); | ||
} | ||
} | ||
|
||
return MessageFormat.format( | ||
"<p><div><b>File</b>: {0}</div><div><b>Description:</b> {1} {2}</div><div><b>Change type:</b> {3}", | ||
jsonIssue.getJSONArray("attachments").getJSONObject(1).getString("value"), | ||
jsonIssue.getString("description"), | ||
jsonIssue.getString("name"), | ||
jsonIssue.getString("code")) + severityDescription; | ||
|
||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/edu/hm/hafner/analysis/registry/RevApiDescriptor.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,33 @@ | ||
package edu.hm.hafner.analysis.registry; | ||
|
||
import edu.hm.hafner.analysis.IssueParser; | ||
import edu.hm.hafner.analysis.parser.RevApiParser; | ||
|
||
/** | ||
* Parser for Revapi Json reports. | ||
* | ||
* @author Dominik Jantschar | ||
*/ | ||
public class RevApiDescriptor extends ParserDescriptor { | ||
private static final String ID = "revapi"; | ||
private static final String NAME = "Revapi"; | ||
|
||
RevApiDescriptor() { | ||
super(ID, NAME); | ||
} | ||
|
||
@Override | ||
public IssueParser createParser(final Option... options) { | ||
return new RevApiParser(); | ||
} | ||
|
||
@Override | ||
public String getPattern() { | ||
return "**/target/revapi-result.json"; | ||
} | ||
|
||
@Override | ||
public String getUrl() { | ||
return "https://revapi.org/revapi-site/main/index.html"; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.