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

cmd/gist-logs: On 404, the PAT probably needs more permissions #14523

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion Library/Homebrew/cmd/gist-logs.rb
Expand Up @@ -63,7 +63,15 @@ def gistify_logs(f, args:)
else
"#{f.name} (#{f.full_name}) on #{OS_VERSION} - Homebrew build logs"
end
url = GitHub.create_gist(files, descr, private: args.private?)

begin
url = GitHub.create_gist(files, descr, private: args.private?)
rescue GitHub::API::HTTPNotFoundError
odie <<~EOS
Your GitHub API token likely doesn't have the `gist` scope.
#{GitHub.pat_blurb(GitHub::CREATE_GIST_SCOPES)}
EOS
end

url = GitHub.create_issue(f.tap, "#{f.name} failed to build on #{MacOS.full_version}", url) if args.new_issue?

Expand Down
27 changes: 15 additions & 12 deletions Library/Homebrew/utils/github/api.rb
Expand Up @@ -5,7 +5,18 @@
require "utils/shell"
require "utils/formatter"

# A module that interfaces with GitHub, code like PAT scopes, credential handling and API errors.
module GitHub
def self.pat_blurb(scopes = ALL_SCOPES)
<<~EOS
Create a GitHub personal access token:
#{Formatter.url(
"https://github.com/settings/tokens/new?scopes=#{scopes.join(",")}&description=Homebrew",
)}
#{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")}
EOS
end

API_URL = "https://api.github.com"
API_MAX_PAGES = 50
API_MAX_ITEMS = 5000
Expand All @@ -14,14 +25,6 @@ module GitHub
CREATE_ISSUE_FORK_OR_PR_SCOPES = ["repo"].freeze
CREATE_WORKFLOW_SCOPES = ["workflow"].freeze
ALL_SCOPES = (CREATE_GIST_SCOPES + CREATE_ISSUE_FORK_OR_PR_SCOPES + CREATE_WORKFLOW_SCOPES).freeze
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
GITHUB_PERSONAL_ACCESS_TOKEN_REGEX = /^(?:[a-f0-9]{40}|gh[po]_\w{36,251})$/.freeze

# Helper functions to access the GitHub API.
Expand Down Expand Up @@ -49,7 +52,7 @@ def initialize(github_message)
class RateLimitExceededError < Error
def initialize(reset, github_message)
@github_message = github_message
new_pat_message = ", or:\n#{CREATE_GITHUB_PAT_MESSAGE}" if API.credentials.blank?
new_pat_message = ", or:\n#{pat_blurb}" if API.credentials.blank?
issyl0 marked this conversation as resolved.
Show resolved Hide resolved
super <<~EOS
GitHub API Error: #{github_message}
Try again in #{pretty_ratelimit_reset(reset)}#{new_pat_message}
Expand All @@ -76,7 +79,7 @@ def initialize(github_message)
The GitHub credentials in the macOS keychain may be invalid.
Clear them with:
printf "protocol=https\\nhost=github.com\\n" | git credential-osxkeychain erase
#{CREATE_GITHUB_PAT_MESSAGE}
#{pat_blurb}
issyl0 marked this conversation as resolved.
Show resolved Hide resolved
EOS
end
super message.freeze
Expand All @@ -87,7 +90,7 @@ def initialize(github_message)
class MissingAuthenticationError < Error
def initialize
message = +"No GitHub credentials found in macOS Keychain or environment.\n"
message << CREATE_GITHUB_PAT_MESSAGE
message << pat_blurb
issyl0 marked this conversation as resolved.
Show resolved Hide resolved
super message
end
end
Expand Down Expand Up @@ -177,7 +180,7 @@ def credentials_error_message(response_headers, needed_scopes)
Your #{what} credentials do not have sufficient scope!
Scopes required: #{needed_scopes}
Scopes present: #{credentials_scopes}
#{CREATE_GITHUB_PAT_MESSAGE}
#{pat_blurb}
issyl0 marked this conversation as resolved.
Show resolved Hide resolved
EOS
end

Expand Down