class RooOnRails::Checks::GitHub::Token

Output context:

Constants

TOKEN_FILE

Public Instance Methods

call() click to toggle source
# File lib/roo_on_rails/checks/github/token.rb, line 18
def call
  token = File.exist?(TOKEN_FILE) && File.read(TOKEN_FILE)
  fail! 'no token found' unless token && !token.empty?

  oauth_client = Octokit::Client.new(access_token: token)
  oauth_client.user # idempotent call to check access

  context.github!.api_client = oauth_client
  pass "connected to GitHub's API"
rescue Octokit::Error => e
  fail! "#{e.class}: #{e.message}"
end
fix() click to toggle source
# File lib/roo_on_rails/checks/github/token.rb, line 31
def fix
  token = create_access_token
  FileUtils.mkpath(File.dirname(TOKEN_FILE))
  File.write(TOKEN_FILE, token)
rescue Octokit::Error => e
  final_fail! "#{e.class}: #{e.message}"
end
intro() click to toggle source
# File lib/roo_on_rails/checks/github/token.rb, line 14
def intro
  'Obtaining GitHub auth token...'
end

Private Instance Methods

basic_client() click to toggle source
# File lib/roo_on_rails/checks/github/token.rb, line 60
def basic_client
  @basic_client ||= begin
    username = ask 'Enter your GitHub username:'
    password = ask 'Enter your GitHub password (typing will be hidden):', echo: false
    say # line break after non-echoed password
    Octokit::Client.new(login: username, password: password)
  end
end
create_access_token() click to toggle source
# File lib/roo_on_rails/checks/github/token.rb, line 41
def create_access_token
  delete_existing_access_token
  result = basic_client.create_authorization(
    scopes: %w(repo),
    note: token_name,
    note_url: 'https://github.com/deliveroo/roo_on_rails',
    headers: two_factor_headers
  )
  result[:token]
end
delete_existing_access_token() click to toggle source
# File lib/roo_on_rails/checks/github/token.rb, line 52
def delete_existing_access_token
  authorizations = basic_client.authorizations(headers: two_factor_headers)
  authorization = authorizations.find { |a| a[:note] == token_name }
  return unless authorization

  basic_client.delete_authorization(authorization[:id], headers: two_factor_headers)
end
token_name() click to toggle source
# File lib/roo_on_rails/checks/github/token.rb, line 79
def token_name
  "Roo on Rails @ #{Socket.gethostname}"
end
two_factor_headers() click to toggle source
# File lib/roo_on_rails/checks/github/token.rb, line 69
def two_factor_headers
  @two_factor_headers ||= begin
    basic_client.user # idempotent call to check access
    {}
  rescue Octokit::OneTimePasswordRequired
    otp = ask 'Enter your GitHub 2FA code:'
    { 'X-GitHub-OTP' => otp }
  end
end