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, `true` by default, which means that the message will be crossed out instead of being removed. If it’s not use on subsequent runs.

By default, using ‘fail` would fail the corresponding build. Either via an API call, or via the return value for the danger command.

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:

Failing a build


fail "This build didn't pass tests"

Failing a build, but not keeping its value around on subsequent runs


fail("This build didn't pass tests", sticky: false)

Passing a warning


warn "This build didn't pass linting"

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

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.



52
53
54
55
56
57
58
59
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 52

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:



64
65
66
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 64

def self.instance_name
  "messaging"
end

Instance Method Details

#fail(message, sticky: true, file: nil, line: nil) ⇒ void

This method returns an undefined value.

Declares a CI blocking error

Parameters:

  • message (String)

    The message to present to the user

  • sticky (Boolean) (defaults to: true)

    Whether the message should be kept after it was fixed, defaults to ‘true`.

  • file (String) (defaults to: nil)

    Optional. Path to the file that the message is for.

  • line (String) (defaults to: nil)

    Optional. The line in the file to present the message in.



134
135
136
137
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 134

def fail(message, sticky: true, file: nil, line: nil)
  return if should_ignore_violation(message)
  @errors << Violation.new(message, sticky, file, line)
end

#markdown(message, file: nil, line: nil) ⇒ void

This method returns an undefined value.

Print markdown to below the table

Parameters:

  • message (String)

    The markdown based message to be printed below the table

  • file (String) (defaults to: nil)

    Optional. Path to the file that the message is for.

  • line (String) (defaults to: nil)

    Optional. The line in the file to present the message in.



79
80
81
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 79

def markdown(message, file: nil, line: nil)
  @markdowns << Markdown.new(message, file, line)
end

#message(message, sticky: true, file: nil, line: nil) ⇒ void

This method returns an undefined value.

Print out a generate message on the PR

Parameters:

  • message (String)

    The message to present to the user

  • sticky (Boolean) (defaults to: true)

    Whether the message should be kept after it was fixed, defaults to ‘true`.

  • file (String) (defaults to: nil)

    Optional. Path to the file that the message is for.

  • line (String) (defaults to: nil)

    Optional. The line in the file to present the message in.



97
98
99
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 97

def message(message, sticky: true, file: nil, line: nil)
  @messages << Violation.new(message, sticky, file, line)
end

#status_reportHash

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

Returns:

  • (Hash)


145
146
147
148
149
150
151
152
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 145

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)


160
161
162
163
164
165
166
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 160

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

#warn(message, sticky: true, file: nil, line: nil) ⇒ void

This method returns an undefined value.

Specifies a problem, but not critical

Parameters:

  • message (String)

    The message to present to the user

  • sticky (Boolean) (defaults to: true)

    Whether the message should be kept after it was fixed, defaults to ‘true`.

  • file (String) (defaults to: nil)

    Optional. Path to the file that the message is for.

  • line (String) (defaults to: nil)

    Optional. The line in the file to present the message in.



115
116
117
118
# File 'lib/danger/danger_core/plugins/dangerfile_messaging_plugin.rb', line 115

def warn(message, sticky: true, file: nil, line: nil)
  return if should_ignore_violation(message)
  @warnings << Violation.new(message, sticky, file, line)
end