class CodeScanning::Rule

Public Class Methods

new(cop_name, severity = nil) click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 7
def initialize(cop_name, severity = nil)
  @cop_name = cop_name
  @severity = severity.to_s
  @cop = RuboCop::Cop::Cop.registry.find_by_cop_name(cop_name)
  @help = StringIO.new
end

Public Instance Methods

==(other) click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 26
def ==(other)
  badge.match?(other.badge)
end
Also aliased as: eql?
append_help(line) click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 18
def append_help(line)
  @help.print(line)
end
badge() click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 31
def badge
  @cop.badge
end
cop_config() click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 56
def cop_config
  @config ||= RuboCop::ConfigStore.new.for(Pathname.new(Dir.pwd))
  @cop_config ||= @config.for_cop(@cop.department.to_s)
                         .merge(@config.for_cop(@cop))
end
eql?(other)
Alias for: ==
help_empty?() click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 22
def help_empty?
  @help.size.zero?
end
id() click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 14
def id
  @cop_name
end
query_uri() click to toggle source

The URL for the docs are in this format: docs.rubocop.org/en/stable/cops_layout/#layoutblockendnewline

# File lib/code_scanning/rubocop/rule.rb, line 46
def query_uri
  kind = badge.department.to_s.downcase
  full_name = "#{kind}#{badge.cop_name.downcase}"
  "https://docs.rubocop.org/en/stable/cops_#{kind}/##{full_name}"
end
sarif_severity() click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 35
def sarif_severity
  cop_severity = @cop.new.send(:find_severity, nil, @severity)
  return cop_severity if %w[warning error].include?(cop_severity)
  return "note" if %w[refactor convention].include?(cop_severity)
  return "error" if cop_severity == "fatal"

  "none"
end
to_h() click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 62
def to_h
  properties = {
    "precision" => "very-high"
  }

  h = {
    "id" => @cop_name,
    "name" => @cop_name,
    "defaultConfiguration" => {
      "level" => sarif_severity
    },
    "properties" => properties
  }

  desc = cop_config["Description"]
  unless desc.nil?
    h["shortDescription"] = { "text" => desc }
    h["fullDescription"] = { "text" => desc }
    properties["description"] = desc
  end

  unless help_empty?
    help = @help.string
    h["help"] = {
      "text" => help,
      "markdown" => help
    }
    properties["queryURI"] = query_uri if badge.qualified?
  end

  if badge.qualified?
    kind = badge.department.to_s
    properties["tags"] = [kind.downcase]
  end
  h
end
to_json(opts = {}) click to toggle source
# File lib/code_scanning/rubocop/rule.rb, line 52
def to_json(opts = {})
  to_h.to_json(opts)
end