diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-20 12:40:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-20 12:40:42 +0300 |
commit | ee664acb356f8123f4f6b00b73c1e1cf0866c7fb (patch) | |
tree | f8479f94a28f66654c6a4f6fb99bad6b4e86a40e /app/controllers/import | |
parent | 62f7d5c5b69180e82ae8196b7b429eeffc8e7b4f (diff) |
Add latest changes from gitlab-org/gitlab@15-5-stable-eev15.5.0-rc42
Diffstat (limited to 'app/controllers/import')
-rw-r--r-- | app/controllers/import/bulk_imports_controller.rb | 8 | ||||
-rw-r--r-- | app/controllers/import/github_controller.rb | 89 | ||||
-rw-r--r-- | app/controllers/import/github_groups_controller.rb | 57 |
3 files changed, 77 insertions, 77 deletions
diff --git a/app/controllers/import/bulk_imports_controller.rb b/app/controllers/import/bulk_imports_controller.rb index 893c0b6ac54..655fc7854fe 100644 --- a/app/controllers/import/bulk_imports_controller.rb +++ b/app/controllers/import/bulk_imports_controller.rb @@ -47,6 +47,8 @@ class Import::BulkImportsController < ApplicationController end def create + return render json: { success: false }, status: :unprocessable_entity unless valid_create_params? + responses = create_params.map do |entry| if entry[:destination_name] entry[:destination_slug] ||= entry[:destination_name] @@ -102,6 +104,10 @@ class Import::BulkImportsController < ApplicationController params.permit(bulk_import: bulk_import_params)[:bulk_import] end + def valid_create_params? + create_params.all? { _1[:source_type] == 'group_entity' } + end + def bulk_import_params %i[ source_type @@ -113,7 +119,7 @@ class Import::BulkImportsController < ApplicationController end def ensure_group_import_enabled - render_404 unless Feature.enabled?(:bulk_import) + render_404 unless ::BulkImports::Features.enabled? end def access_token_key diff --git a/app/controllers/import/github_controller.rb b/app/controllers/import/github_controller.rb index 8a3e6809736..92763e09ba3 100644 --- a/app/controllers/import/github_controller.rb +++ b/app/controllers/import/github_controller.rb @@ -5,14 +5,12 @@ class Import::GithubController < Import::BaseController include ImportHelper include ActionView::Helpers::SanitizeHelper + include Import::GithubOauth before_action :verify_import_enabled before_action :provider_auth, only: [:status, :realtime_changes, :create] before_action :expire_etag_cache, only: [:status, :create] - OAuthConfigMissingError = Class.new(StandardError) - - rescue_from OAuthConfigMissingError, with: :missing_oauth_config rescue_from Octokit::Unauthorized, with: :provider_unauthorized rescue_from Octokit::TooManyRequests, with: :provider_rate_limit rescue_from Gitlab::GithubImport::RateLimitError, with: :rate_limit_threshold_exceeded @@ -73,6 +71,17 @@ class Import::GithubController < Import::BaseController } end + def cancel + project = Project.imported_from(provider_name).find(params[:project_id]) + result = Import::Github::CancelProjectImportService.new(project, current_user).execute + + if result[:status] == :success + render json: serialized_imported_projects(result[:project]) + else + render json: { errors: result[:message] }, status: result[:http_status] + end + end + protected override :importable_repos @@ -104,7 +113,7 @@ class Import::GithubController < Import::BaseController end def permitted_import_params - [:repo_id, :new_name, :target_namespace] + [:repo_id, :new_name, :target_namespace, { optional_stages: {} }] end def serialized_imported_projects(projects = already_added_projects) @@ -143,58 +152,10 @@ class Import::GithubController < Import::BaseController @filter = @filter&.tr(' ', '')&.tr(':', '') end - def oauth_client - raise OAuthConfigMissingError unless oauth_config - - @oauth_client ||= ::OAuth2::Client.new( - oauth_config.app_id, - oauth_config.app_secret, - oauth_options.merge(ssl: { verify: oauth_config['verify_ssl'] }) - ) - end - - def oauth_config - @oauth_config ||= Gitlab::Auth::OAuth::Provider.config_for('github') - end - - def oauth_options - if oauth_config - oauth_config.dig('args', 'client_options').deep_symbolize_keys - else - OmniAuth::Strategies::GitHub.default_options[:client_options].symbolize_keys - end - end - - def authorize_url - state = SecureRandom.base64(64) - session[auth_state_key] = state - if Feature.enabled?(:remove_legacy_github_client) - oauth_client.auth_code.authorize_url( - redirect_uri: callback_import_url, - scope: 'repo, user, user:email', - state: state - ) - else - client.authorize_url(callback_import_url, state) - end - end - - def get_token(code) - if Feature.enabled?(:remove_legacy_github_client) - oauth_client.auth_code.get_token(code).token - else - client.get_token(code) - end - end - def verify_import_enabled render_404 unless import_enabled? end - def go_to_provider_for_permissions - redirect_to authorize_url - end - def import_enabled? __send__("#{provider_name}_import_enabled?") # rubocop:disable GitlabSecurity/PublicSend end @@ -211,10 +172,6 @@ class Import::GithubController < Import::BaseController public_send("status_import_#{provider_name}_url", extra_import_params.merge({ namespace_id: params[:namespace_id].presence })) # rubocop:disable GitlabSecurity/PublicSend end - def callback_import_url - public_send("users_import_#{provider_name}_callback_url", extra_import_params.merge({ namespace_id: params[:namespace_id] })) # rubocop:disable GitlabSecurity/PublicSend - end - def provider_unauthorized session[access_token_key] = nil redirect_to new_import_url, @@ -228,12 +185,6 @@ class Import::GithubController < Import::BaseController alert: _("GitHub API rate limit exceeded. Try again after %{reset_time}") % { reset_time: reset_time } end - def missing_oauth_config - session[access_token_key] = nil - redirect_to new_import_url, - alert: _('Missing OAuth configuration for GitHub.') - end - def auth_state_key :"#{provider_name}_auth_state_key" end @@ -252,24 +203,10 @@ class Import::GithubController < Import::BaseController end # rubocop: enable CodeReuse/ActiveRecord - def provider_auth - if !ci_cd_only? && session[access_token_key].blank? - go_to_provider_for_permissions - end - end - - def ci_cd_only? - %w[1 true].include?(params[:ci_cd_only]) - end - def client_options { wait_for_rate_limit_reset: false } end - def extra_import_params - {} - end - def rate_limit_threshold_exceeded head :too_many_requests end diff --git a/app/controllers/import/github_groups_controller.rb b/app/controllers/import/github_groups_controller.rb new file mode 100644 index 00000000000..6c0773bcfb3 --- /dev/null +++ b/app/controllers/import/github_groups_controller.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +module Import + class GithubGroupsController < ApplicationController + include Import::GithubOauth + + before_action :provider_auth, only: [:status] + feature_category :importers + + PAGE_LENGTH = 25 + + def status + respond_to do |format| + format.json do + render json: { provider_groups: serialized_provider_groups } + end + end + end + + private + + def serialized_provider_groups + Import::GithubOrgSerializer.new.represent(importable_orgs) + end + + def importable_orgs + client_orgs.to_a + end + + def client_orgs + @client_orgs ||= client.octokit.organizations(nil, pagination_options) + end + + def client + @client ||= Gitlab::GithubImport::Client.new(session[access_token_key]) + end + + def pagination_options + { + page: [1, params[:page].to_i].max, + per_page: PAGE_LENGTH + } + end + + def auth_state_key + :"#{provider_name}_auth_state_key" + end + + def access_token_key + :"#{provider_name}_access_token" + end + + def provider_name + :github + end + end +end |