class Pronto::YAMLLint

runner for yamllint

Public Instance Methods

run() click to toggle source
# File lib/pronto/yamllint_runner.rb, line 11
def run
  return [] if !@patches || @patches.count.zero?

  @patches
    .select { |patch| patch.additions > 0 && yaml?(patch.new_file_full_path) }
    .map { |patch| inspect(patch) }
    .flatten.compact
end
yamllint_opts() click to toggle source
# File lib/pronto/yamllint_runner.rb, line 7
def yamllint_opts
  ENV['YAMLLINT_OPTS']
end

Private Instance Methods

git_repo_path() click to toggle source
# File lib/pronto/yamllint_runner.rb, line 26
def git_repo_path
  @git_repo_path ||= Rugged::Repository.discover(File.expand_path(Dir.pwd)).workdir
end
inspect(patch) click to toggle source
# File lib/pronto/yamllint_runner.rb, line 30
def inspect(patch)
  line_numbers = offending_line_numbers(patch)[0]
  lines = offending_line_numbers(patch)[1]
  line_numbers.map do |line_number|
    patch
      .added_lines
      .select { |line| line.new_lineno == line_number }
      .map { |line| new_message(lines[line_number], line) }
  end
end
new_message(offence, line) click to toggle source
# File lib/pronto/yamllint_runner.rb, line 41
def new_message(offence, line)
  path = line.patch.delta.new_file[:path]
  level = if offence.include?('[error]')
            :error
          else
            :warning
          end

  Message.new(path, line, level, offence, nil, self.class)
end
offending_line_numbers(patch) click to toggle source
# File lib/pronto/yamllint_runner.rb, line 52
def offending_line_numbers(patch)
  line_numbers = []
  lines = []

  Dir.chdir(git_repo_path) do
    escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s)

    output = `yamllint -f parsable #{yamllint_opts} #{escaped_file_path}`
    output.split("\n").each do |l|
      line_number = l.split(':')[1].to_i
      line_numbers << line_number
      lines[line_number] = l.split(patch.new_file_full_path.to_s + ':')[1]
    end

    [line_numbers, lines]
  end
end
yaml?(path) click to toggle source
# File lib/pronto/yamllint_runner.rb, line 22
def yaml?(path)
  path.to_s.end_with?('.yaml', 'yml')
end