PageRenderTime 73ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/import/gitlab_controller.rb

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