PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/import/gitlab_controller.rb

https://gitlab.com/artofhuman/gitlab-ce
Ruby | 76 lines | 55 code | 18 blank | 3 comment | 3 complexity | 510fda22429bf728e719dc193e820459 MD5 | raw file
  1. # frozen_string_literal: true
  2. class Import::GitlabController < Import::BaseController
  3. MAX_PROJECT_PAGES = 15
  4. PER_PAGE_PROJECTS = 100
  5. before_action :verify_gitlab_import_enabled
  6. before_action :gitlab_auth, except: :callback
  7. rescue_from OAuth2::Error, with: :gitlab_unauthorized
  8. def callback
  9. session[:gitlab_access_token] = client.get_token(params[:code], callback_import_gitlab_url)
  10. redirect_to status_import_gitlab_url
  11. end
  12. # rubocop: disable CodeReuse/ActiveRecord
  13. def status
  14. @repos = client.projects(starting_page: 1, page_limit: MAX_PROJECT_PAGES, per_page: PER_PAGE_PROJECTS)
  15. @already_added_projects = find_already_added_projects('gitlab')
  16. already_added_projects_names = @already_added_projects.pluck(:import_source)
  17. @repos = @repos.to_a.reject { |repo| already_added_projects_names.include? repo["path_with_namespace"] }
  18. end
  19. # rubocop: enable CodeReuse/ActiveRecord
  20. def jobs
  21. render json: find_jobs('gitlab')
  22. end
  23. def create
  24. repo = client.project(params[:repo_id].to_i)
  25. target_namespace = find_or_create_namespace(repo['namespace']['path'], client.user['username'])
  26. if current_user.can?(:create_projects, target_namespace)
  27. project = Gitlab::GitlabImport::ProjectCreator.new(repo, target_namespace, current_user, access_params).execute
  28. if project.persisted?
  29. render json: ProjectSerializer.new.represent(project)
  30. else
  31. render json: { errors: project_save_error(project) }, status: :unprocessable_entity
  32. end
  33. else
  34. render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
  35. end
  36. end
  37. private
  38. def client
  39. @client ||= Gitlab::GitlabImport::Client.new(session[:gitlab_access_token])
  40. end
  41. def verify_gitlab_import_enabled
  42. render_404 unless gitlab_import_enabled?
  43. end
  44. def gitlab_auth
  45. if session[:gitlab_access_token].blank?
  46. go_to_gitlab_for_permissions
  47. end
  48. end
  49. def go_to_gitlab_for_permissions
  50. redirect_to client.authorize_url(callback_import_gitlab_url)
  51. end
  52. def gitlab_unauthorized
  53. go_to_gitlab_for_permissions
  54. end
  55. def access_params
  56. { gitlab_access_token: session[:gitlab_access_token] }
  57. end
  58. end