From ccb6d5e834dbfe91043b324af95872840903aec7 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Thu, 4 Feb 2021 23:35:15 +0000 Subject: [PATCH 1/3] utils/github: Handle users not having any GitHub credentials - When running `brew request-bottle`, users who don't have credentials in the macOS keychain (ie, Linux users) or `HOMEBREW_GITHUB_API_TOKEN` receive "Error: Not Found" from the GitHub API returning a 404. - This is cryptic and confusing for newcomers to Linux maintenance, and potentially confusing to other folks using `open_api` where credentials are expected yet unset. - This adds a new `MissingAuthenticationError` to handle the case where the GitHub API returns 404 and there are no creds yet API scopes are required. Before: ``` issyl0@sky:/home/linuxbrew/.linuxbrew/Homebrew$ brew request-bottle hello ==> Dispatching request to Homebrew/linuxbrew-core for hello Error: Not Found ``` After: ``` issyl0@sky:/home/linuxbrew/.linuxbrew/Homebrew$ brew request-bottle hello ==> Dispatching request to Homebrew/linuxbrew-core for hello Error: No GitHub credentials found in Keychain or environment. Create a GitHub personal access token: https://github.com/settings/tokens/new?scopes=gist,public_repo&description=Homebrew echo 'export HOMEBREW_GITHUB_API_TOKEN=your_token_here' >> ~/.profile ``` --- Library/Homebrew/utils/github.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 75fd5ae3d1..27b832daa6 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -24,6 +24,11 @@ module GitHub ALL_SCOPES_URL = Formatter.url( "https://github.com/settings/tokens/new?scopes=#{ALL_SCOPES.join(",")}&description=Homebrew", ).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. class Error < RuntimeError @@ -79,6 +84,15 @@ module GitHub 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 Keychain or environment.\n" + message << CREATE_GITHUB_PAT_MESSAGE + super message + end + end + # Error when the API returns a validation error. class ValidationFailedError < Error def initialize(github_message, errors) @@ -277,6 +291,8 @@ module GitHub when "401", "403" raise AuthenticationFailedError, message when "404" + raise MissingAuthenticationError if api_credentials_type == :none && scopes.present? + raise HTTPNotFoundError, message when "422" errors = json&.[]("errors") || [] From 9394fe2b5211e5a5f5a3d2bb5b991a3c7ccc7bc4 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Fri, 5 Feb 2021 12:34:11 +0000 Subject: [PATCH 2/3] utils/github: Use constant everywhere for "create a PAT" message - This way if we ever change this messaging, we only have to do so once. --- Library/Homebrew/utils/github.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 27b832daa6..d8d5effc24 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -49,9 +49,8 @@ module GitHub @github_message = github_message super <<~EOS GitHub API Error: #{github_message} - Try again in #{pretty_ratelimit_reset(reset)}, or create a personal access token: - #{ALL_SCOPES_URL} - #{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")} + Try again in #{pretty_ratelimit_reset(reset)}, or: + #{CREATE_GITHUB_PAT_MESSAGE} EOS end @@ -75,9 +74,7 @@ module GitHub The GitHub credentials in the macOS keychain may be invalid. Clear them with: printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase - Or create a personal access token: - #{ALL_SCOPES_URL} - #{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")} + #{CREATE_GITHUB_PAT_MESSAGE} EOS end super message.freeze @@ -182,9 +179,7 @@ module GitHub Your #{what} credentials do not have sufficient scope! Scopes required: #{needed_scopes} Scopes present: #{credentials_scopes} - Create a personal access token: - #{ALL_SCOPES_URL} - #{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")} + #{CREATE_GITHUB_PAT_MESSAGE} EOS end From f71ea65ee0951b7992a0fa9a1d4a6d469276b628 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Fri, 5 Feb 2021 13:52:32 +0000 Subject: [PATCH 3/3] utils/github: Say "macOS keychain" in `MissingAuthenticationError` Co-authored-by: Mike McQuaid --- Library/Homebrew/utils/github.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index d8d5effc24..fa3439f967 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -84,7 +84,7 @@ module GitHub # 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 Keychain or environment.\n" + message = +"No GitHub credentials found in macOS Keychain or environment.\n" message << CREATE_GITHUB_PAT_MESSAGE super message end