github_controller.rb 3.64 KB
Newer Older
1
2
# frozen_string_literal: true

Valery Sizov's avatar
Valery Sizov committed
3
class Import::GithubController < Import::BaseController
4
5
  before_action :verify_import_enabled
  before_action :provider_auth, only: [:status, :jobs, :create]
Valery Sizov's avatar
Valery Sizov committed
6

7
  rescue_from Octokit::Unauthorized, with: :provider_unauthorized
8
9

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

Valery Sizov's avatar
Valery Sizov committed
17
  def callback
18
    session[access_token_key] = client.get_token(params[:code])
19
    redirect_to status_import_url
Valery Sizov's avatar
Valery Sizov committed
20
21
  end

22
  def personal_access_token
23
    session[access_token_key] = params[:personal_access_token]&.strip
24
    redirect_to status_import_url
25
26
  end

27
  # rubocop: disable CodeReuse/ActiveRecord
Valery Sizov's avatar
Valery Sizov committed
28
  def status
Valery Sizov's avatar
Valery Sizov committed
29
    @repos = client.repos
30
    @already_added_projects = find_already_added_projects(provider)
Valery Sizov's avatar
Valery Sizov committed
31
32
    already_added_projects_names = @already_added_projects.pluck(:import_source)

33
    @repos.reject! { |repo| already_added_projects_names.include? repo.full_name }
Valery Sizov's avatar
Valery Sizov committed
34
  end
35
  # rubocop: enable CodeReuse/ActiveRecord
Valery Sizov's avatar
Valery Sizov committed
36

37
  def jobs
38
    render json: find_jobs(provider)
39
40
  end

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

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

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

  private

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

68
69
  def verify_import_enabled
    render_404 unless import_enabled?
Douwe Maan's avatar
Douwe Maan committed
70
71
  end

72
73
74
75
76
  def go_to_provider_for_permissions
    redirect_to client.authorize_url(callback_import_url)
  end

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

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

84
  def status_import_url
85
    public_send("status_import_#{provider}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
Valery Sizov's avatar
Valery Sizov committed
86
  end
87

88
  def callback_import_url
89
    public_send("callback_import_#{provider}_url", extra_import_params) # rubocop:disable GitlabSecurity/PublicSend
90
91
92
  end

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

98
99
100
101
  def access_token_key
    :"#{provider}_access_token"
  end

102
  def access_params
103
    { github_access_token: session[access_token_key] }
104
105
  end

106
  # The following methods are overridden in subclasses
107
108
109
110
  def provider
    :github
  end

111
  # rubocop: disable CodeReuse/ActiveRecord
112
113
114
  def logged_in_with_provider?
    current_user.identities.exists?(provider: provider)
  end
115
  # rubocop: enable CodeReuse/ActiveRecord
116
117

  def provider_auth
118
    if session[access_token_key].blank?
119
120
121
122
123
124
      go_to_provider_for_permissions
    end
  end

  def client_options
    {}
125
  end
126
127
128
129
130
131
132
133

  def extra_project_attrs
    {}
  end

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