diff options
Diffstat (limited to 'lib/bulk_imports')
-rw-r--r-- | lib/bulk_imports/clients/http.rb | 47 | ||||
-rw-r--r-- | lib/bulk_imports/error.rb | 9 | ||||
-rw-r--r-- | lib/bulk_imports/features.rb | 4 | ||||
-rw-r--r-- | lib/bulk_imports/groups/stage.rb | 6 | ||||
-rw-r--r-- | lib/bulk_imports/groups/transformers/subgroup_to_entity_transformer.rb | 3 |
5 files changed, 50 insertions, 19 deletions
diff --git a/lib/bulk_imports/clients/http.rb b/lib/bulk_imports/clients/http.rb index 8129ff6151c..6c36875111b 100644 --- a/lib/bulk_imports/clients/http.rb +++ b/lib/bulk_imports/clients/http.rb @@ -8,6 +8,7 @@ module BulkImports API_VERSION = 'v4' DEFAULT_PAGE = 1 DEFAULT_PER_PAGE = 30 + PAT_ENDPOINT_MIN_VERSION = '15.5.0' def initialize(url:, token:, page: DEFAULT_PAGE, per_page: DEFAULT_PER_PAGE, api_version: API_VERSION) @url = url @@ -66,38 +67,57 @@ module BulkImports instance_version >= BulkImport.min_gl_version_for_project_migration end - private + def options + { headers: { 'Content-Type' => 'application/json' }, query: { private_token: @token } } + end - def validate_instance_version! - return if @compatible_instance_version + def validate_import_scopes! + return true unless instance_version >= ::Gitlab::VersionInfo.parse(PAT_ENDPOINT_MIN_VERSION) - if instance_version.major < BulkImport::MIN_MAJOR_VERSION - raise ::BulkImports::Error.unsupported_gitlab_version - else - @compatible_instance_version = true + response = with_error_handling do + Gitlab::HTTP.get(resource_url("personal_access_tokens/self"), options) end + + return true if response['scopes']&.include?('api') + + raise ::BulkImports::Error.scope_validation_failure + end + + def validate_instance_version! + return true unless instance_version.major < BulkImport::MIN_MAJOR_VERSION + + raise ::BulkImports::Error.unsupported_gitlab_version end + private + def metadata response = begin with_error_handling do - Gitlab::HTTP.get(resource_url(:version), default_options) + Gitlab::HTTP.get(resource_url(:version), options) end rescue BulkImports::NetworkError # `version` endpoint is not available, try `metadata` endpoint instead with_error_handling do - Gitlab::HTTP.get(resource_url(:metadata), default_options) + Gitlab::HTTP.get(resource_url(:metadata), options) end end response.parsed_response + rescue BulkImports::NetworkError => e + case e&.response&.code + when 401, 403 + raise ::BulkImports::Error.scope_validation_failure + when 404 + raise ::BulkImports::Error.invalid_url + else + raise + end end strong_memoize_attr :metadata # rubocop:disable GitlabSecurity/PublicSend def request(method, resource, options = {}, &block) - validate_instance_version! - with_error_handling do Gitlab::HTTP.public_send( method, @@ -134,9 +154,10 @@ module BulkImports def with_error_handling response = yield - raise ::BulkImports::NetworkError.new("Unsuccessful response #{response.code} from #{response.request.path.path}. Body: #{response.parsed_response}", response: response) unless response.success? + return response if response.success? + + raise ::BulkImports::NetworkError.new("Unsuccessful response #{response.code} from #{response.request.path.path}. Body: #{response.parsed_response}", response: response) - response rescue *Gitlab::HTTP::HTTP_ERRORS => e raise ::BulkImports::NetworkError, e end diff --git a/lib/bulk_imports/error.rb b/lib/bulk_imports/error.rb index 988982d3cdf..38f26028276 100644 --- a/lib/bulk_imports/error.rb +++ b/lib/bulk_imports/error.rb @@ -5,5 +5,14 @@ module BulkImports def self.unsupported_gitlab_version self.new("Unsupported GitLab Version. Minimum Supported Gitlab Version #{BulkImport::MIN_MAJOR_VERSION}.") end + + def self.scope_validation_failure + self.new("Import aborted as the provided personal access token does not have the required 'api' scope or " \ + "is no longer valid.") + end + + def self.invalid_url + self.new("Import aborted as it was not possible to connect to the provided GitLab instance URL.") + end end end diff --git a/lib/bulk_imports/features.rb b/lib/bulk_imports/features.rb index 952e8e62d71..9fdceb03655 100644 --- a/lib/bulk_imports/features.rb +++ b/lib/bulk_imports/features.rb @@ -2,10 +2,6 @@ module BulkImports module Features - def self.enabled? - ::Feature.enabled?(:bulk_import) - end - def self.project_migration_enabled?(destination_namespace = nil) if destination_namespace.present? root_ancestor = Namespace.find_by_full_path(destination_namespace)&.root_ancestor diff --git a/lib/bulk_imports/groups/stage.rb b/lib/bulk_imports/groups/stage.rb index 0db2b1f0698..7a777f1c8e1 100644 --- a/lib/bulk_imports/groups/stage.rb +++ b/lib/bulk_imports/groups/stage.rb @@ -71,7 +71,7 @@ module BulkImports end def project_entities_pipeline - if project_pipeline_available? && feature_flag_enabled? + if migrate_projects? && project_pipeline_available? && feature_flag_enabled? { project_entities: { pipeline: BulkImports::Groups::Pipelines::ProjectEntitiesPipeline, @@ -83,6 +83,10 @@ module BulkImports end end + def migrate_projects? + bulk_import_entity.migrate_projects + end + def project_pipeline_available? @bulk_import.source_version_info >= BulkImport.min_gl_version_for_project_migration end diff --git a/lib/bulk_imports/groups/transformers/subgroup_to_entity_transformer.rb b/lib/bulk_imports/groups/transformers/subgroup_to_entity_transformer.rb index d8fb937ecd2..fcf9ed62388 100644 --- a/lib/bulk_imports/groups/transformers/subgroup_to_entity_transformer.rb +++ b/lib/bulk_imports/groups/transformers/subgroup_to_entity_transformer.rb @@ -10,7 +10,8 @@ module BulkImports source_full_path: entry['full_path'], destination_name: entry['path'], destination_namespace: context.entity.group.full_path, - parent_id: context.entity.id + parent_id: context.entity.id, + migrate_projects: context.entity.migrate_projects } end end |