Skip to content

Instantly share code, notes, and snippets.

@capripot
Created January 22, 2020 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capripot/140c1dc77431a9797195038d83fcdac3 to your computer and use it in GitHub Desktop.
Save capripot/140c1dc77431a9797195038d83fcdac3 to your computer and use it in GitHub Desktop.
Homebrew Private Download Strategy
# GitHubPrivateRepositoryDownloadStrategy downloads contents from GitHub
# Private Repository. To use it, add
# `:using => GitHubPrivateRepositoryDownloadStrategy` to the URL section of
# your formula. This download strategy uses Homebrew GitHub.api_credentials
# to authenticate the request.
# This strategy is suitable for corporate use just like S3DownloadStrategy,
# because it lets you use a private GitHub repository for internal distribution.
class GitHubPrivateRepositoryDownloadStrategy < CurlDownloadStrategy
require "utils/formatter"
require "utils/github"
def initialize(url, name, version, **meta)
super
parse_url_pattern
verify_credentials!
end
def parse_url_pattern
unless match = url.match(%r{https://github.com/([^/]+)/([^/]+)/(\S+)})
raise CurlDownloadStrategyError, "Invalid url pattern for GitHub Repository."
end
_, @owner, @repo, @filepath = *match
end
def download_url
"https://#{@github_token}@github.com/#{@owner}/#{@repo}/#{@filepath}"
end
private
def _fetch(*)
curl_download download_url, *credentials, :to => temporary_path
end
def credentials
token, username = GitHub.api_credentials
case GitHub.api_credentials_type
when :keychain_username_password
["--user", "#{username}:#{token}"]
when :keychain # replaced by :keychain_username_password
["--user", "#{username}:#{token}"]
when :environment # replaced by :env_token
["--header", "Authorization: token #{token}"]
when :env_token
["--header", "Authorization: token #{token}"]
else
raise CurlDownloadStrategyError, "Unkown API Credentials Type: #{GitHub.api_credentials_type}"
end
end
def verify_credentials!
return unless GitHub.api_credentials_type == :none
raise CurlDownloadStrategyError, "Make sure to have credentials set up for GitHub,\n"\
"either through the environment variable HOMEBREW_GITHUB_API_TOKEN\n"\
"or throuh osx-keychain (preferred): "\
"https://help.github.com/en/articles/caching-your-github-password-in-git"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment