Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

enable downloading from private github repos

  • Loading branch information...
commit 45b910fb25e4fda83b8444df7c301183d9deaad3 1 parent 9d12f89
@punkle punkle authored
View
1  berkshelf.gemspec
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
s.add_dependency 'ridley', '~> 2.3'
s.add_dependency 'solve', '>= 0.8.0'
s.add_dependency 'thor', '~> 0.18.0'
+ s.add_dependency 'octokit', '~> 2.6'
s.add_development_dependency 'aruba', '~> 0.5'
s.add_development_dependency 'chef-zero', '~> 1.5.0'
View
4 lib/berkshelf/config.rb
@@ -123,5 +123,9 @@ def initialize(path = self.class.path, options = {})
type: Boolean,
default: true,
required: true
+ attribute 'github.access_token',
+ type: String,
+ default: '',
+ required: false
end
end
View
25 lib/berkshelf/downloader.rb
@@ -1,6 +1,7 @@
require 'net/http'
require 'zlib'
require 'archive/tar/minitar'
+require 'octokit'
module Berkshelf
class Downloader
@@ -72,18 +73,32 @@ def try_download(source, name, version)
when :github
tmp_dir = Dir.mktmpdir
archive_path = File.join(tmp_dir, "#{name}-#{version}.tar.gz")
- out_dir = File.join(tmp_dir, "#{name}-#{version}")
- url = URI("https://codeload.github.com/#{remote_cookbook.location_path}/tar.gz/v#{version}")
+ unpack_dir = File.join(tmp_dir, "#{name}-#{version}")
+
+ github_access_token = Berkshelf::Config.instance.github.access_token
+ github_config = {}
+ github_config[:access_token] = github_access_token unless github_access_token == ''
+ github_client = Octokit::Client.new github_config
+
+ begin
+ url = URI(github_client.archive_link(remote_cookbook.location_path, ref: "v#{version}"))
+ rescue Octokit::Unauthorized
+ raise CookbookNotFound
+ end
Net::HTTP.start(url.host, use_ssl: url.scheme == "https") do |http|
- resp = http.get(url.path)
+ resp = http.get(url.request_uri)
+ raise CookbookNotFound unless resp.is_a?(Net::HTTPSuccess)
open(archive_path, "wb") { |file| file.write(resp.body) }
end
tgz = Zlib::GzipReader.new(File.open(archive_path, "rb"))
- Archive::Tar::Minitar.unpack(tgz, tmp_dir)
+ Archive::Tar::Minitar.unpack(tgz, unpack_dir)
- out_dir
+ # we need to figure out where the cookbook is located in the archive. This is because the directory name
+ # pattern is not cosistant between private and public github repositories
+ cookbook_directory = Dir.entries(unpack_dir).select{ |f| f.start_with? "#{remote_cookbook.location_path.sub('/', '-')}"}[0]
+ File.join(unpack_dir, cookbook_directory)
else
raise RuntimeError, "unknown location type #{remote_cookbook.location_type}"
end
Please sign in to comment.
Something went wrong with that request. Please try again.