Validate a token when initializing GitHubPrivateRepositoryDownloadStrategy
This commit is contained in:
parent
335be35acf
commit
560d5bdd71
@ -541,10 +541,13 @@ end
|
|||||||
# it lets you use a private GttHub repository for internal distribution. It
|
# it lets you use a private GttHub repository for internal distribution. It
|
||||||
# works with public one, but in that case simply use CurlDownloadStrategy.
|
# works with public one, but in that case simply use CurlDownloadStrategy.
|
||||||
class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy
|
class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy
|
||||||
|
require "utils/formatter"
|
||||||
|
require 'utils/github'
|
||||||
|
|
||||||
def initialize(name, resource)
|
def initialize(name, resource)
|
||||||
super
|
super
|
||||||
set_github_token
|
|
||||||
parse_url_pattern
|
parse_url_pattern
|
||||||
|
set_github_token
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_url_pattern
|
def parse_url_pattern
|
||||||
@ -571,6 +574,22 @@ class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy
|
|||||||
unless @github_token
|
unless @github_token
|
||||||
raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required."
|
raise CurlDownloadStrategyError, "Environmental variable HOMEBREW_GITHUB_API_TOKEN is required."
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -580,9 +599,6 @@ end
|
|||||||
# 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 HOMEBREW_GITHUB_API_TOKEN) to sign the request.
|
# environment variables HOMEBREW_GITHUB_API_TOKEN) to sign the request.
|
||||||
class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDownloadStrategy
|
class GitHubPrivateRepositoryReleaseDownloadStrategy < GitHubPrivateRepositoryDownloadStrategy
|
||||||
require "utils/formatter"
|
|
||||||
require 'utils/github'
|
|
||||||
|
|
||||||
def parse_url_pattern
|
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
|
unless @url =~ url_pattern
|
||||||
|
|||||||
@ -65,6 +65,7 @@ class GitHubPrivateRepositoryDownloadStrategyTests < Homebrew::TestCase
|
|||||||
def setup
|
def setup
|
||||||
resource = ResourceDouble.new("https://github.com/owner/repo/archive/1.1.5.tar.gz")
|
resource = ResourceDouble.new("https://github.com/owner/repo/archive/1.1.5.tar.gz")
|
||||||
ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
|
ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
|
||||||
|
GitHub.stubs(:repository).returns {}
|
||||||
@strategy = GitHubPrivateRepositoryDownloadStrategy.new("foo", resource)
|
@strategy = GitHubPrivateRepositoryDownloadStrategy.new("foo", resource)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -88,6 +89,7 @@ class GitHubPrivateRepositoryReleaseDownloadStrategyTests < 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["HOMEBREW_GITHUB_API_TOKEN"] = "token"
|
ENV["HOMEBREW_GITHUB_API_TOKEN"] = "token"
|
||||||
|
GitHub.stubs(:repository).returns {}
|
||||||
@strategy = GitHubPrivateRepositoryReleaseDownloadStrategy.new("foo", resource)
|
@strategy = GitHubPrivateRepositoryReleaseDownloadStrategy.new("foo", resource)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user