Sha256: 62aa9d88c5a55b5b84b6e14e04c2b79648af1f9a93c384dfce54d2c419fd759b

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

require 'danger/plugin_support/plugin'

module Danger
  class DangerfileImportPlugin < Plugin
    # @!group Plugins
    # Download a local or remote plugin and use it locally
    #
    # @param    [String] path
    #           a local path or a https URL to the Ruby file to import
    #           a danger plugin from.
    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

    # @!group Plugins
    # Download a remote plugin and use it locally
    #
    # @param    [String] url
    #           https URL to the Ruby file to use
    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

    # @!group Plugins
    # Import one or more local plugins
    #
    # @param    [String] path
    #           The path to the file to import
    #           Can also be a pattern (./**/*plugin.rb)
    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
danger-0.8.3 lib/danger/danger_core/plugins/dangerfile_import_plugin.rb
danger-0.8.2 lib/danger/danger_core/plugins/dangerfile_import_plugin.rb
danger-0.8.1 lib/danger/danger_core/plugins/dangerfile_import_plugin.rb
danger-0.8.0 lib/danger/danger_core/plugins/dangerfile_import_plugin.rb