bitbucket_controller.rb 3.47 KB
Newer Older
Douwe Maan's avatar
Douwe Maan committed
1
class Import::BitbucketController < Import::BaseController
2 3
  before_action :verify_bitbucket_import_enabled
  before_action :bitbucket_auth, except: :callback
Douwe Maan's avatar
Douwe Maan committed
4

Valery Sizov's avatar
Valery Sizov committed
5
  rescue_from OAuth2::Error, with: :bitbucket_unauthorized
6
  rescue_from Bitbucket::Error::Unauthorized, with: :bitbucket_unauthorized
Douwe Maan's avatar
Douwe Maan committed
7 8

  def callback
9
    response = client.auth_code.get_token(params[:code], redirect_uri: callback_import_bitbucket_url)
Douwe Maan's avatar
Douwe Maan committed
10

11 12 13 14
    session[:bitbucket_token]         = response.token
    session[:bitbucket_expires_at]    = response.expires_at
    session[:bitbucket_expires_in]    = response.expires_in
    session[:bitbucket_refresh_token] = response.refresh_token
Douwe Maan's avatar
Douwe Maan committed
15 16 17 18 19

    redirect_to status_import_bitbucket_url
  end

  def status
Stan Hu's avatar
Stan Hu committed
20 21
    bitbucket_client = Bitbucket::Client.new(credentials)
    repos = bitbucket_client.repos
22

Stan Hu's avatar
Stan Hu committed
23
    @repos, @incompatible_repos = repos.partition { |repo| repo.valid? }
24

25
    @already_added_projects = current_user.created_projects.where(import_type: 'bitbucket')
Douwe Maan's avatar
Douwe Maan committed
26 27
    already_added_projects_names = @already_added_projects.pluck(:import_source)

28
    @repos.to_a.reject! { |repo| already_added_projects_names.include?(repo.full_name) }
Douwe Maan's avatar
Douwe Maan committed
29 30 31
  end

  def jobs
32 33 34
    render json: current_user.created_projects
                             .where(import_type: 'bitbucket')
                             .to_json(only: [:id, :import_status])
Douwe Maan's avatar
Douwe Maan committed
35 36 37
  end

  def create
Stan Hu's avatar
Stan Hu committed
38
    bitbucket_client = Bitbucket::Client.new(credentials)
39

40 41
    repo_id = params[:repo_id].to_s
    name = repo_id.gsub('___', '/')
Stan Hu's avatar
Stan Hu committed
42
    repo = bitbucket_client.repo(name)
43
    project_name = params[:new_name].presence || repo.name
Douwe Maan's avatar
Douwe Maan committed
44

45
    repo_owner = repo.owner
Stan Hu's avatar
Stan Hu committed
46
    repo_owner = current_user.username if repo_owner == bitbucket_client.user.username
47
    namespace_path = params[:new_namespace].presence || repo_owner
48
    target_namespace = find_or_create_namespace(namespace_path, current_user)
49

50
    if current_user.can?(:create_projects, target_namespace)
51 52 53
      # The token in a session can be expired, we need to get most recent one because
      # Bitbucket::Connection class refreshes it.
      session[:bitbucket_token] = bitbucket_client.connection.token
54 55 56 57 58 59 60 61

      project = Gitlab::BitbucketImport::ProjectCreator.new(repo, project_name, target_namespace, current_user, credentials).execute

      if project.persisted?
        render json: ProjectSerializer.new.represent(project)
      else
        render json: { errors: project.errors.full_messages }, status: :unprocessable_entity
      end
62
    else
63
      render json: { errors: 'This namespace has already been taken! Please choose another one.' }, status: :unprocessable_entity
64
    end
Douwe Maan's avatar
Douwe Maan committed
65 66 67 68 69
  end

  private

  def client
70 71 72 73
    @client ||= OAuth2::Client.new(provider.app_id, provider.app_secret, options)
  end

  def provider
74
    Gitlab::Auth::OAuth::Provider.config_for('bitbucket')
75 76 77 78
  end

  def options
    OmniAuth::Strategies::Bitbucket.default_options[:client_options].deep_symbolize_keys
Douwe Maan's avatar
Douwe Maan committed
79 80
  end

81
  def verify_bitbucket_import_enabled
82
    render_404 unless bitbucket_import_enabled?
83 84
  end

Douwe Maan's avatar
Douwe Maan committed
85
  def bitbucket_auth
86
    go_to_bitbucket_for_permissions if session[:bitbucket_token].blank?
Douwe Maan's avatar
Douwe Maan committed
87 88 89
  end

  def go_to_bitbucket_for_permissions
90
    redirect_to client.auth_code.authorize_url(redirect_uri: callback_import_bitbucket_url)
Douwe Maan's avatar
Douwe Maan committed
91 92 93 94 95
  end

  def bitbucket_unauthorized
    go_to_bitbucket_for_permissions
  end
96

97
  def credentials
98
    {
99 100 101 102
      token: session[:bitbucket_token],
      expires_at: session[:bitbucket_expires_at],
      expires_in: session[:bitbucket_expires_in],
      refresh_token: session[:bitbucket_refresh_token]
103 104
    }
  end
Douwe Maan's avatar
Douwe Maan committed
105
end