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.
-
#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_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.
-
#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
- #composer_token ⇒ Object
- #each_token ⇒ Object
- #git_tokens ⇒ Object
- #github_for_mac_token(&block) ⇒ Object
- #host ⇒ Object
- #hub ⇒ Object
- #hub_tokens ⇒ Object
-
#initialize(options = nil) {|_self| ... } ⇒ Github
constructor
A new instance of Github.
- #issuepost_token(&block) ⇒ Object
- #oauth_file_tokens ⇒ Object
- #possible_tokens(&block) ⇒ Object
- #with_session ⇒ Object
- #with_token ⇒ Object
Constructor Details
#initialize(options = nil) {|_self| ... } ⇒ Github
Returns a new instance of Github.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/travis/tools/github.rb', line 20 def initialize( = nil) @check_token = true @ask_login = proc { raise 'ask_login callback not set' } @after_tokens = proc {} @debug = proc { |_| } @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}=" } yield self if block_given? end |
Instance Attribute Details
#after_tokens ⇒ Object
Returns the value of attribute after_tokens.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def after_tokens @after_tokens end |
#api_url ⇒ Object
Returns the value of attribute api_url.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def api_url @api_url end |
#auto_token ⇒ Object
Returns the value of attribute auto_token.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def auto_token @auto_token end |
#callback ⇒ Object
Returns the value of attribute callback.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def callback @callback end |
#check_token ⇒ Object
Returns the value of attribute check_token.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def check_token @check_token end |
#composer_path ⇒ Object
Returns the value of attribute composer_path.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def composer_path @composer_path end |
#debug=(value) ⇒ Object
Sets the attribute debug
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def debug=(value) @debug = value end |
#drop_token ⇒ Object
Returns the value of attribute drop_token.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def drop_token @drop_token end |
#explode ⇒ Object
Returns the value of attribute explode.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def explode @explode end |
#git_config_keys ⇒ Object
Returns the value of attribute git_config_keys.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def git_config_keys @git_config_keys end |
#github_token ⇒ Object
Returns the value of attribute github_token.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def github_token @github_token end |
#hub_path ⇒ Object
Returns the value of attribute hub_path.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def hub_path @hub_path end |
#login_header ⇒ Object
Returns the value of attribute login_header.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def login_header @login_header end |
#no_token ⇒ Object
Returns the value of attribute no_token.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def no_token @no_token end |
#note ⇒ Object
Returns the value of attribute note.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def note @note end |
#oauth_paths ⇒ Object
Returns the value of attribute oauth_paths.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def oauth_paths @oauth_paths end |
#scopes ⇒ Object
Returns the value of attribute scopes.
16 17 18 |
# File 'lib/travis/tools/github.rb', line 16 def scopes @scopes end |
Instance Method Details
#acceptable?(token) ⇒ Boolean
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/travis/tools/github.rb', line 130 def acceptable?(token) return true unless check_token gh = GH.with(token:) gh['user'] true rescue GH::Error => e debug "token is not acceptable: #{gh_error(e)}" false end |
#api_host ⇒ Object
124 125 126 127 128 |
# File 'lib/travis/tools/github.rb', line 124 def api_host return GITHUB_API unless api_url api_url[%r{^(?:https?://)?([^/]+)}, 1] end |
#composer_token ⇒ Object
83 84 85 86 87 88 |
# File 'lib/travis/tools/github.rb', line 83 def composer_token file(composer_path) do |content| token = JSON.parse(content)['config'].fetch('github-oauth', {})[host] yield token if token end end |
#each_token ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/travis/tools/github.rb', line 40 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 end |
#git_tokens ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/travis/tools/github.rb', line 72 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
115 116 117 118 |
# File 'lib/travis/tools/github.rb', line 115 def github_for_mac_token(&block) command = '-s "github.com/mac"' security(:internet, :w, command, 'GitHub for Mac token', &block) if host == 'github.com' end |
#host ⇒ Object
120 121 122 |
# File 'lib/travis/tools/github.rb', line 120 def host api_host == GITHUB_API ? GITHUB_HOST : api_host end |
#hub ⇒ Object
105 106 107 108 109 |
# File 'lib/travis/tools/github.rb', line 105 def hub file(hub_path, {}) do |contents| YAML.load(contents) end end |
#hub_tokens ⇒ Object
90 91 92 93 94 |
# File 'lib/travis/tools/github.rb', line 90 def hub_tokens hub.fetch(host, []).each do |entry| yield entry['oauth_token'] if entry['oauth_token'] end end |
#issuepost_token(&block) ⇒ Object
111 112 113 |
# File 'lib/travis/tools/github.rb', line 111 def issuepost_token(&block) security(:generic, :w, '-l issuepost.github.access_token', 'issuepost token', &block) if host == 'github.com' end |
#oauth_file_tokens ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/travis/tools/github.rb', line 96 def oauth_file_tokens oauth_paths.each do |path| file(path) do |content| token = content.strip yield token unless token.empty? end end end |
#possible_tokens(&block) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/travis/tools/github.rb', line 53 def possible_tokens(&block) return block[github_token] if github_token if auto_token git_tokens(&block) hub_tokens(&block) oauth_file_tokens(&block) github_for_mac_token(&block) issuepost_token(&block) composer_token(&block) end if github_token || auto_token after_tokens.call elsif login_header login_header.call end end |
#with_session ⇒ Object
49 50 51 |
# File 'lib/travis/tools/github.rb', line 49 def with_session 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 |