Class: Travis::Tools::Github

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) {|_self| ... } ⇒ Github

Returns a new instance of Github.

Yields:

  • (_self)

Yield Parameters:



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(options = 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
  options&.each_pair { |k, v| send("#{k}=", v) if respond_to? "#{k}=" }
  yield self if block_given?
end

Instance Attribute Details

#after_tokensObject

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_urlObject

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_tokenObject

Returns the value of attribute auto_token.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def auto_token
  @auto_token
end

#callbackObject

Returns the value of attribute callback.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def callback
  @callback
end

#check_tokenObject

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_pathObject

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

Parameters:

  • value

    the value to set the attribute debug to.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def debug=(value)
  @debug = value
end

#drop_tokenObject

Returns the value of attribute drop_token.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def drop_token
  @drop_token
end

#explodeObject

Returns the value of attribute explode.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def explode
  @explode
end

#git_config_keysObject

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_tokenObject

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_pathObject

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_headerObject

Returns the value of attribute login_header.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def 
  @login_header
end

#no_tokenObject

Returns the value of attribute no_token.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def no_token
  @no_token
end

#noteObject

Returns the value of attribute note.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def note
  @note
end

#oauth_pathsObject

Returns the value of attribute oauth_paths.



16
17
18
# File 'lib/travis/tools/github.rb', line 16

def oauth_paths
  @oauth_paths
end

#scopesObject

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

Returns:

  • (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_hostObject



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_tokenObject



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_tokenObject



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_tokensObject



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

#hostObject



120
121
122
# File 'lib/travis/tools/github.rb', line 120

def host
  api_host == GITHUB_API ? GITHUB_HOST : api_host
end

#hubObject



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_tokensObject



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_tokensObject



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 
    .call
  end
end

#with_sessionObject



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_tokenObject



36
37
38
# File 'lib/travis/tools/github.rb', line 36

def with_token
  each_token { |t| break yield(t) }
end