2019-01-17 05:37:08 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class ImportGithub < ::API::Base
|
2020-10-29 14:09:11 -04:00
|
|
|
feature_category :importers
|
2022-04-27 14:10:15 -04:00
|
|
|
urgency :low
|
2020-10-29 14:09:11 -04:00
|
|
|
|
2019-01-17 05:37:08 -05:00
|
|
|
rescue_from Octokit::Unauthorized, with: :provider_unauthorized
|
|
|
|
|
|
|
|
helpers do
|
|
|
|
def client
|
2020-08-11 08:09:55 -04:00
|
|
|
@client ||= if Feature.enabled?(:remove_legacy_github_client)
|
2020-10-27 23:08:41 -04:00
|
|
|
Gitlab::GithubImport::Client.new(params[:personal_access_token], host: params[:github_hostname])
|
2020-08-11 08:09:55 -04:00
|
|
|
else
|
2022-04-26 17:08:42 -04:00
|
|
|
Gitlab::LegacyGithubImport::Client.new(params[:personal_access_token], **client_options)
|
2020-08-11 08:09:55 -04:00
|
|
|
end
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def access_params
|
|
|
|
{ github_access_token: params[:personal_access_token] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_options
|
2020-10-27 23:08:41 -04:00
|
|
|
{ host: params[:github_hostname] }
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
|
|
|
:github
|
|
|
|
end
|
2019-04-10 17:37:05 -04:00
|
|
|
|
2019-04-10 08:48:53 -04:00
|
|
|
def provider_unauthorized
|
2019-04-11 02:43:09 -04:00
|
|
|
error!("Access denied to your #{Gitlab::ImportSources.title(provider.to_s)} account.", 401)
|
2019-04-10 08:48:53 -04:00
|
|
|
end
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Import a GitHub project' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.3.4.'
|
2019-07-01 04:19:48 -04:00
|
|
|
success ::ProjectEntity
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :personal_access_token, type: String, desc: 'GitHub personal access token'
|
|
|
|
requires :repo_id, type: Integer, desc: 'GitHub repository ID'
|
|
|
|
optional :new_name, type: String, desc: 'New repo name'
|
|
|
|
requires :target_namespace, type: String, desc: 'Namespace to import repo into'
|
2020-10-27 23:08:41 -04:00
|
|
|
optional :github_hostname, type: String, desc: 'Custom GitHub enterprise hostname'
|
2019-01-17 05:37:08 -05:00
|
|
|
end
|
|
|
|
post 'import/github' do
|
|
|
|
result = Import::GithubService.new(client, current_user, params).execute(access_params, provider)
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
present ProjectSerializer.new.represent(result[:project])
|
|
|
|
else
|
|
|
|
status result[:http_status]
|
|
|
|
{ errors: result[:message] }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|