|
| 1 | +require 'safe_yaml' |
| 2 | +SafeYAML::OPTIONS[:default_mode] = :safe |
| 3 | + |
| 4 | +module CC |
| 5 | + module Engine |
| 6 | + class Issue < SimpleDelegator |
| 7 | + MULTIPLIER_REGEX = %r{\[([\d\.]+)\/([\d\.]+)\]} |
| 8 | + DEFAULT_REMEDIATION_POINTS = 50_000 |
| 9 | + DEFAULT_BASE_POINTS = 200_000 |
| 10 | + DEFAULT_OVERAGE_POINTS = 50_000 |
| 11 | + |
| 12 | + def initialize(issue, path, cop_list: nil) |
| 13 | + @path = path |
| 14 | + @cop_list = cop_list |
| 15 | + |
| 16 | + super(issue) |
| 17 | + end |
| 18 | + |
| 19 | + # rubocop:disable Metrics/MethodLength |
| 20 | + def to_json |
| 21 | + hash = { |
| 22 | + type: "Issue", |
| 23 | + check_name: "Rubocop/#{cop_name}", |
| 24 | + description: message, |
| 25 | + categories: [category], |
| 26 | + remediation_points: remediation_points, |
| 27 | + location: { |
| 28 | + path: path, |
| 29 | + positions: positions, |
| 30 | + }, |
| 31 | + } |
| 32 | + hash[:content] = { body: content_body } if content_body.present? |
| 33 | + hash.to_json |
| 34 | + end |
| 35 | + |
| 36 | + def remediation_points |
| 37 | + if multiplier? |
| 38 | + base_points + overage_points |
| 39 | + else |
| 40 | + cop_definition.fetch("remediation_points", DEFAULT_REMEDIATION_POINTS) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + private |
| 45 | + |
| 46 | + attr_reader :path |
| 47 | + |
| 48 | + def multiplier? |
| 49 | + message.match(MULTIPLIER_REGEX) |
| 50 | + end |
| 51 | + |
| 52 | + def base_points |
| 53 | + cop_definition.fetch("base_points", DEFAULT_BASE_POINTS) |
| 54 | + end |
| 55 | + |
| 56 | + def cop_definition |
| 57 | + @cop_definition ||= cop_list.fetch(cop_name, {}) |
| 58 | + end |
| 59 | + |
| 60 | + def cop_list |
| 61 | + @cop_list ||= YAML.load_file(expand_config_path("cops.yml")) |
| 62 | + end |
| 63 | + |
| 64 | + def expand_config_path(path) |
| 65 | + File.expand_path("../../../../config/#{path}", __FILE__) |
| 66 | + end |
| 67 | + |
| 68 | + def overage_points |
| 69 | + overage_points = cop_definition. |
| 70 | + fetch("overage_points", DEFAULT_OVERAGE_POINTS) |
| 71 | + |
| 72 | + overage_points * multiplier |
| 73 | + end |
| 74 | + |
| 75 | + def multiplier |
| 76 | + result = message.scan(MULTIPLIER_REGEX) |
| 77 | + score, threshold = result[0] |
| 78 | + score.to_i - threshold.to_i |
| 79 | + end |
| 80 | + |
| 81 | + def category |
| 82 | + CategoryParser.new(cop_name).category |
| 83 | + end |
| 84 | + |
| 85 | + def positions |
| 86 | + { |
| 87 | + begin: { |
| 88 | + column: columns.first, |
| 89 | + line: lines.first, |
| 90 | + }, |
| 91 | + end: { |
| 92 | + column: columns.last, |
| 93 | + line: lines.last, |
| 94 | + } |
| 95 | + } |
| 96 | + end |
| 97 | + |
| 98 | + # Increments column values as columns are 0-based in parser |
| 99 | + def columns |
| 100 | + return @columns if defined?(@columns) |
| 101 | + |
| 102 | + end_column = location.try(:last_column) || location.column |
| 103 | + @columns = [location.column + 1, end_column + 1] |
| 104 | + end |
| 105 | + |
| 106 | + def lines |
| 107 | + return @lines if defined?(@lines) |
| 108 | + |
| 109 | + begin_line = location.try(:first_line) || location.line |
| 110 | + end_line = location.try(:last_line) || location.line |
| 111 | + @lines = [begin_line, end_line] |
| 112 | + end |
| 113 | + |
| 114 | + def content_body |
| 115 | + return @content_body if defined?(@content_body) |
| 116 | + |
| 117 | + content_path = expand_config_path("contents/#{cop_name.underscore}.md") |
| 118 | + @content_body = File.exist?(content_path) && File.read(content_path) |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments