Class: Librarian::Puppet::Source::GitHubTarball::Repo

Inherits:
Repo
  • Object
show all
Includes:
Util
Defined in:
lib/librarian/puppet/source/githubtarball/repo.rb

Constant Summary collapse

TOKEN_KEY =
'GITHUB_API_TOKEN'

Instance Attribute Summary

Attributes inherited from Repo

#name, #source

Instance Method Summary collapse

Methods included from Util

#clean_uri, #cp_r, #debug, #info, #module_name, #normalize_name, #rsync?, #warn

Methods inherited from Repo

#cache_path, #environment, #initialize, #vendored?, #vendored_path, #version_unpacked_cache_path

Constructor Details

This class inherits a constructor from Librarian::Puppet::Source::Repo

Instance Method Details

#add_api_token_to_url(url) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 106

def add_api_token_to_url url
  if token_key_nil?
    debug { "#{TOKEN_KEY} environment value is empty or missing" }
  elsif url.include? "?"
    url << "&access_token=#{ENV[TOKEN_KEY]}"
  else
    url << "?access_token=#{ENV[TOKEN_KEY]}"
  end
  url
end

#cache_version_unpacked!(version) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 59

def cache_version_unpacked!(version)
  path = version_unpacked_cache_path(version)
  return if path.directory?

  path.mkpath

  target = vendored?(vendored_name, version) ? vendored_path(vendored_name, version) : name

  Librarian::Posix.run!(%W{tar xzf #{target} -C #{path}})
end

#clean_up_old_cached_versions(name) ⇒ Object



92
93
94
95
96
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 92

def clean_up_old_cached_versions(name)
  Dir["#{environment.vendor_cache}/#{name}*.tar.gz"].each do |old_version|
    FileUtils.rm old_version
  end
end

#install_version!(version, install_path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 42

def install_version!(version, install_path)
  if environment.local? && !vendored?(vendored_name, version)
    raise Error, "Could not find a local copy of #{source.uri} at #{version}."
  end

  vendor_cache(source.uri.to_s, version) unless vendored?(vendored_name, version)

  cache_version_unpacked! version

  if install_path.exist? && rsync? != true
    install_path.rmtree
  end

  unpacked_path = version_unpacked_cache_path(version).children.first
  cp_r(unpacked_path, install_path)
end

#manifestsObject



36
37
38
39
40
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 36

def manifests
  versions.map do |version|
    Manifest.new(source, name, version)
  end
end

#token_key_nil?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 102

def token_key_nil?
  token_key_value.nil? || token_key_value.empty?
end

#token_key_valueObject



98
99
100
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 98

def token_key_value
  ENV[TOKEN_KEY]
end

#vendor_cache(name, version) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 70

def vendor_cache(name, version)
  clean_up_old_cached_versions(vendored_name(name))

  url = "https://api.github.com/repos/#{name}/tarball/#{version}"
  add_api_token_to_url(url)

  environment.vendor!
  File.open(vendored_path(vendored_name(name), version).to_s, 'wb') do |f|
    begin
      debug { "Downloading <#{url}> to <#{f.path}>" }
      URI.open(url,
        "User-Agent" => "librarian-puppet v#{Librarian::Puppet::VERSION}") do |res|
        while buffer = res.read(8192)
          f.write(buffer)
        end
      end
    rescue OpenURI::HTTPError => e
      raise e, "Error requesting <#{url}>: #{e.to_s}"
    end
  end
end

#versionsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 18

def versions
  return @versions if @versions
  data = api_call("/repos/#{source.uri}/tags")
  if data.nil?
    raise Error, "Unable to find module '#{source.uri}' on https://github.com"
  end

  all_versions = data.map { |r| r['name'].gsub(/^v/, '') }.sort.reverse

  all_versions.delete_if do |version|
    version !~ /\A\d+\.\d+(\.\d+.*)?\z/
  end

  @versions = all_versions.compact
  debug { "  Module #{name} found versions: #{@versions.join(", ")}" }
  @versions
end