Class: Danger::DangerKtlint
- Inherits:
-
Plugin
- Object
- Plugin
- Danger::DangerKtlint
- 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
-
#filtering ⇒ Object
TODO: Lint all files if ‘filtering: false`.
-
#report_file ⇒ Object
Returns the value of attribute report_file.
-
#report_files_pattern ⇒ Object
Returns the value of attribute report_files_pattern.
-
#skip_lint ⇒ Object
Returns the value of attribute skip_lint.
Instance Method Summary collapse
- #limit ⇒ Object
- #limit=(limit) ⇒ Object
-
#lint(inline_mode: false) ⇒ void
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).
-
#relative_file_path(file_path) ⇒ Object
Make it a relative path so it can compare it to git.added_files.
- #send_inline_comments(ktlint_results, targets) ⇒ Object
-
#send_markdown_comment(ktlint_results, targets) ⇒ Object
Comment to a PR by ktlint result json.
- #target_files(changed_files) ⇒ Object
Instance Attribute Details
#filtering ⇒ Object
TODO: Lint all files if ‘filtering: false`
18 19 20 |
# File 'lib/ktlint/plugin.rb', line 18 def filtering @filtering end |
#report_file ⇒ Object
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_pattern ⇒ Object
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_lint ⇒ Object
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
#limit ⇒ Object
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) = error['message'] line = error['line'] fail(, 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) = "#{file_html_link(file_path, error['line'])}: #{error['message']}" fail() 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 |