Class: Danger::DangerLocalRules

Inherits:
Plugin
  • Object
show all
Defined in:
lib/local_rules/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rules_fileObject

Returns the value of attribute rules_file.



6
7
8
# File 'lib/local_rules/plugin.rb', line 6

def rules_file
  @rules_file
end

Instance Method Details

#checkObject



12
13
14
15
16
17
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
# File 'lib/local_rules/plugin.rb', line 12

def check
  diff = github.pr_diff
  return if diff.nil?

  failure_rules = rules['failure']
  warning_rules = rules['warning']
  return if failure_rules.nil? && warning_rules.nil?

  if diff.match(Regexp.union(failure_rules.keys + warning_rules.keys))
    regexp_to_fail = Regexp.union(failure_rules.keys)
    regexp_to_warn = Regexp.union(warning_rules.keys)

    GitDiffParser.parse(diff).each do |changed_file|
      next if changed_file.file == rules_file
      changed_file.changed_lines.each do |changed_line|
        content = changed_line.content
        # Only checks added contents
        next unless content.start_with?('+')

        if (content.match(regexp_to_fail))
          content.match(regexp_to_fail) do |data|
            fail(failure_rules[data[0]], file: changed_file.file, line: changed_line.number)
          end
        end

        if (content.match(regexp_to_warn))
          content.match(regexp_to_warn) do |data|
            warn(warning_rules[data[0]], file: changed_file.file, line: changed_line.number)
          end
        end
      end
    end
  end
end