Use util/github insted of open-uri in GitHubReleaseDownloadStrategy

This commit is contained in:
Masayuki Morita 2017-01-03 14:58:08 +09:00
parent 248beb9bf6
commit a4330f458a
2 changed files with 7 additions and 16 deletions

View File

@ -535,18 +535,19 @@ end
# GitHubReleaseDownloadStrategy downloads tarballs from GitHub Release assets. # GitHubReleaseDownloadStrategy downloads tarballs from GitHub Release assets.
# To use it, add ":using => GitHubReleaseDownloadStrategy" to the URL section # To use it, add ":using => GitHubReleaseDownloadStrategy" to the URL section
# of your formula. This download strategy uses GitHub access tokens (in the # of your formula. This download strategy uses GitHub access tokens (in the
# environment variables GITHUB_TOKEN) to sign the request. # environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request.
# This strategy is suitable for corporate use just like S3DownloadStrategy, # This strategy is suitable for corporate use just like S3DownloadStrategy,
# because it lets you use a private GttHub repository for internal distribution. # because it lets you use a private GttHub repository for internal distribution.
# It works with public one, but in that case simply use CurlDownloadStrategy. # It works with public one, but in that case simply use CurlDownloadStrategy.
class GitHubReleaseDownloadStrategy < CurlDownloadStrategy class GitHubReleaseDownloadStrategy < CurlDownloadStrategy
require 'open-uri' require "utils/formatter"
require 'utils/github'
def initialize(name, resource) def initialize(name, resource)
super super
@github_token = ENV["GITHUB_TOKEN"] @github_token = ENV["HOMEBREW_GITHUB_API_TOKEN"]
raise CurlDownloadStrategyError, "Environmental variable GITHUB_TOKEN is required." unless @github_token raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required." unless @github_token
url_pattern = %r|https://github.com/(\S+)/(\S+)/releases/download/(\S+)/(\S+)| url_pattern = %r|https://github.com/(\S+)/(\S+)/releases/download/(\S+)/(\S+)|
raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." unless @url =~ url_pattern raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." unless @url =~ url_pattern
@ -584,17 +585,7 @@ class GitHubReleaseDownloadStrategy < CurlDownloadStrategy
end end
def fetch_release_metadata def fetch_release_metadata
begin GitHub.open(release_url)
release_response = open(release_url, {:http_basic_authentication => [@github_token]}).read
rescue OpenURI::HTTPError => e
if e.message == '404 Not Found'
raise CurlDownloadStrategyError, "GitHub Release not found."
else
raise e
end
end
return JSON.parse(release_response)
end end
end end

View File

@ -64,7 +64,7 @@ end
class GitHubReleaseDownloadStrategyTests < Homebrew::TestCase class GitHubReleaseDownloadStrategyTests < Homebrew::TestCase
def setup def setup
resource = ResourceDouble.new("https://github.com/owner/repo/releases/download/tag/foo_v0.1.0_darwin_amd64.tar.gz") resource = ResourceDouble.new("https://github.com/owner/repo/releases/download/tag/foo_v0.1.0_darwin_amd64.tar.gz")
ENV["GITHUB_TOKEN"] = "token" ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
@strategy = GitHubReleaseDownloadStrategy.new("foo", resource) @strategy = GitHubReleaseDownloadStrategy.new("foo", resource)
end end