Module: RailVerdict::Reporters::Sarif

Defined in:
lib/rail_verdict/reporters/sarif.rb

Constant Summary collapse

SEVERITY_LEVEL =
{
  "critical" => "error",
  "high" => "error",
  "medium" => "warning",
  "low" => "note",
  "info" => "note"
}.freeze

Class Method Summary collapse

Class Method Details

.render(result, findings: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rail_verdict/reporters/sarif.rb', line 18

def render(result, findings: nil)
  all_findings = findings || result.findings
  rules = dedup_rules(all_findings)
  results = all_findings.map { |finding| finding_to_result(finding) }.sort_by { |entry| [entry["ruleId"].to_s, entry.dig("locations", 0, "physicalLocation", "artifactLocation", "uri").to_s, entry.dig("locations", 0, "physicalLocation", "region", "startLine").to_i] }

  {
    "version" => "2.1.0",
    "$schema" => "https://json.schemastore.org/sarif-2.1.0.json",
    "runs" => [
      {
        "tool" => {
          "driver" => {
            "name" => "RailVerdict",
            "version" => RailVerdict::VERSION,
            "informationUri" => "https://railverdict.dev",
            "rules" => rules
          }
        },
        "results" => results,
        "invocations" => [
          {
            "executionSuccessful" => result.completion_status == "complete",
            "toolExecutionNotifications" => result.decision_reasons.map { |reason| { "message" => { "text" => reason.fetch("message") }, "level" => "note", "descriptor" => { "id" => reason.fetch("code") } } }
          }
        ]
      }
    ]
  }
end

.render_json(result, findings: nil) ⇒ Object



48
49
50
# File 'lib/rail_verdict/reporters/sarif.rb', line 48

def render_json(result, findings: nil)
  JSON.generate(render(result, findings: findings)) + "\n"
end