Class: Pronto::AnsibleLint

Inherits:
Runner
  • Object
show all
Defined in:
lib/pronto/ansible_lint.rb

Overview

AnsibleLint Pronto Runner. Entry point is run

Constant Summary collapse

YAML_FILE_EXTENSIONS =
['.yml', '.yaml'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(patches, commit = nil) ⇒ AnsibleLint

Returns a new instance of AnsibleLint.



10
11
12
# File 'lib/pronto/ansible_lint.rb', line 10

def initialize(patches, commit = nil)
  super(patches, commit)
end

Instance Method Details

#executableObject



14
15
16
# File 'lib/pronto/ansible_lint.rb', line 14

def executable
  'ansible-lint'.freeze
end

#filesObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/pronto/ansible_lint.rb', line 18

def files
  return [] if @patches.nil?

  @files ||= begin
   @patches
     .select { |patch| patch.additions > 0 }
     .map(&:new_file_full_path)
     .compact
 end
end

#filter_yaml_files(all_files) ⇒ Object



67
68
69
70
# File 'lib/pronto/ansible_lint.rb', line 67

def filter_yaml_files(all_files)
  all_files.select { |file| yaml? file.to_s }
           .map { |file| file.to_s.shellescape }
end

#git_repo_pathObject



110
111
112
113
# File 'lib/pronto/ansible_lint.rb', line 110

def git_repo_path
  @git_repo_path ||= Rugged::Repository.discover(File.expand_path(Dir.pwd))
                                       .workdir
end

#messages(json_list) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pronto/ansible_lint.rb', line 97

def messages(json_list)
  json_list.map do |error|
    patch_line = patch_line_for_offence(error[:file_path],
                                        error[:line_number])
    next if patch_line.nil?

    description = error[:message]
    path = patch_line.patch.delta.new_file[:path]
    Message.new(path, patch_line, error[:level].to_sym,
                description, nil, self.class)
  end
end

#parse_output(executable_output) ⇒ Object



72
73
74
75
# File 'lib/pronto/ansible_lint.rb', line 72

def parse_output(executable_output)
  lines = executable_output.split("\n")
  lines.map { |line| parse_output_line(line) }
end

#parse_output_line(line) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pronto/ansible_lint.rb', line 77

def parse_output_line(line)
  splits = line.strip.split(':')
  message = splits[2..].join(':').strip
  {
    file_path: splits[0],
    line_number: splits[1].to_i,
    column_number: 0,
    message: message,
    level: violation_level(message)
  }
end

#patch_line_for_offence(path, lineno) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pronto/ansible_lint.rb', line 29

def patch_line_for_offence(path, lineno)
  patch_node = @patches.find do |patch|
    patch.new_file_full_path.to_s == path
  end

  return if patch_node.nil?

  patch_node.added_lines.find do |patch_line|
    patch_line.new_lineno == lineno
  end
end

#runObject



41
42
43
44
45
46
47
# File 'lib/pronto/ansible_lint.rb', line 41

def run
  if files.any?
    messages(run_ansible_lint)
  else
    []
  end
end

#run_ansible_lintObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pronto/ansible_lint.rb', line 49

def run_ansible_lint
  Dir.chdir(git_repo_path) do
    yaml_files = filter_yaml_files(files)
    files = yaml_files.join(' ')
    extra = ENV['PRONTO_ANSIBLE_LINT_OPTS']
    if !files.empty?
      cmd = "#{executable} --nocolor --parseable-severity #{extra} #{files}"
      parse_output `#{cmd}`
    else
      []
    end
  end
end

#violation_level(message) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/pronto/ansible_lint.rb', line 89

def violation_level(message)
  if message.split[1].include? 'HIGH'
    'error'
  else
    'warning'
  end
end

#yaml?(file) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/pronto/ansible_lint.rb', line 63

def yaml?(file)
  YAML_FILE_EXTENSIONS.select { |extension| file.end_with? extension }.any?
end