Sha256: 7a4adffd3e5b5bf0ad4355da9e42e9ffbc3a6f108663177c6bc8054e3cc02299

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require 'json'

module Danger
  class DangerKtlint < Plugin
    # TODO: Lint all files if `filtering: false`
    attr_accessor :filtering

    # Run ktlint task using command line interface
    # Will fail if `ktlint` is not installed
    # Skip lint task if files changed are empty
    # @return [void]
    # def lint(inline_mode: false)
    def lint(inline_mode: false)
      unless ktlint_exists?
        fail("Couldn't find ktlint command. Install first.")
        return
      end

      targets = target_files(git.added_files + git.modified_files)
      return if targets.empty?

      results = JSON.parse(`ktlint #{targets.join(' ')} --reporter=json --relative`)
      return if results.empty?

      if inline_mode
        send_inline_comments(results)
      else
        send_markdown_comment(results)
      end
    end

    # Comment to a PR by ktlint result json
    #
    # // Sample ktlint result
    # [
    #   {
    #     "file": "app/src/main/java/com/mataku/Model.kt",
    # 		"errors": [
    # 			{
    # 				"line": 46,
    # 				"column": 1,
    # 				"message": "Unexpected blank line(s) before \"}\"",
    # 				"rule": "no-blank-line-before-rbrace"
    # 			}
    # 		]
    # 	}
    # ]
    def send_markdown_comment(results)
      results.each {|result|
        result['errors'].each {|error|
          file = "#{result['file']}#L#{error['line']}"
          message = "#{github.html_link(file)} has linter issue: #{error['message']}"
          fail(message)
        }
      }
    end

    def send_inline_comments(results)
      results.each do |result|
        result['errors'].each do |error|
          file = result['file']
          message = error['message']
          line = error['line']
          fail(message, file: result['file'], line: line)
        end
      end
    end

    def target_files(changed_files)
      changed_files.select do |file|
        file.end_with?('.kt')
      end
    end

    private

    def ktlint_exists?
      system 'which ktlint > /dev/null 2>&1' 
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danger-ktlint-0.0.2 lib/ktlint/plugin.rb