BigW Consortium Gitlab

bitbucket_controller.rb 3.17 KB
Newer Older
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 committed
4

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

  def callback
9
    response = client.auth_code.get_token(params[:code], redirect_uri: callback_import_bitbucket_url)
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 committed
15 16 17 18 19

    redirect_to status_import_bitbucket_url
  end

  def status
20 21
    bitbucket_client = Bitbucket::Client.new(credentials)
    repos = bitbucket_client.repos
22

23
    @repos, @incompatible_repos = repos.partition { |repo| repo.valid? }
24

25
    @already_added_projects = current_user.created_projects.where(import_type: 'bitbucket')
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 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 committed
35 36 37
  end

  def create
38
    bitbucket_client = Bitbucket::Client.new(credentials)
39

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

45
    repo_owner = repo.owner
46
    repo_owner = current_user.username if repo_owner == bitbucket_client.user.username
47
    namespace_path = params[:new_namespace].presence || repo_owner
48

49
    @target_namespace = find_or_create_namespace(namespace_path, current_user)
Douwe Maan committed
50

51
    if current_user.can?(:create_projects, @target_namespace)
52 53 54
      # 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
55
      @project = Gitlab::BitbucketImport::ProjectCreator.new(repo, @project_name, @target_namespace, current_user, credentials).execute
56 57 58
    else
      render 'unauthorized'
    end
Douwe Maan committed
59 60 61 62 63
  end

  private

  def client
64 65 66 67
    @client ||= OAuth2::Client.new(provider.app_id, provider.app_secret, options)
  end

  def provider
68
    Gitlab::OAuth::Provider.config_for('bitbucket')
69 70 71 72
  end

  def options
    OmniAuth::Strategies::Bitbucket.default_options[:client_options].deep_symbolize_keys
Douwe Maan committed
73 74
  end

75
  def verify_bitbucket_import_enabled
76
    render_404 unless bitbucket_import_enabled?
77 78
  end

Douwe Maan committed
79
  def bitbucket_auth
80
    go_to_bitbucket_for_permissions if session[:bitbucket_token].blank?
Douwe Maan committed
81 82 83
  end

  def go_to_bitbucket_for_permissions
84
    redirect_to client.auth_code.authorize_url(redirect_uri: callback_import_bitbucket_url)
Douwe Maan committed
85 86 87 88 89
  end

  def bitbucket_unauthorized
    go_to_bitbucket_for_permissions
  end
90

91
  def credentials
92
    {
93 94 95 96
      token: session[:bitbucket_token],
      expires_at: session[:bitbucket_expires_at],
      expires_in: session[:bitbucket_expires_in],
      refresh_token: session[:bitbucket_refresh_token]
97 98
    }
  end
Douwe Maan committed
99
end