Module: Danger::Dangerfile::DSL

Included in:
Danger::Dangerfile
Defined in:
lib/danger/plugin.rb,
lib/danger/dangerfile_dsl.rb,
lib/danger/plugins/protect_files.rb

Defined Under Namespace

Classes: Plugin, ProtectFiles

Enviroment collapse

Enviroment collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &_block) ⇒ Object

When an undefined method is called, we check to see if it’s something that either the ‘scm` or the `request_source` can handle. This opens us up to letting those object extend themselves naturally. This will also look for plugins



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/dangerfile_dsl.rb', line 114

def method_missing(method_sym, *arguments, &_block)
  # SCM Source
  if AvailableValues.scm.include?(method_sym)
    return env.scm.send(method_sym)
  end

  # Request Source
  if AvailableValues.request_source.include?(method_sym)
    return env.request_source.send(method_sym)
  end

  # Plugins
  class_name = method_sym.to_s.danger_class
  if Danger::Dangerfile::DSL.const_defined?(class_name)
    plugin_ref = Danger::Dangerfile::DSL.const_get(class_name)
    if plugin_ref < Plugin
      plugin_ref.new(self).run(*arguments)
    else
      raise "'#{method_sym}' is not a valid danger plugin".red
    end
  else
    raise "Unknown method '#{method_sym}', please check out the documentation for available plugins".red
  end
end

Instance Attribute Details

#envEnvironmentManager (readonly)

objects, which you can use to pull out extra bits of information. Warning the api of these objects is not considered a part of the Dangerfile public API, and is viable to change occasionally on the whims of developers.

Returns:



12
13
14
# File 'lib/danger/dangerfile_dsl.rb', line 12

def env
  @env
end

Instance Method Details

#fail(message, sticky: true) ⇒ Object

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



72
73
74
75
76
# File 'lib/danger/dangerfile_dsl.rb', line 72

def fail(message, sticky: true)
  return if should_ignore_violation(message)
  self.errors << Violation.new(message, sticky)
  puts "Raising error '#{message}'"
end

#import(path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/danger/dangerfile_dsl.rb', line 23

def import(path)
  raise "`import` requires a string" unless path.kind_of?(String)
  path += ".rb" unless path.end_with?(".rb")

  if path.start_with?("http")
    import_url(path)
  else
    import_local(path)
  end
end

#import_local(path) ⇒ Object

Import one or more local plugins

Parameters:

  • path (String)

    The path to the file to import Can also be a pattern (./**/*plugin.rb)



56
57
58
59
60
# File 'lib/danger/dangerfile_dsl.rb', line 56

def import_local(path)
  Dir[path].each do |file|
    require File.expand_path(file) # without the expand_path it would fail if the path doesn't start with ./
  end
end

#import_url(url) ⇒ Object

Download a remote plugin and use it locally

Parameters:

  • url (String)

    https URL to the Ruby file to use



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/danger/dangerfile_dsl.rb', line 38

def import_url(url)
  raise "URL is not https, for security reasons `danger` only supports encrypted requests" unless url.start_with?("https://")

  require 'tmpdir'
  require 'faraday'
  content = Faraday.get(url)
  Dir.mktmpdir do |dir|
    path = File.join(dir, "temporary_remote_action.rb")
    File.write(path, content.body)
    import_local(path)
  end
end

#initializeObject



14
15
16
17
18
19
20
21
# File 'lib/danger/dangerfile_dsl.rb', line 14

def initialize
  self.warnings = []
  self.errors = []
  self.messages = []
  self.markdowns = []

  load_default_plugins
end

#markdown(message) ⇒ Object

Print markdown to below the table

Parameters:

  • message (String)

    The markdown based message to be printed below the table



105
106
107
108
# File 'lib/danger/dangerfile_dsl.rb', line 105

def markdown(message)
  self.markdowns << message
  puts "Printing markdown #{message}"
end

#message(message, sticky: true) ⇒ Object

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



96
97
98
99
# File 'lib/danger/dangerfile_dsl.rb', line 96

def message(message, sticky: true)
  self.messages << Violation.new(message, sticky)
  puts "Printing message '#{message}'"
end

#should_ignore_violation(message) ⇒ Object



62
63
64
# File 'lib/danger/dangerfile_dsl.rb', line 62

def should_ignore_violation(message)
  env.request_source.ignored_violations.include? message
end

#warn(message, sticky: true) ⇒ Object

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



84
85
86
87
88
# File 'lib/danger/dangerfile_dsl.rb', line 84

def warn(message, sticky: true)
  return if should_ignore_violation(message)
  self.warnings << Violation.new(message, sticky)
  puts "Printing warning '#{message}'"
end