Class: HybridPlatformsConductor::Bitbucket
- Inherits:
-
Object
- Object
- HybridPlatformsConductor::Bitbucket
- Includes:
- LoggerHelpers
- Defined in:
- lib/hybrid_platforms_conductor/bitbucket.rb
Overview
Object used to access Bitbucket API
Constant Summary
Constants included from LoggerHelpers
LoggerHelpers::LEVELS_MODIFIERS, LoggerHelpers::LEVELS_TO_STDERR
Instance Attribute Summary collapse
-
#bitbucket_url ⇒ Object
readonly
The Bitbucket URL String.
Class Method Summary collapse
-
.with_bitbucket(bitbucket_url, logger, logger_stderr) ⇒ Object
Provide a Bitbucket connector, and make sure the password is being cleaned when exiting.
Instance Method Summary collapse
-
#branch_permissions(project, repo) ⇒ Object
Get the branch permissions of a given repository.
-
#default_reviewers(project, repo) ⇒ Object
Get the default reviewers of a given repository.
-
#get_api(path, api_domain: 'api', api_version: '1.0', retries: 0) ⇒ Object
Issue an HTTP get on the API.
-
#initialize(bitbucket_url, bitbucket_user_name, bitbucket_password, logger: Logger.new(STDOUT), logger_stderr: Logger.new(STDERR)) ⇒ Bitbucket
constructor
Constructor.
-
#repos(project) ⇒ Object
Get the repositories of a given project.
-
#settings_pr(project, repo) ⇒ Object
Get the PR settings of a given repository.
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)) ⇒ Bitbucket
Constructor
- Parameters
-
bitbucket_url (String): The Bitbucket URL
-
bitbucket_user_name (String): Bitbucket user name to be used when querying the API
-
bitbucket_password (String): 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)]
41 42 43 44 45 46 |
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 41 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_url ⇒ Object (readonly)
The Bitbucket URL String
31 32 33 |
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 31 def bitbucket_url @bitbucket_url end |
Class Method Details
.with_bitbucket(bitbucket_url, logger, logger_stderr) ⇒ Object
Provide a Bitbucket connector, and make sure the password is being cleaned when exiting.
- Parameters
-
bitbucket_url (String): The Bitbucket URL
-
logger (Logger): Logger to be used
-
logger_stderr (Logger): Logger to be used for stderr
-
Proc: Code called with the Bitbucket instance.
-
bitbucket (Bitbucket): The Bitbucket instance to use.
-
23 24 25 26 27 |
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 23 def self.with_bitbucket(bitbucket_url, logger, logger_stderr) Credentials.with_credentials_for(:bitbucket, logger, logger_stderr, url: bitbucket_url) do |bitbucket_user, bitbucket_password| yield Bitbucket.new(bitbucket_url, bitbucket_user, bitbucket_password, logger: logger, logger_stderr: logger_stderr) end 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
88 89 90 91 |
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 88 def (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
77 78 79 |
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 77 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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 103 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.open(api_url, http_basic_authentication: [@bitbucket_user_name, @bitbucket_password]) rescue raise if retries == 0 log_warn "Got error #{$!} 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
55 56 57 |
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 55 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
66 67 68 |
# File 'lib/hybrid_platforms_conductor/bitbucket.rb', line 66 def settings_pr(project, repo) get_api("projects/#{project}/repos/#{repo}/settings/pull-requests") end |