From b9cc52db455b879fa048421851d7bd43bddde817 Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Mon, 2 Jan 2017 12:56:20 +0900 Subject: New feature: GitHubReleaseDownloadStrategy 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. 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. --- Library/Homebrew/download_strategy.rb | 76 +++++++++++++++++++++++ Library/Homebrew/test/download_strategies_test.rb | 47 +++++++++++++- 2 files changed, 122 insertions(+), 1 deletion(-) (limited to 'Library') diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 9f9b2abab..fa54ef716 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -532,6 +532,82 @@ class S3DownloadStrategy < CurlDownloadStrategy end 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. +# 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' + + def initialize(name, resource) + super + + @github_token = ENV["GITHUB_TOKEN"] + unless @github_token + puts "Environmental variable GITHUB_TOKEN is required." + raise CurlDownloadStrategyError, @url + end + + url_pattern = %r|https://github.com/(\S+)/(\S+)/releases/download/(\S+)/(\S+)| + unless @url =~ url_pattern + puts "Invalid url pattern for GitHub Release." + raise CurlDownloadStrategyError, @url + end + + _, @owner, @repo, @tag, @filename = *(@url.match(url_pattern)) + end + + def _fetch + puts "Download asset_id: #{asset_id}" + # HTTP request header `Accept: application/octet-stream` is required. + # Without this, the GitHub API will respond with metadata, not binary. + curl asset_url, "-C", downloaded_size, "-o", temporary_path, "-H", 'Accept: application/octet-stream' + end + + private + + def asset_url + "https://#{@github_token}@api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}" + end + + def asset_id + @asset_id ||= resolve_asset_id + end + + def resolve_asset_id + release_metadata = fetch_release_metadata + assets = release_metadata["assets"].select{ |a| a["name"] == @filename } + if assets.empty? + puts "Asset file not found." + raise CurlDownloadStrategyError, @url + end + + return assets.first["id"] + end + + def release_url + "https://api.github.com/repos/#{@owner}/#{@repo}/releases/tags/#{@tag}" + 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' + puts "GitHub Release not found." + raise CurlDownloadStrategyError, @url + else + raise e + end + end + + return JSON.parse(release_response) + end +end + class SubversionDownloadStrategy < VCSDownloadStrategy def initialize(name, resource) super diff --git a/Library/Homebrew/test/download_strategies_test.rb b/Library/Homebrew/test/download_strategies_test.rb index 87218fb12..1df0267af 100644 --- a/Library/Homebrew/test/download_strategies_test.rb +++ b/Library/Homebrew/test/download_strategies_test.rb @@ -2,11 +2,12 @@ require "testing_env" require "download_strategy" class ResourceDouble - attr_reader :url, :specs, :version + attr_reader :url, :specs, :version, :mirrors def initialize(url = "http://example.com/foo.tar.gz", specs = {}) @url = url @specs = specs + @mirrors = [] end end @@ -60,6 +61,50 @@ class VCSDownloadStrategyTests < Homebrew::TestCase end 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" + @strategy = GitHubReleaseDownloadStrategy.new("foo", resource) + end + + def test_initialize + assert_equal "token", @strategy.instance_variable_get(:@github_token) + assert_equal "owner", @strategy.instance_variable_get(:@owner) + assert_equal "repo", @strategy.instance_variable_get(:@repo) + assert_equal "tag", @strategy.instance_variable_get(:@tag) + assert_equal "foo_v0.1.0_darwin_amd64.tar.gz", @strategy.instance_variable_get(:@filename) + end + + def test_asset_url + @strategy.stubs(:resolve_asset_id).returns(456) + expected = "https://token@api.github.com/repos/owner/repo/releases/assets/456" + assert_equal expected, @strategy.send(:asset_url) + end + + def test_resolve_asset_id + release_metadata = { + "assets" => [ + { + "id" => 123, + "name" => "foo_v0.1.0_linux_amd64.tar.gz", + }, + { + "id" => 456, + "name" => "foo_v0.1.0_darwin_amd64.tar.gz", + }, + ] + } + @strategy.stubs(:fetch_release_metadata).returns(release_metadata) + assert_equal 456, @strategy.send(:resolve_asset_id) + end + + def test_release_url + expected = "https://api.github.com/repos/owner/repo/releases/tags/tag" + assert_equal expected, @strategy.send(:release_url) + end +end + class GitDownloadStrategyTests < Homebrew::TestCase include FileUtils -- cgit v1.2.3 From 248beb9bf6ce0b9afc97dfc72067b6acfcb5eeb8 Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Tue, 3 Jan 2017 14:36:08 +0900 Subject: Move error messages in GitHubReleaseDownloadStrategy to raise argument --- Library/Homebrew/download_strategy.rb | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index fa54ef716..6e618f720 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -546,16 +546,10 @@ class GitHubReleaseDownloadStrategy < CurlDownloadStrategy super @github_token = ENV["GITHUB_TOKEN"] - unless @github_token - puts "Environmental variable GITHUB_TOKEN is required." - raise CurlDownloadStrategyError, @url - end + raise CurlDownloadStrategyError, "Environmental variable GITHUB_TOKEN is required." unless @github_token url_pattern = %r|https://github.com/(\S+)/(\S+)/releases/download/(\S+)/(\S+)| - unless @url =~ url_pattern - puts "Invalid url pattern for GitHub Release." - raise CurlDownloadStrategyError, @url - end + raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." unless @url =~ url_pattern _, @owner, @repo, @tag, @filename = *(@url.match(url_pattern)) end @@ -580,10 +574,7 @@ class GitHubReleaseDownloadStrategy < CurlDownloadStrategy def resolve_asset_id release_metadata = fetch_release_metadata assets = release_metadata["assets"].select{ |a| a["name"] == @filename } - if assets.empty? - puts "Asset file not found." - raise CurlDownloadStrategyError, @url - end + raise CurlDownloadStrategyError, "Asset file not found." if assets.empty? return assets.first["id"] end @@ -597,8 +588,7 @@ class GitHubReleaseDownloadStrategy < CurlDownloadStrategy release_response = open(release_url, {:http_basic_authentication => [@github_token]}).read rescue OpenURI::HTTPError => e if e.message == '404 Not Found' - puts "GitHub Release not found." - raise CurlDownloadStrategyError, @url + raise CurlDownloadStrategyError, "GitHub Release not found." else raise e end -- cgit v1.2.3 From a4330f458a09e946ef7bac912fba63628dbf67ca Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Tue, 3 Jan 2017 14:58:08 +0900 Subject: Use util/github insted of open-uri in GitHubReleaseDownloadStrategy --- Library/Homebrew/download_strategy.rb | 21 ++++++--------------- Library/Homebrew/test/download_strategies_test.rb | 2 +- 2 files changed, 7 insertions(+), 16 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 6e618f720..19af8820c 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 1df0267af..e69bdfda2 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 -- cgit v1.2.3 From 335be35acf805a2853a6fe92b06d9a643616f463 Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Sun, 8 Jan 2017 16:44:54 +0900 Subject: Generalize GitHubReleaseDownloadStrategy in order to support archive URL --- Library/Homebrew/download_strategy.rb | 81 ++++++++++++++++------- Library/Homebrew/test/download_strategies_test.rb | 45 ++++++++++--- 2 files changed, 93 insertions(+), 33 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 19af8820c..3e028949b 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -532,42 +532,78 @@ class S3DownloadStrategy < CurlDownloadStrategy end 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 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 "utils/formatter" - require 'utils/github' - +# GitHubPrivateRepositoryDownloadStrategy downloads contents from GitHub +# Private Repository. To use it, add +# ":using => GitHubPrivateRepositoryDownloadStrategy" to the URL section of +# your formula. This download strategy uses GitHub access tokens (in the +# 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 GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy def initialize(name, resource) super + set_github_token + parse_url_pattern + end + + def parse_url_pattern + url_pattern = %r|https://github.com/([^/]+)/([^/]+)/(\S+)| + unless @url =~ url_pattern + raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Repository." + end + + _, @owner, @repo, @filepath = *(@url.match(url_pattern)) + end + + def download_url + "https://#{@github_token}@github.com/#{@owner}/#{@repo}/#{@filepath}" + end + + def _fetch + curl download_url, "-C", downloaded_size, "-o", temporary_path + end + + private + def set_github_token @github_token = ENV["HOMEBREW_GITHUB_API_TOKEN"] - raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required." unless @github_token + unless @github_token + raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required." + end + end +end - url_pattern = %r|https://github.com/(\S+)/(\S+)/releases/download/(\S+)/(\S+)| - raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." unless @url =~ url_pattern +# GitHubPrivateRepositoryReleaseDownloadStrategy downloads tarballs from GitHub +# Release assets. To use it, add +# ":using => GitHubPrivateRepositoryReleaseDownloadStrategy" to the URL section +# of your formula. This download strategy uses GitHub access tokens (in the +# environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request. +class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDownloadStrategy + require "utils/formatter" + require 'utils/github' + + def parse_url_pattern + url_pattern = %r|https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(\S+)| + unless @url =~ url_pattern + raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." + end _, @owner, @repo, @tag, @filename = *(@url.match(url_pattern)) end + def download_url + "https://#{@github_token}@api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}" + end + def _fetch - puts "Download asset_id: #{asset_id}" # HTTP request header `Accept: application/octet-stream` is required. # Without this, the GitHub API will respond with metadata, not binary. - curl asset_url, "-C", downloaded_size, "-o", temporary_path, "-H", 'Accept: application/octet-stream' + curl download_url, "-C", downloaded_size, "-o", temporary_path, "-H", 'Accept: application/octet-stream' end private - def asset_url - "https://#{@github_token}@api.github.com/repos/#{@owner}/#{@repo}/releases/assets/#{asset_id}" - end - def asset_id @asset_id ||= resolve_asset_id end @@ -580,11 +616,8 @@ class GitHubReleaseDownloadStrategy < CurlDownloadStrategy return assets.first["id"] end - def release_url - "https://api.github.com/repos/#{@owner}/#{@repo}/releases/tags/#{@tag}" - end - def fetch_release_metadata + release_url = "https://api.github.com/repos/#{@owner}/#{@repo}/releases/tags/#{@tag}" 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 e69bdfda2..21f7e6906 100644 --- a/Library/Homebrew/test/download_strategies_test.rb +++ b/Library/Homebrew/test/download_strategies_test.rb @@ -61,25 +61,47 @@ class VCSDownloadStrategyTests < Homebrew::TestCase end end -class GitHubReleaseDownloadStrategyTests < Homebrew::TestCase +class GitHubPrivateRepositoryDownloadStrategyTests < Homebrew::TestCase 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/archive/1.1.5.tar.gz") ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token" - @strategy = GitHubReleaseDownloadStrategy.new("foo", resource) + @strategy = GitHubPrivateRepositoryDownloadStrategy.new("foo", resource) end - def test_initialize + def test_set_github_token assert_equal "token", @strategy.instance_variable_get(:@github_token) + end + + def test_parse_url_pattern + assert_equal "owner", @strategy.instance_variable_get(:@owner) + assert_equal "repo", @strategy.instance_variable_get(:@repo) + assert_equal "archive/1.1.5.tar.gz", @strategy.instance_variable_get(:@filepath) + end + + def test_download_url + expected = "https://token@github.com/owner/repo/archive/1.1.5.tar.gz" + assert_equal expected, @strategy.download_url + end +end + +class GitHubPrivateRepositoryReleaseDownloadStrategyTests < Homebrew::TestCase + def setup + resource = ResourceDouble.new("https://github.com/owner/repo/releases/download/tag/foo_v0.1.0_darwin_amd64.tar.gz") + ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token" + @strategy = GitHubPrivateRepositoryReleaseDownloadStrategy.new("foo", resource) + end + + def test_parse_url_pattern assert_equal "owner", @strategy.instance_variable_get(:@owner) assert_equal "repo", @strategy.instance_variable_get(:@repo) assert_equal "tag", @strategy.instance_variable_get(:@tag) assert_equal "foo_v0.1.0_darwin_amd64.tar.gz", @strategy.instance_variable_get(:@filename) end - def test_asset_url + def test_download_url @strategy.stubs(:resolve_asset_id).returns(456) expected = "https://token@api.github.com/repos/owner/repo/releases/assets/456" - assert_equal expected, @strategy.send(:asset_url) + assert_equal expected, @strategy.download_url end def test_resolve_asset_id @@ -99,9 +121,14 @@ class GitHubReleaseDownloadStrategyTests < Homebrew::TestCase assert_equal 456, @strategy.send(:resolve_asset_id) end - def test_release_url - expected = "https://api.github.com/repos/owner/repo/releases/tags/tag" - assert_equal expected, @strategy.send(:release_url) + def test_fetch_release_metadata + expected_release_url = "https://api.github.com/repos/owner/repo/releases/tags/tag" + github_mock = MiniTest::Mock.new + github_mock.expect :call, {}, [expected_release_url] + GitHub.stub :open, github_mock do + @strategy.send(:fetch_release_metadata) + end + github_mock.verify end end -- cgit v1.2.3 From 560d5bdd7101d4e73f6501b5ac1601b0a434cece Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Sun, 8 Jan 2017 18:00:31 +0900 Subject: Validate a token when initializing GitHubPrivateRepositoryDownloadStrategy --- Library/Homebrew/download_strategy.rb | 24 +++++++++++++++++++---- Library/Homebrew/test/download_strategies_test.rb | 2 ++ 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 3e028949b..d22ab9550 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -541,10 +541,13 @@ end # it lets you use a private GttHub repository for internal distribution. It # works with public one, but in that case simply use CurlDownloadStrategy. class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy + require "utils/formatter" + require 'utils/github' + def initialize(name, resource) super - set_github_token parse_url_pattern + set_github_token end def parse_url_pattern @@ -571,6 +574,22 @@ class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy unless @github_token raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required." end + validate_github_repository_access! + end + + def validate_github_repository_access! + begin + # Test access to the repository + GitHub.repository(@owner, @repo) + rescue GitHub::HTTPNotFoundError + # We only handle HTTPNotFoundError here, + # becase AuthenticationFailedError is handled within util/github. + message = <<-EOS.undent + HOMEBREW_GITHUB_API_TOKEN can not access the repository: #{@owner}/#{@repo} + This token may not have permission to access the repository or the url of formula may be incorrect. + EOS + raise CurlDownloadStrategyError, message + end end end @@ -580,9 +599,6 @@ end # of your formula. This download strategy uses GitHub access tokens (in the # environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request. class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDownloadStrategy - require "utils/formatter" - require 'utils/github' - def parse_url_pattern url_pattern = %r|https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(\S+)| unless @url =~ url_pattern diff --git a/Library/Homebrew/test/download_strategies_test.rb b/Library/Homebrew/test/download_strategies_test.rb index 21f7e6906..ff4cbbf7a 100644 --- a/Library/Homebrew/test/download_strategies_test.rb +++ b/Library/Homebrew/test/download_strategies_test.rb @@ -65,6 +65,7 @@ class GitHubPrivateRepositoryDownloadStrategyTests < Homebrew::TestCase def setup resource = ResourceDouble.new("https://github.com/owner/repo/archive/1.1.5.tar.gz") ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token" + GitHub.stubs(:repository).returns {} @strategy = GitHubPrivateRepositoryDownloadStrategy.new("foo", resource) end @@ -88,6 +89,7 @@ class GitHubPrivateRepositoryReleaseDownloadStrategyTests < Homebrew::TestCase def setup resource = ResourceDouble.new("https://github.com/owner/repo/releases/download/tag/foo_v0.1.0_darwin_amd64.tar.gz") ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token" + GitHub.stubs(:repository).returns {} @strategy = GitHubPrivateRepositoryReleaseDownloadStrategy.new("foo", resource) end -- cgit v1.2.3 From 12b9cb7f4c5e0927cb1049db4f1aa53fbe371a6d Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Sun, 8 Jan 2017 18:29:20 +0900 Subject: Fix rubocop style warning of download_strategy --- Library/Homebrew/download_strategy.rb | 34 +++++++++++------------ Library/Homebrew/test/download_strategies_test.rb | 2 +- 2 files changed, 17 insertions(+), 19 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index d22ab9550..bd036067d 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -542,7 +542,7 @@ end # works with public one, but in that case simply use CurlDownloadStrategy. class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy require "utils/formatter" - require 'utils/github' + require "utils/github" def initialize(name, resource) super @@ -551,12 +551,12 @@ class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy end def parse_url_pattern - url_pattern = %r|https://github.com/([^/]+)/([^/]+)/(\S+)| + url_pattern = %r{https://github.com/([^/]+)/([^/]+)/(\S+)} unless @url =~ url_pattern raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Repository." end - _, @owner, @repo, @filepath = *(@url.match(url_pattern)) + _, @owner, @repo, @filepath = *@url.match(url_pattern) end def download_url @@ -578,18 +578,16 @@ class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy end def validate_github_repository_access! - begin - # Test access to the repository - GitHub.repository(@owner, @repo) - rescue GitHub::HTTPNotFoundError - # We only handle HTTPNotFoundError here, - # becase AuthenticationFailedError is handled within util/github. - message = <<-EOS.undent + # Test access to the repository + GitHub.repository(@owner, @repo) + rescue GitHub::HTTPNotFoundError + # We only handle HTTPNotFoundError here, + # becase AuthenticationFailedError is handled within util/github. + message = <<-EOS.undent HOMEBREW_GITHUB_API_TOKEN can not access the repository: #{@owner}/#{@repo} This token may not have permission to access the repository or the url of formula may be incorrect. - EOS - raise CurlDownloadStrategyError, message - end + EOS + raise CurlDownloadStrategyError, message end end @@ -600,12 +598,12 @@ end # environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request. class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDownloadStrategy def parse_url_pattern - url_pattern = %r|https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(\S+)| + url_pattern = %r{https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(\S+)} unless @url =~ url_pattern raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Release." end - _, @owner, @repo, @tag, @filename = *(@url.match(url_pattern)) + _, @owner, @repo, @tag, @filename = *@url.match(url_pattern) end def download_url @@ -615,7 +613,7 @@ class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDo def _fetch # HTTP request header `Accept: application/octet-stream` is required. # Without this, the GitHub API will respond with metadata, not binary. - curl download_url, "-C", downloaded_size, "-o", temporary_path, "-H", 'Accept: application/octet-stream' + curl download_url, "-C", downloaded_size, "-o", temporary_path, "-H", "Accept: application/octet-stream" end private @@ -626,10 +624,10 @@ class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDo def resolve_asset_id release_metadata = fetch_release_metadata - assets = release_metadata["assets"].select{ |a| a["name"] == @filename } + assets = release_metadata["assets"].select { |a| a["name"] == @filename } raise CurlDownloadStrategyError, "Asset file not found." if assets.empty? - return assets.first["id"] + assets.first["id"] end def fetch_release_metadata diff --git a/Library/Homebrew/test/download_strategies_test.rb b/Library/Homebrew/test/download_strategies_test.rb index ff4cbbf7a..2b64abbf9 100644 --- a/Library/Homebrew/test/download_strategies_test.rb +++ b/Library/Homebrew/test/download_strategies_test.rb @@ -117,7 +117,7 @@ class GitHubPrivateRepositoryReleaseDownloadStrategyTests < Homebrew::TestCase "id" => 456, "name" => "foo_v0.1.0_darwin_amd64.tar.gz", }, - ] + ], } @strategy.stubs(:fetch_release_metadata).returns(release_metadata) assert_equal 456, @strategy.send(:resolve_asset_id) -- cgit v1.2.3