Skip to content

Commit 827eb24

Browse files
authored
Merge pull request #75 from advanced-security/fix/path_resolution
fix : file location resolving and add an error message if it's not resolved
2 parents 023a43b + 26a782f commit 827eb24

File tree

1 file changed

+54
-41
lines changed

1 file changed

+54
-41
lines changed

src/main/kotlin/com/github/adrienpessu/sarifviewer/toolWindow/SarifViewerWindowFactory.kt

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import com.github.adrienpessu.sarifviewer.models.Leaf
1313
import com.github.adrienpessu.sarifviewer.models.View
1414
import com.github.adrienpessu.sarifviewer.services.SarifService
1515
import com.github.adrienpessu.sarifviewer.utils.GitHubInstance
16+
import com.intellij.notification.Notification
1617
import com.intellij.notification.NotificationGroupManager
1718
import com.intellij.notification.NotificationType
19+
import com.intellij.notification.Notifications
1820
import com.intellij.openapi.actionSystem.ActionManager
1921
import com.intellij.openapi.actionSystem.AnAction
2022
import com.intellij.openapi.components.service
@@ -113,6 +115,7 @@ class SarifViewerWindowFactory : ToolWindowFactory {
113115
private var disableComboBoxEvent = false
114116
private var currentView = View.RULE
115117
private var cacheSarif: SarifSchema210? = null
118+
private var currentLeaf: Leaf? = null
116119

117120
fun getContent() = JBPanel<JBPanel<*>>().apply {
118121

@@ -442,13 +445,13 @@ class SarifViewerWindowFactory : ToolWindowFactory {
442445
if (e != null && e.isAddedPath) {
443446
val leaves = map[e.path.parentPath.lastPathComponent.toString().split(" ").first()]
444447
if (!leaves.isNullOrEmpty()) {
445-
val leaf = try {
448+
currentLeaf = try {
446449
leaves.first { it.address == ((e.path.lastPathComponent as DefaultMutableTreeNode).userObject as Leaf).address }
447450
} catch (e: Exception) {
448451
leaves.first()
449452
}
450453

451-
val githubAlertUrl = leaf.githubAlertUrl
454+
val githubAlertUrl = currentLeaf!!.githubAlertUrl
452455
.replace("api.", "")
453456
.replace("api/v3/", "")
454457
.replace("repos/", "")
@@ -465,12 +468,12 @@ class SarifViewerWindowFactory : ToolWindowFactory {
465468
tableInfos.model = defaultTableModel
466469

467470
// Add some data
468-
defaultTableModel.addRow(arrayOf<Any>("Name", leaf.leafName))
469-
defaultTableModel.addRow(arrayOf<Any>("Level", leaf.level))
470-
defaultTableModel.addRow(arrayOf<Any>("Rule's name", leaf.ruleName))
471-
defaultTableModel.addRow(arrayOf<Any>("Rule's description", leaf.ruleDescription))
472-
defaultTableModel.addRow(arrayOf<Any>("Location", leaf.location))
473-
defaultTableModel.addRow(arrayOf<Any>("GitHub alert number", leaf.githubAlertNumber))
471+
defaultTableModel.addRow(arrayOf<Any>("Name", currentLeaf!!.leafName))
472+
defaultTableModel.addRow(arrayOf<Any>("Level", currentLeaf!!.level))
473+
defaultTableModel.addRow(arrayOf<Any>("Rule's name", currentLeaf!!.ruleName))
474+
defaultTableModel.addRow(arrayOf<Any>("Rule's description", currentLeaf!!.ruleDescription))
475+
defaultTableModel.addRow(arrayOf<Any>("Location", currentLeaf!!.location))
476+
defaultTableModel.addRow(arrayOf<Any>("GitHub alert number", currentLeaf!!.githubAlertNumber))
474477
defaultTableModel.addRow(
475478
arrayOf<Any>(
476479
"GitHub alert url",
@@ -527,27 +530,27 @@ class SarifViewerWindowFactory : ToolWindowFactory {
527530

528531
tableSteps.model = DefaultTableModel(arrayOf<Any>("Path"), 0)
529532

530-
leaf.steps.forEachIndexed { index, step ->
533+
currentLeaf!!.steps.forEachIndexed { index, step ->
531534
(tableSteps.model as DefaultTableModel).addRow(arrayOf<Any>("$index $step"))
532535
}
533536

534537
tableSteps.addMouseListener(object : MouseAdapter() {
535538
override fun mouseClicked(e: MouseEvent) {
536539
val row = tableInfos.rowAtPoint(e.point)
537-
val path = leaf.steps[row].split(":")
540+
val path = currentLeaf!!.steps[row].split(":")
538541
openFile(project, path[0], path[1].toInt())
539542
}
540543
})
541544

542545
details.isVisible = true
543546
openFile(
544547
project,
545-
leaf.location,
546-
leaf.address.split(":")[1].toInt(),
548+
currentLeaf!!.location,
549+
currentLeaf!!.address.split(":")[1].toInt(),
547550
0,
548-
leaf.level,
549-
leaf.ruleId,
550-
leaf.ruleDescription
551+
currentLeaf!!.level,
552+
currentLeaf!!.ruleId,
553+
currentLeaf!!.ruleDescription
551554
)
552555

553556
splitPane.setDividerLocation(0.5)
@@ -584,33 +587,43 @@ class SarifViewerWindowFactory : ToolWindowFactory {
584587
inlayModel.getBlockElementsInRange(0, editor.document.textLength).filter { it.renderer is MyCustomInlayRenderer }
585588
.forEach { it.dispose() }
586589

587-
VirtualFileManager.getInstance().findFileByNioPath(Path.of("${project.basePath}/$path"))
588-
?.let { virtualFile ->
589-
FileEditorManager.getInstance(project).openTextEditor(
590-
OpenFileDescriptor(
591-
project,
592-
virtualFile,
593-
lineNumber - 1,
594-
columnNumber
595-
),
596-
true // request focus to editor
597-
)
598-
val editor: Editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
599-
val inlayModel = editor.inlayModel
600-
601-
val offset = editor.document.getLineStartOffset(lineNumber - 1)
602-
603-
val icon = when (level) {
604-
"error" -> "🛑"
605-
"warning" -> "⚠️"
606-
"note" -> "📝"
607-
else -> ""
608-
}
609-
val description = "$icon $rule: $description"
610-
if (description.isNotEmpty()) {
611-
inlayModel.addBlockElement(offset, true, true, 1, MyCustomInlayRenderer(description))
612-
}
590+
val virtualFile =
591+
VirtualFileManager.getInstance().findFileByNioPath(Path.of("${project.basePath}/$path"))
592+
if (virtualFile != null) {
593+
FileEditorManager.getInstance(project).openTextEditor(
594+
OpenFileDescriptor(
595+
project,
596+
virtualFile,
597+
lineNumber - 1,
598+
columnNumber
599+
),
600+
true // request focus to editor
601+
)
602+
val editor: Editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
603+
val inlayModel = editor.inlayModel
604+
605+
val offset = editor.document.getLineStartOffset(lineNumber - 1)
606+
607+
val icon = when (level) {
608+
"error" -> "🛑"
609+
"warning" -> "⚠️"
610+
"note" -> "📝"
611+
else -> ""
612+
}
613+
val description = "$icon $rule: $description"
614+
if (description.isNotEmpty()) {
615+
inlayModel.addBlockElement(offset, true, true, 1, MyCustomInlayRenderer(description))
613616
}
617+
} else {
618+
// display error message
619+
Notifications.Bus.notify(
620+
Notification(
621+
"Sarif viewer",
622+
"File not found",
623+
"Can't find the file ${project.basePath}/$path",
624+
NotificationType.WARNING
625+
), project)
626+
}
614627
}
615628

616629
private fun clearJSplitPane() {

0 commit comments

Comments
 (0)