Class: Danger::DangerL10nlint

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

Overview

This is your plugin class. Any attributes or methods you expose here will be available from within your Dangerfile.

To be published on the Danger plugins site, you will need to have the public interface documented. Danger uses [YARD](yardoc.org/) for generating documentation from your plugin source, and you can verify by running ‘danger plugins lint` or `bundle exec rake spec`.

You should replace these comments with a public description of your library.

Examples:

Ensure people are well warned about merging on Mondays


my_plugin.warn_on_mondays

See Also:

  • Shimomura/danger-l10nlint

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#binary_pathString

The path to L10nLint’s execution

Returns:

  • (String)

    binary_path



27
28
29
# File 'lib/danger_plugin.rb', line 27

def binary_path
  @binary_path
end

#config_fileString

The path to L10nLint’s configuration file

Returns:

  • (String)

    config_file



31
32
33
# File 'lib/danger_plugin.rb', line 31

def config_file
  @config_file
end

#errorsArray<Hash>

Errors found

Returns:

  • (Array<Hash>)

    errors



51
52
53
# File 'lib/danger_plugin.rb', line 51

def errors
  @errors
end

#inline_except_rulesArray<String>

Rules for not wanting to make inline comments

Returns:

  • (Array<String>)

    rule identifiers



59
60
61
# File 'lib/danger_plugin.rb', line 59

def inline_except_rules
  @inline_except_rules
end

#issuesArray<Hash>

All issues found

Returns:

  • (Array<Hash>)

    issues



55
56
57
# File 'lib/danger_plugin.rb', line 55

def issues
  @issues
end

#max_num_violationsInteger

Maximum number of issues to be reported.

Returns:

  • (Integer)

    max_num_violations



35
36
37
# File 'lib/danger_plugin.rb', line 35

def max_num_violations
  @max_num_violations
end

#strictBoolean

Whether we should fail on warnings

Returns:

  • (Boolean)

    strict



43
44
45
# File 'lib/danger_plugin.rb', line 43

def strict
  @strict
end

#verboseBoolean

Provides additional logging diagnostic information.

Returns:

  • (Boolean)

    verbose



39
40
41
# File 'lib/danger_plugin.rb', line 39

def verbose
  @verbose
end

#warningsArray<Hash>

Warnings found

Returns:

  • (Array<Hash>)

    warnings



47
48
49
# File 'lib/danger_plugin.rb', line 47

def warnings
  @warnings
end

Instance Method Details

#l10nlintL10nLint

Make L10nLint object for binary_path

Returns:



142
143
144
# File 'lib/danger_plugin.rb', line 142

def l10nlint
  L10nLint.new(binary_path)
end

#lint_files(inline_mode: false, fail_on_error: false, additional_l10nlint_args: '', subtitle: '') ⇒ void

This method returns an undefined value.

Lints Localizable.strings



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/danger_plugin.rb', line 64

def lint_files(inline_mode: false, fail_on_error: false, additional_l10nlint_args: '', subtitle: '')
  raise 'l10nlint is not installed' unless l10nlint.installed?

  config_file_path = config_file
  if config_file_path
    log "Using config file: #{config_file_path}"
  else
    log 'config file was not specified'
  end

  # Prepare l10nlint options
  options = {
    # Make sure we don't fail when config path has spaces
    config: config_file_path ? Shellwords.escape(config_file_path) : nil,
    reporter: 'json'
  }

  log "linting with options: #{options}"

  issues = run_l10nlint(options, additional_l10nlint_args)

  @issues = issues
  other_issues_count = 0
  unless @max_num_violations.nil? || no_comment
    other_issues_count = issues.count - @max_num_violations if issues.count > @max_num_violations
    issues = issues.take(@max_num_violations)
  end

  log "Received issues from L10nLint: #{issues.count}"

  # Filter warnings and errors
  @warnings = issues.select { |issue| issue['severity'] == 'warning' }
  @errors = issues.select { |issue| issue['severity'] == 'error' }

  if inline_mode
    # Separate each warnings and errors by inline_except_rules
    if inline_except_rules
      markdown_warnings = warnings.select { |issue| inline_except_rules.include?(issue['ruleIdentifier']) }
      inline_warnings = warnings - markdown_warnings
      markdown_errors = @errors.select { |issue| inline_except_rules.include?(issue['ruleIdentifier']) }
      inline_errors = @errors - markdown_errors
    end

    # Report with inline comment
    send_inline_comment(inline_warnings, strict ? :fail : :warn)
    send_inline_comment(inline_errors, (fail_on_error || strict) ? :fail : :warn)

    if markdown_warnings.count > 0 || markdown_errors.count > 0
      message = "### L10nLint found issues\n\n".dup
      message << markdown_issues(markdown_warnings, 'Warnings') unless markdown_warnings.empty?
      message << markdown_issues(markdown_errors, 'Errors') unless markdown_errors.empty?
      markdown message
    end

    warn other_issues_message(other_issues_count) if other_issues_count > 0

  elsif warnings.count > 0 || errors.count > 0
    # Report if any warning or error
    subtitle_in_title = subtitle.empty? ? '' : "(#{subtitle})"
    message = "### L10nLint found issues #{subtitle_in_title}\n\n".dup
    message << markdown_issues(warnings, 'Warnings') unless warnings.empty?
    message << markdown_issues(errors, 'Errors') unless errors.empty?
    message << "\n#{other_issues_message(other_issues_count)}" if other_issues_count > 0
    markdown message

    # Fail danger on errors
    should_fail_by_errors = fail_on_error && errors.count > 0
    # Fail danger if any warnings or errors and we are strict
    should_fail_by_strict = strict && (errors.count > 0 || warnings.count > 0)
    if should_fail_by_errors || should_fail_by_strict
      fail 'Failed due to L10nLint errors'
    end
  end
end

#log(text) ⇒ Object



146
147
148
# File 'lib/danger_plugin.rb', line 146

def log(text)
  puts text if @verbose
end

#markdown_issues(results, heading) ⇒ String

Create a markdown table from l10nlint issues

Returns:

  • (String)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/danger_plugin.rb', line 165

def markdown_issues(results, heading)
  message = "#### #{heading}\n\n".dup

  message << "File | Line | Reason |\n"
  message << "| --- | ----- | ----- |\n"

  results.each do |r|
    filename = r['location']['file'].split('/').last(2).join("/")
    line = r['location']['line']
    reason = r['reason']
    rule = r['ruleIdentifier']
    # Other available properties can be found int L10nLint/…/JSONReporter.swift
    message << "#{filename} | #{line} | #{reason} (#{rule})\n"
  end

  message
end

#other_issues_message(issues_count) ⇒ Object



202
203
204
205
# File 'lib/danger_plugin.rb', line 202

def other_issues_message(issues_count)
  violations = issues_count == 1 ? 'violation' : 'violations'
  "L10nLint also found #{issues_count} more #{violations} with this PR."
end

#run_l10nlint(options, additional_l10nlint_args) ⇒ Array

Run l10nlint on all files and returns the issues

Returns:

  • (Array)

    l10nlint issues



153
154
155
156
157
158
159
160
# File 'lib/danger_plugin.rb', line 153

def run_l10nlint(options, additional_l10nlint_args)
  result = l10nlint.lint(options, additional_l10nlint_args)
  if result == ''
    {}
  else
    JSON.parse(result)
  end
end

#send_inline_comment(results, method) ⇒ void

This method returns an undefined value.

Send inline comment with danger’s warn or fail method



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/danger_plugin.rb', line 186

def send_inline_comment(results, method)
  dir = "#{Dir.pwd}/"
  results.each do |r|
    github_filename = r['location']['file'].gsub(dir, '')
    message = "#{r['reason']}".dup

    # extended content here
    filename = r['location']['file'].split('/').last(2).join("/")
    message << "\n"
    message << "`#{r['ruleIdentifier']}`"
    message << " `#{filename}:#{r['location']['line']}`" # file:line for pasting into Xcode Quick Open

    send(method, message, file: github_filename, line: r['location']['line'])
  end
end