class Danger::DangerfileDangerPlugin

def self.instance_name

Returns:
  • (String) -
def self.instance_name
  "danger"
end

def download(path_or_url)

Returns:
  • (String) - The path to the downloaded Ruby file

Parameters:
  • path_or_url (String) --
def download(path_or_url)
  raise "`download` requires a string" unless path_or_url.kind_of?(String)
  raise "URL is not https, for security reasons `danger` only supports encrypted requests" if URI.parse(path_or_url).scheme != "https"
  require "tmpdir"
  require "faraday"
  @http_client ||= Faraday.new do |b|
    b.adapter :net_http
  end
  content = @http_client.get(path_or_url)
  path = File.join(Dir.mktmpdir, "temporary_danger.rb")
  File.write(path, content.body)
  return path
end

def import_dangerfile(opts)

Returns:
  • (void) -

Options Hash: (**opts)
  • :path (String) -- Path to Dangerfile
  • :branch (String) -- Alias of :ref
  • :ref (String) -- The name of branch, tag or commit
  • :gem (String) -- Gem name
  • :gitlab (String) -- GitLab repo
  • :github (String) -- GitHub repo

Parameters:
  • opts (Hash) --
def import_dangerfile(opts)
  if opts.kind_of?(String)
    warn "Use `import_dangerfile(github: '#{opts}')` instead of `import_dangerfile '#{opts}'`."
    import_dangerfile_from_github(opts)
  elsif opts.kind_of?(Hash)
    if opts.key?(:github)
      import_dangerfile_from_github(opts[:github], opts[:ref] || opts[:branch], opts[:path])
    elsif opts.key?(:gitlab)
      import_dangerfile_from_gitlab(opts[:gitlab], opts[:ref] || opts[:branch], opts[:path])
    elsif opts.key?(:path)
      import_dangerfile_from_path(opts[:path])
    elsif opts.key?(:gem)
      import_dangerfile_from_gem(opts[:gem])
    elsif opts.key?(:url)
      import_dangerfile_from_url(opts[:url])
    else
      raise "`import` requires a Hash with either :github, :gitlab, :gem, :path or :url"
    end
  else
    raise "`import` requires a Hash"
  end
end

def import_dangerfile_from_gem(name)

Returns:
  • (void) -

Parameters:
  • name (String) --
def import_dangerfile_from_gem(name)
  raise "`import_dangerfile_from_gem` requires a string" unless name.kind_of?(String)
  spec = Gem::Specification.find_by_name(name)
  import_dangerfile_from_path(spec.gem_dir)
rescue Gem::MissingSpecError
  raise "`import_dangerfile_from_gem` tried to load `#{name}` and failed, did you forget to include it in your Gemfile?"
end

def import_dangerfile_from_github(slug, branch = nil, path = nil)

Returns:
  • (void) -

Parameters:
  • path (String) --
  • branch (String) --
  • slug (String) --
def import_dangerfile_from_github(slug, branch = nil, path = nil)
  raise "`import_dangerfile_from_github` requires a string" unless slug.kind_of?(String)
  org, repo = slug.split("/")
  download_url = env.request_source.file_url(organisation: org, repository: repo, branch: branch, path: path || "Dangerfile")
  local_path = download(download_url)
  @dangerfile.parse(Pathname.new(local_path))
end

def import_dangerfile_from_gitlab(slug_or_project_id, branch = nil, path = nil)

Returns:
  • (void) -

Parameters:
  • path (String) --
  • branch (String) --
  • slug_or_project_id (Int) --
def import_dangerfile_from_gitlab(slug_or_project_id, branch = nil, path = nil)
  download_url = env.request_source.file_url(repository: slug_or_project_id, branch: branch, path: path || "Dangerfile")
  local_path = download(download_url)
  @dangerfile.parse(Pathname.new(local_path))
end

def import_dangerfile_from_path(path)

Returns:
  • (void) -

Parameters:
  • path (String) --
def import_dangerfile_from_path(path)
  raise "`import_dangerfile_from_path` requires a string" unless path.kind_of?(String)
  local_path = File.file?(path) ? path : File.join(path, "Dangerfile")
  @dangerfile.parse(Pathname.new(local_path))
end

def import_dangerfile_from_url(url)

Returns:
  • (void) -

Parameters:
  • url (String) --
def import_dangerfile_from_url(url)
  raise "`import_dangerfile_from_url` requires a string" unless url.kind_of?(String)
  local_path = download(url)
  @dangerfile.parse(Pathname.new(local_path))
end

def import_local(path)

Returns:
  • (void) -

Parameters:
  • path (String) --
def import_local(path)
  Dir[path].each do |file|
    validate_file_contains_plugin!(file) do
      # Without the expand_path it would fail if the path doesn't start with ./
      require File.expand_path(file)
    end
    refresh_plugins
  end
end

def import_plugin(path_or_url)

Returns:
  • (void) -

Parameters:
  • path_or_url (String) --
def import_plugin(path_or_url)
  raise "`import_plugin` requires a string" unless path_or_url.kind_of?(String)
  if path_or_url.start_with?("http")
    import_url(path_or_url)
  else
    import_local(path_or_url)
  end
end

def import_url(url)

Returns:
  • (void) -

Parameters:
  • url (String) --
def import_url(url)
  path = download(url)
  import_local(path)
end

def scm_provider

Returns:
  • (Symbol) - The name of the SCM Provider used for the active repository.
def scm_provider
  return :unknown unless env.request_source
  case env.request_source
  when Danger::RequestSources::GitHub
    :github
  when Danger::RequestSources::GitLab
    :gitlab
  when Danger::RequestSources::BitbucketServer
    :bitbucket_server
  when Danger::RequestSources::BitbucketCloud
    :bitbucket_cloud
  when Danger::RequestSources::VSTS
    :vsts
  else
    :unknown
  end
end

def validate_file_contains_plugin!(file)

Raises an error when the given block does not register a plugin.
def validate_file_contains_plugin!(file)
  plugin_count_was = Danger::Plugin.all_plugins.length
  yield
  if Danger::Plugin.all_plugins.length == plugin_count_was
    raise("#{file} doesn't contain any valid danger plugins.")
  end
end