All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jetbrains.pluginverifier.response.sarif.SarifFormatConverter.kt Maven / Gradle / Ivy

Go to download

JetBrains Plugin Verifier Classes for IntelliJ Platform integration with API usage detection and reporting.

There is a newer version: 1.379
Show newest version
package com.jetbrains.pluginverifier.response.sarif

import com.jetbrains.pluginverifier.PluginVerificationResult
import com.jetbrains.pluginverifier.PluginVerificationTarget
import com.jetbrains.pluginverifier.response.VerificationResultTypeDto
import com.jetbrains.pluginverifier.response.convert
import com.jetbrains.pluginverifier.response.convertResultType

fun PluginVerificationResult.toSarif(): PluginVerificationResultSARIF {
  return when (this) {
    is PluginVerificationResult.Verified -> generateReport(
      rules = buildRules(),
      invocations = buildVerifiedInspections(),
    )
    is PluginVerificationResult.InvalidPlugin -> generateReport(
      rules = buildPluginStructureRules(),
      invocations = buildPluginStructureInspections(),
    )
    else -> generateReport(
      rules = buildSingleRule(),
      invocations = buildSingleInvocation(),
    )
  }
}


private fun PluginVerificationResult.generateReport(
  rules: List,
  invocations: List
): PluginVerificationResultSARIF {
  return PluginVerificationResultSARIF(
    runs = listOf(
      Runner(
        properties = toPluginVerifierPropertiesBag(),
        automationDetails = toAutomationDetails(),
        versionControlProvenance = toVersionControlProvenance(),
        invocations = toInvocationStatus(),
        tool = toToolWithRules(rules),
        results = invocations
      )
    )
  )
}

private fun PluginVerificationResult.toInvocationStatus(): List {
  val code = when (this) {
    is PluginVerificationResult.NotFound, is PluginVerificationResult.FailedToDownload -> 127
    is PluginVerificationResult.InvalidPlugin -> 1
    is PluginVerificationResult.Verified -> 0
  }
  val executionSuccessful = if (this is PluginVerificationResult.Verified) {
    this.convertResultType() != VerificationResultTypeDto.PROBLEMS
  } else false
  return listOf(
    InvocationStatus(
      exitCode = code,
      executionSuccessful = executionSuccessful,
      exitCodeDescription = this.verificationVerdict
    )
  )
}

private fun PluginVerificationResult.toAutomationDetails(): AutomationDetails {
  val plugin = this.plugin
  return AutomationDetails(id = plugin.presentableName + " " + plugin.presentableSinceUntilRange)
}

private fun PluginVerificationResult.toPluginVerifierPropertiesBag(): PluginVerifierPropertiesBag {
  val dependenciesGraph = if (this is PluginVerificationResult.Verified) dependenciesGraph.convert() else null
  val dynamicPluginStatus = if (this is PluginVerificationResult.Verified) dynamicPluginStatus?.convert() else null
  val ideVersion = verificationTarget as PluginVerificationTarget.IDE
  val type = when (this) {
    is PluginVerificationResult.Verified -> this.convertResultType()
    is PluginVerificationResult.NotFound, is PluginVerificationResult.FailedToDownload -> VerificationResultTypeDto.NON_DOWNLOADABLE
    is PluginVerificationResult.InvalidPlugin -> VerificationResultTypeDto.INVALID_PLUGIN
  }
  return PluginVerifierPropertiesBag(
    ideVersion = ideVersion.ideVersion.asString(),
    javaVersion = ideVersion.jdkVersion.javaVersion,
    resultType = type,
    verdict = this.verificationVerdict,
    dependenciesGraph = dependenciesGraph,
    dynamicPluginStatus = dynamicPluginStatus,
  )
}

private fun toVersionControlProvenance(): List {
  return listOf(
    VersionControlProvenance(
      repositoryUri = "TODO()" // TODO()
    )
  )
}

private fun toToolWithRules(rules: List): Tool {
  return Tool(Driver(rules = rules))
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy