2019-03-02 22:35:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
2020-07-28 23:09:34 +05:30
|
|
|
class ImportGithub < Grape::API::Instance
|
2019-03-02 22:35:43 +05:30
|
|
|
rescue_from Octokit::Unauthorized, with: :provider_unauthorized
|
|
|
|
|
2020-07-02 01:45:43 +05:30
|
|
|
before do
|
|
|
|
forbidden! unless Gitlab::CurrentSettings.import_sources&.include?('github')
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
helpers do
|
|
|
|
def client
|
2020-10-24 23:57:45 +05:30
|
|
|
@client ||= if Feature.enabled?(:remove_legacy_github_client)
|
|
|
|
Gitlab::GithubImport::Client.new(params[:personal_access_token])
|
|
|
|
else
|
|
|
|
Gitlab::LegacyGithubImport::Client.new(params[:personal_access_token], client_options)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def access_params
|
|
|
|
{ github_access_token: params[:personal_access_token] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_options
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def provider
|
|
|
|
:github
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
def provider_unauthorized
|
|
|
|
error!("Access denied to your #{Gitlab::ImportSources.title(provider.to_s)} account.", 401)
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Import a GitHub project' do
|
|
|
|
detail 'This feature was introduced in GitLab 11.3.4.'
|
2019-09-30 21:07:59 +05:30
|
|
|
success ::ProjectEntity
|
2019-03-02 22:35:43 +05:30
|
|
|
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'
|
|
|
|
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
|