BigW Consortium Gitlab

github_controller.rb 2.89 KB
Newer Older
1
class Import::GithubController < Import::BaseController
2 3
  before_action :verify_import_enabled
  before_action :provider_auth, only: [:status, :jobs, :create]
Valery Sizov committed
4

5
  rescue_from Octokit::Unauthorized, with: :provider_unauthorized
6 7

  def new
8 9
    if logged_in_with_provider?
      go_to_provider_for_permissions
10
    elsif session[access_token_key]
11
      redirect_to status_import_url
12 13 14
    end
  end

Valery Sizov committed
15
  def callback
16
    session[access_token_key] = client.get_token(params[:code])
17
    redirect_to status_import_url
Valery Sizov committed
18 19
  end

20
  def personal_access_token
21
    session[access_token_key] = params[:personal_access_token]
22
    redirect_to status_import_url
23 24
  end

Valery Sizov committed
25
  def status
26
    @repos = client.repos
27
    @already_added_projects = current_user.created_projects.where(import_type: provider)
Valery Sizov committed
28 29
    already_added_projects_names = @already_added_projects.pluck(:import_source)

30
    @repos.reject! { |repo| already_added_projects_names.include? repo.full_name }
Valery Sizov committed
31 32
  end

33
  def jobs
34
    jobs = current_user.created_projects.where(import_type: provider).to_json(only: [:id, :import_status])
35 36 37
    render json: jobs
  end

Valery Sizov committed
38 39
  def create
    @repo_id = params[:repo_id].to_i
40
    repo = client.repo(@repo_id)
41 42 43
    @project_name = params[:new_name].presence || repo.name
    namespace_path = params[:target_namespace].presence || current_user.namespace_path
    @target_namespace = find_or_create_namespace(namespace_path, current_user.namespace_path)
Valery Sizov committed
44

45 46
    if can?(current_user, :create_projects, @target_namespace)
      @project = Gitlab::GithubImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, access_params, type: provider).execute
47 48 49
    else
      render 'unauthorized'
    end
Valery Sizov committed
50 51 52 53 54
  end

  private

  def client
55
    @client ||= Gitlab::GithubImport::Client.new(session[access_token_key], client_options)
Valery Sizov committed
56 57
  end

58 59
  def verify_import_enabled
    render_404 unless import_enabled?
60 61
  end

62 63 64 65 66 67
  def go_to_provider_for_permissions
    redirect_to client.authorize_url(callback_import_url)
  end

  def import_enabled?
    __send__("#{provider}_import_enabled?")
Valery Sizov committed
68 69
  end

70 71
  def new_import_url
    public_send("new_import_#{provider}_url")
Valery Sizov committed
72 73
  end

74 75
  def status_import_url
    public_send("status_import_#{provider}_url")
Valery Sizov committed
76
  end
77

78 79 80 81 82
  def callback_import_url
    public_send("callback_import_#{provider}_url")
  end

  def provider_unauthorized
83
    session[access_token_key] = nil
84
    redirect_to new_import_url,
85
      alert: "Access denied to your #{Gitlab::ImportSources.title(provider.to_s)} account."
86
  end
87

88 89 90 91
  def access_token_key
    :"#{provider}_access_token"
  end

92
  def access_params
93
    { github_access_token: session[access_token_key] }
94 95 96 97 98 99 100 101 102 103 104 105
  end

  # The following methods are overriden in subclasses
  def provider
    :github
  end

  def logged_in_with_provider?
    current_user.identities.exists?(provider: provider)
  end

  def provider_auth
106
    if session[access_token_key].blank?
107 108 109 110 111 112
      go_to_provider_for_permissions
    end
  end

  def client_options
    {}
113
  end
Valery Sizov committed
114
end