Sha256: 4858f82787555656f6d4939410275c7dfd36d97f14e0687a7b95dc74f1adb371

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module Danger
  # Lints files via Pronto through specified runners
  # Results are displayed as a table in markdown
  #
  # [Runners](https://github.com/mmozuras/pronto#runners)
  #
  # @example Lint files with Pronto and specified Pronto Runners
  #
  #          pronto.lint
  #
  # @see  RestlessThinker/danger-pronto
  # @tags pronto, linter
  #
  class DangerPronto < Plugin

    # Runs files through Pronto. Generates a `markdown` list of warnings.
    def lint(commit = nil)
      files = pronto(commit)
      return if files.empty?

      markdown offenses_message(files)
    end

    private

    # Executes pronto command
    # @param commit [String] hash/branch/tag
    # @return [Hash] Converted hash from pronto json output
    def pronto(specified_commit = nil)
      commit = "origin/master"
      commit = specified_commit if specified_commit.present?
      pronto_output = `#{'bundle exec ' if File.exists?('Gemfile')}pronto run -f json -c #{commit}`
      JSON.parse(pronto_output)
    end

    # Builds the message
    def offenses_message(offending_files)
      require 'terminal-table'

      message = "### Pronto violations\n\n"
      table = Terminal::Table.new(
        headings: %w(File Line Reason Runner),
        style: { border_i: '|' },
        rows: offending_files.map do |file|
          [file['path'], file['line'], file['message'], file['runner']]
        end
      ).to_s
      message + table.split("\n")[1..-2].join("\n")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danger-pronto-0.3.0 lib/danger_pronto/plugin.rb