Sha256: c8519a7e02ae241738d5ab8085c37b0ed661cd0d23edf201e2f0bd6563280188

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require "json"
require_relative "rule"

module CodeScanning
  class SarifFormatter < RuboCop::Formatter::BaseFormatter
    def initialize(output, options = {})
      super
      @sarif = {
        "$schema" => "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
        "version" => "2.1.0"
      }
      @rules_map = {}
      @rules = []
      @results = []
      @sarif["runs"] = [
        {
          "tool" => {
            "driver" => {"name" => "Standard", "rules" => @rules }
          },
          "results" => @results
        }
      ]
    end

    def get_rule(cop_name, severity)
      r = @rules_map[cop_name]
      if r.nil?
        rule = Rule.new(cop_name, severity&.name)
        r = @rules_map[cop_name] = [rule, @rules.size]
        @rules << rule
      end

      r
    end

    def file_finished(file, offenses)
      relative_path = RuboCop::PathUtil.relative_path(file)

      offenses.each do |o|
        rule, rule_index = get_rule(o.cop_name, o.severity)
        @results << {
          "ruleId" => rule.id,
          "ruleIndex" => rule_index,
          "message" => {
            "text" => o.message
          },
          "locations" => [
            {
              "physicalLocation" => {
                "artifactLocation" => {
                  "uri" => relative_path,
                  "uriBaseId" => "%SRCROOT%",
                  "index" => 0
                },
                "region" => {
                  "startLine" => o.line,
                  "startColumn" => o.real_column,
                  "endColumn" => o.last_column
                }
              }
            }
          ],
          "partialFingerprints" => {
            # This will be computed by the upload action for now
          }
        }
      end
    end

    def finished(_inspected_files)
      output.print(sarif_json)
    end

    def sarif_json
      JSON.pretty_generate(@sarif)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
code-scanning-standard-0.0.1.alpha lib/code_scanning/standard/sarif_formatter.rb