Class: Danger::Dangerfile
- Inherits:
-
Object
- Object
- Danger::Dangerfile
- Includes:
- DSL
- Defined in:
- lib/danger/danger_core/dangerfile.rb,
lib/danger/danger_core/dangerfile_dsl.rb
Defined Under Namespace
Modules: DSL
Instance Attribute Summary collapse
-
#defined_in_file ⇒ Pathname
The path where the Dangerfile was loaded from.
-
#env ⇒ Object
Returns the value of attribute env.
-
#plugins ⇒ Object
Returns the value of attribute plugins.
-
#ui ⇒ Object
Returns the value of attribute ui.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
-
.core_plugin_classes ⇒ Object
These are the classes that are allowed to also use method_missing in order to provide broader plugin support.
-
.essential_plugin_classes ⇒ Object
The ones that everything would break without.
Instance Method Summary collapse
- #core_dsl_attributes ⇒ Object
- #external_dsl_attributes ⇒ Object
- #fail(*args, &blk) ⇒ Object
-
#initialize(env_manager, cork_board) ⇒ Dangerfile
constructor
A new instance of Dangerfile.
-
#method_missing(method_sym, *arguments, &_block) ⇒ Object
rubocop:disable Style/MethodMissing.
- #method_values_for_plugin_hashes(plugin_hashes) ⇒ Object
-
#parse(path, contents = nil) ⇒ Object
Parses the file at a path, optionally takes the content of the file for DI.
-
#print_known_info ⇒ Object
Iterates through the DSL’s attributes, and table’s the output.
- #print_results ⇒ Object
-
#refresh_plugins ⇒ Object
(also: #init_plugins)
Iterate through available plugin classes and initialize them with a reference to this Dangerfile.
-
#to_s ⇒ String
A string useful to represent the Dangerfile in a message presented to the user.
-
#warn(*args, &blk) ⇒ Object
Both of these methods exist on all objects ruby-doc.org/core-2.2.3/Kernel.html#method-i-warn ruby-doc.org/core-2.2.3/Kernel.html#method-i-fail However, as we’re using using them in the DSL, they won’t get method_missing called correctly without overriding them.
Constructor Details
#initialize(env_manager, cork_board) ⇒ Dangerfile
Returns a new instance of Dangerfile.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/danger/danger_core/dangerfile.rb', line 71 def initialize(env_manager, cork_board) @plugins = {} @core_plugins = [] @ui = cork_board # Triggers the core plugins @env = env_manager # Triggers local plugins from the root of a project Dir["./danger_plugins/*.rb"].each do |file| require File.(file) end refresh_plugins if env_manager.pr? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &_block) ⇒ Object
rubocop:disable Style/MethodMissing
62 63 64 65 66 67 68 69 |
# File 'lib/danger/danger_core/dangerfile.rb', line 62 def method_missing(method_sym, *arguments, &_block) @core_plugins.each do |plugin| if plugin.public_methods(false).include?(method_sym) return plugin.send(method_sym, *arguments) end end super end |
Instance Attribute Details
#defined_in_file ⇒ Pathname
Returns the path where the Dangerfile was loaded from. It is nil if the Dangerfile was generated programmatically.
23 24 25 |
# File 'lib/danger/danger_core/dangerfile.rb', line 23 def defined_in_file @defined_in_file end |
#env ⇒ Object
Returns the value of attribute env.
18 19 20 |
# File 'lib/danger/danger_core/dangerfile.rb', line 18 def env @env end |
#plugins ⇒ Object
Returns the value of attribute plugins.
18 19 20 |
# File 'lib/danger/danger_core/dangerfile.rb', line 18 def plugins @plugins end |
#ui ⇒ Object
Returns the value of attribute ui.
18 19 20 |
# File 'lib/danger/danger_core/dangerfile.rb', line 18 def ui @ui end |
#verbose ⇒ Object
Returns the value of attribute verbose.
18 19 20 |
# File 'lib/danger/danger_core/dangerfile.rb', line 18 def verbose @verbose end |
Class Method Details
.core_plugin_classes ⇒ Object
These are the classes that are allowed to also use method_missing in order to provide broader plugin support
34 35 36 |
# File 'lib/danger/danger_core/dangerfile.rb', line 34 def self.core_plugin_classes [DangerfileMessagingPlugin] end |
.essential_plugin_classes ⇒ Object
The ones that everything would break without
39 40 41 |
# File 'lib/danger/danger_core/dangerfile.rb', line 39 def self.essential_plugin_classes [DangerfileMessagingPlugin, DangerfileGitPlugin, DangerfileDangerPlugin, DangerfileGitHubPlugin, DangerfileGitLabPlugin, DangerfileBitbucketServerPlugin, DangerfileBitbucketCloudPlugin] end |
Instance Method Details
#core_dsl_attributes ⇒ Object
106 107 108 |
# File 'lib/danger/danger_core/dangerfile.rb', line 106 def core_dsl_attributes @core_plugins.map { |plugin| { plugin: plugin, methods: plugin.public_methods(false) } } end |
#external_dsl_attributes ⇒ Object
110 111 112 |
# File 'lib/danger/danger_core/dangerfile.rb', line 110 def external_dsl_attributes plugins.values.reject { |plugin| @core_plugins.include? plugin } .map { |plugin| { plugin: plugin, methods: plugin.public_methods(false) } } end |
#fail(*args, &blk) ⇒ Object
53 54 55 |
# File 'lib/danger/danger_core/dangerfile.rb', line 53 def fail(*args, &blk) method_missing(:fail, *args, &blk) end |
#method_values_for_plugin_hashes(plugin_hashes) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/danger/danger_core/dangerfile.rb', line 114 def method_values_for_plugin_hashes(plugin_hashes) plugin_hashes.flat_map do |plugin_hash| plugin = plugin_hash[:plugin] methods = plugin_hash[:methods].select { |name| plugin.method(name).parameters.empty? } methods.map do |method| case method when :api value = "Octokit::Client" when :pr_json value = "[Skipped]" else value = plugin.send(method) value = wrap_text(value) if value.kind_of?(String) # 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 end [method.to_s, value] end end end |
#parse(path, contents = nil) ⇒ Object
Parses the file at a path, optionally takes the content of the file for DI
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/danger/danger_core/dangerfile.rb', line 168 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 ui.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") ui.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 = "Invalid `#{path.basename}` file: #{e.}" raise DSLError.new(, path, e.backtrace, contents) end # rubocop:enable Lint/RescueException end end |
#print_known_info ⇒ Object
Iterates through the DSL’s attributes, and table’s the output
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/danger/danger_core/dangerfile.rb', line 141 def print_known_info rows = [] rows += method_values_for_plugin_hashes(core_dsl_attributes) rows << ["---", "---"] rows += method_values_for_plugin_hashes(external_dsl_attributes) rows << ["---", "---"] rows << ["SCM", env.scm.class] rows << ["Source", env.ci_source.class] rows << ["Requests", env.request_source.class] rows << ["Base Commit", env.] rows << ["Head Commit", env.] params = {} params[:rows] = rows.each { |current| current[0] = current[0].yellow } params[:title] = "Danger v#{Danger::VERSION}\nDSL Attributes".green ui.section("Info:") do ui.puts table = Terminal::Table.new(params) table.align_column(0, :right) ui.puts table ui.puts end end |
#print_results ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/danger/danger_core/dangerfile.rb', line 205 def print_results status = status_report violations = violation_report return if (violations[:errors] + violations[:warnings] + violations[:messages] + status[:markdowns]).count.zero? ui.section("Results:") do [:errors, :warnings, :messages].each do |key| formatted = key.to_s.capitalize + ":" title = case key when :errors formatted.red when :warnings formatted.yellow else formatted end rows = violations[key] print_list(title, rows) end if status[:markdowns].count > 0 ui.title("Markdown:") do status[:markdowns].each do |current_markdown| ui.puts "#{current_markdown.file}\#L#{current_markdown.line}" if current_markdown.file && current_markdown.line ui.puts current_markdown. end end end end end |
#refresh_plugins ⇒ Object Also known as: init_plugins
Iterate through available plugin classes and initialize them with a reference to this Dangerfile
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/danger/danger_core/dangerfile.rb', line 89 def refresh_plugins plugins = Plugin.all_plugins plugins.each do |klass| next if klass.respond_to?(:singleton_class?) && klass.singleton_class? plugin = klass.new(self) next if plugin.nil? || @plugins[klass] name = plugin.class.instance_name self.class.send(:attr_reader, name) instance_variable_set("@#{name}", plugin) @plugins[klass] = plugin @core_plugins << plugin if self.class.core_plugin_classes.include? klass end end |
#to_s ⇒ String
Returns a string useful to represent the Dangerfile in a message presented to the user.
28 29 30 |
# File 'lib/danger/danger_core/dangerfile.rb', line 28 def to_s "Dangerfile" end |
#warn(*args, &blk) ⇒ Object
Both of these methods exist on all objects ruby-doc.org/core-2.2.3/Kernel.html#method-i-warn ruby-doc.org/core-2.2.3/Kernel.html#method-i-fail However, as we’re using using them in the DSL, they won’t get method_missing called correctly without overriding them.
49 50 51 |
# File 'lib/danger/danger_core/dangerfile.rb', line 49 def warn(*args, &blk) method_missing(:warn, *args, &blk) end |