Class: GitHubService::Configuration

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns the OAuth token.

Parameters:

Options Hash (opts):

  • :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



38
39
40
41
42
43
# File 'lib/git-process/github_configuration.rb', line 38

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

Instance Attribute Details

#git_configObject (readonly)

Returns the value of attribute git_config.



26
27
28
# File 'lib/git-process/github_configuration.rb', line 26

def git_config
  @git_config
end

Class Method Details

.url_to_base_github_api_url(url) ⇒ String

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

Parameters:

  • url (String)

    the URL to translate

Returns:

  • (String)

    the base GitHub API URL



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/git-process/github_configuration.rb', line 170

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

  if /github.com$/ =~ host
    'https://api.github.com'
  else
    scheme = uri.scheme
    scheme = 'https' unless scheme.start_with?('http')
    host = 'unknown-host' unless host
    "#{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



122
123
124
125
126
127
128
129
# File 'lib/git-process/github_configuration.rb', line 122

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::Configuration::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:



219
220
221
# File 'lib/git-process/github_configuration.rb', line 219

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



157
158
159
160
# File 'lib/git-process/github_configuration.rb', line 157

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:

  • (Octokit::Client)


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

def client
  create_client
end

#configure_octokit(opts = {}) ⇒ void

This method returns an undefined value.

Configures Octokit to use the appropriate URLs for GitHub server.

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



100
101
102
103
104
105
106
107
108
109
# 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)
    c.faraday_config do |f|
      #f.response :logger
    end
  end
end

#create_authorization(opts = {}) ⇒ String

Connects to GitHub to get an OAuth 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:

  • (String)

    the OAuth token



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/git-process/github_configuration.rb', line 235

def create_authorization(opts = {})
  username = opts[:user] || self.user
  remote = opts[:remote_name] || self.remote_name
  logger.info("Authorizing #{username} to work with #{remote}.")

  auth = create_pw_client(opts).create_authorization(
      :scopes => %w(repo user gist),
      :note => 'Git-Process',
      :note_url => 'http://jdigger.github.com/git-process')

  config_auth_token = auth['token']

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

  config_auth_token
end

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

Returns:

  • (Octokit::Client)


81
82
83
84
85
86
87
88
89
# File 'lib/git-process/github_configuration.rb', line 81

def create_client(opts = {})
  logger.debug { "Creating GitHub client for user #{user} using token '#{auth_token}'" }

  base_url = opts[:base_url] || base_github_api_url_for_remote

  configure_octokit(:base_url => base_url)

  Octokit::Client.new(:login => user, :oauth_token => auth_token)
end

#create_pw_client(opts = {}) ⇒ Object

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



195
196
197
198
199
200
201
202
203
204
# File 'lib/git-process/github_configuration.rb', line 195

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

  logger.debug { "Creating GitHub client for user #{usr} using BasicAuth w/ password" }

  configure_octokit(opts)

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

#get_config_auth_tokenString

Returns:



255
256
257
258
# File 'lib/git-process/github_configuration.rb', line 255

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

#gitlibGitProc::GitLib

Returns:



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

def gitlib
  @git_config.gitlib
end

#loggerObject



261
262
263
# File 'lib/git-process/github_configuration.rb', line 261

def logger
  gitlib.logger
end

#passwordString

Returns:



63
64
65
# File 'lib/git-process/github_configuration.rb', line 63

def password
  @password ||= Configuration.ask_for_password
end

#remote_nameString

Returns:



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

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

#userString

Returns:



57
58
59
# File 'lib/git-process/github_configuration.rb', line 57

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



142
143
144
145
146
147
148
149
# File 'lib/git-process/github_configuration.rb', line 142

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