Read about our upcoming Code of Conduct on this issue

github_controller.rb 3.43 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1
class Import::GithubController < Import::BaseController
2
3
  before_action :verify_import_enabled
  before_action :provider_auth, only: [:status, :jobs, :create]
Valery Sizov's avatar
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's avatar
Valery Sizov committed
15
  def callback
16
    session[access_token_key] = client.get_token(params[:code])
17
    redirect_to status_import_url
Valery Sizov's avatar
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's avatar
Valery Sizov committed
25
  def status
Valery Sizov's avatar
Valery Sizov committed
26
    @repos = client.repos
27
    @already_added_projects = find_already_added_projects(provider)
Valery Sizov's avatar
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's avatar
Valery Sizov committed
31
32
  end

33
  def jobs
34
    render json: find_jobs(provider)
35
36
  end

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

43
    if can?(current_user, :create_projects, target_namespace)
44
45
46
      project = Gitlab::LegacyGithubImport::ProjectCreator
                  .new(repo, project_name, target_namespace, current_user, access_params, type: provider)
                  .execute(extra_project_attrs)
47
48
49
50
51
52

      if project.persisted?
        render json: ProjectSerializer.new.represent(project)
      else
        render json: { errors: project.errors.full_messages }, status: :unprocessable_entity
      end
53
    else
54
      render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
55
    end
Valery Sizov's avatar
Valery Sizov committed
56
57
58
59
60
  end

  private

  def client
61
    @client ||= Gitlab::LegacyGithubImport::Client.new(session[access_token_key], client_options)
Valery Sizov's avatar
Valery Sizov committed
62
63
  end

64
65
  def verify_import_enabled
    render_404 unless import_enabled?
Douwe Maan's avatar
Douwe Maan committed
66
67
  end

68
69
70
71
72
  def go_to_provider_for_permissions
    redirect_to client.authorize_url(callback_import_url)
  end

  def import_enabled?
73
    __send__("#{provider}_import_enabled?") # rubocop:disable GitlabSecurity/PublicSend
Valery Sizov's avatar
Valery Sizov committed
74
75
  end

76
  def new_import_url
77
    public_send("new_import_#{provider}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
Valery Sizov's avatar
Valery Sizov committed
78
79
  end

80
  def status_import_url
81
    public_send("status_import_#{provider}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
Valery Sizov's avatar
Valery Sizov committed
82
  end
83

84
  def callback_import_url
85
    public_send("callback_import_#{provider}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
86
87
88
  end

  def provider_unauthorized
89
    session[access_token_key] = nil
90
    redirect_to new_import_url,
Rémy Coutable's avatar
Rémy Coutable committed
91
      alert: "Access denied to your #{Gitlab::ImportSources.title(provider.to_s)} account."
92
  end
93

94
95
96
97
  def access_token_key
    :"#{provider}_access_token"
  end

98
  def access_params
99
    { github_access_token: session[access_token_key] }
100
101
102
103
104
105
106
107
108
109
110
111
  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
112
    if session[access_token_key].blank?
113
114
115
116
117
118
      go_to_provider_for_permissions
    end
  end

  def client_options
    {}
119
  end
120
121
122
123
124
125
126
127

  def extra_project_attrs
    {}
  end

  def extra_import_params
    {}
  end
Valery Sizov's avatar
Valery Sizov committed
128
end