2014-12-31 08:07:48 -05:00
|
|
|
module Gitlab
|
2015-02-02 17:26:29 -05:00
|
|
|
module GithubImport
|
2014-12-31 08:07:48 -05:00
|
|
|
class Client
|
2016-06-08 21:48:44 -04:00
|
|
|
GITHUB_SAFE_REMAINING_REQUESTS = 100
|
|
|
|
GITHUB_SAFE_SLEEP_TIME = 500
|
|
|
|
|
2016-10-17 11:58:57 -04:00
|
|
|
attr_reader :access_token, :host, :api_version
|
2014-12-31 08:07:48 -05:00
|
|
|
|
2016-10-17 11:58:57 -04:00
|
|
|
def initialize(access_token, host: nil, api_version: 'v3')
|
2016-05-02 11:22:38 -04:00
|
|
|
@access_token = access_token
|
2016-12-15 11:36:53 -05:00
|
|
|
@host = host.to_s.sub(%r{/+\z}, '')
|
2016-10-17 11:58:57 -04:00
|
|
|
@api_version = api_version
|
2017-02-19 23:05:20 -05:00
|
|
|
@users = {}
|
2015-02-05 19:57:27 -05:00
|
|
|
|
|
|
|
if access_token
|
2016-06-08 21:48:44 -04:00
|
|
|
::Octokit.auto_paginate = false
|
2016-05-02 11:22:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def api
|
|
|
|
@api ||= ::Octokit::Client.new(
|
|
|
|
access_token: access_token,
|
2016-10-17 11:58:57 -04:00
|
|
|
api_endpoint: api_endpoint,
|
2016-05-02 11:22:38 -04:00
|
|
|
# If there is no config, we're connecting to github.com and we
|
|
|
|
# should verify ssl.
|
|
|
|
connection_options: {
|
2016-12-15 11:36:53 -05:00
|
|
|
ssl: { verify: config ? config['verify_ssl'] : true }
|
2016-05-02 11:22:38 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2016-04-22 15:43:10 -04:00
|
|
|
|
2016-05-02 11:22:38 -04:00
|
|
|
def client
|
|
|
|
unless config
|
|
|
|
raise Projects::ImportService::Error,
|
|
|
|
'OAuth configuration for GitHub missing.'
|
2015-02-05 19:57:27 -05:00
|
|
|
end
|
2016-05-02 11:22:38 -04:00
|
|
|
|
|
|
|
@client ||= ::OAuth2::Client.new(
|
|
|
|
config.app_id,
|
|
|
|
config.app_secret,
|
|
|
|
github_options.merge(ssl: { verify: config['verify_ssl'] })
|
|
|
|
)
|
2015-02-05 19:57:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_url(redirect_uri)
|
|
|
|
client.auth_code.authorize_url({
|
|
|
|
redirect_uri: redirect_uri,
|
|
|
|
scope: "repo, user, user:email"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_token(code)
|
|
|
|
client.auth_code.get_token(code).token
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
if api.respond_to?(method)
|
2016-09-27 13:32:47 -04:00
|
|
|
request(method, *args, &block)
|
2015-02-05 19:57:27 -05:00
|
|
|
else
|
|
|
|
super(method, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(method)
|
|
|
|
api.respond_to?(method) || super
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
2017-02-19 23:05:20 -05:00
|
|
|
def user(login)
|
|
|
|
return nil unless login.present?
|
|
|
|
return @users[login] if @users.key?(login)
|
|
|
|
|
|
|
|
@users[login] = api.user(login)
|
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
private
|
|
|
|
|
2016-12-15 11:36:53 -05:00
|
|
|
def api_endpoint
|
|
|
|
if host.present? && api_version.present?
|
|
|
|
"#{host}/api/#{api_version}"
|
|
|
|
else
|
|
|
|
github_options[:site]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-31 08:07:48 -05:00
|
|
|
def config
|
2016-04-22 15:43:10 -04:00
|
|
|
Gitlab.config.omniauth.providers.find { |provider| provider.name == "github" }
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def github_options
|
2016-05-02 11:22:38 -04:00
|
|
|
if config
|
|
|
|
config["args"]["client_options"].deep_symbolize_keys
|
|
|
|
else
|
|
|
|
OmniAuth::Strategies::GitHub.default_options[:client_options].symbolize_keys
|
|
|
|
end
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
2016-06-08 21:48:44 -04:00
|
|
|
|
|
|
|
def rate_limit
|
|
|
|
api.rate_limit!
|
2016-07-11 15:15:59 -04:00
|
|
|
# GitHub Rate Limit API returns 404 when the rate limit is
|
|
|
|
# disabled. In this case we just want to return gracefully
|
|
|
|
# instead of spitting out an error.
|
|
|
|
rescue Octokit::NotFound
|
2016-07-11 17:53:39 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_rate_limit?
|
2016-07-12 09:44:46 -04:00
|
|
|
return @has_rate_limit if defined?(@has_rate_limit)
|
|
|
|
|
|
|
|
@has_rate_limit = rate_limit.present?
|
2016-06-08 21:48:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rate_limit_exceed?
|
2016-07-11 17:53:39 -04:00
|
|
|
has_rate_limit? && rate_limit.remaining <= GITHUB_SAFE_REMAINING_REQUESTS
|
2016-06-08 21:48:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rate_limit_sleep_time
|
|
|
|
rate_limit.resets_in + GITHUB_SAFE_SLEEP_TIME
|
|
|
|
end
|
|
|
|
|
2016-09-27 13:32:47 -04:00
|
|
|
def request(method, *args, &block)
|
2016-06-08 21:48:44 -04:00
|
|
|
sleep rate_limit_sleep_time if rate_limit_exceed?
|
|
|
|
|
2016-10-05 15:20:00 -04:00
|
|
|
data = api.send(method, *args)
|
|
|
|
return data unless data.is_a?(Array)
|
2016-06-08 21:48:44 -04:00
|
|
|
|
2016-10-27 09:07:18 -04:00
|
|
|
last_response = api.last_response
|
|
|
|
|
2016-10-05 15:20:00 -04:00
|
|
|
if block_given?
|
|
|
|
yield data
|
2016-10-27 09:07:18 -04:00
|
|
|
# api.last_response could change while we're yielding (e.g. fetching labels for each PR)
|
2016-10-27 09:00:31 -04:00
|
|
|
# so we cache our own last response
|
2016-10-27 09:07:18 -04:00
|
|
|
each_response_page(last_response, &block)
|
2016-10-05 15:20:00 -04:00
|
|
|
else
|
2016-10-27 09:07:18 -04:00
|
|
|
each_response_page(last_response) { |page| data.concat(page) }
|
2016-10-05 15:20:00 -04:00
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-27 09:07:18 -04:00
|
|
|
def each_response_page(last_response)
|
2016-06-08 21:48:44 -04:00
|
|
|
while last_response.rels[:next]
|
|
|
|
sleep rate_limit_sleep_time if rate_limit_exceed?
|
|
|
|
last_response = last_response.rels[:next].get
|
2016-09-27 13:32:47 -04:00
|
|
|
yield last_response.data if last_response.data.is_a?(Array)
|
2016-06-08 21:48:44 -04:00
|
|
|
end
|
|
|
|
end
|
2014-12-31 08:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|