Skip to content
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

More precise flow step navigation #51

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Expand Up @@ -9,7 +9,8 @@ data class View(
companion object {
val RULE = View("rules", "View by rules")
val LOCATION = View("location", "View by location")
val views = arrayOf(RULE, LOCATION)
val ALERT_NUMBER = View("alert num", "View by GHAS alert number")
val views = arrayOf(RULE, LOCATION, ALERT_NUMBER)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.github.adrienpessu.sarifviewer.models.Root
import com.github.adrienpessu.sarifviewer.models.View
import com.github.adrienpessu.sarifviewer.utils.GitHubInstance
import com.google.common.base.Strings
import com.intellij.openapi.components.Service
import com.intellij.util.alsoIfNull
import java.net.HttpURLConnection
import java.net.URL
import java.util.*


@Service(Service.Level.PROJECT)
Expand All @@ -28,7 +30,7 @@
return ids.map { id ->
val sarifFromGitHub = getSarifFromGitHub(github, repositoryFullName, id)
val sarif: SarifSchema210 = objectMapper.readValue(sarifFromGitHub)
sarif.alsoIfNull { SarifSchema210() }
sarif.alsoIfNull { SarifSchema210() }
}
}

Expand All @@ -47,7 +49,7 @@

}

fun analyseSarif(sarif: SarifSchema210, view: View): HashMap<String, MutableList<Leaf>> {
fun analyseSarif(sarif: SarifSchema210, view: View): MutableMap<String, MutableList<Leaf>> {

when (view) {
View.RULE -> {
Expand All @@ -69,6 +71,7 @@
}
return map
}

View.LOCATION -> {
val map = HashMap<String, MutableList<Leaf>>()
try {
Expand All @@ -88,37 +91,72 @@
}
return map
}

View.ALERT_NUMBER -> {
val map = TreeMap<String, MutableList<Leaf>>();

Check warning on line 96 in src/main/kotlin/com/github/adrienpessu/sarifviewer/services/SarifService.kt

View workflow job for this annotation

GitHub Actions / Inspect code

Redundant semicolon

Redundant semicolon
try {
sarif.runs.forEach { run ->
run?.results?.forEach { result ->
val element = leaf(result)
val key = if (Strings.isNullOrEmpty(element.githubAlertNumber)) {
"Missing alert number"
} else {
element.githubAlertNumber
}
if (map.containsKey(key)) {
map[key]?.add(element)
} else {
map[key] = mutableListOf(element)
}
}
}
} catch (e: Exception) {
throw SarifViewerException.INVALID_SARIF
}
return map.toSortedMap(Comparator.comparingInt { k ->
try {
Integer.valueOf(k)
} catch (e: NumberFormatException) {
Integer.MIN_VALUE
}
})
}

else -> {
throw SarifViewerException.INVALID_VIEW
}
}


}

private fun leaf(result: Result): Leaf {
val additionalProperties = result.properties?.additionalProperties ?: mapOf()
val element = Leaf(
leafName = result.message.text ?: "",
address = "${result.locations[0].physicalLocation.artifactLocation.uri}:${result.locations[0].physicalLocation.region.startLine}",
steps = result.codeFlows?.get(0)?.threadFlows?.get(0)?.locations?.map { "${it.location.physicalLocation.artifactLocation.uri}:${it.location.physicalLocation.region.startLine}" }
?: listOf(),
location = result.locations[0].physicalLocation.artifactLocation.uri,
ruleId = result.ruleId,
ruleName = result.rule?.id ?: "",
ruleDescription = result.message.text ?: "",
level = result.level.toString(),
kind = result.kind.toString(),
githubAlertNumber = additionalProperties["github/alertNumber"]?.toString() ?: "",
githubAlertUrl = additionalProperties["github/alertUrl"]?.toString() ?: ""
leafName = result.message.text ?: "",
address = "${result.locations[0].physicalLocation.artifactLocation.uri}:${result.locations[0].physicalLocation.region.startLine}",
steps = result.codeFlows?.get(0)?.threadFlows?.get(0)?.locations?.map {
"${it.location.physicalLocation.artifactLocation.uri}:" +
"${it.location.physicalLocation.region.startLine}:" +
"${it.location.physicalLocation.region.startColumn}:" +
"${it.location.physicalLocation.region.endLine}:" +
"${it.location.physicalLocation.region.endColumn}"
}
?: listOf(),
location = result.locations[0].physicalLocation.artifactLocation.uri,
ruleId = result.ruleId,
ruleName = result.rule?.id ?: "",
ruleDescription = result.message.text ?: "",
level = result.level.toString(),
kind = result.kind.toString(),
githubAlertNumber = additionalProperties["github/alertNumber"]?.toString() ?: "",
githubAlertUrl = additionalProperties["github/alertUrl"]?.toString() ?: ""
)
return element
}

fun getPullRequests(github: GitHubInstance, repositoryFullName: String, branchName: String = "main"): List<*>? {
val head = "${repositoryFullName.split("/")[0]}:$branchName"
val connection = URL("${github.apiBase}/repos/$repositoryFullName/pulls?state=open&head=$head")
.openConnection() as HttpURLConnection
.openConnection() as HttpURLConnection

connection.apply {
requestMethod = "GET"
Expand All @@ -139,14 +177,14 @@
}

private fun getAnalysisFromGitHub(
github: GitHubInstance,
repositoryFullName: String,
branchName: String = "main"
github: GitHubInstance,
repositoryFullName: String,
branchName: String = "main"
): String {

val s = "${github.apiBase}/repos/$repositoryFullName/code-scanning/analyses?ref=$branchName"
val connection = URL(s)
.openConnection() as HttpURLConnection
.openConnection() as HttpURLConnection

connection.apply {
requestMethod = "GET"
Expand Down Expand Up @@ -189,7 +227,7 @@

private fun getSarifFromGitHub(github: GitHubInstance, repositoryFullName: String, analysisId: Int): String {
val connection = URL("${github.apiBase}/repos/$repositoryFullName/code-scanning/analyses/$analysisId")
.openConnection() as HttpURLConnection
.openConnection() as HttpURLConnection

connection.apply {
requestMethod = "GET"
Expand Down
Loading
Loading