debian-mirror-gitlab/app/services/import/github_service.rb

94 lines
2.3 KiB
Ruby
Raw Normal View History

2019-03-02 22:35:43 +05:30
# frozen_string_literal: true
module Import
class GithubService < Import::BaseService
attr_accessor :client
attr_reader :params, :current_user
def execute(access_params, provider)
2021-01-29 00:20:46 +05:30
if blocked_url?
return log_and_return_error("Invalid URL: #{url}", :bad_request)
end
2019-03-02 22:35:43 +05:30
unless authorized?
2019-07-31 22:56:46 +05:30
return error(_('This namespace has already been taken! Please choose another one.'), :unprocessable_entity)
2019-03-02 22:35:43 +05:30
end
2020-06-23 00:09:42 +05:30
project = create_project(access_params, provider)
2019-03-02 22:35:43 +05:30
if project.persisted?
success(project)
else
error(project_save_error(project), :unprocessable_entity)
end
2020-06-23 00:09:42 +05:30
rescue Octokit::Error => e
log_error(e)
end
def create_project(access_params, provider)
Gitlab::LegacyGithubImport::ProjectCreator.new(
repo,
project_name,
target_namespace,
current_user,
2021-02-22 17:27:13 +05:30
type: provider,
**access_params).execute(extra_project_attrs)
2019-03-02 22:35:43 +05:30
end
def repo
2020-10-24 23:57:45 +05:30
@repo ||= client.repository(params[:repo_id].to_i)
2019-03-02 22:35:43 +05:30
end
def project_name
@project_name ||= params[:new_name].presence || repo.name
end
def namespace_path
@namespace_path ||= params[:target_namespace].presence || current_user.namespace_path
end
def target_namespace
@target_namespace ||= find_or_create_namespace(namespace_path, current_user.namespace_path)
end
def extra_project_attrs
{}
end
def authorized?
can?(current_user, :create_projects, target_namespace)
end
2020-06-23 00:09:42 +05:30
2021-01-29 00:20:46 +05:30
def url
@url ||= params[:github_hostname]
end
def allow_local_requests?
Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
end
def blocked_url?
Gitlab::UrlBlocker.blocked_url?(
url,
2021-02-22 17:27:13 +05:30
allow_localhost: allow_local_requests?,
allow_local_network: allow_local_requests?,
schemes: %w(http https)
2021-01-29 00:20:46 +05:30
)
end
2020-06-23 00:09:42 +05:30
private
def log_error(exception)
Gitlab::Import::Logger.error(
message: 'Import failed due to a GitHub error',
status: exception.response_status,
error: exception.response_body
)
error(_('Import failed due to a GitHub error: %{original}') % { original: exception.response_body }, :unprocessable_entity)
end
2019-03-02 22:35:43 +05:30
end
end
2019-12-04 20:38:33 +05:30
Import::GithubService.prepend_if_ee('EE::Import::GithubService')