Class: Gitlab::BitbucketImport::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/bitbucket_import/client.rb

Defined Under Namespace

Classes: Unauthorized

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil, access_token_secret = nil) ⇒ Client

Returns a new instance of Client


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/bitbucket_import/client.rb', line 19

def initialize(access_token = nil, access_token_secret = nil)
  @consumer = ::OAuth::Consumer.new(
    config.app_id,
    config.app_secret,
    bitbucket_options
  )

  if access_token && access_token_secret
    @api = ::OAuth::AccessToken.new(@consumer, access_token, access_token_secret)
  end
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api


6
7
8
# File 'lib/gitlab/bitbucket_import/client.rb', line 6

def api
  @api
end

#consumerObject (readonly)

Returns the value of attribute consumer


6
7
8
# File 'lib/gitlab/bitbucket_import/client.rb', line 6

def consumer
  @consumer
end

Class Method Details

.from_project(project) ⇒ Object


8
9
10
11
12
13
14
15
16
17
# File 'lib/gitlab/bitbucket_import/client.rb', line 8

def self.from_project(project)
  import_data_credentials = project.import_data.credentials if project.import_data
  if import_data_credentials && import_data_credentials[:bb_session]
    token = import_data_credentials[:bb_session][:bitbucket_access_token]
    token_secret = import_data_credentials[:bb_session][:bitbucket_access_token_secret]
    new(token, token_secret)
  else
    raise Projects::ImportService::Error, "Unable to find project import data credentials for project ID: #{project.id}"
  end
end

Instance Method Details

#add_deploy_key(project_identifier, key) ⇒ Object


98
99
100
101
102
103
# File 'lib/gitlab/bitbucket_import/client.rb', line 98

def add_deploy_key(project_identifier, key)
  deploy_key = find_deploy_key(project_identifier, key)
  return if deploy_key

  JSON.parse(api.post("/api/1.0/repositories/#{project_identifier}/deploy-keys", key: key, label: "GitLab import key").body)
end

#authorize_url(request_token, redirect_uri) ⇒ Object


41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/bitbucket_import/client.rb', line 41

def authorize_url(request_token, redirect_uri)
  request_token = ::OAuth::RequestToken.from_hash(consumer, request_token) if request_token.is_a?(Hash)

  if request_token.callback_confirmed?
    request_token.authorize_url
  else
    request_token.authorize_url(oauth_callback: redirect_uri)
  end
end

#delete_deploy_key(project_identifier, key) ⇒ Object


105
106
107
108
109
110
# File 'lib/gitlab/bitbucket_import/client.rb', line 105

def delete_deploy_key(project_identifier, key)
  deploy_key = find_deploy_key(project_identifier, key)
  return unless deploy_key

  api.delete("/api/1.0/repositories/#{project_identifier}/deploy-keys/#{deploy_key["pk"]}").code == "204"
end

#find_deploy_key(project_identifier, key) ⇒ Object


92
93
94
95
96
# File 'lib/gitlab/bitbucket_import/client.rb', line 92

def find_deploy_key(project_identifier, key)
  JSON.parse(get("/api/1.0/repositories/#{project_identifier}/deploy-keys").body).find do |deploy_key|
    deploy_key["key"].chomp == key.chomp
  end
end

#get_token(request_token, oauth_verifier, redirect_uri) ⇒ Object


51
52
53
54
55
56
57
58
59
# File 'lib/gitlab/bitbucket_import/client.rb', line 51

def get_token(request_token, oauth_verifier, redirect_uri)
  request_token = ::OAuth::RequestToken.from_hash(consumer, request_token) if request_token.is_a?(Hash)

  if request_token.callback_confirmed?
    request_token.get_access_token(oauth_verifier: oauth_verifier)
  else
    request_token.get_access_token(oauth_callback: redirect_uri)
  end
end

#incompatible_projectsObject


116
117
118
# File 'lib/gitlab/bitbucket_import/client.rb', line 116

def incompatible_projects
  JSON.parse(get("/api/1.0/user/repositories").body).reject { |repo| repo["scm"] == "git" }
end

#issue_comments(project_identifier, issue_id) ⇒ Object


83
84
85
86
# File 'lib/gitlab/bitbucket_import/client.rb', line 83

def issue_comments(project_identifier, issue_id)
  comments = JSON.parse(get("/api/1.0/repositories/#{project_identifier}/issues/#{issue_id}/comments").body)
  comments.sort_by { |comment| comment["utc_created_on"] }
end

#issues(project_identifier) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gitlab/bitbucket_import/client.rb', line 65

def issues(project_identifier)
  all_issues = []
  offset = 0
  per_page = 50 # Maximum number allowed by Bitbucket
  index = 0

  begin
    issues = JSON.parse(get(issue_api_endpoint(project_identifier, per_page, offset)).body)
    # Find out how many total issues are present
    total = issues["count"] if index == 0
    all_issues.concat(issues["issues"])
    offset += issues["issues"].count
    index += 1
  end while all_issues.count < total

  all_issues
end

#project(project_identifier) ⇒ Object


88
89
90
# File 'lib/gitlab/bitbucket_import/client.rb', line 88

def project(project_identifier)
  JSON.parse(get("/api/1.0/repositories/#{project_identifier}").body)
end

#projectsObject


112
113
114
# File 'lib/gitlab/bitbucket_import/client.rb', line 112

def projects
  JSON.parse(get("/api/1.0/user/repositories").body).select { |repo| repo["scm"] == "git" }
end

#request_token(redirect_uri) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'lib/gitlab/bitbucket_import/client.rb', line 31

def request_token(redirect_uri)
  request_token = consumer.get_request_token(oauth_callback: redirect_uri)

  {
    oauth_token:              request_token.token,
    oauth_token_secret:       request_token.secret,
    oauth_callback_confirmed: request_token.callback_confirmed?.to_s
  }
end

#userObject


61
62
63
# File 'lib/gitlab/bitbucket_import/client.rb', line 61

def user
  JSON.parse(get("/api/1.0/user").body)
end