Class: Danger::DangerKtlint

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

Defined Under Namespace

Classes: UnexpectedLimitTypeError, UnsupportedServiceError

Constant Summary collapse

AVAILABLE_SERVICES =
[:github, :gitlab, :bitbucket_server]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filteringObject

TODO: Lint all files if ‘filtering: false`



18
19
20
# File 'lib/ktlint/plugin.rb', line 18

def filtering
  @filtering
end

#report_fileObject

Returns the value of attribute report_file.



20
21
22
# File 'lib/ktlint/plugin.rb', line 20

def report_file
  @report_file
end

#report_files_patternObject

Returns the value of attribute report_files_pattern.



20
21
22
# File 'lib/ktlint/plugin.rb', line 20

def report_files_pattern
  @report_files_pattern
end

#skip_lintObject

Returns the value of attribute skip_lint.



20
21
22
# File 'lib/ktlint/plugin.rb', line 20

def skip_lint
  @skip_lint
end

Instance Method Details

#limitObject



22
23
24
# File 'lib/ktlint/plugin.rb', line 22

def limit
  @limit ||= nil
end

#limit=(limit) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ktlint/plugin.rb', line 26

def limit=(limit)
  if limit != nil && limit.integer?
    @limit = limit
  else
    raise UnexpectedLimitTypeError
  end
end

#lint(inline_mode: false) ⇒ void

This method returns an undefined value.

Run ktlint task using command line interface Will fail if ‘ktlint` is not installed Skip lint task if files changed are empty def lint(inline_mode: false)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ktlint/plugin.rb', line 39

def lint(inline_mode: false)
  unless supported_service?
    raise UnsupportedServiceError.new
  end

  targets = target_files(git.added_files + git.modified_files)

  results = ktlint_results(targets)
  if results.nil? || results.empty?
    return
  end

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

#relative_file_path(file_path) ⇒ Object

Make it a relative path so it can compare it to git.added_files



127
128
129
# File 'lib/ktlint/plugin.rb', line 127

def relative_file_path(file_path)
  file_path.gsub(/#{pwd}\//, '')
end

#send_inline_comments(ktlint_results, targets) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ktlint/plugin.rb', line 97

def send_inline_comments(ktlint_results, targets)
  catch(:loop_break) do
    count = 0
    ktlint_results.each do |ktlint_result|
      ktlint_result.each do |result|
        result['errors'].each do |error|
          file_path = relative_file_path(result['file'])
          next unless targets.include?(file_path)
          message = error['message']
          line = error['line']
          fail(message, file: result['file'], line: line)
          unless limit.nil?
            count += 1
            if count >= limit
              throw(:loop_break)
            end
          end
        end
      end
    end
  end
end

#send_markdown_comment(ktlint_results, targets) ⇒ Object

Comment to a PR by ktlint result json

// Sample single 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” } ] } ]



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ktlint/plugin.rb', line 74

def send_markdown_comment(ktlint_results, targets)
  catch(:loop_break) do
    count = 0
    ktlint_results.each do |ktlint_result|
      ktlint_result.each do |result|
        result['errors'].each do |error|
          file_path = relative_file_path(result['file'])
          next unless targets.include?(file_path)

          message = "#{file_html_link(file_path, error['line'])}: #{error['message']}"
          fail(message)
          unless limit.nil?
            count += 1
            if count >= limit
              throw(:loop_break)
            end
          end
        end
      end
    end
  end
end

#target_files(changed_files) ⇒ Object



120
121
122
123
124
# File 'lib/ktlint/plugin.rb', line 120

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