4dfe26cd8b
Prior to this MR there were two GitHub related importers: * Github::Import: the main importer used for GitHub projects * Gitlab::GithubImport: importer that's somewhat confusingly used for importing Gitea projects (apparently they have a compatible API) This MR renames the Gitea importer to Gitlab::LegacyGithubImport and introduces a new GitHub importer in the Gitlab::GithubImport namespace. This new GitHub importer uses Sidekiq for importing multiple resources in parallel, though it also has the ability to import data sequentially should this be necessary. The new code is spread across the following directories: * lib/gitlab/github_import: this directory contains most of the importer code such as the classes used for importing resources. * app/workers/gitlab/github_import: this directory contains the Sidekiq workers, most of which simply use the code from the directory above. * app/workers/concerns/gitlab/github_import: this directory provides a few modules that are included in every GitHub importer worker. == Stages The import work is divided into separate stages, with each stage importing a specific set of data. Stages will schedule the work that needs to be performed, followed by scheduling a job for the "AdvanceStageWorker" worker. This worker will periodically check if all work is completed and schedule the next stage if this is the case. If work is not yet completed this worker will reschedule itself. Using this approach we don't have to block threads by calling `sleep()`, as doing so for large projects could block the thread from doing any work for many hours. == Retrying Work Workers will reschedule themselves whenever necessary. For example, hitting the GitHub API's rate limit will result in jobs rescheduling themselves. These jobs are not processed until the rate limit has been reset. == User Lookups Part of the importing process involves looking up user details in the GitHub API so we can map them to GitLab users. The old importer used an in-memory cache, but this obviously doesn't work when the work is spread across different threads. The new importer uses a Redis cache and makes sure we only perform API/database calls if absolutely necessary. Frequently used keys are refreshed, and lookup misses are also cached; removing the need for performing API/database calls if we know we don't have the data we're looking for. == Performance & Models The new importer in various places uses raw INSERT statements (as generated by `Gitlab::Database.bulk_insert`) instead of using Rails models. This allows us to bypass any validations and callbacks, drastically reducing the number of SQL queries and Gitaly RPC calls necessary to import projects. To ensure the code produces valid data the corresponding tests check if the produced rows are valid according to the model validation rules.
148 lines
3.9 KiB
Ruby
148 lines
3.9 KiB
Ruby
module Gitlab
|
|
module LegacyGithubImport
|
|
class Client
|
|
GITHUB_SAFE_REMAINING_REQUESTS = 100
|
|
GITHUB_SAFE_SLEEP_TIME = 500
|
|
|
|
attr_reader :access_token, :host, :api_version
|
|
|
|
def initialize(access_token, host: nil, api_version: 'v3')
|
|
@access_token = access_token
|
|
@host = host.to_s.sub(%r{/+\z}, '')
|
|
@api_version = api_version
|
|
@users = {}
|
|
|
|
if access_token
|
|
::Octokit.auto_paginate = false
|
|
end
|
|
end
|
|
|
|
def api
|
|
@api ||= ::Octokit::Client.new(
|
|
access_token: access_token,
|
|
api_endpoint: api_endpoint,
|
|
# 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.'
|
|
end
|
|
|
|
@client ||= ::OAuth2::Client.new(
|
|
config.app_id,
|
|
config.app_secret,
|
|
github_options.merge(ssl: { verify: config['verify_ssl'] })
|
|
)
|
|
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)
|
|
request(method, *args, &block)
|
|
else
|
|
super(method, *args, &block)
|
|
end
|
|
end
|
|
|
|
def respond_to?(method)
|
|
api.respond_to?(method) || super
|
|
end
|
|
|
|
def user(login)
|
|
return nil unless login.present?
|
|
return @users[login] if @users.key?(login)
|
|
|
|
@users[login] = api.user(login)
|
|
end
|
|
|
|
private
|
|
|
|
def api_endpoint
|
|
if host.present? && api_version.present?
|
|
"#{host}/api/#{api_version}"
|
|
else
|
|
github_options[:site]
|
|
end
|
|
end
|
|
|
|
def config
|
|
Gitlab.config.omniauth.providers.find { |provider| provider.name == "github" }
|
|
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.
|
|
rescue Octokit::NotFound
|
|
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)
|
|
sleep rate_limit_sleep_time if rate_limit_exceed?
|
|
|
|
data = api.__send__(method, *args) # rubocop:disable GitlabSecurity/PublicSend
|
|
return data unless data.is_a?(Array)
|
|
|
|
last_response = api.last_response
|
|
|
|
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)
|
|
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
|