-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
107 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
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/br/com/felipezorzo/zpa/cli/exporters/ConsoleExporter.kt
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,31 @@ | ||
package br.com.felipezorzo.zpa.cli.exporters | ||
|
||
import br.com.felipezorzo.zpa.cli.InputFile | ||
import org.sonar.plsqlopen.squid.ZpaIssue | ||
|
||
class ConsoleExporter : IssueExporter { | ||
override fun export(issues: List<ZpaIssue>) { | ||
for ((file, fileIssues) in issues.groupBy { (it.file as InputFile).pathRelativeToBase }.toSortedMap()) { | ||
println("File: $file") | ||
|
||
for (issue in fileIssues.sortedWith( | ||
compareBy( | ||
{ it.primaryLocation.startLine() }, | ||
{ it.primaryLocation.startLineOffset() }) | ||
)) { | ||
val startLine = issue.primaryLocation.startLine() | ||
val startColumn = issue.primaryLocation.startLineOffset() | ||
val activeRule = issue.check.activeRule | ||
val severity = activeRule.severity | ||
|
||
var positionFormatted = "$startLine" | ||
if (startColumn != -1) { | ||
positionFormatted += ":$startColumn" | ||
} | ||
println("${positionFormatted.padEnd(10)}${severity.padEnd(10)}${issue.primaryLocation.message()}") | ||
} | ||
|
||
println("") | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/kotlin/br/com/felipezorzo/zpa/cli/exporters/GenericIssueFormatExporter.kt
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 br.com.felipezorzo.zpa.cli.exporters | ||
|
||
import br.com.felipezorzo.zpa.cli.InputFile | ||
import br.com.felipezorzo.zpa.cli.sqissue.* | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import org.sonar.plsqlopen.squid.ZpaIssue | ||
import java.io.File | ||
|
||
class GenericIssueFormatExporter(private val outputFile: String) : IssueExporter { | ||
override fun export(issues: List<ZpaIssue>) { | ||
val genericIssues = mutableListOf<Issue>() | ||
for (issue in issues) { | ||
val relativeFilePathStr = (issue.file as InputFile).pathRelativeToBase | ||
|
||
val issuePrimaryLocation = issue.primaryLocation | ||
|
||
val primaryLocation = PrimaryLocation( | ||
issuePrimaryLocation.message(), | ||
relativeFilePathStr, | ||
createTextRange( | ||
issuePrimaryLocation.startLine(), | ||
issuePrimaryLocation.endLine(), | ||
issuePrimaryLocation.startLineOffset(), | ||
issuePrimaryLocation.endLineOffset() | ||
) | ||
) | ||
|
||
val secondaryLocations = mutableListOf<SecondaryLocation>() | ||
|
||
for (secondary in issue.secondaryLocations) { | ||
secondaryLocations += SecondaryLocation( | ||
secondary.message(), | ||
relativeFilePathStr, | ||
createTextRange( | ||
secondary.startLine(), | ||
secondary.endLine(), | ||
secondary.startLineOffset(), | ||
secondary.endLineOffset() | ||
) | ||
) | ||
} | ||
|
||
val activeRule = issue.check.activeRule | ||
|
||
val type = when { | ||
activeRule.tags.contains("vulnerability") -> "VULNERABILITY" | ||
activeRule.tags.contains("bug") -> "BUG" | ||
else -> "CODE_SMELL" | ||
} | ||
|
||
genericIssues += Issue( | ||
ruleId = activeRule.ruleKey.toString(), | ||
severity = activeRule.severity, | ||
type = type, | ||
primaryLocation = primaryLocation, | ||
duration = activeRule.remediationConstant, | ||
secondaryLocations = secondaryLocations | ||
) | ||
} | ||
val genericReport = GenericIssueData(genericIssues) | ||
|
||
val mapper = ObjectMapper() | ||
val generatedOutput = mapper.writeValueAsString(genericReport) | ||
val file = File(outputFile) | ||
file.parentFile?.mkdirs() | ||
file.writeText(generatedOutput) | ||
} | ||
|
||
private fun createTextRange(startLine: Int, endLine: Int, startLineOffset: Int, endLineOffset: Int): TextRange { | ||
return TextRange( | ||
startLine = startLine, | ||
endLine = if (endLine > -1) endLine else null, | ||
startColumn = if (startLineOffset > -1) startLineOffset else null, | ||
endColumn = if (endLineOffset > -1) endLineOffset else null | ||
) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/br/com/felipezorzo/zpa/cli/exporters/IssueExporter.kt
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,7 @@ | ||
package br.com.felipezorzo.zpa.cli.exporters | ||
|
||
import org.sonar.plsqlopen.squid.ZpaIssue | ||
|
||
fun interface IssueExporter { | ||
fun export(issues: List<ZpaIssue>) | ||
} |