@@ -9,10 +9,13 @@ import com.github.adrienpessu.sarifviewer.models.Leaf
99import com.github.adrienpessu.sarifviewer.models.Root
1010import com.github.adrienpessu.sarifviewer.models.View
1111import com.github.adrienpessu.sarifviewer.utils.GitHubInstance
12+ import com.google.common.base.Strings
1213import com.intellij.openapi.components.Service
1314import com.intellij.util.alsoIfNull
1415import java.net.HttpURLConnection
1516import java.net.URL
17+ import java.util.Comparator
18+ import java.util.TreeMap
1619
1720
1821@Service(Service .Level .PROJECT )
@@ -28,7 +31,7 @@ class SarifService {
2831 return ids.map { id ->
2932 val sarifFromGitHub = getSarifFromGitHub(github, repositoryFullName, id)
3033 val sarif: SarifSchema210 = objectMapper.readValue(sarifFromGitHub)
31- sarif.alsoIfNull { SarifSchema210 () }
34+ sarif.alsoIfNull { SarifSchema210 () }
3235 }
3336 }
3437
@@ -47,7 +50,7 @@ class SarifService {
4750
4851 }
4952
50- fun analyseSarif (sarif : SarifSchema210 , view : View ): HashMap <String , MutableList <Leaf >> {
53+ fun analyseSarif (sarif : SarifSchema210 , view : View ): MutableMap <String , MutableList <Leaf >> {
5154
5255 when (view) {
5356 View .RULE -> {
@@ -69,6 +72,7 @@ class SarifService {
6972 }
7073 return map
7174 }
75+
7276 View .LOCATION -> {
7377 val map = HashMap <String , MutableList <Leaf >>()
7478 try {
@@ -88,37 +92,66 @@ class SarifService {
8892 }
8993 return map
9094 }
95+
96+ View .ALERT_NUMBER -> {
97+ val map = TreeMap <String , MutableList <Leaf >>();
98+ try {
99+ sarif.runs.forEach { run ->
100+ run?.results?.forEach { result ->
101+ val element = leaf(result)
102+ val key = if (Strings .isNullOrEmpty(element.githubAlertNumber)) {
103+ " Missing alert number"
104+ } else {
105+ element.githubAlertNumber
106+ }
107+ if (map.containsKey(key)) {
108+ map[key]?.add(element)
109+ } else {
110+ map[key] = mutableListOf (element)
111+ }
112+ }
113+ }
114+ } catch (e: Exception ) {
115+ throw SarifViewerException .INVALID_SARIF
116+ }
117+ return map.toSortedMap(Comparator .comparingInt { k ->
118+ try {
119+ Integer .valueOf(k)
120+ } catch (e: NumberFormatException ) {
121+ Integer .MIN_VALUE
122+ }
123+ })
124+ }
125+
91126 else -> {
92127 throw SarifViewerException .INVALID_VIEW
93128 }
94129 }
95-
96-
97130 }
98131
99132 private fun leaf (result : Result ): Leaf {
100133 val additionalProperties = result.properties?.additionalProperties ? : mapOf ()
101134 val element = Leaf (
102- leafName = result.message.text ? : " " ,
103- address = " ${result.locations[0 ].physicalLocation.artifactLocation.uri} :${result.locations[0 ].physicalLocation.region.startLine} " ,
104- steps = result.codeFlows?.get(0 )?.threadFlows?.get(0 )?.locations?.map { " ${it.location.physicalLocation.artifactLocation.uri} :${it.location.physicalLocation.region.startLine} " }
105- ? : listOf (),
106- location = result.locations[0 ].physicalLocation.artifactLocation.uri,
107- ruleId = result.ruleId,
108- ruleName = result.rule?.id ? : " " ,
109- ruleDescription = result.message.text ? : " " ,
110- level = result.level.toString(),
111- kind = result.kind.toString(),
112- githubAlertNumber = additionalProperties[" github/alertNumber" ]?.toString() ? : " " ,
113- githubAlertUrl = additionalProperties[" github/alertUrl" ]?.toString() ? : " "
135+ leafName = result.message.text ? : " " ,
136+ address = " ${result.locations[0 ].physicalLocation.artifactLocation.uri} :${result.locations[0 ].physicalLocation.region.startLine} " ,
137+ steps = result.codeFlows?.get(0 )?.threadFlows?.get(0 )?.locations?.map { " ${it.location.physicalLocation.artifactLocation.uri} :${it.location.physicalLocation.region.startLine} " }
138+ ? : listOf (),
139+ location = result.locations[0 ].physicalLocation.artifactLocation.uri,
140+ ruleId = result.ruleId,
141+ ruleName = result.rule?.id ? : " " ,
142+ ruleDescription = result.message.text ? : " " ,
143+ level = result.level.toString(),
144+ kind = result.kind.toString(),
145+ githubAlertNumber = additionalProperties[" github/alertNumber" ]?.toString() ? : " " ,
146+ githubAlertUrl = additionalProperties[" github/alertUrl" ]?.toString() ? : " "
114147 )
115148 return element
116149 }
117150
118151 fun getPullRequests (github : GitHubInstance , repositoryFullName : String , branchName : String = "main"): List <* >? {
119152 val head = " ${repositoryFullName.split(" /" )[0 ]} :$branchName "
120153 val connection = URL (" ${github.apiBase} /repos/$repositoryFullName /pulls?state=open&head=$head " )
121- .openConnection() as HttpURLConnection
154+ .openConnection() as HttpURLConnection
122155
123156 connection.apply {
124157 requestMethod = " GET"
@@ -139,14 +172,14 @@ class SarifService {
139172 }
140173
141174 private fun getAnalysisFromGitHub (
142- github : GitHubInstance ,
143- repositoryFullName : String ,
144- branchName : String = "main"
175+ github : GitHubInstance ,
176+ repositoryFullName : String ,
177+ branchName : String = "main"
145178 ): String {
146179
147180 val s = " ${github.apiBase} /repos/$repositoryFullName /code-scanning/analyses?ref=$branchName "
148181 val connection = URL (s)
149- .openConnection() as HttpURLConnection
182+ .openConnection() as HttpURLConnection
150183
151184 connection.apply {
152185 requestMethod = " GET"
@@ -189,7 +222,7 @@ class SarifService {
189222
190223 private fun getSarifFromGitHub (github : GitHubInstance , repositoryFullName : String , analysisId : Int ): String {
191224 val connection = URL (" ${github.apiBase} /repos/$repositoryFullName /code-scanning/analyses/$analysisId " )
192- .openConnection() as HttpURLConnection
225+ .openConnection() as HttpURLConnection
193226
194227 connection.apply {
195228 requestMethod = " GET"
0 commit comments