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



101
102
103
104
105
106
107
108
109
110
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 101

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.fetch(TOKEN_KEY, nil)}"
  else
    url << "?access_token=#{ENV.fetch(TOKEN_KEY, nil)}"
  end
  url
end

#cache_version_unpacked!(version) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 56

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



87
88
89
90
91
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 87

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



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

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

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

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

#manifestsObject



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

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

#token_key_nil?Boolean

Returns:

  • (Boolean)


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

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

#token_key_valueObject



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

def token_key_value
  ENV.fetch(TOKEN_KEY, nil)
end

#vendor_cache(name, version) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/librarian/puppet/source/githubtarball/repo.rb', line 67

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|
    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}"
  end
end

#versionsObject

Raises:

  • (Error)


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

def versions
  return @versions if @versions

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

  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