Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub::API: Allow passing options to curl #15364

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Library/Homebrew/livecheck/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,16 @@ module Strategy

# Baseline `curl` options used in {Strategy} methods.
DEFAULT_CURL_OPTIONS = {
print_stdout: false,
print_stderr: false,
# By default, `Utils::Curl` methods will print text (e.g. the command)
# when `--debug` is set. `debug` is set to `false` to ensure that this
# unwanted text doesn't appear in livecheck's debug output.
debug: false,
verbose: false,
timeout: CURL_PROCESS_TIMEOUT,
connect_timeout: CURL_CONNECT_TIMEOUT,
max_time: CURL_MAX_TIME,
# `Utils::Curl` uses curl's `--retry` option by default but this isn't
# valuable enough in the context of livecheck to justify the additional
# effort. A zero value ensures that the `--retry` option is omitted.
retries: 0,
}.freeze

Expand Down
8 changes: 6 additions & 2 deletions Library/Homebrew/livecheck/strategy/github_latest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def self.generate_input_values(url)
match = url.sub(/\.git$/i, "").match(URL_MATCH_REGEX)
return values if match.blank?

values[:url] = "https://api.github.com/repos/#{match[:username]}/#{match[:repository]}/releases/latest"
values[:url] = "#{GitHub::API_URL}/repos/#{match[:username]}/#{match[:repository]}/releases/latest"
values[:username] = match[:username]
values[:repository] = match[:repository]

Expand Down Expand Up @@ -141,7 +141,11 @@ def self.find_versions(url:, regex: DEFAULT_REGEX, **_unused, &block)

match_data[:url] = generated[:url]

release = GitHub.get_latest_release(generated[:username], generated[:repository])
release = GitHub.get_latest_release(
generated[:username],
generated[:repository],
**Strategy::DEFAULT_CURL_OPTIONS,
)
versions_from_content(release, regex, &block).each do |match_text|
match_data[:matches][match_text] = Version.new(match_text)
end
Expand Down
31 changes: 29 additions & 2 deletions Library/Homebrew/utils/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,36 @@ def self.get_release(user, repo, tag)
API.open_rest(url, request_method: :GET)
end

def self.get_latest_release(user, repo)
sig {
params(
user: String,
repo: String,
debug: T.nilable(T::Boolean),
timeout: T.nilable(T.any(Integer, Float)),
connect_timeout: T.nilable(T.any(Integer, Float)),
max_time: T.nilable(T.any(Integer, Float)),
retries: T.nilable(Integer),
).returns(T::Hash[String, T.untyped])
}
def self.get_latest_release(
user,
repo,
debug: nil,
timeout: nil,
connect_timeout: nil,
max_time: nil,
retries: nil
)
url = "#{API_URL}/repos/#{user}/#{repo}/releases/latest"
API.open_rest(url, request_method: :GET)

curl_options = {
debug: debug,
timeout: timeout,
connect_timeout: connect_timeout,
max_time: max_time,
retries: retries,
}.compact
API.open_rest(url, request_method: :GET, **curl_options)
end

def self.generate_release_notes(user, repo, tag, previous_tag: nil)
Expand Down
39 changes: 36 additions & 3 deletions Library/Homebrew/utils/github/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,33 @@ def self.credentials_error_message(response_headers, needed_scopes)
EOS
end

sig {
params(
url: T.any(String, URI::HTTPS),
data: T.nilable(T::Hash[String, T.untyped]),
data_binary_path: T.nilable(String),
request_method: T.nilable(Symbol),
scopes: Array,
parse_json: T::Boolean,
debug: T.nilable(T::Boolean),
timeout: T.nilable(T.any(Integer, Float)),
connect_timeout: T.nilable(T.any(Integer, Float)),
max_time: T.nilable(T.any(Integer, Float)),
retries: T.nilable(Integer),
).returns(T.untyped)
}
def self.open_rest(
url, data: nil, data_binary_path: nil, request_method: nil, scopes: [].freeze, parse_json: true
url,
data: nil,
data_binary_path: nil,
request_method: nil,
scopes: [].freeze,
parse_json: true,
debug: nil,
timeout: nil,
connect_timeout: nil,
max_time: nil,
retries: nil
)
# This is a no-op if the user is opting out of using the GitHub API.
return block_given? ? yield({}) : {} if Homebrew::EnvConfig.no_github_api?
Expand Down Expand Up @@ -222,7 +247,15 @@ def self.open_rest(

args += ["--dump-header", T.must(headers_tmpfile.path)]

output, errors, status = curl_output("--location", url.to_s, *args, secrets: [token])
curl_options = {
debug: debug,
timeout: timeout,
connect_timeout: connect_timeout,
max_time: max_time,
retries: retries,
}.compact

output, errors, status = curl_output("--location", url.to_s, *args, secrets: [token], **curl_options)
output, _, http_code = output.rpartition("\n")
output, _, http_code = output.rpartition("\n") if http_code == "000"
headers = headers_tmpfile.read
Expand Down Expand Up @@ -262,7 +295,7 @@ def self.paginate_rest(url, additional_query_params: nil, per_page: 100)

def self.open_graphql(query, variables: nil, scopes: [].freeze, raise_errors: true)
data = { query: query, variables: variables }
result = open_rest("#{API_URL}/graphql", scopes: scopes, data: data, request_method: "POST")
result = open_rest("#{API_URL}/graphql", scopes: scopes, data: data, request_method: :POST)

if raise_errors
if result["errors"].present?
Expand Down