Module: InteractiveSetup::GitHubAccessToken

Includes:
Contracts
Defined in:
lib/git-ready/interactive_setup/github_access_token.rb

Class Method Summary collapse

Class Method Details

.ask_loginObject



31
32
33
34
# File 'lib/git-ready/interactive_setup/github_access_token.rb', line 31

def self.
   = ask('Enter your GitHub login:')
  .empty? ?  : 
end

.ask_passwordObject



37
38
39
40
# File 'lib/git-ready/interactive_setup/github_access_token.rb', line 37

def self.ask_password
  password = ask('Enter your GitHub password:') { |c| c.echo = '*' }
  password.empty? ? ask_password : password
end

.delete_existing_authorization(session, headers) ⇒ Object



61
62
63
64
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/git-ready/interactive_setup/github_access_token.rb', line 43

def self.generate(, password, headers = {}, first_attempt = true)
  github = Octokit::Client.new 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 , password, headers, 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_generationObject



20
21
22
23
24
25
26
27
28
# File 'lib/git-ready/interactive_setup/github_access_token.rb', line 20

def self.guided_generation
   = 
  password = ask_password
  generate , 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 , password, headers
end

.old_auth_tokens(note, session, headers) ⇒ Object



67
68
69
70
71
# 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

.setupObject



11
12
13
14
15
16
17
# 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) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# 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