Class: Danger::DangerfileMessagingPlugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb

Overview

Provides the feedback mechanism for Danger. Danger can keep track of messages, warnings, failure and post arbitrary markdown into a comment.

The message within which Danger communicates back is amended on each run in a session.

Each of ‘message`, `warn` and `fail` have a `sticky` flag, `false` by default, which when `true` means that the message will be crossed out instead of being removed. If it’s not called again on subsequent runs.

Each of ‘message`, `warn`, `fail` and `markdown` support multiple passed arguments

By default, using ‘failure` would fail the corresponding build. Either via an API call, or via the return value for the danger command. Older code examples use `fail` which is an alias of `failure`, but the default Rubocop settings would have an issue with it.

You can optionally add ‘file` and `line` to provide inline feedback on a PR in GitHub, note that only feedback inside the PR’s diff will show up inline. Others will appear inside the main comment.

It is possible to have Danger ignore specific warnings or errors by writing ‘Danger: Ignore “[warning/error text]”`.

Sidenote: Messaging is the only plugin which adds functions to the root of the Dangerfile.

Examples:

Multiple passed arguments


message 'Hello', 'World', file: "Dangerfile", line: 1
warn ['This', 'is', 'warning'], file: "Dangerfile", line: 1
failure 'Ooops', 'bad bad error', sticky: false
markdown '# And', '# Even', '# Markdown', file: "Dangerfile", line: 1

Failing a build


failure "This build didn't pass tests"
failure "Ooops!", "Something bad happened"
failure ["This is example", "with array"]

Failing a build, and note that on subsequent runs


failure("This build didn't pass tests", sticky: true)

Passing a warning


warn "This build didn't pass linting"
warn "Hm...", "This is not really good"
warn ["Multiple warnings", "via array"]

Displaying a markdown table


message = "### Proselint found issues\n\n"
message << "Line | Message | Severity |\n"
message << "| --- | ----- | ----- |\n"
message << "20 | No documentation | Error \n"
markdown message

markdown "### First issue", "### Second issue"
markdown ["### First issue", "### Second issue"]

Adding an inline warning to a file


warn("You shouldn't use puts in your Dangerfile", file: "Dangerfile", line: 10)

See Also:

  • danger/danger

Core collapse

Reporting collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

all_plugins, clear_external_plugins, inherited, #method_missing

Constructor Details

#initialize(dangerfile) ⇒ DangerfileMessagingPlugin

Returns a new instance of DangerfileMessagingPlugin.



74
75
76
77
78
79
80
81
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 74

def initialize(dangerfile)
  super(dangerfile)

  @warnings = []
  @errors = []
  @messages = []
  @markdowns = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Danger::Plugin

Class Method Details

.instance_nameString

The instance name used in the Dangerfile

Returns:



86
87
88
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 86

def self.instance_name
  "messaging"
end

Instance Method Details

#fail(*failures, **options) ⇒ void Also known as: failure

This method returns an undefined value.

Declares a CI blocking error

Parameters:

  • failures (String, Array<String>)

    The message to present to the user

  • options
  • [Boolean] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 194

def fail(*failures, **options)
  sticky = options.fetch(:sticky, false)
  file = options.fetch(:file, nil)
  line = options.fetch(:line, nil)

  failures.flatten.each do |failure|
    next if should_ignore_violation(failure)

    @errors << Violation.new(failure, sticky, file, line, type: :error) if failure
  end
end

#markdown(*markdowns, **options) ⇒ void

This method returns an undefined value.

Print markdown to below the table

Parameters:

  • options (Hash)
  • [String, (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Integer] (Hash)

    a customizable set of options



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 108

def markdown(*markdowns, **options)
  file = options.fetch(:file, nil)
  line = options.fetch(:line, nil)
  start_line = options.fetch(:start_line, nil)
  side = options.fetch(:side, nil)
  start_side = options.fetch(:start_side, nil)

  markdowns.flatten.each do |markdown|
    @markdowns << Markdown.new(
      markdown,
      file,
      line,
      start_line: start_line,
      side: side,
      start_side: start_side
    )
  end
end

#message(*messages, **options) ⇒ void

This method returns an undefined value.

Print out a generate message on the PR

Parameters:

  • messages (String, Array<String>)

    The message to present to the user

  • options (Hash)
  • [Boolean] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options



142
143
144
145
146
147
148
149
150
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 142

def message(*messages, **options)
  sticky = options.fetch(:sticky, false)
  file = options.fetch(:file, nil)
  line = options.fetch(:line, nil)

  messages.flatten.each do |message|
    @messages << Violation.new(message, sticky, file, line, type: :message) if message
  end
end

#status_reportHash

A list of all messages passed to Danger, including the markdowns.

Returns:

  • (Hash)


214
215
216
217
218
219
220
221
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 214

def status_report
  {
    errors: @errors.map(&:message).clone.freeze,
    warnings: @warnings.map(&:message).clone.freeze,
    messages: @messages.map(&:message).clone.freeze,
    markdowns: @markdowns.clone.freeze
  }
end

#violation_reportHash

A list of all violations passed to Danger, we don’t anticipate users of Danger needing to use this.

Returns:

  • (Hash)


229
230
231
232
233
234
235
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 229

def violation_report
  {
    errors: @errors.clone.freeze,
    warnings: @warnings.clone.freeze,
    messages: @messages.clone.freeze
  }
end

#warn(*warnings, **options) ⇒ void

This method returns an undefined value.

Specifies a problem, but not critical

Parameters:

  • warnings (String, Array<String>)

    The message to present to the user

  • options
  • [Boolean] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 167

def warn(*warnings, **options)
  sticky = options.fetch(:sticky, false)
  file = options.fetch(:file, nil)
  line = options.fetch(:line, nil)

  warnings.flatten.each do |warning|
    next if should_ignore_violation(warning)

    @warnings << Violation.new(warning, sticky, file, line, type: :warning) if warning
  end
end