Merge pull request #10535 from issyl0/utils-github-error-if-no-creds-are-detected-and-scopes-are-required
utils/github: Provide a better error message when users don't have any GitHub credentials set
This commit is contained in:
commit
984eb3f878
@ -24,6 +24,11 @@ module GitHub
|
|||||||
ALL_SCOPES_URL = Formatter.url(
|
ALL_SCOPES_URL = Formatter.url(
|
||||||
"https://github.com/settings/tokens/new?scopes=#{ALL_SCOPES.join(",")}&description=Homebrew",
|
"https://github.com/settings/tokens/new?scopes=#{ALL_SCOPES.join(",")}&description=Homebrew",
|
||||||
).freeze
|
).freeze
|
||||||
|
CREATE_GITHUB_PAT_MESSAGE = <<~EOS
|
||||||
|
Create a GitHub personal access token:
|
||||||
|
#{ALL_SCOPES_URL}
|
||||||
|
#{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")}
|
||||||
|
EOS
|
||||||
|
|
||||||
# Generic API error.
|
# Generic API error.
|
||||||
class Error < RuntimeError
|
class Error < RuntimeError
|
||||||
@ -44,9 +49,8 @@ module GitHub
|
|||||||
@github_message = github_message
|
@github_message = github_message
|
||||||
super <<~EOS
|
super <<~EOS
|
||||||
GitHub API Error: #{github_message}
|
GitHub API Error: #{github_message}
|
||||||
Try again in #{pretty_ratelimit_reset(reset)}, or create a personal access token:
|
Try again in #{pretty_ratelimit_reset(reset)}, or:
|
||||||
#{ALL_SCOPES_URL}
|
#{CREATE_GITHUB_PAT_MESSAGE}
|
||||||
#{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")}
|
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -70,15 +74,22 @@ module GitHub
|
|||||||
The GitHub credentials in the macOS keychain may be invalid.
|
The GitHub credentials in the macOS keychain may be invalid.
|
||||||
Clear them with:
|
Clear them with:
|
||||||
printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase
|
printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase
|
||||||
Or create a personal access token:
|
#{CREATE_GITHUB_PAT_MESSAGE}
|
||||||
#{ALL_SCOPES_URL}
|
|
||||||
#{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")}
|
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
super message.freeze
|
super message.freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Error when the user has no GitHub API credentials set at all (macOS keychain or envvar).
|
||||||
|
class MissingAuthenticationError < Error
|
||||||
|
def initialize
|
||||||
|
message = +"No GitHub credentials found in macOS Keychain or environment.\n"
|
||||||
|
message << CREATE_GITHUB_PAT_MESSAGE
|
||||||
|
super message
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Error when the API returns a validation error.
|
# Error when the API returns a validation error.
|
||||||
class ValidationFailedError < Error
|
class ValidationFailedError < Error
|
||||||
def initialize(github_message, errors)
|
def initialize(github_message, errors)
|
||||||
@ -168,9 +179,7 @@ module GitHub
|
|||||||
Your #{what} credentials do not have sufficient scope!
|
Your #{what} credentials do not have sufficient scope!
|
||||||
Scopes required: #{needed_scopes}
|
Scopes required: #{needed_scopes}
|
||||||
Scopes present: #{credentials_scopes}
|
Scopes present: #{credentials_scopes}
|
||||||
Create a personal access token:
|
#{CREATE_GITHUB_PAT_MESSAGE}
|
||||||
#{ALL_SCOPES_URL}
|
|
||||||
#{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")}
|
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -277,6 +286,8 @@ module GitHub
|
|||||||
when "401", "403"
|
when "401", "403"
|
||||||
raise AuthenticationFailedError, message
|
raise AuthenticationFailedError, message
|
||||||
when "404"
|
when "404"
|
||||||
|
raise MissingAuthenticationError if api_credentials_type == :none && scopes.present?
|
||||||
|
|
||||||
raise HTTPNotFoundError, message
|
raise HTTPNotFoundError, message
|
||||||
when "422"
|
when "422"
|
||||||
errors = json&.[]("errors") || []
|
errors = json&.[]("errors") || []
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user