module PuppetfileResolver::SpecSearchers::Git

Public Class Methods

find_all(puppetfile_module, dependency, cache, resolver_ui, config) click to toggle source
# File lib/puppetfile-resolver/spec_searchers/git.rb, line 10
def self.find_all(puppetfile_module, dependency, cache, resolver_ui, config)
  dep_id = ::PuppetfileResolver::SpecSearchers::Common.dependency_cache_id(self, dependency)
  # Has the information been cached?
  return cache.load(dep_id) if cache.exist?(dep_id)

  # We _could_ git clone this, but it'll take too long. So for now, just
  # try and resolve github based repositories by crafting a URL

  repo_url = nil
  if puppetfile_module.remote.start_with?('git@github.com:')
    repo_url = puppetfile_module.remote.slice(15..-1)
    repo_url = repo_url.slice(0..-5) if repo_url.end_with?('.git')
  end
  if puppetfile_module.remote.start_with?('https://github.com/')
    repo_url = puppetfile_module.remote.slice(19..-1)
    repo_url = repo_url.slice(0..-5) if repo_url.end_with?('.git')
  end

  return [] if repo_url.nil?

  metadata_url = 'https://raw.githubusercontent.com/' + repo_url + '/'
  if puppetfile_module.ref
    metadata_url += puppetfile_module.ref + '/'
  elsif puppetfile_module.tag
    metadata_url += puppetfile_module.tag + '/'
  else
    # Default to master.  Should it raise?
    metadata_url += 'master/'
  end
  metadata_url += 'metadata.json'

  require 'net/http'
  require 'uri'

  resolver_ui.debug { "Querying GitHub with #{metadata_url}" }
  err_msg = "Unable to find module at #{puppetfile_module.remote}"
  err_msg += config.proxy ? " with proxy #{config.proxy}: " : ': '
  response = nil

  begin
    response = ::PuppetfileResolver::Util.net_http_get(metadata_url, config.proxy)
  rescue ::StandardError => e
    raise err_msg + e.message
  end

  if response.code != '200'
    resolver_ui.debug(err_msg + "Expected HTTP Code 200, but received #{response.code}")
    cache.save(dep_id, [])
    return []
  end

  # TODO: symbolise_object should be in a Util namespace
  metadata = ::PuppetfileResolver::Util.symbolise_object(::JSON.parse(response.body))
  result = [Models::ModuleSpecification.new(
    name: metadata[:name],
    origin: :git,
    version: metadata[:version],
    metadata: metadata
  )]

  cache.save(dep_id, result)

  result
end