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
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
before_action :authorize_owner_access!, except: [:new, :callback, :personal_access_token, :status, :details, :create,
|
|
|
|
:realtime_changes, :cancel_all, :counts]
|
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
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
before_action only: [:status] do
|
|
|
|
push_frontend_feature_flag(:import_details_page)
|
|
|
|
end
|
|
|
|
|
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,
|
2023-05-27 22:25:52 +05:30
|
|
|
page_info: client_repos_response[:page_info],
|
|
|
|
provider_repo_count: client_repos_response[:count] }
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
format.html do
|
|
|
|
if params[:namespace_id].present?
|
|
|
|
@namespace = Namespace.find_by_id(params[:namespace_id])
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
render_404 unless current_user.can?(:import_projects, @namespace)
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
def details
|
|
|
|
render_404 unless Feature.enabled?(:import_details_page)
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
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)
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
render json: Import::GithubRealtimeRepoSerializer.new.represent(already_added_projects)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
def failures
|
|
|
|
unless project.import_finished?
|
|
|
|
return render status: :bad_request, json: {
|
|
|
|
message: _('The import is not complete.')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
failures = project.import_failures.with_external_identifiers
|
|
|
|
serializer = Import::GithubFailureSerializer.new.with_pagination(request, response)
|
|
|
|
|
|
|
|
render json: serializer.represent(failures)
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def cancel
|
|
|
|
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
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
def cancel_all
|
|
|
|
projects_to_cancel = Project.imported_from(provider_name).created_by(current_user).is_importing
|
|
|
|
|
|
|
|
canceled = projects_to_cancel.map do |project|
|
|
|
|
# #reset is called to make sure project was not finished/canceled brefore calling service
|
|
|
|
result = Import::Github::CancelProjectImportService.new(project.reset, current_user).execute
|
|
|
|
|
|
|
|
{
|
|
|
|
id: project.id,
|
|
|
|
status: result[:status],
|
|
|
|
error: result[:message]
|
|
|
|
}.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: canceled
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def counts
|
|
|
|
render json: {
|
|
|
|
owned: client_proxy.count_repos_by('owned', current_user.id),
|
|
|
|
collaborated: client_proxy.count_repos_by('collaborated', current_user.id),
|
|
|
|
organization: client_proxy.count_repos_by('organization', current_user.id)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
def project
|
|
|
|
@project ||= Project.imported_from(provider_name).find(params[:project_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_owner_access!
|
|
|
|
return render_404 unless current_user.can?(:owner_access, project)
|
|
|
|
end
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
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)
|
2023-05-27 22:25:52 +05:30
|
|
|
ProjectSerializer.new.represent(
|
|
|
|
projects,
|
|
|
|
serializer: :import, provider_url: provider_url, client: client_proxy
|
|
|
|
)
|
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,
|
2023-05-27 22:25:52 +05:30
|
|
|
first: PAGE_LENGTH
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|
|
|
|
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')
|