debian-mirror-gitlab/lib/gitlab/legacy_github_import/client.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

174 lines
4.6 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
module Gitlab
module LegacyGithubImport
class Client
GITHUB_SAFE_REMAINING_REQUESTS = 100
GITHUB_SAFE_SLEEP_TIME = 500
2020-04-22 19:07:51 +05:30
attr_reader :access_token, :host, :api_version, :wait_for_rate_limit_reset
2018-03-17 18:26:18 +05:30
2021-09-30 23:02:18 +05:30
def initialize(access_token, host: nil, api_version: 'v3', wait_for_rate_limit_reset: true, hostname: nil)
2018-03-17 18:26:18 +05:30
@access_token = access_token
@host = host.to_s.sub(%r{/+\z}, '')
2021-09-30 23:02:18 +05:30
@hostname = hostname
2018-03-17 18:26:18 +05:30
@api_version = api_version
@users = {}
2020-04-22 19:07:51 +05:30
@wait_for_rate_limit_reset = wait_for_rate_limit_reset
2018-03-17 18:26:18 +05:30
if access_token
::Octokit.auto_paginate = false
end
end
def api
@api ||= ::Octokit::Client.new(
access_token: access_token,
api_endpoint: api_endpoint,
2021-01-29 00:20:46 +05:30
web_endpoint: web_endpoint,
2018-03-17 18:26:18 +05:30
# If there is no config, we're connecting to github.com and we
# should verify ssl.
connection_options: {
2021-09-30 23:02:18 +05:30
ssl: { verify: config ? config['verify_ssl'] : true },
headers: { host: @hostname }.compact
2018-03-17 18:26:18 +05:30
}
)
end
def client
unless config
raise Projects::ImportService::Error,
'OAuth configuration for GitHub missing.'
end
@client ||= ::OAuth2::Client.new(
config.app_id,
config.app_secret,
github_options.merge(ssl: { verify: config['verify_ssl'] })
)
end
2022-01-12 12:59:36 +05:30
def authorize_url(redirect_uri, state = nil)
2018-03-17 18:26:18 +05:30
client.auth_code.authorize_url({
redirect_uri: redirect_uri,
2022-01-12 12:59:36 +05:30
scope: "repo, user, user:email",
state: state
2018-03-17 18:26:18 +05:30
})
end
def get_token(code)
client.auth_code.get_token(code).token
end
def method_missing(method, *args, &block)
if api.respond_to?(method)
request(method, *args, &block)
else
super(method, *args, &block)
end
end
def respond_to?(method)
api.respond_to?(method) || super
end
def user(login)
2019-07-07 11:18:12 +05:30
return unless login.present?
2018-03-17 18:26:18 +05:30
return @users[login] if @users.key?(login)
@users[login] = api.user(login)
end
2022-10-11 01:57:18 +05:30
def repository(id)
request(:repository, id).to_h
end
def repos
repositories = request(:repos, nil)
if repositories.is_a?(Array)
repositories.map(&:to_h)
else
repositories
end
end
2018-03-17 18:26:18 +05:30
private
def api_endpoint
if host.present? && api_version.present?
"#{host}/api/#{api_version}"
else
2020-03-13 15:44:24 +05:30
github_options[:site] || ::Octokit::Default.api_endpoint
2018-03-17 18:26:18 +05:30
end
end
2021-01-29 00:20:46 +05:30
def web_endpoint
host.presence || ::Octokit::Default.web_endpoint
end
2018-03-17 18:26:18 +05:30
def config
2018-05-09 12:01:36 +05:30
Gitlab::Auth::OAuth::Provider.config_for('github')
2018-03-17 18:26:18 +05:30
end
def github_options
if config
config["args"]["client_options"].deep_symbolize_keys
else
OmniAuth::Strategies::GitHub.default_options[:client_options].symbolize_keys
end
end
def rate_limit
api.rate_limit!
# 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.
2019-07-31 22:56:46 +05:30
rescue ::Octokit::NotFound
2018-03-17 18:26:18 +05:30
nil
end
def has_rate_limit?
return @has_rate_limit if defined?(@has_rate_limit)
@has_rate_limit = rate_limit.present?
end
def rate_limit_exceed?
has_rate_limit? && rate_limit.remaining <= GITHUB_SAFE_REMAINING_REQUESTS
end
def rate_limit_sleep_time
rate_limit.resets_in + GITHUB_SAFE_SLEEP_TIME
end
def request(method, *args, &block)
2020-04-22 19:07:51 +05:30
sleep rate_limit_sleep_time if wait_for_rate_limit_reset && rate_limit_exceed?
2018-03-17 18:26:18 +05:30
data = api.__send__(method, *args) # rubocop:disable GitlabSecurity/PublicSend
return data unless data.is_a?(Array)
last_response = api.last_response
2022-08-27 11:52:29 +05:30
if block
2018-03-17 18:26:18 +05:30
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)
while last_response.rels[:next]
sleep rate_limit_sleep_time if rate_limit_exceed?
last_response = last_response.rels[:next].get
yield last_response.data if last_response.data.is_a?(Array)
end
end
end
end
end