2015-04-26 12:48:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
class Client
|
2016-06-16 23:09:34 +05:30
|
|
|
GITHUB_SAFE_REMAINING_REQUESTS = 100
|
|
|
|
GITHUB_SAFE_SLEEP_TIME = 500
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
attr_reader :access_token
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
def initialize(access_token)
|
2016-08-24 12:49:21 +05:30
|
|
|
@access_token = access_token
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
if access_token
|
2016-06-16 23:09:34 +05:30
|
|
|
::Octokit.auto_paginate = false
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def api
|
|
|
|
@api ||= ::Octokit::Client.new(
|
|
|
|
access_token: access_token,
|
|
|
|
api_endpoint: github_options[:site],
|
|
|
|
# If there is no config, we're connecting to github.com and we
|
|
|
|
# should verify ssl.
|
|
|
|
connection_options: {
|
|
|
|
ssl: { verify: config ? config['verify_ssl'] : true }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def client
|
|
|
|
unless config
|
|
|
|
raise Projects::ImportService::Error,
|
|
|
|
'OAuth configuration for GitHub missing.'
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
@client ||= ::OAuth2::Client.new(
|
|
|
|
config.app_id,
|
|
|
|
config.app_secret,
|
|
|
|
github_options.merge(ssl: { verify: config['verify_ssl'] })
|
|
|
|
)
|
2015-04-26 12:48:37 +05:30
|
|
|
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-11-03 12:29:30 +05:30
|
|
|
request(method, *args, &block)
|
2015-04-26 12:48:37 +05:30
|
|
|
else
|
|
|
|
super(method, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(method)
|
|
|
|
api.respond_to?(method) || super
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def config
|
2016-06-02 11:05:42 +05:30
|
|
|
Gitlab.config.omniauth.providers.find { |provider| provider.name == "github" }
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def github_options
|
2016-08-24 12:49:21 +05:30
|
|
|
if config
|
|
|
|
config["args"]["client_options"].deep_symbolize_keys
|
|
|
|
else
|
|
|
|
OmniAuth::Strategies::GitHub.default_options[:client_options].symbolize_keys
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
def rate_limit
|
|
|
|
api.rate_limit!
|
2016-08-24 12:49:21 +05:30
|
|
|
# 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
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_rate_limit?
|
|
|
|
return @has_rate_limit if defined?(@has_rate_limit)
|
|
|
|
|
|
|
|
@has_rate_limit = rate_limit.present?
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def rate_limit_exceed?
|
2016-08-24 12:49:21 +05:30
|
|
|
has_rate_limit? && rate_limit.remaining <= GITHUB_SAFE_REMAINING_REQUESTS
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def rate_limit_sleep_time
|
|
|
|
rate_limit.resets_in + GITHUB_SAFE_SLEEP_TIME
|
|
|
|
end
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def request(method, *args, &block)
|
2016-06-16 23:09:34 +05:30
|
|
|
sleep rate_limit_sleep_time if rate_limit_exceed?
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
data = api.send(method, *args)
|
|
|
|
return data unless data.is_a?(Array)
|
2016-06-16 23:09:34 +05:30
|
|
|
|
|
|
|
last_response = api.last_response
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
if block_given?
|
|
|
|
yield data
|
|
|
|
# api.last_response could change while we're yielding (e.g. fetching labels for each PR)
|
|
|
|
# so we cache our own last response
|
|
|
|
each_response_page(last_response, &block)
|
|
|
|
else
|
|
|
|
each_response_page(last_response) { |page| data.concat(page) }
|
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_response_page(last_response)
|
2016-06-16 23:09:34 +05:30
|
|
|
while last_response.rels[:next]
|
|
|
|
sleep rate_limit_sleep_time if rate_limit_exceed?
|
|
|
|
last_response = last_response.rels[:next].get
|
2016-11-03 12:29:30 +05:30
|
|
|
yield last_response.data if last_response.data.is_a?(Array)
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|