Class: BitbucketServer::Connection

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
lib/bitbucket_server/connection.rb

Constant Summary collapse

DEFAULT_API_VERSION =
'1.0'
SEPARATOR =
'/'
NETWORK_ERRORS =
[
  SocketError,
  OpenSSL::SSL::SSLError,
  Errno::ECONNRESET,
  Errno::ECONNREFUSED,
  Errno::EHOSTUNREACH,
  Net::OpenTimeout,
  Net::ReadTimeout,
  Gitlab::HTTP::BlockedUrlError
].freeze
ConnectionError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.


25
26
27
28
29
30
# File 'lib/bitbucket_server/connection.rb', line 25

def initialize(options = {})
  @api_version = options.fetch(:api_version, DEFAULT_API_VERSION)
  @base_uri = options[:base_uri]
  @username = options[:user]
  @token = options[:password]
end

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version


21
22
23
# File 'lib/bitbucket_server/connection.rb', line 21

def api_version
  @api_version
end

#base_uriObject (readonly)

Returns the value of attribute base_uri


21
22
23
# File 'lib/bitbucket_server/connection.rb', line 21

def base_uri
  @base_uri
end

#tokenObject (readonly)

Returns the value of attribute token


21
22
23
# File 'lib/bitbucket_server/connection.rb', line 21

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username


21
22
23
# File 'lib/bitbucket_server/connection.rb', line 21

def username
  @username
end

Instance Method Details

#delete(resource, path, body) ⇒ Object

We need to support two different APIs for deletion:

/rest/api/1.0/projects/projectKey/repos/repositorySlug/branches/default /rest/branch-utils/1.0/projects/projectKey/repos/repositorySlug/branches


62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bitbucket_server/connection.rb', line 62

def delete(resource, path, body)
  url = delete_url(resource, path)

  response = Gitlab::HTTP.delete(url,
                                 basic_auth: auth,
                                 headers: post_headers,
                                 body: body)

  check_errors!(response)

  response.parsed_response
rescue *NETWORK_ERRORS => e
  raise ConnectionError, e
end

#get(path, extra_query = {}) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bitbucket_server/connection.rb', line 32

def get(path, extra_query = {})
  response = Gitlab::HTTP.get(build_url(path),
                              basic_auth: auth,
                              headers: accept_headers,
                              query: extra_query)

  check_errors!(response)

  response.parsed_response
rescue *NETWORK_ERRORS => e
  raise ConnectionError, e
end

#post(path, body) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bitbucket_server/connection.rb', line 45

def post(path, body)
  response = Gitlab::HTTP.post(build_url(path),
                               basic_auth: auth,
                               headers: post_headers,
                               body: body)

  check_errors!(response)

  response.parsed_response
rescue *NETWORK_ERRORS => e
  raise ConnectionError, e
end