Class: BitbucketServer::Connection

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper, RetryWithDelay
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,
  URI::InvalidURIError,
  Gitlab::HTTP::BlockedUrlError
].freeze
ConnectionError =
Class.new(StandardError)

Constants included from RetryWithDelay

RetryWithDelay::MAXIMUM_DELAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RetryWithDelay

#retry_with_delay

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



27
28
29
30
31
32
# File 'lib/bitbucket_server/connection.rb', line 27

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.



23
24
25
# File 'lib/bitbucket_server/connection.rb', line 23

def api_version
  @api_version
end

#base_uriObject (readonly)

Returns the value of attribute base_uri.



23
24
25
# File 'lib/bitbucket_server/connection.rb', line 23

def base_uri
  @base_uri
end

#tokenObject (readonly)

Returns the value of attribute token.



23
24
25
# File 'lib/bitbucket_server/connection.rb', line 23

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



23
24
25
# File 'lib/bitbucket_server/connection.rb', line 23

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
# File 'lib/bitbucket_server/connection.rb', line 62

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

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

  check_errors!(response)

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

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



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

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

  check_errors!(response)

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

#post(path, body) ⇒ Object



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

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

  check_errors!(response)

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