class GitHubPr::FileToken
Public Class Methods
new(token_file_path)
click to toggle source
@token_file_path = File.expand_path '~/.config/gh-pr-token'
# File lib/github-pr/token.rb, line 42 def initialize(token_file_path) @token_file_path = token_file_path end
Public Instance Methods
create_dir_if_needed()
click to toggle source
# File lib/github-pr/token.rb, line 78 def create_dir_if_needed token_dir = File.dirname(@token_file_path) unless File.directory?(token_dir) FileUtils.mkdir_p(token_dir) end end
get(regenerate: false)
click to toggle source
# File lib/github-pr/token.rb, line 93 def get(regenerate: false) create_dir_if_needed() if regenerate puts 'Token has been reveked' FileUtils.rm(@token_file_path) end token = token_from_file() if token return token end puts 'Token is nil regenerating' token = token_from_github() File.open(@token_file_path, 'w+') {|file| file.write token} return token end
get_username_and_pass()
click to toggle source
# File lib/github-pr/token.rb, line 85 def get_username_and_pass cli = HighLine.new github_username = cli.ask("GitHub Username: ") github_password = cli.ask("GitHub Password(Only one time): ") { |q| q.echo = "*" } return {:login => github_username, :password => github_password} end
retry_logic(count, initial_note) { |note| ... }
click to toggle source
# File lib/github-pr/token.rb, line 50 def retry_logic(count, initial_note) note = initial_note count.times do |i| begin yield(note) break rescue Exception => e if i == 8 raise 'Please remove some personal tokens from your github account, you have too many tokens' end if e.errors[0][:code] != "already_exists" || e.errors[0][:field] != "description" raise e else note = initial_note + (i + 2).to_s end end end end
token_from_file()
click to toggle source
# File lib/github-pr/token.rb, line 46 def token_from_file File.exist?(@token_file_path) ? IO.read(@token_file_path) : nil end
token_from_github()
click to toggle source
# File lib/github-pr/token.rb, line 69 def token_from_github client = Octokit::Client.new(get_username_and_pass()) # trying 9 different notes, if all is failed, # then you have to remove some of them retry_logic(9, 'CLI uses') do |note| return client.create_authorization(:scopes => ["repo", "user"], :note => note)[:token] end end