Skip to content

Added a toggle Action Button to allow the user to switch the labelling of warnings in the plugin #134

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.adrienpessu.sarifviewer.actions

import com.github.adrienpessu.sarifviewer.toolWindow.SarifViewerWindowFactory
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent

class RuleIDorDescriptionAction : AnAction("Toggle Rule ID or Description") {

var myToolWindow: SarifViewerWindowFactory.MyToolWindow? = null

override fun actionPerformed(e: AnActionEvent) {
myToolWindow?.toggleRuleIdOrDescription()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ data class View(
val value: String = ""
) {
override fun toString() = value

var ruleIdToggle: Boolean = true

companion object {
val RULE = View("rules", "View by rules")
val LOCATION = View("location", "View by location")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ class SarifService {
sarif.runs.forEach { run ->
run?.results?.forEach { result ->
val element = leaf(result)
val key = result.rule?.id ?: result.correlationGuid?.toString() ?: result.message.text
var key = "init"
if (view.ruleIdToggle)
key = result.rule?.id ?: result.correlationGuid?.toString() ?: result.message.text
else
key = result.rule?.id ?: result.correlationGuid?.toString() ?: result.ruleId
Comment on lines +59 to +63
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var key = if (view.ruleIdToggle)
                                (result?.message?.text ?: result.rule?.id ?: result.correlationGuid?.toString())!!
                            else
                                (result?.ruleId ?: result.rule?.id ?: result.correlationGuid?.toString())!!

if (map.containsKey(key)) {
map[key]?.add(element)
} else {
Expand All @@ -74,6 +78,8 @@ class SarifService {
try {
sarif.runs.forEach { run ->
run?.results?.forEach { result ->
if (view.ruleIdToggle)
result.message.text = result.ruleId
val element = leaf(result)
val key = result.locations[0].physicalLocation.artifactLocation.uri
if (map.containsKey(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.contrastsecurity.sarif.SarifSchema210
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.adrienpessu.sarifviewer.actions.OpenLocalAction
import com.github.adrienpessu.sarifviewer.actions.RefreshAction
import com.github.adrienpessu.sarifviewer.actions.RuleIDorDescriptionAction
import com.github.adrienpessu.sarifviewer.configurable.Settings
import com.github.adrienpessu.sarifviewer.configurable.SettingsState
import com.github.adrienpessu.sarifviewer.exception.SarifViewerException
Expand Down Expand Up @@ -82,11 +83,14 @@ class SarifViewerWindowFactory : ToolWindowFactory {
init {
val actionManager = ActionManager.getInstance()

val toggleRuleIdOrDescriptionAction = actionManager.getAction("RuleIDorDescriptionAction")
(toggleRuleIdOrDescriptionAction as RuleIDorDescriptionAction).myToolWindow = this
val openLocalFileAction = actionManager.getAction("OpenLocalFileAction")
(openLocalFileAction as OpenLocalAction).myToolWindow = this
val refreshAction = actionManager.getAction("RefreshAction")
(refreshAction as RefreshAction).myToolWindow = this
val actions = ArrayList<AnAction>()
actions.add(toggleRuleIdOrDescriptionAction)
actions.add(openLocalFileAction)
actions.add(refreshAction)

Expand All @@ -98,6 +102,8 @@ class SarifViewerWindowFactory : ToolWindowFactory {
internal var currentBranch: GitLocalBranch? = null

private var localMode = false
private lateinit var extractSarifFromFile: HashMap<String, MutableList<Leaf>>
private lateinit var selectedFile: File
private val service = toolWindow.project.service<SarifService>()
private val project = toolWindow.project
private var main = ScrollPaneFactory.createScrollPane()
Expand Down Expand Up @@ -380,14 +386,21 @@ class SarifViewerWindowFactory : ToolWindowFactory {
treeBuilding(map)
}

fun toggleRuleIdOrDescription() {
if (localMode) {
currentView.ruleIdToggle = !currentView.ruleIdToggle
extractSarifFromFile = extractSarifFromFile(selectedFile)
treeBuilding(extractSarifFromFile)
}
}
fun openLocalFile() {
val fileChooser = JFileChooser()
fileChooser.fileFilter = FileNameExtensionFilter("SARIF files", "sarif")
SwingUtilities.invokeLater {
val returnValue = fileChooser.showOpenDialog(null)
if (returnValue == JFileChooser.APPROVE_OPTION) {
val selectedFile: File = fileChooser.selectedFile
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
val selectedFile: File = fileChooser.selectedFile
selectedFile = fileChooser.selectedFile

without that the selectedFile attribute is never initiated and the toggle doesn't refresh the tree

val extractSarifFromFile = extractSarifFromFile(selectedFile)
extractSarifFromFile = extractSarifFromFile(selectedFile)
treeBuilding(extractSarifFromFile)
localMode = true
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<resource-bundle>messages.MyBundle</resource-bundle>

<actions>
<action id="RuleIDorDescriptionAction" icon="AllIcons.Actions.Diff" class="com.github.adrienpessu.sarifviewer.actions.RuleIDorDescriptionAction" text="Toggle Rule ID or Description">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the AllIcons.Actions.Diff misleading.
Could you use a more neutral icon like : AllIcons.Debugger.VariablesTab and make it change color when toggled (using AllIcons.Debugger.Value for example)

</action>
<action id="OpenLocalFileAction" icon="AllIcons.Actions.MenuOpen" class="com.github.adrienpessu.sarifviewer.actions.OpenLocalAction" text="Open local SARIF file">
</action>
<action id="RefreshAction" icon="AllIcons.Actions.ForceRefresh" class="com.github.adrienpessu.sarifviewer.actions.RefreshAction" text="Refresh from GitHub">
Expand Down