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
Xml Report Merger now merges duplicate smells across input report files #5033
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package io.gitlab.arturbosch.detekt.report | |
|
||
import org.w3c.dom.Document | ||
import org.w3c.dom.Node | ||
import org.w3c.dom.NodeList | ||
import java.io.File | ||
import javax.xml.parsers.DocumentBuilderFactory | ||
import javax.xml.transform.OutputKeys | ||
|
@@ -16,42 +17,133 @@ object XmlReportMerger { | |
|
||
private val documentBuilder by lazy { DocumentBuilderFactory.newInstance().newDocumentBuilder() } | ||
|
||
fun merge(inputs: Collection<File>, output: File) { | ||
val document = documentBuilder.newDocument().apply { | ||
xmlStandalone = true | ||
val checkstyleNode = createElement("checkstyle") | ||
checkstyleNode.setAttribute("version", "4.3") | ||
appendChild(checkstyleNode) | ||
} | ||
inputs.filter { it.exists() }.forEach { | ||
importNodesFromInput(it, document) | ||
} | ||
fun merge(reportFiles: Collection<File>, output: File) { | ||
val distinctErrorsBySourceFile = DetektCheckstyleReports(reportFiles) | ||
.parseCheckstyleToSourceFileNodes() | ||
.distinctErrorsGroupedBySourceFile() | ||
|
||
val mergedCheckstyle = createMergedCheckstyle(distinctErrorsBySourceFile) | ||
|
||
TransformerFactory.newInstance().newTransformer().run { | ||
setOutputProperty(OutputKeys.INDENT, "yes") | ||
setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2") | ||
transform(DOMSource(document), StreamResult(output.writer())) | ||
transform(DOMSource(mergedCheckstyle), StreamResult(output.writer())) | ||
} | ||
} | ||
|
||
private fun importNodesFromInput(input: File, document: Document) { | ||
val checkstyleNode = documentBuilder.parse(input.inputStream()).documentElement.also { removeWhitespaces(it) } | ||
(0 until checkstyleNode.childNodes.length).forEach { | ||
val node = checkstyleNode.childNodes.item(it) | ||
document.documentElement.appendChild(document.importNode(node, true)) | ||
private fun createMergedCheckstyle(distinctErrorsBySourceFile: Map<String, List<Node>>): Document { | ||
val mergedDocument = documentBuilder.newDocument().apply { | ||
xmlStandalone = true | ||
} | ||
val mergedCheckstyleNode = mergedDocument.createElement("checkstyle") | ||
mergedCheckstyleNode.setAttribute("version", "4.3") | ||
mergedDocument.appendChild(mergedCheckstyleNode) | ||
|
||
distinctErrorsBySourceFile.forEach { (fileName, errorNodes) -> | ||
mergedCheckstyleNode.appendChild( | ||
mergedDocument.createElement("file").apply { | ||
setAttribute("name", fileName) | ||
errorNodes.forEach { | ||
appendChild(mergedDocument.importNode(it, true)) | ||
} | ||
} | ||
) | ||
} | ||
return mergedDocument | ||
} | ||
|
||
/** A list of checkstyle xml files written by Detekt */ | ||
private class DetektCheckstyleReports(private val files: Collection<File>) { | ||
|
||
/** | ||
* Parses a list of `file` nodes matching the following topology | ||
* | ||
* ```xml | ||
* <checkstyle> | ||
* <file/> | ||
* </checkstyle> | ||
* ``` | ||
* | ||
* @see CheckstyleSourceFileNodes | ||
*/ | ||
fun parseCheckstyleToSourceFileNodes() = | ||
CheckstyleSourceFileNodes( | ||
files.filter { reportFile -> reportFile.exists() } | ||
.flatMap { existingReportFile -> | ||
val checkstyleNode = documentBuilder.parse(existingReportFile.inputStream()) | ||
val sourceFileNodes = checkstyleNode.documentElement.childNodes.asSequence().filterWhitespace() | ||
sourceFileNodes | ||
chao2zhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
) | ||
} | ||
|
||
/** | ||
* Use code instead of XSLT to exclude whitespaces. | ||
* A list of checkstyle `file` nodes that may contain 0 to many `error` nodes | ||
* | ||
* ```xml | ||
* <file> | ||
* <error> | ||
* </file> | ||
Comment on lines
+83
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :+100: on this documentation |
||
* ``` | ||
*/ | ||
private fun removeWhitespaces(node: Node) { | ||
(node.childNodes.length - 1 downTo 0).forEach { idx -> | ||
val childNode = node.childNodes.item(idx) | ||
if (childNode.nodeType == Node.TEXT_NODE && childNode.textContent.isBlank()) { | ||
node.removeChild(childNode) | ||
private class CheckstyleSourceFileNodes(private val nodes: List<Node>) { | ||
|
||
/** Returns a map containing only distinct error nodes, grouped by file name */ | ||
fun distinctErrorsGroupedBySourceFile() = nodes | ||
.flatMap { fileNode -> | ||
val fileNameAttribute = fileNode.attributes.getNamedItem("name").nodeValue | ||
val errorNodes = fileNode.childNodes.asSequence().filterWhitespace() | ||
errorNodes.map { errorNode -> | ||
CheckstyleErrorNodeWithFileData( | ||
errorID = errorID(fileNameAttribute, errorNode), | ||
fileName = fileNameAttribute, | ||
errorNode = errorNode | ||
) | ||
} | ||
} | ||
.distinctBy { it.errorID } | ||
.groupBy({ it.fileName }, { it.errorNode }) | ||
|
||
private fun errorID(fileNameAttribute: String, errorNode: Node): Any { | ||
// error nodes are expected to take the form of at least <error line="#" column="#" source="ruleName"/> | ||
val line = errorNode.attributes.getNamedItem("line")?.nodeValue | ||
val column = errorNode.attributes.getNamedItem("column")?.nodeValue | ||
val source = errorNode.attributes.getNamedItem("source")?.nodeValue | ||
|
||
return if (line != null && column != null && source != null) { | ||
// data class provides convenient hashCode/equals based on these attributes | ||
ErrorID(fileName = fileNameAttribute, line = line, column = column, source = source) | ||
} else { | ||
removeWhitespaces(childNode) | ||
// if the error node does not contain the expected attributes, | ||
// use org.w3c.dom.Node's more strict hashCode/equals method to determine error uniqueness | ||
errorNode | ||
} | ||
} | ||
|
||
private class CheckstyleErrorNodeWithFileData( | ||
val errorID: Any, | ||
val fileName: String, | ||
val errorNode: Node | ||
) | ||
|
||
private data class ErrorID( | ||
val fileName: String, | ||
val line: String, | ||
val column: String, | ||
val source: String | ||
) | ||
} | ||
|
||
/** | ||
* Use code instead of XSLT to exclude whitespaces. | ||
*/ | ||
private fun Sequence<Node>.filterWhitespace(): Sequence<Node> = asSequence().filterNot { | ||
it.nodeType == Node.TEXT_NODE && it.textContent.isBlank() | ||
} | ||
|
||
private fun NodeList.asSequence() = sequence { | ||
for (index in 0 until length) { | ||
yield(item(index)) | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using classes here to help call out preconditons and assumptions about the input data. These functions started out as extension functions, but I didn't like the idea of applying something so specific to broad types such as
Colleciton<File>
andList<Node>
Thoughs?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have strong opinions, so I would just conform to the pattern in this file (Wrapping inside a class seems to be the pattern)