Class: Danger::Dangerfile

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/danger/plugin.rb,
lib/danger/dangerfile.rb,
lib/danger/dangerfile_dsl.rb,
lib/danger/plugins/protect_files.rb

Defined Under Namespace

Modules: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DSL

#fail, #import, #import_local, #import_url, #initialize, #markdown, #message, #method_missing, #should_ignore_violation, #warn

Dynamic Method Handling

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

Instance Attribute Details

#defined_in_filePathname

Returns the path where the Dangerfile was loaded from. It is nil if the Dangerfile was generated programmatically.

Returns:

  • (Pathname)

    the path where the Dangerfile was loaded from. It is nil if the Dangerfile was generated programmatically.



15
16
17
# File 'lib/danger/dangerfile.rb', line 15

def defined_in_file
  @defined_in_file
end

#envObject

Returns the value of attribute env.



10
11
12
# File 'lib/danger/dangerfile.rb', line 10

def env
  @env
end

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/danger/dangerfile.rb', line 10

def errors
  @errors
end

#markdownsObject

Returns the value of attribute markdowns.



10
11
12
# File 'lib/danger/dangerfile.rb', line 10

def markdowns
  @markdowns
end

#messagesObject

Returns the value of attribute messages.



10
11
12
# File 'lib/danger/dangerfile.rb', line 10

def messages
  @messages
end

#verboseObject

Returns the value of attribute verbose.



10
11
12
# File 'lib/danger/dangerfile.rb', line 10

def verbose
  @verbose
end

#warningsObject

Returns the value of attribute warnings.



10
11
12
# File 'lib/danger/dangerfile.rb', line 10

def warnings
  @warnings
end

Instance Method Details

#parse(path, contents = nil) ⇒ Object

Parses the file at a path, optionally takes the content of the file for DI



57
58
59
60
61
62
63
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
# File 'lib/danger/dangerfile.rb', line 57

def parse(path, contents = nil)
  print_known_info if verbose

  contents ||= File.open(path, 'r:utf-8', &:read)

  # Work around for Rubinius incomplete encoding in 1.9 mode
  if contents.respond_to?(:encoding) && contents.encoding.name != 'UTF-8'
    contents.encode!('UTF-8')
  end

  if contents.tr!('“”‘’‛', %(""'''))
    # Changes have been made
    puts "Your #{path.basename} has had smart quotes sanitised. " \
                'To avoid issues in the future, you should not use ' \
                'TextEdit for editing it. If you are not using TextEdit, ' \
                'you should turn off smart quotes in your editor of choice.'.red
  end

  if contents.include?("puts")
    puts "You used `puts` in your Dangerfile. To print out text to GitHub use `message` instead"
  end

  self.defined_in_file = path
  instance_eval do
    # rubocop:disable Lint/RescueException
    begin
      # rubocop:disable Eval
      eval(contents, nil, path.to_s)
      # rubocop:enable Eval
    rescue Exception => e
      message = "Invalid `#{path.basename}` file: #{e.message}"
      raise DSLError.new(message, path, e.backtrace, contents)
    end
    # rubocop:enable Lint/RescueException
  end
end

Iterates through the DSL’s attributes, and table’s the output



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/danger/dangerfile.rb', line 25

def print_known_info
  rows = []

  AvailableValues.all.each do |key|
    value = self.send(key)
    value = value.scan(/.{,80}/).to_a.each(&:strip!).join("\n") if key == :pr_body

    # So that we either have one value per row
    # or we have [] for an empty array
    value = value.join("\n") if value.kind_of?(Array) && value.count > 0

    rows << [key.to_s, value]
  end

  rows << ["---", "---"]
  rows << ["SCM", env.scm.class]
  rows << ["Source", env.ci_source.class]
  rows << ["Requests", env.request_source.class]
  rows << ["Base Commit", env.meta_info_for_base]
  rows << ["Head Commit", env.meta_info_for_head]

  params = {}
  params[:rows] = rows.each { |current| current[0] = current[0].yellow }
  params[:title] = "Danger v#{Danger::VERSION}\nDSL Attributes".green

  puts ""
  puts Terminal::Table.new(params)
  puts ""
end


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
# File 'lib/danger/dangerfile.rb', line 94

def print_results
  return if (self.errors + self.warnings + self.messages + self.markdowns).count == 0

  puts ""
  puts "danger results:"
  [:errors, :warnings, :messages].each do |current|
    params = {}
    params[:rows] = self.send(current).collect { |a| [a.message] }
    next unless params[:rows].count > 0
    params[:title] = case current
                     when :errors
                       current.to_s.capitalize.red
                     when :warnings
                       current.to_s.capitalize.yellow
                     else
                       current.to_s.capitalize
                     end

    puts ""
    puts Terminal::Table.new(params)
    puts ""
  end

  puts "Markdown: ".green if self.markdowns.count > 0
  self.markdowns.each do |current_markdown|
    puts current_markdown
  end
end

#to_sString

Returns a string useful to represent the Dangerfile in a message presented to the user.

Returns:

  • (String)

    a string useful to represent the Dangerfile in a message presented to the user.



20
21
22
# File 'lib/danger/dangerfile.rb', line 20

def to_s
  'Dangerfile'
end