From a4330f458a09e946ef7bac912fba63628dbf67ca Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Tue, 3 Jan 2017 14:58:08 +0900 Subject: [PATCH] Use util/github insted of open-uri in GitHubReleaseDownloadStrategy --- Library/Homebrew/download_strategy.rb | 21 ++++++------------- .../Homebrew/test/download_strategies_test.rb | 2 +- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 6e618f720e..19af8820c5 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -535,18 +535,19 @@ end # GitHubReleaseDownloadStrategy downloads tarballs from GitHub Release assets. # To use it, add ":using => GitHubReleaseDownloadStrategy" to the URL section # 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, # because it lets you use a private GttHub repository for internal distribution. # It works with public one, but in that case simply use CurlDownloadStrategy. class GitHubReleaseDownloadStrategy < CurlDownloadStrategy - require 'open-uri' + require "utils/formatter" + require 'utils/github' def initialize(name, resource) super - @github_token = ENV["GITHUB_TOKEN"] - raise CurlDownloadStrategyError, "Environmental variable GITHUB_TOKEN is required." unless @github_token + @github_token = ENV["HOMEBREW_GITHUB_API_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+)| raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." unless @url =~ url_pattern @@ -584,17 +585,7 @@ class GitHubReleaseDownloadStrategy < CurlDownloadStrategy end def fetch_release_metadata - begin - 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) + GitHub.open(release_url) end end diff --git a/Library/Homebrew/test/download_strategies_test.rb b/Library/Homebrew/test/download_strategies_test.rb index 1df0267af3..e69bdfda23 100644 --- a/Library/Homebrew/test/download_strategies_test.rb +++ b/Library/Homebrew/test/download_strategies_test.rb @@ -64,7 +64,7 @@ end class GitHubReleaseDownloadStrategyTests < Homebrew::TestCase def setup 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) end