Class: GitHubService::Configuration
- Inherits:
-
Object
- Object
- GitHubService::Configuration
- Defined in:
- lib/git-process/github_configuration.rb
Overview
Provides methods related to GitHub configuration
Class Method Summary collapse
-
.url_to_base_github_api_url(url) ⇒ String
Translate any “git known” URL to the HTTP(S) URL needed for GitHub API calls.
Instance Method Summary collapse
-
#api_endpoint(base_url = nil) ⇒ String
Determines the URL used for using the GitHub REST interface based on a “base” URL.
-
#auth_token(opts = {}) ⇒ String
Returns to OAuth token.
-
#base_github_api_url_for_remote ⇒ String
Determines the base URL for GitHub API calls.
-
#client ⇒ Octokit::Client
The client for communicating with GitHub using #user and #auth_token.
-
#configure_octokit(opts = {}) ⇒ void
Configures Octokit to use the appropriate URLs for GitHub server.
-
#create_2f_authorization(client) ⇒ String
Connects to GitHub to get an OAuth token.
-
#create_authorization(opts = {}) ⇒ String
Connects to GitHub to get an OAuth token.
- #create_authorization_token(client, two_factor) ⇒ Object
-
#create_pw_client(opts = {}) ⇒ Octokit::Client
Create a GitHub client using username and password specifically.
-
#get_config_auth_token ⇒ String?
The OAuth token, or nil if not found.
- #git_config ⇒ GitProc::GitConfig
- #gitlib ⇒ GitProc::GitLib
-
#initialize(git_config, opts = {}) ⇒ String
constructor
The OAuth token.
- #logger ⇒ Object
-
#password ⇒ String
The password for GitHub.
-
#remote_name ⇒ String
The “remote name” (e.g., origin) for GitHub.
- #two_factor_auth(authorization_count, opts) ⇒ Object
-
#user ⇒ String
The user name for GitHub.
-
#web_endpoint(base_url = nil) ⇒ String
Determines the URL used for using the GitHub web interface based on a “base” URL.
Constructor Details
#initialize(git_config, opts = {}) ⇒ String
pass in GitLib instead of GitConfig
Returns the OAuth token.
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
use Octokit’s improved ability to determine this
Translate any “git known” URL to the HTTP(S) URL needed for GitHub API calls.
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.
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.
233 234 235 |
# File 'lib/git-process/github_configuration.rb', line 233 def auth_token(opts = {}) get_config_auth_token() || (opts) end |
#base_github_api_url_for_remote ⇒ String
Determines the base URL for GitHub API calls.
161 162 163 164 |
# File 'lib/git-process/github_configuration.rb', line 161 def base_github_api_url_for_remote url = gitlib.remote.(remote_name) Configuration.url_to_base_github_api_url(url) end |
#client ⇒ Octokit::Client
Returns 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
remove opts and pass in base_url directly
This method returns an undefined value.
Configures Octokit to use the appropriate URLs for GitHub server.
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
294 295 296 297 298 |
# File 'lib/git-process/github_configuration.rb', line 294 def (client) two_factor = Configuration.ask_for_two_factor (client, two_factor) end |
#create_authorization(opts = {}) ⇒ String
Connects to GitHub to get an OAuth token.
noinspection RubyStringKeysInHashInspection
250 251 252 253 254 |
# File 'lib/git-process/github_configuration.rb', line 250 def (opts = {}) client = opts[:client] || create_pw_client(opts) return (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 (client, two_factor) begin # noinspection RubyStringKeysInHashInspection headers = two_factor ? {'X-GitHub-OTP' => two_factor} : nil auth = client.( :scopes => %w(repo user gist), :note => 'Git-Process', :note_url => 'http://jdigger.github.com/git-process', :headers => headers ) rescue Octokit::OneTimePasswordRequired return (client) rescue Octokit::UnprocessableEntity => exp return (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.
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_token ⇒ String?
Returns 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_config ⇒ GitProc::GitConfig
50 51 52 |
# File 'lib/git-process/github_configuration.rb', line 50 def git_config @git_config end |
#gitlib ⇒ GitProc::GitLib
86 87 88 |
# File 'lib/git-process/github_configuration.rb', line 86 def gitlib @git_config.gitlib end |
#logger ⇒ Object
313 314 315 |
# File 'lib/git-process/github_configuration.rb', line 313 def logger gitlib.logger end |
#password ⇒ String
Returns 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_name ⇒ String
Returns the “remote name” (e.g., origin) for GitHub.
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(, opts) return (opts.merge(:two_factor => two_factor, :authorization_count => + 1)) end |
#user ⇒ String
Returns 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.
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 |