Class: GitHubService::Configuration
- Inherits:
-
Object
- Object
- GitHubService::Configuration
- Defined in:
- lib/git-process/github_configuration.rb
Instance Attribute Summary collapse
-
#git_config ⇒ Object
readonly
Returns the value of attribute git_config.
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
-
#configure_octokit(opts = {}) ⇒ void
Configures Octokit to use the appropriate URLs for GitHub server.
-
#create_authorization(opts = {}) ⇒ String
Connects to GitHub to get an OAuth token.
- #create_client(opts = {}) ⇒ Octokit::Client
-
#create_pw_client(opts = {}) ⇒ Object
Create a GitHub client using username and password specifically.
- #get_config_auth_token ⇒ String
- #gitlib ⇒ GitProc::GitLib
-
#initialize(git_config, opts = {}) ⇒ String
constructor
The OAuth token.
- #logger ⇒ Object
- #password ⇒ String
- #remote_name ⇒ String
- #user ⇒ String
-
#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
Returns the OAuth token.
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_config ⇒ Object (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.
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.
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.
219 220 221 |
# File 'lib/git-process/github_configuration.rb', line 219 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.
157 158 159 160 |
# File 'lib/git-process/github_configuration.rb', line 157 def base_github_api_url_for_remote url = gitlib.remote.(remote_name) Configuration.url_to_base_github_api_url(url) end |
#client ⇒ 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.
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.
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 (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).( :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
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.
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_token ⇒ String
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 |
#gitlib ⇒ GitProc::GitLib
75 76 77 |
# File 'lib/git-process/github_configuration.rb', line 75 def gitlib @git_config.gitlib end |
#logger ⇒ Object
261 262 263 |
# File 'lib/git-process/github_configuration.rb', line 261 def logger gitlib.logger end |
#password ⇒ String
63 64 65 |
# File 'lib/git-process/github_configuration.rb', line 63 def password @password ||= Configuration.ask_for_password end |
#remote_name ⇒ String
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 |
#user ⇒ String
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.
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 |