Class: Travis::Tools::Github
- Inherits:
-
Object
- Object
- Travis::Tools::Github
- Defined in:
- lib/travis/tools/github.rb
Constant Summary collapse
- TOKEN_SIZE =
40
- GITHUB_API =
'api.github.com'
- GITHUB_HOST =
'github.com'
Instance Attribute Summary collapse
-
#after_tokens ⇒ Object
Returns the value of attribute after_tokens.
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#ask_login ⇒ Object
Returns the value of attribute ask_login.
-
#ask_otp ⇒ Object
Returns the value of attribute ask_otp.
-
#ask_password ⇒ Object
Returns the value of attribute ask_password.
-
#auto_password ⇒ Object
Returns the value of attribute auto_password.
-
#auto_token ⇒ Object
Returns the value of attribute auto_token.
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#check_token ⇒ Object
Returns the value of attribute check_token.
-
#composer_path ⇒ Object
Returns the value of attribute composer_path.
-
#debug ⇒ Object
writeonly
Sets the attribute debug.
-
#drop_token ⇒ Object
Returns the value of attribute drop_token.
-
#explode ⇒ Object
Returns the value of attribute explode.
-
#git_config_keys ⇒ Object
Returns the value of attribute git_config_keys.
-
#github_login ⇒ Object
Returns the value of attribute github_login.
-
#github_token ⇒ Object
Returns the value of attribute github_token.
-
#hub_path ⇒ Object
Returns the value of attribute hub_path.
-
#login_header ⇒ Object
Returns the value of attribute login_header.
-
#manual_login ⇒ Object
Returns the value of attribute manual_login.
-
#netrc_path ⇒ Object
Returns the value of attribute netrc_path.
-
#no_token ⇒ Object
Returns the value of attribute no_token.
-
#note ⇒ Object
Returns the value of attribute note.
-
#oauth_paths ⇒ Object
Returns the value of attribute oauth_paths.
-
#scopes ⇒ Object
Returns the value of attribute scopes.
Instance Method Summary collapse
- #acceptable?(token) ⇒ Boolean
- #api_host ⇒ Object
- #ask_credentials ⇒ Object
- #basic_auth(user, password, die = true, otp = nil, &block) ⇒ Object
- #composer_token ⇒ Object
- #create_token(gh) ⇒ Object
- #each_token ⇒ Object
- #git_tokens ⇒ Object
- #github_for_mac_token(&block) ⇒ Object
- #host ⇒ Object
- #hub ⇒ Object
- #hub_logins ⇒ Object
- #hub_tokens ⇒ Object
-
#initialize(options = nil) {|_self| ... } ⇒ Github
constructor
A new instance of Github.
- #issuepost_token(&block) ⇒ Object
- #keychain_login ⇒ Object
- #login(user, password, die = true, otp = nil) ⇒ Object
- #netrc ⇒ Object
- #netrc_logins ⇒ Object
- #netrc_tokens ⇒ Object
- #oauth_file_tokens(&block) ⇒ Object
- #possible_logins(&block) ⇒ Object
- #possible_tokens(&block) ⇒ Object
- #with_basic_auth(&block) ⇒ Object
- #with_otp(gh, user, otp, &block) ⇒ Object
- #with_session(&block) ⇒ Object
- #with_token ⇒ Object
Constructor Details
#initialize(options = nil) {|_self| ... } ⇒ Github
Returns a new instance of Github.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/travis/tools/github.rb', line 17 def initialize( = nil) @check_token = true @manual_login = true @ask_login = proc { raise "ask_login callback not set" } @after_tokens = proc { } @ask_password = proc { |_| raise "ask_password callback not set" } @ask_otp = proc { |_| raise "ask_otp callback not set" } @debug = proc { |_| } @netrc_path = '~/.netrc' @hub_path = ENV['HUB_CONFIG'] || '~/.config/hub' @oauth_paths = ['~/.github-oauth-token'] @composer_path = "~/.composer/config.json" @note = 'temporary token' @git_config_keys = %w[github.token github.oauth-token] @scopes = ['user', 'user:email', 'repo'] # overridden by value from /config .each_pair { |k,v| send("#{k}=", v) if respond_to? "#{k}=" } if yield self if block_given? end |
Instance Attribute Details
#after_tokens ⇒ Object
Returns the value of attribute after_tokens.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def after_tokens @after_tokens end |
#api_url ⇒ Object
Returns the value of attribute api_url.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def api_url @api_url end |
#ask_login ⇒ Object
Returns the value of attribute ask_login.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def ask_login @ask_login end |
#ask_otp ⇒ Object
Returns the value of attribute ask_otp.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def ask_otp @ask_otp end |
#ask_password ⇒ Object
Returns the value of attribute ask_password.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def ask_password @ask_password end |
#auto_password ⇒ Object
Returns the value of attribute auto_password.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def auto_password @auto_password end |
#auto_token ⇒ Object
Returns the value of attribute auto_token.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def auto_token @auto_token end |
#callback ⇒ Object
Returns the value of attribute callback.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def callback @callback end |
#check_token ⇒ Object
Returns the value of attribute check_token.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def check_token @check_token end |
#composer_path ⇒ Object
Returns the value of attribute composer_path.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def composer_path @composer_path end |
#debug=(value) ⇒ Object
Sets the attribute debug
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def debug=(value) @debug = value end |
#drop_token ⇒ Object
Returns the value of attribute drop_token.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def drop_token @drop_token end |
#explode ⇒ Object
Returns the value of attribute explode.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def explode @explode end |
#git_config_keys ⇒ Object
Returns the value of attribute git_config_keys.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def git_config_keys @git_config_keys end |
#github_login ⇒ Object
Returns the value of attribute github_login.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def github_login @github_login end |
#github_token ⇒ Object
Returns the value of attribute github_token.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def github_token @github_token end |
#hub_path ⇒ Object
Returns the value of attribute hub_path.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def hub_path @hub_path end |
#login_header ⇒ Object
Returns the value of attribute login_header.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def login_header @login_header end |
#manual_login ⇒ Object
Returns the value of attribute manual_login.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def manual_login @manual_login end |
#netrc_path ⇒ Object
Returns the value of attribute netrc_path.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def netrc_path @netrc_path end |
#no_token ⇒ Object
Returns the value of attribute no_token.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def no_token @no_token end |
#note ⇒ Object
Returns the value of attribute note.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def note @note end |
#oauth_paths ⇒ Object
Returns the value of attribute oauth_paths.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def oauth_paths @oauth_paths end |
#scopes ⇒ Object
Returns the value of attribute scopes.
13 14 15 |
# File 'lib/travis/tools/github.rb', line 13 def scopes @scopes end |
Instance Method Details
#acceptable?(token) ⇒ Boolean
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/travis/tools/github.rb', line 243 def acceptable?(token) return true unless check_token gh = GH.with(:token => token) user = gh['user'] if github_login and github_login != user['login'] debug "token is not acceptable: identifies %p instead of %p" % [user['login'], github_login] false else true end rescue GH::Error => error debug "token is not acceptable: #{gh_error(error)}" false end |
#api_host ⇒ Object
203 204 205 206 |
# File 'lib/travis/tools/github.rb', line 203 def api_host return GITHUB_API unless api_url api_url[%r{^(?:https?://)?([^/]+)}, 1] end |
#ask_credentials ⇒ Object
87 88 89 90 91 92 |
# File 'lib/travis/tools/github.rb', line 87 def ask_credentials login_header.call if login_header user = github_login || ask_login.call password = ask_password.arity == 0 ? ask_password.call : ask_password.call(user) [user, password] end |
#basic_auth(user, password, die = true, otp = nil, &block) ⇒ Object
208 209 210 211 212 213 |
# File 'lib/travis/tools/github.rb', line 208 def basic_auth(user, password, die = true, otp = nil, &block) gh = GH.with(:username => user, :password => password) with_otp(gh, user, otp, &block) rescue GH::Error => error raise gh_error(error) if die end |
#composer_token ⇒ Object
121 122 123 124 125 126 |
# File 'lib/travis/tools/github.rb', line 121 def composer_token file(composer_path) do |content| token = JSON.parse(content)['config'].fetch('github-oauth', {})[host] yield token if token end end |
#create_token(gh) ⇒ Object
224 225 226 227 228 229 230 231 232 |
# File 'lib/travis/tools/github.rb', line 224 def create_token(gh) gh.post('/authorizations', :scopes => scopes, :note => note) rescue GH::Error => error # token might already exist due to bug in earlier CLI version, we'll have to delete it first raise error unless error.info[:response_status] == 422 and error.info[:response_body].to_s =~ /already_exists/ raise error unless reply = gh['/authorizations'].detect { |a| a['note'] == note } gh.delete(reply['_links']['self']['href']) retry end |
#each_token ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/travis/tools/github.rb', line 48 def each_token require 'gh' unless defined? GH possible_tokens { |t| yield(t) if acceptable?(t) } ensure callback, self.callback = self.callback, nil callback.call if callback end |
#git_tokens ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/travis/tools/github.rb', line 111 def git_tokens return unless System.has? 'git' git_config_keys.each do |key| `git config --get-all #{key}`.each_line do |line| token = line.strip yield token unless token.empty? end end end |
#github_for_mac_token(&block) ⇒ Object
193 194 195 196 197 |
# File 'lib/travis/tools/github.rb', line 193 def github_for_mac_token(&block) command = '-s "github.com/mac"' command << " -a #{github_login}" if github_login security(:internet, :w, command, "GitHub for Mac token", &block) if host == 'github.com' end |
#host ⇒ Object
199 200 201 |
# File 'lib/travis/tools/github.rb', line 199 def host api_host == GITHUB_API ? GITHUB_HOST : api_host end |
#hub ⇒ Object
183 184 185 186 187 |
# File 'lib/travis/tools/github.rb', line 183 def hub file(hub_path, {}) do |contents| YAML.load(contents) end end |
#hub_logins ⇒ Object
152 153 154 155 156 157 |
# File 'lib/travis/tools/github.rb', line 152 def hub_logins hub.fetch(host, []).each do |entry| next if github_login and github_login != entry["user"] yield entry["user"], entry["password"] if entry["user"] and entry["password"] end end |
#hub_tokens ⇒ Object
128 129 130 131 132 133 |
# File 'lib/travis/tools/github.rb', line 128 def hub_tokens hub.fetch(host, []).each do |entry| next if github_login and github_login != entry["user"] yield entry["oauth_token"] if entry["oauth_token"] end end |
#issuepost_token(&block) ⇒ Object
189 190 191 |
# File 'lib/travis/tools/github.rb', line 189 def issuepost_token(&block) security(:generic, :w, "-l issuepost.github.access_token", "issuepost token", &block) if host == 'github.com' end |
#keychain_login ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/travis/tools/github.rb', line 159 def keychain_login if github_login security(:internet, :w, "-s #{host} -a #{github_login}", "#{host} password for #{github_login}") do |password| yield github_login, password if password and not password.empty? end else security(:internet, :g, "-s #{host}", "#{host} login and password") do |data| username = data[/^\s+"acct"<blob>="(.*)"$/, 1].to_s password = data[/^password: "(.*)"$/, 1].to_s yield username, password unless username.empty? or password.empty? end end end |
#login(user, password, die = true, otp = nil) ⇒ Object
215 216 217 218 219 220 221 222 |
# File 'lib/travis/tools/github.rb', line 215 def login(user, password, die = true, otp = nil) basic_auth(user, password, die, otp) do |gh, new_otp| reply = create_token(gh) auth_href = reply['_links']['self']['href'] self.callback = proc { with_otp(gh, user, new_otp) { |g| g.delete(auth_href) } } if drop_token reply['token'] end end |
#netrc ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/travis/tools/github.rb', line 173 def netrc file(netrc_path, []) do |contents| contents.scan(/^\s*(\S+)\s+(\S+)\s*$/).inject([]) do |mapping, (key, value)| mapping << {} if key == "machine" mapping.last[key] = value if mapping.last mapping end end end |
#netrc_logins ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/travis/tools/github.rb', line 144 def netrc_logins netrc.each do |entry| next unless entry["machine"] == api_host or entry["machine"] == host next if github_login and github_login != entry["login"] yield entry["login"], entry["password"] if entry["login"] and entry["password"] end end |
#netrc_tokens ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/travis/tools/github.rb', line 100 def netrc_tokens netrc.each do |entry| next unless entry["machine"] == api_host or entry["machine"] == host entry.values_at("token", "login", "password").each do |entry| next if entry.to_s.size != TOKEN_SIZE debug "found oauth token in netrc" yield entry end end end |
#oauth_file_tokens(&block) ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/travis/tools/github.rb', line 135 def oauth_file_tokens(&block) oauth_paths.each do |path| file(path) do |content| token = content.strip yield token unless token.empty? end end end |
#possible_logins(&block) ⇒ Object
94 95 96 97 98 |
# File 'lib/travis/tools/github.rb', line 94 def possible_logins(&block) netrc_logins(&block) hub_logins(&block) keychain_login(&block) end |
#possible_tokens(&block) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/travis/tools/github.rb', line 60 def possible_tokens(&block) return block[github_token] if github_token if auto_token netrc_tokens(&block) git_tokens(&block) hub_tokens(&block) oauth_file_tokens(&block) github_for_mac_token(&block) issuepost_token(&block) composer_token(&block) end if auto_password possible_logins do |user, password| yield login(user, password, false) end end if manual_login user, password = ask_credentials yield login(user, password, true) end after_tokens.call end |
#with_basic_auth(&block) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/travis/tools/github.rb', line 40 def with_basic_auth(&block) user, password = ask_credentials basic_auth(user, password, true) do |gh, _| gh['user'] # so otp kicks in yield gh end end |
#with_otp(gh, user, otp, &block) ⇒ Object
234 235 236 237 238 239 240 241 |
# File 'lib/travis/tools/github.rb', line 234 def with_otp(gh, user, otp, &block) gh = GH.with(gh..merge(:headers => { "X-GitHub-OTP" => otp })) if otp block.call(gh, otp) rescue GH::Error => error raise error unless error.info[:response_status] == 401 and error.info[:response_headers]['x-github-otp'].to_s =~ /required/ otp = ask_otp.arity == 0 ? ask_otp.call : ask_otp.call(user) retry end |
#with_session(&block) ⇒ Object
56 57 58 |
# File 'lib/travis/tools/github.rb', line 56 def with_session(&block) with_token { |t| GH.with(:token => t) { yield(t) } } end |
#with_token ⇒ Object
36 37 38 |
# File 'lib/travis/tools/github.rb', line 36 def with_token each_token { |t| break yield(t) } end |