Module: GitHub::API Private
- Extended by:
- T::Sig
- Defined in:
- brew/Library/Homebrew/utils/github/api.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Helper functions to access the GitHub API.
Defined Under Namespace
Classes: AuthenticationFailedError, Error, HTTPNotFoundError, MissingAuthenticationError, RateLimitExceededError, ValidationFailedError
Constant Summary collapse
- ERRORS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
[ AuthenticationFailedError, HTTPNotFoundError, RateLimitExceededError, Error, JSON::ParserError, ].freeze
Class Method Summary collapse
-
.credentials ⇒ Object
private
-
.credentials_error_message(response_headers, needed_scopes) ⇒ Object
private
Given an API response from GitHub, warn the user if their credentials have insufficient permissions.
-
.credentials_type ⇒ Symbol
private
-
.keychain_username_password ⇒ String?
private
Gets the password field from
git-credential-osxkeychain
for github.com, but only if that password looks like a GitHub Personal Access Token. -
.open_graphql(query, scopes: [].freeze) ⇒ Object
private
-
.open_rest(url, data: nil, data_binary_path: nil, request_method: nil, scopes: [].freeze, parse_json: true) ⇒ Object
private
-
.raise_error(output, errors, http_code, headers, scopes) ⇒ Object
private
Class Method Details
.credentials ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
141 142 143 144 145 |
# File 'brew/Library/Homebrew/utils/github/api.rb', line 141 def credentials @credentials ||= begin Homebrew::EnvConfig.github_api_token || keychain_username_password end end |
.credentials_error_message(response_headers, needed_scopes) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Given an API response from GitHub, warn the user if their credentials have insufficient permissions.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'brew/Library/Homebrew/utils/github/api.rb', line 160 def (response_headers, needed_scopes) return if response_headers.empty? scopes = response_headers["x-accepted-oauth-scopes"].to_s.split(", ") needed_scopes = Set.new(scopes || needed_scopes) credentials_scopes = response_headers["x-oauth-scopes"] return if needed_scopes.subset?(Set.new(credentials_scopes.to_s.split(", "))) needed_scopes = needed_scopes.to_a.join(", ").presence || "none" credentials_scopes = "none" if credentials_scopes.blank? what = case credentials_type when :keychain_username_password "macOS keychain GitHub" when :env_token "HOMEBREW_GITHUB_API_TOKEN" end @credentials_error_message ||= onoe <<~EOS Your #{what} credentials do not have sufficient scope! Scopes required: #{needed_scopes} Scopes present: #{credentials_scopes} #{CREATE_GITHUB_PAT_MESSAGE} EOS end |
.credentials_type ⇒ Symbol
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
148 149 150 151 152 153 154 155 156 |
# File 'brew/Library/Homebrew/utils/github/api.rb', line 148 def credentials_type if Homebrew::EnvConfig.github_api_token :env_token elsif keychain_username_password :keychain_username_password else :none end end |
.keychain_username_password ⇒ String?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Gets the password field from git-credential-osxkeychain
for github.com,
but only if that password looks like a GitHub Personal Access Token.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'brew/Library/Homebrew/utils/github/api.rb', line 117 def keychain_username_password github_credentials = Utils.popen(["git", "credential-osxkeychain", "get"], "w+") do |pipe| pipe.write "protocol=https\nhost=github.com\n" pipe.close_write pipe.read end github_username = github_credentials[/username=(.+)/, 1] github_password = github_credentials[/password=(.+)/, 1] return unless github_username # Don't use passwords from the keychain unless they look like # GitHub Personal Access Tokens: # https://github.com/Homebrew/brew/issues/6862#issuecomment-572610344 return unless /^[a-f0-9]{40}$/i.match?(github_password) github_password rescue Errno::EPIPE # The above invocation via `Utils.popen` can fail, causing the pipe to be # prematurely closed (before we can write to it) and thus resulting in a # broken pipe error. The root cause is usually a missing or malfunctioning # `git-credential-osxkeychain` helper. nil end |
.open_graphql(query, scopes: [].freeze) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'brew/Library/Homebrew/utils/github/api.rb', line 252 def open_graphql(query, scopes: [].freeze) data = { query: query } result = open_rest("https://api.github.com/graphql", scopes: scopes, data: data, request_method: "POST") if result["errors"].present? raise Error, result["errors"].map { |e| "#{e["type"]}: #{e["message"]}" }.join("\n") end result["data"] end |
.open_rest(url, data: nil, data_binary_path: nil, request_method: nil, scopes: [].freeze, parse_json: true) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'brew/Library/Homebrew/utils/github/api.rb', line 186 def open_rest(url, data: nil, data_binary_path: nil, request_method: nil, scopes: [].freeze, parse_json: true) # 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? args = ["--header", "Accept: application/vnd.github.v3+json", "--write-out", "\n%\{http_code}"] args += ["--header", "Accept: application/vnd.github.antiope-preview+json"] token = credentials args += ["--header", "Authorization: token #{token}"] unless credentials_type == :none data_tmpfile = nil if data begin data = JSON.generate data data_tmpfile = Tempfile.new("github_api_post", HOMEBREW_TEMP) rescue JSON::ParserError => e raise Error, "Failed to parse JSON request:\n#{e.}\n#{data}", e.backtrace end end if data_binary_path.present? args += ["--data-binary", "@#{data_binary_path}"] args += ["--header", "Content-Type: application/gzip"] end headers_tmpfile = Tempfile.new("github_api_headers", HOMEBREW_TEMP) begin if data data_tmpfile.write data data_tmpfile.close args += ["--data", "@#{data_tmpfile.path}"] args += ["--request", request_method.to_s] if request_method end args += ["--dump-header", headers_tmpfile.path] output, errors, status = curl_output("--location", url.to_s, *args, secrets: [token]) output, _, http_code = output.rpartition("\n") output, _, http_code = output.rpartition("\n") if http_code == "000" headers = headers_tmpfile.read ensure if data_tmpfile data_tmpfile.close data_tmpfile.unlink end headers_tmpfile.close headers_tmpfile.unlink end begin raise_error(output, errors, http_code, headers, scopes) if !http_code.start_with?("2") || !status.success? return if http_code == "204" # No Content output = JSON.parse output if parse_json if block_given? yield output else output end rescue JSON::ParserError => e raise Error, "Failed to parse JSON response\n#{e.}", e.backtrace end end |
.raise_error(output, errors, http_code, headers, scopes) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'brew/Library/Homebrew/utils/github/api.rb', line 265 def raise_error(output, errors, http_code, headers, scopes) json = begin JSON.parse(output) rescue nil end = json&.[]("message") || "curl failed! #{errors}" = {} headers.lines.each do |l| key, _, value = l.delete(":").partition(" ") key = key.downcase.strip next if key.empty? [key] = value.strip end if .fetch("x-ratelimit-remaining", 1).to_i <= 0 reset = .fetch("x-ratelimit-reset").to_i raise RateLimitExceededError.new(reset, ) end (, scopes) case http_code when "401", "403" raise AuthenticationFailedError, when "404" raise MissingAuthenticationError if credentials_type == :none && scopes.present? raise HTTPNotFoundError, when "422" errors = json&.[]("errors") || [] raise ValidationFailedError.new(, errors) else raise Error, end end |