Class: GitHubService::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/git-process/github_configuration.rb

Overview

Provides methods related to GitHub configuration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_config, opts = {}) ⇒ String

TODO:

pass in GitLib instead of GitConfig

Returns the OAuth token.

Parameters:

Options Hash (opts):

  • :remote_name (String) — default: Configuration#remote_name

    The “remote” name to use (e.g., ‘origin’)

  • :user (String)

    the username to authenticate with

  • :password (String) — default: Configuration#password

    the password to authenticate with



41
42
43
44
45
46
# File 'lib/git-process/github_configuration.rb', line 41

def initialize(git_config, opts = {})
  @git_config = git_config
  @user = opts[:user]
  @password = opts[:password]
  @remote_name = opts[:remote_name]
end

Class Method Details

.url_to_base_github_api_url(url) ⇒ String

TODO:

use Octokit’s improved ability to determine this

Translate any “git known” URL to the HTTP(S) URL needed for GitHub API calls.

Examples:

url_to_base_github_api_url('[email protected]:jdigger/git-process.git') #=> 'https://api.github.com'

url_to_base_github_api_url('http://ghe.myco.com/jdigger/git-process.git') #=> 'http://ghe.myco.com'

Parameters:

  • url (String)

    the URL to translate

Returns:

  • (String)

    the base GitHub API URL



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/git-process/github_configuration.rb', line 181

def self.url_to_base_github_api_url(url)
  uri = URI.parse(url)
  host = uri.host

  if /github.com$/ =~ host
    return 'https://api.github.com'
  else
    scheme = uri.scheme
    scheme = 'https' unless scheme.start_with?('http')
    host = 'unknown-host' unless host
    return "#{scheme}://#{host}"
  end
end

Instance Method Details

#api_endpoint(base_url = nil) ⇒ String

Determines the URL used for using the GitHub REST interface based on a “base” URL.

If the “base_url” is not provided, then it assumes that this object has a “remote_name” property that it can ask.

Parameters:

  • base_url (String) (defaults to: nil)

    the base GitHub URL

Returns:

  • (String)

    the GitHub REST API URL



126
127
128
129
130
131
132
133
# File 'lib/git-process/github_configuration.rb', line 126

def api_endpoint(base_url = nil)
  base_url ||= base_github_api_url_for_remote
  if /github.com/ !~ base_url
    "#{base_url}/api/v3"
  else
    Octokit::Default::API_ENDPOINT
  end
end

#auth_token(opts = {}) ⇒ String

Returns to OAuth token. If it’s in .git/config, returns that.

Otherwise it connects to GitHub to get the authorization token.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :base_url (String)

    The base URL to use for the GitHub server

  • :remote_name (String) — default: #remote_name

    The “remote” name to use (e.g., ‘origin’)

  • :user (String)

    the username to authenticate with

  • :password (String) — default: #password

    the password to authenticate with

Returns:



233
234
235
# File 'lib/git-process/github_configuration.rb', line 233

def auth_token(opts = {})
  get_config_auth_token() || create_authorization(opts)
end

#base_github_api_url_for_remoteString

Determines the base URL for GitHub API calls.

Returns:

  • (String)

    the base GitHub API URL



161
162
163
164
# File 'lib/git-process/github_configuration.rb', line 161

def base_github_api_url_for_remote
  url = gitlib.remote.expanded_url(remote_name)
  Configuration.url_to_base_github_api_url(url)
end

#clientOctokit::Client

Returns the client for communicating with GitHub using #user and #auth_token.

Returns:

  • (Octokit::Client)

    the client for communicating with GitHub using #user and #auth_token



80
81
82
# File 'lib/git-process/github_configuration.rb', line 80

def client
  @client ||= create_client
end

#configure_octokit(opts = {}) ⇒ void

TODO:

remove opts and pass in base_url directly

This method returns an undefined value.

Configures Octokit to use the appropriate URLs for GitHub server.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/git-process/github_configuration.rb', line 100

def configure_octokit(opts = {})
  base_url = opts[:base_url] || base_github_api_url_for_remote
  Octokit.configure do |c|
    c.api_endpoint = api_endpoint(base_url)
    c.web_endpoint = web_endpoint(base_url)
  end
  if logger.level < ::GitProc::GitLogger::INFO
    Octokit.middleware = Faraday::RackBuilder.new do |builder|
      builder.response :logger, logger
      builder.use Octokit::Response::RaiseError
      builder.adapter Faraday.default_adapter
    end
  end
end

#create_2f_authorization(client) ⇒ String

Connects to GitHub to get an OAuth token.

noinspection RubyStringKeysInHashInspection

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:

  • (String)

    the OAuth token



294
295
296
297
298
# File 'lib/git-process/github_configuration.rb', line 294

def create_2f_authorization(client)
  two_factor = Configuration.ask_for_two_factor

  create_authorization_token(client, two_factor)
end

#create_authorization(opts = {}) ⇒ String

Connects to GitHub to get an OAuth token.

noinspection RubyStringKeysInHashInspection

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :base_url (String)

    The base URL to use for the GitHub server

  • :remote_name (String) — default: Configuration#remote_name

    The “remote” name to use (e.g., ‘origin’)

  • :user (String)

    the username to authenticate with

  • :password (String) — default: #password

    the password to authenticate with

  • :two_factor (String) — default: #password

    the password to authenticate with

Returns:

  • (String)

    the OAuth token



250
251
252
253
254
# File 'lib/git-process/github_configuration.rb', line 250

def create_authorization(opts = {})
  client = opts[:client] || create_pw_client(opts)

  return create_authorization_token(client, opts[:two_factor])
end

#create_authorization_token(client, two_factor) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/git-process/github_configuration.rb', line 257

def create_authorization_token(client, two_factor)
  begin
    # noinspection RubyStringKeysInHashInspection
    headers = two_factor ? {'X-GitHub-OTP' => two_factor} : nil
    auth = client.create_authorization(
        :scopes => %w(repo user gist),
        :note => 'Git-Process',
        :note_url => 'http://jdigger.github.com/git-process',
        :headers => headers
    )
  rescue Octokit::OneTimePasswordRequired
    return create_2f_authorization(client)
  rescue Octokit::UnprocessableEntity => exp
    return unprocessable_authorization(exp)
  end

  config_auth_token = auth[:token]

  # remember it for next time
  gitlib.config['gitProcess.github.authToken'] = config_auth_token

  return config_auth_token
end

#create_pw_client(opts = {}) ⇒ Octokit::Client

Create a GitHub client using username and password specifically. Meant to be used to get an OAuth token for “regular” client calls.

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a message with

Options Hash (opts):

  • :base_url (String)

    The base URL to use for the GitHub server

  • :remote_name (String) — default: #remote_name

    The “remote” name to use (e.g., ‘origin’)

  • :user (String)

    the username to authenticate with

  • :password (String) — default: #password

    the password to authenticate with

Returns:

  • (Octokit::Client)

    the Octokit client for communicating with GitHub



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/git-process/github_configuration.rb', line 208

def create_pw_client(opts = {})
  usr = opts[:user] || user()
  pw = opts[:password] || password()
  remote = opts[:remote_name] || self.remote_name

  logger.info("Authorizing #{usr} to work with #{remote}.")

  configure_octokit(opts)

  Octokit::Client.new(:login => usr, :password => pw)
end

#get_config_auth_tokenString?

Returns the OAuth token, or nil if not found.

Returns:

  • (String, nil)

    the OAuth token, or nil if not found



307
308
309
310
# File 'lib/git-process/github_configuration.rb', line 307

def get_config_auth_token
  c_auth_token = gitlib.config['gitProcess.github.authToken']
  (c_auth_token.nil? or c_auth_token.empty?) ? nil : c_auth_token
end

#git_configGitProc::GitConfig

Returns:



50
51
52
# File 'lib/git-process/github_configuration.rb', line 50

def git_config
  @git_config
end

#gitlibGitProc::GitLib

Returns:



86
87
88
# File 'lib/git-process/github_configuration.rb', line 86

def gitlib
  @git_config.gitlib
end

#loggerObject



313
314
315
# File 'lib/git-process/github_configuration.rb', line 313

def logger
  gitlib.logger
end

#passwordString

Returns the password for GitHub.

Returns:

  • (String)

    the password for GitHub



74
75
76
# File 'lib/git-process/github_configuration.rb', line 74

def password
  @password ||= Configuration.ask_for_password
end

#remote_nameString

Returns the “remote name” (e.g., origin) for GitHub.

Returns:

  • (String)

    the “remote name” (e.g., origin) for GitHub

See Also:



58
59
60
61
62
63
64
# File 'lib/git-process/github_configuration.rb', line 58

def remote_name
  unless @remote_name
    @remote_name = gitlib.remote.name
    raise NoRemoteRepository.new('No remote repository is defined') unless @remote_name
  end
  @remote_name
end

#two_factor_auth(authorization_count, opts) ⇒ Object



301
302
303
# File 'lib/git-process/github_configuration.rb', line 301

def two_factor_auth(authorization_count, opts)
  return create_authorization(opts.merge(:two_factor => two_factor, :authorization_count => authorization_count + 1))
end

#userString

Returns the user name for GitHub.

Returns:

  • (String)

    the user name for GitHub



68
69
70
# File 'lib/git-process/github_configuration.rb', line 68

def user
  @user ||= Configuration.ask_for_user(gitlib)
end

#web_endpoint(base_url = nil) ⇒ String

Determines the URL used for using the GitHub web interface based on a “base” URL.

If the “base_url” is not provided, then it assumes that this object has a “remote_name” property that it can ask.

Parameters:

  • base_url (String) (defaults to: nil)

    the base GitHub URL

Returns:

  • (String)

    the GitHub web URL



146
147
148
149
150
151
152
153
# File 'lib/git-process/github_configuration.rb', line 146

def web_endpoint(base_url = nil)
  base_url ||= base_github_api_url_for_remote
  if /github.com/ !~ base_url
    base_url
  else
    Octokit::Default::WEB_ENDPOINT
  end
end