Class: Danger::DangerfileImportPlugin

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

Plugins collapse

Methods inherited from Plugin

all_plugins, inherited, #initialize, instance_name, #method_missing

Constructor Details

This class inherits a constructor from Danger::Plugin

Dynamic Method Handling

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

Instance Method Details

#import(path) ⇒ Object

Download a local or remote plugin and use it locally

Parameters:

  • path (String)

    a local path or a https URL to the Ruby file to import a danger plugin from.



11
12
13
14
15
16
17
18
19
20
# File 'lib/danger/danger_core/plugins/dangerfile_import_plugin.rb', line 11

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)



51
52
53
54
55
56
# File 'lib/danger/danger_core/plugins/dangerfile_import_plugin.rb', line 51

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 ./
    refresh_plugins
  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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/danger/danger_core/plugins/dangerfile_import_plugin.rb', line 27

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'

  @http_client ||= Faraday.new do |b|
    b.adapter :net_http
  end
  content = @http_client.get(url)

  Dir.mktmpdir do |dir|
    path = File.join(dir, "temporary_remote_action.rb")
    File.write(path, content.body)
    import_local(path)
  end
end