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 ++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 24 deletions(-) (limited to 'Library/Homebrew/download_strategy.rb') 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 -- cgit v1.2.3