Class: HybridPlatformsConductor::Bitbucket::BitbucketApi

Inherits:
Object
  • Object
show all
Includes:
LoggerHelpers
Defined in:
lib/hybrid_platforms_conductor/bitbucket.rb

Overview

Provide an API to Bitbucket

Constant Summary

Constants included from LoggerHelpers

LoggerHelpers::LEVELS_MODIFIERS, LoggerHelpers::LEVELS_TO_STDERR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LoggerHelpers

#err, #init_loggers, #log_component=, #log_debug?, #log_level=, #out, #section, #set_loggers_format, #stderr_device, #stderr_device=, #stderr_displayed?, #stdout_device, #stdout_device=, #stdout_displayed?, #stdouts_to_s, #with_progress_bar

Constructor Details

#initialize(bitbucket_url, bitbucket_user_name, bitbucket_password, logger: Logger.new($stdout), logger_stderr: Logger.new($stderr)) ⇒ BitbucketApi

Constructor

Parameters
  • bitbucket_url (String): The Bitbucket URL

  • bitbucket_user_name (String): Bitbucket user name to be used when querying the API

  • bitbucket_password (SecretString): Bitbucket password to be used when querying the API

  • logger (Logger): Logger to be used [default = Logger.new(STDOUT)]

  • logger_stderr (Logger): Logger to be used for stderr [default = Logger.new(STDERR)]



83
84
85
86
87
88
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 83

def initialize(bitbucket_url, bitbucket_user_name, bitbucket_password, logger: Logger.new($stdout), logger_stderr: Logger.new($stderr))
  init_loggers(logger, logger_stderr)
  @bitbucket_url = bitbucket_url
  @bitbucket_user_name = bitbucket_user_name
  @bitbucket_password = bitbucket_password
end

Instance Attribute Details

#bitbucket_urlObject (readonly)

The Bitbucket URL String



73
74
75
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 73

def bitbucket_url
  @bitbucket_url
end

Instance Method Details

#branch_permissions(project, repo) ⇒ Object

Get the branch permissions of a given repository

Parameters
  • project (String): Project name

  • repo (String): Repository name

Result
  • Object: Corresponding JSON



130
131
132
133
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 130

def branch_permissions(project, repo)
  # Put 3 retries here as the Bitbucket installation has a very unstable API 2.0 and often returns random 401 errors.
  get_api("projects/#{project}/repos/#{repo}/restrictions", api_domain: 'branch-permissions', api_version: '2.0', retries: 3)
end

#default_reviewers(project, repo) ⇒ Object

Get the default reviewers of a given repository

Parameters
  • project (String): Project name

  • repo (String): Repository name

Result
  • Object: Corresponding JSON



119
120
121
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 119

def default_reviewers(project, repo)
  get_api("projects/#{project}/repos/#{repo}/conditions", api_domain: 'default-reviewers')
end

#get_api(path, api_domain: 'api', api_version: '1.0', retries: 0) ⇒ Object

Issue an HTTP get on the API. Handle authentication.

Parameters
  • path (String): API path to access

  • api_domain (String): API domain to access [default: ‘api’]

  • api_version (String): API version to access [default: ‘1.0’]

  • retries (Integer): Number of retries in case of failures [default: 0]

Result
  • Object: Returned JSON



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 145

def get_api(path, api_domain: 'api', api_version: '1.0', retries: 0)
  api_url = "#{@bitbucket_url}/rest/#{api_domain}/#{api_version}/#{path}"
  log_debug "Call Bitbucket API #{@bitbucket_user_name}@#{api_url}..."
  http_response = nil
  loop do
    begin
      http_response = URI.parse(api_url).open(http_basic_authentication: [@bitbucket_user_name, @bitbucket_password&.to_unprotected])
    rescue
      raise if retries.zero?

      log_warn "Got error #{$ERROR_INFO} on #{@bitbucket_user_name}@#{api_url}. Will retry #{retries} times..."
      retries -= 1
      sleep 1
    end
    break unless http_response.nil?
  end
  JSON.parse(http_response.read)
end

#repos(project) ⇒ Object

Get the repositories of a given project. Limit to 1000 results max.

Parameters
  • project (String): Project name

Result
  • Object: Corresponding JSON



97
98
99
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 97

def repos(project)
  get_api("projects/#{project}/repos?limit=1000")
end

#settings_pr(project, repo) ⇒ Object

Get the PR settings of a given repository

Parameters
  • project (String): Project name

  • repo (String): Repository name

Result
  • Object: Corresponding JSON



108
109
110
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 108

def settings_pr(project, repo)
  get_api("projects/#{project}/repos/#{repo}/settings/pull-requests")
end