module InteractiveSetup::GitHubAccessToken

Public Class Methods

ask_login() click to toggle source
# File lib/git-ready/interactive_setup/github_access_token.rb, line 31
def self.ask_login
  login = ask('Enter your GitHub login:')
  login.empty? ? ask_login : login
end
ask_password() click to toggle source
# File lib/git-ready/interactive_setup/github_access_token.rb, line 37
def self.ask_password
  password = ask('Enter your GitHub password:') { |input| input.echo = '*' }
  password.empty? ? ask_password : password
end
delete_existing_authorization(session, headers) click to toggle source
# File lib/git-ready/interactive_setup/github_access_token.rb, line 61
def self.delete_existing_authorization(session, headers)
  existing = old_auth_tokens('git-ready', session, headers).first[:id]
  session.delete_authorization existing, headers: headers
end
generate(login, password, headers = {}, first_attempt: true) click to toggle source
# File lib/git-ready/interactive_setup/github_access_token.rb, line 43
def self.generate(login, password, headers = {}, first_attempt: true)
  github = Octokit::Client.new login: login, password: password
  github.create_authorization(note: 'git-ready',
                              scopes: ['repo'],
                              headers: headers)
rescue Octokit::Unauthorized
  Announce.failure 'Invalid Credentials'
rescue Octokit::UnprocessableEntity
  if first_attempt
    Announce.warning 'Found an old token. Replacing it.'
    delete_existing_authorization github, headers
    generate login, password, headers, first_attempt: false
  else
    Announce.failure "It looked like you had already issued a token, but deleting it didn't help. You're on your own at this point. You should should use the link at the start to generate a token manually."
  end
end
guided_generation() click to toggle source
# File lib/git-ready/interactive_setup/github_access_token.rb, line 20
def self.guided_generation
  login = ask_login
  password = ask_password
  generate login, password
rescue Octokit::OneTimePasswordRequired
  Announce.info 'Your account has 2-Factor Authentication enabled. Awesome!'
  headers = { 'X-GitHub-OTP' => ask('Enter a valid 2-Factor Auth Token') }
  generate login, password, headers
end
old_auth_tokens(note, session, headers) click to toggle source
# File lib/git-ready/interactive_setup/github_access_token.rb, line 67
def self.old_auth_tokens(note, session, headers)
  session.authorizations(headers: headers).select do |auth|
    auth[:note] == note
  end
end
setup() click to toggle source
# File lib/git-ready/interactive_setup/github_access_token.rb, line 11
def self.setup
  Announce.info 'If you leave this blank, git-ready will do most of the work for you. As a fallback, you can generate your own at https://github.com/settings/tokens/new'
  token = ask 'Enter your GitHub Personal Access Token:', String
  generated = guided_generation if token.empty?
  token = generated[:token] if generated
  token_works?(token) ? token : setup
end
token_works?(token) click to toggle source
# File lib/git-ready/interactive_setup/github_access_token.rb, line 74
def self.token_works?(token)
  github = Octokit::Client.new access_token: token
  true if github.repos
rescue Octokit::Unauthorized
  Announce.failure 'Invalid Credentials'
  false
end