debian-mirror-gitlab/lib/api/import_github.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

129 lines
4.2 KiB
Ruby
Raw Normal View History

2019-03-02 22:35:43 +05:30
# frozen_string_literal: true
module API
2021-01-03 14:25:43 +05:30
class ImportGithub < ::API::Base
2023-01-13 00:05:48 +05:30
before { authenticate! }
2021-01-29 00:20:46 +05:30
feature_category :importers
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-29 00:20:46 +05:30
2019-03-02 22:35:43 +05:30
rescue_from Octokit::Unauthorized, with: :provider_unauthorized
2023-03-17 16:20:25 +05:30
rescue_from Gitlab::GithubImport::RateLimitError, with: :too_many_requests
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)
2021-01-29 00:20:46 +05:30
Gitlab::GithubImport::Client.new(params[:personal_access_token], host: params[:github_hostname])
2020-10-24 23:57:45 +05:30
else
2022-07-16 23:28:13 +05:30
Gitlab::LegacyGithubImport::Client.new(params[:personal_access_token], **client_options)
2020-10-24 23:57:45 +05:30
end
2019-03-02 22:35:43 +05:30
end
def access_params
{ github_access_token: params[:personal_access_token] }
end
def client_options
2021-01-29 00:20:46 +05:30
{ host: params[:github_hostname] }
2019-03-02 22:35:43 +05:30
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
2023-03-17 16:20:25 +05:30
def too_many_requests
error!('Too Many Requests', 429)
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.'
2023-01-13 00:05:48 +05:30
success code: 201, model: ::ProjectEntity
failure [
{ code: 400, message: 'Bad request' },
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 422, message: 'Unprocessable entity' },
{ code: 503, message: 'Service unavailable' }
]
tags ['project_import_github']
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'
2023-03-17 16:20:25 +05:30
requires :target_namespace, type: String, allow_blank: false, desc: 'Namespace or group to import repository into'
2021-01-29 00:20:46 +05:30
optional :github_hostname, type: String, desc: 'Custom GitHub enterprise hostname'
2022-11-25 23:54:43 +05:30
optional :optional_stages, type: Hash, desc: 'Optional stages of import to be performed'
2019-03-02 22:35:43 +05:30
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
2022-11-25 23:54:43 +05:30
2023-01-13 00:05:48 +05:30
desc 'Cancel GitHub project import' do
detail 'This feature was introduced in GitLab 15.5'
success code: 200, model: ProjectImportEntity
failure [
{ code: 400, message: 'Bad request' },
{ code: 401, message: 'Unauthorized' },
{ code: 403, message: 'Forbidden' },
{ code: 404, message: 'Not found' },
{ code: 503, message: 'Service unavailable' }
]
tags ['project_import_github']
end
2022-11-25 23:54:43 +05:30
params do
requires :project_id, type: Integer, desc: 'ID of importing project to be canceled'
end
post 'import/github/cancel' do
project = Project.imported_from(provider.to_s).find(params[:project_id])
result = Import::Github::CancelProjectImportService.new(project, current_user).execute
if result[:status] == :success
status :ok
present ProjectSerializer.new.represent(project, serializer: :import)
else
render_api_error!(result[:message], result[:http_status])
end
end
2023-03-17 16:20:25 +05:30
desc 'Import User Gists' do
detail 'This feature was introduced in GitLab 15.8'
success code: 202
failure [
{ code: 401, message: 'Unauthorized' },
{ code: 422, message: 'Unprocessable Entity' },
{ code: 429, message: 'Too Many Requests' }
]
end
params do
requires :personal_access_token, type: String, desc: 'GitHub personal access token'
end
post 'import/github/gists' do
not_found! if Feature.disabled?(:github_import_gists)
authorize! :create_snippet
result = Import::Github::GistsImportService.new(current_user, client, access_params).execute
if result[:status] == :success
status 202
else
status result[:http_status]
{ errors: result[:message] }
end
end
2019-03-02 22:35:43 +05:30
end
end