/app/controllers/import/gitlab_controller.rb

https://gitlab.com/sacuiu.andy/gitlab-ce · Ruby · 67 lines · 50 code · 17 blank · 0 comment · 5 complexity · 004292f22a0079f652fc8f3310902570 MD5 · raw file

  1. class Import::GitlabController < Import::BaseController
  2. before_action :verify_gitlab_import_enabled
  3. before_action :gitlab_auth, except: :callback
  4. rescue_from OAuth2::Error, with: :gitlab_unauthorized
  5. def callback
  6. session[:gitlab_access_token] = client.get_token(params[:code], callback_import_gitlab_url)
  7. redirect_to status_import_gitlab_url
  8. end
  9. def status
  10. @repos = client.projects
  11. @already_added_projects = current_user.created_projects.where(import_type: "gitlab")
  12. already_added_projects_names = @already_added_projects.pluck(:import_source)
  13. @repos = @repos.to_a.reject{ |repo| already_added_projects_names.include? repo["path_with_namespace"] }
  14. end
  15. def jobs
  16. jobs = current_user.created_projects.where(import_type: "gitlab").to_json(only: [:id, :import_status])
  17. render json: jobs
  18. end
  19. def create
  20. @repo_id = params[:repo_id].to_i
  21. repo = client.project(@repo_id)
  22. @project_name = repo["name"]
  23. repo_owner = repo["namespace"]["path"]
  24. repo_owner = current_user.username if repo_owner == client.user["username"]
  25. @target_namespace = params[:new_namespace].presence || repo_owner
  26. namespace = get_or_create_namespace || (render and return)
  27. @project = Gitlab::GitlabImport::ProjectCreator.new(repo, namespace, current_user, access_params).execute
  28. end
  29. private
  30. def client
  31. @client ||= Gitlab::GitlabImport::Client.new(session[:gitlab_access_token])
  32. end
  33. def verify_gitlab_import_enabled
  34. render_404 unless gitlab_import_enabled?
  35. end
  36. def gitlab_auth
  37. if session[:gitlab_access_token].blank?
  38. go_to_gitlab_for_permissions
  39. end
  40. end
  41. def go_to_gitlab_for_permissions
  42. redirect_to client.authorize_url(callback_import_gitlab_url)
  43. end
  44. def gitlab_unauthorized
  45. go_to_gitlab_for_permissions
  46. end
  47. def access_params
  48. { gitlab_access_token: session[:gitlab_access_token] }
  49. end
  50. end