Class: GitHubPr::FileToken
Instance Method Summary collapse
- #create_dir_if_needed ⇒ Object
- #get(regenerate: false) ⇒ Object
- #get_username_and_pass ⇒ Object
-
#initialize(token_file_path) ⇒ FileToken
constructor
A new instance of FileToken.
- #retry_logic(count, initial_note) ⇒ Object
- #token_from_file ⇒ Object
- #token_from_github ⇒ Object
Methods inherited from Token
Constructor Details
#initialize(token_file_path) ⇒ FileToken
Returns a new instance of FileToken.
41 42 43 |
# File 'lib/github-pr/token.rb', line 41 def initialize(token_file_path) @token_file_path = token_file_path end |
Instance Method Details
#create_dir_if_needed ⇒ Object
77 78 79 80 81 82 |
# File 'lib/github-pr/token.rb', line 77 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) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/github-pr/token.rb', line 92 def get(regenerate: false) create_dir_if_needed() if regenerate FileUtils.rm(@token_file_path) end token = token_from_file() if token return token end token = token_from_github() File.open(token_file_path, 'w+') {|file| file.write token} return token end |
#get_username_and_pass ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/github-pr/token.rb', line 84 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) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/github-pr/token.rb', line 49 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 ⇒ Object
45 46 47 |
# File 'lib/github-pr/token.rb', line 45 def token_from_file File.exists? @token_file_path ? IO.read(@token_file_path) : nil end |
#token_from_github ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/github-pr/token.rb', line 68 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.(:scopes => ["repo", "user"], :note => note)[:token] end end |