debian-mirror-gitlab/app/controllers/import/github_controller.rb

254 lines
6.8 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
class Import::GithubController < Import::BaseController
2020-07-28 23:09:34 +05:30
extend ::Gitlab::Utils::Override
2019-07-07 11:18:12 +05:30
include ImportHelper
2019-12-21 20:55:43 +05:30
include ActionView::Helpers::SanitizeHelper
2022-11-25 23:54:43 +05:30
include Import::GithubOauth
2019-07-07 11:18:12 +05:30
2017-08-17 22:00:37 +05:30
before_action :verify_import_enabled
2019-07-07 11:18:12 +05:30
before_action :provider_auth, only: [:status, :realtime_changes, :create]
before_action :expire_etag_cache, only: [:status, :create]
2015-04-26 12:48:37 +05:30
2017-08-17 22:00:37 +05:30
rescue_from Octokit::Unauthorized, with: :provider_unauthorized
2020-04-22 19:07:51 +05:30
rescue_from Octokit::TooManyRequests, with: :provider_rate_limit
2021-01-29 00:20:46 +05:30
rescue_from Gitlab::GithubImport::RateLimitError, with: :rate_limit_threshold_exceeded
2016-08-24 12:49:21 +05:30
2023-03-04 22:38:38 +05:30
delegate :client, to: :client_proxy, private: true
2021-02-22 17:27:13 +05:30
PAGE_LENGTH = 25
2016-08-24 12:49:21 +05:30
def new
2019-10-12 21:52:04 +05:30
if !ci_cd_only? && github_import_configured? && logged_in_with_provider?
2017-08-17 22:00:37 +05:30
go_to_provider_for_permissions
elsif session[access_token_key]
2022-08-13 15:12:31 +05:30
redirect_to status_import_url
2016-08-24 12:49:21 +05:30
end
end
2015-04-26 12:48:37 +05:30
def callback
2022-06-21 17:19:12 +05:30
auth_state = session.delete(auth_state_key)
2022-01-12 12:59:36 +05:30
if auth_state.blank? || !ActiveSupport::SecurityUtils.secure_compare(auth_state, params[:state])
provider_unauthorized
else
session[access_token_key] = get_token(params[:code])
2022-08-13 15:12:31 +05:30
redirect_to status_import_url
2022-01-12 12:59:36 +05:30
end
2015-04-26 12:48:37 +05:30
end
2016-08-24 12:49:21 +05:30
def personal_access_token
2018-12-13 13:39:08 +05:30
session[access_token_key] = params[:personal_access_token]&.strip
2022-08-13 15:12:31 +05:30
redirect_to status_import_url
2016-08-24 12:49:21 +05:30
end
2015-04-26 12:48:37 +05:30
def status
2019-07-07 11:18:12 +05:30
# Request repos to display error page if provider token is invalid
2019-12-04 20:38:33 +05:30
# Improving in https://gitlab.com/gitlab-org/gitlab-foss/issues/55585
2019-07-07 11:18:12 +05:30
client_repos
2023-03-04 22:38:38 +05:30
respond_to do |format|
format.json do
render json: { imported_projects: serialized_imported_projects,
provider_repos: serialized_provider_repos,
incompatible_repos: serialized_incompatible_repos,
page_info: client_repos_response[:page_info] }
end
format.html do
if params[:namespace_id].present?
@namespace = Namespace.find_by_id(params[:namespace_id])
render_404 unless current_user.can?(:create_projects, @namespace)
end
end
end
2015-04-26 12:48:37 +05:30
end
def create
2020-07-28 23:09:34 +05:30
result = Import::GithubService.new(client, current_user, import_params).execute(access_params, provider_name)
2019-03-02 22:35:43 +05:30
if result[:status] == :success
2019-07-07 11:18:12 +05:30
render json: serialized_imported_projects(result[:project])
2016-09-29 09:46:39 +05:30
else
2019-03-02 22:35:43 +05:30
render json: { errors: result[:message] }, status: result[:http_status]
2016-09-29 09:46:39 +05:30
end
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
def realtime_changes
2022-06-21 17:19:12 +05:30
Gitlab::PollingInterval.set_header(response, interval: 3_000)
render json: already_added_projects.map { |project|
{
id: project.id,
import_status: project.import_status,
stats: ::Gitlab::GithubImport::ObjectCounter.summary(project)
}
}
2019-07-07 11:18:12 +05:30
end
2022-11-25 23:54:43 +05:30
def cancel
project = Project.imported_from(provider_name).find(params[:project_id])
result = Import::Github::CancelProjectImportService.new(project, current_user).execute
if result[:status] == :success
render json: serialized_imported_projects(result[:project])
else
render json: { errors: result[:message] }, status: result[:http_status]
end
end
2020-07-28 23:09:34 +05:30
protected
2015-04-26 12:48:37 +05:30
2020-07-28 23:09:34 +05:30
override :importable_repos
def importable_repos
2021-11-11 11:23:49 +05:30
client_repos.to_a
2019-03-02 22:35:43 +05:30
end
2020-07-28 23:09:34 +05:30
override :incompatible_repos
def incompatible_repos
[]
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
override :provider_name
def provider_name
:github
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
override :provider_url
def provider_url
strong_memoize(:provider_url) do
2020-10-24 23:57:45 +05:30
oauth_config&.dig('url').presence || 'https://github.com'
2020-07-28 23:09:34 +05:30
end
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
private
def import_params
params.permit(permitted_import_params)
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
def permitted_import_params
2022-11-25 23:54:43 +05:30
[:repo_id, :new_name, :target_namespace, { optional_stages: {} }]
2019-07-07 11:18:12 +05:30
end
2020-07-28 23:09:34 +05:30
def serialized_imported_projects(projects = already_added_projects)
ProjectSerializer.new.represent(projects, serializer: :import, provider_url: provider_url)
2019-07-07 11:18:12 +05:30
end
def expire_etag_cache
Gitlab::EtagCaching::Store.new.tap do |store|
store.touch(realtime_changes_path)
end
end
2023-03-04 22:38:38 +05:30
def client_proxy
@client_proxy ||= Gitlab::GithubImport::Clients::Proxy.new(
session[access_token_key], client_options
)
end
def client_repos_response
2023-03-17 16:20:25 +05:30
@client_repos_response ||= client_proxy.repos(sanitized_filter_param, fetch_repos_options)
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
def client_repos
2023-03-04 22:38:38 +05:30
client_repos_response[:repos]
2020-10-24 23:57:45 +05:30
end
2021-01-29 00:20:46 +05:30
def sanitized_filter_param
super
2023-03-17 16:20:25 +05:30
@filter = sanitize_query_param(@filter)
end
def sanitize_query_param(value)
value.to_s.first(255).gsub(/[ :]/, '')
2020-10-24 23:57:45 +05:30
end
2017-08-17 22:00:37 +05:30
def verify_import_enabled
render_404 unless import_enabled?
2015-04-26 12:48:37 +05:30
end
2017-08-17 22:00:37 +05:30
def import_enabled?
2020-07-28 23:09:34 +05:30
__send__("#{provider_name}_import_enabled?") # rubocop:disable GitlabSecurity/PublicSend
2015-04-26 12:48:37 +05:30
end
2019-07-07 11:18:12 +05:30
def realtime_changes_path
2020-07-28 23:09:34 +05:30
public_send("realtime_changes_import_#{provider_name}_path", format: :json) # rubocop:disable GitlabSecurity/PublicSend
2019-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
def new_import_url
2022-08-13 15:12:31 +05:30
public_send("new_import_#{provider_name}_url", extra_import_params.merge({ namespace_id: params[:namespace_id] })) # rubocop:disable GitlabSecurity/PublicSend
2015-04-26 12:48:37 +05:30
end
2015-09-25 12:07:36 +05:30
2022-08-13 15:12:31 +05:30
def status_import_url
public_send("status_import_#{provider_name}_url", extra_import_params.merge({ namespace_id: params[:namespace_id].presence })) # rubocop:disable GitlabSecurity/PublicSend
2017-08-17 22:00:37 +05:30
end
def provider_unauthorized
session[access_token_key] = nil
redirect_to new_import_url,
2020-07-28 23:09:34 +05:30
alert: "Access denied to your #{Gitlab::ImportSources.title(provider_name.to_s)} account."
2017-08-17 22:00:37 +05:30
end
2020-04-22 19:07:51 +05:30
def provider_rate_limit(exception)
2020-05-24 23:13:21 +05:30
reset_time = Time.zone.at(exception.response_headers['x-ratelimit-reset'].to_i)
2020-04-22 19:07:51 +05:30
session[access_token_key] = nil
redirect_to new_import_url,
alert: _("GitHub API rate limit exceeded. Try again after %{reset_time}") % { reset_time: reset_time }
end
2022-01-12 12:59:36 +05:30
def auth_state_key
:"#{provider_name}_auth_state_key"
end
2017-08-17 22:00:37 +05:30
def access_token_key
2020-07-28 23:09:34 +05:30
:"#{provider_name}_access_token"
2016-08-24 12:49:21 +05:30
end
2015-09-25 12:07:36 +05:30
def access_params
2017-08-17 22:00:37 +05:30
{ github_access_token: session[access_token_key] }
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def logged_in_with_provider?
2020-07-28 23:09:34 +05:30
current_user.identities.exists?(provider: provider_name)
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def client_options
2020-04-22 19:07:51 +05:30
{ wait_for_rate_limit_reset: false }
2015-09-25 12:07:36 +05:30
end
2018-03-27 19:54:05 +05:30
2021-01-29 00:20:46 +05:30
def rate_limit_threshold_exceeded
head :too_many_requests
2019-12-21 20:55:43 +05:30
end
2021-02-22 17:27:13 +05:30
2023-03-17 16:20:25 +05:30
def fetch_repos_options
pagination_options.merge(relation_options)
end
2021-02-22 17:27:13 +05:30
def pagination_options
{
2023-03-04 22:38:38 +05:30
before: params[:before].presence,
after: params[:after].presence,
first: PAGE_LENGTH,
# TODO: remove after rollout FF github_client_fetch_repos_via_graphql
# https://gitlab.com/gitlab-org/gitlab/-/issues/385649
2021-02-22 17:27:13 +05:30
page: [1, params[:page].to_i].max,
per_page: PAGE_LENGTH
}
end
2023-03-17 16:20:25 +05:30
def relation_options
{
relation_type: params[:relation_type],
organization_login: sanitize_query_param(params[:organization_login])
}
end
2015-04-26 12:48:37 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Import::GithubController.prepend_mod_with('Import::GithubController')