Class: Dependabot::Clients::Bitbucket
- Inherits:
-
Object
- Object
- Dependabot::Clients::Bitbucket
- Extended by:
- T::Sig
- Includes:
- JsonResponseParser
- Defined in:
- lib/dependabot/clients/bitbucket.rb
Defined Under Namespace
Classes: Forbidden, NotFound, TimedOut, Unauthorized
Constant Summary collapse
- JsonObject =
T.type_alias { JsonResponseParser::JsonObject }
- JsonObjects =
T.type_alias { T::Array[JsonObject] }
Class Method Summary collapse
Instance Method Summary collapse
- #branch(repo, branch_name) ⇒ Object
- #commits(repo, branch_name = nil) ⇒ Object
- #compare(repo, previous_tag, new_tag) ⇒ Object
- #create_commit(repo, branch_name, base_commit, commit_message, files, author_details) ⇒ Object
- #create_pull_request(repo, pr_name, source_branch, target_branch, pr_description, _labels, _work_item = nil) ⇒ Object
- #current_user ⇒ Object
- #decline_pull_request(repo, pr_id, comment = nil) ⇒ Object
- #default_reviewers(repo) ⇒ Object
- #fetch_commit(repo, branch) ⇒ Object
- #fetch_default_branch(repo) ⇒ Object
- #fetch_file_contents(repo, commit, path) ⇒ Object
- #fetch_repo_contents(repo, commit = nil, path = nil) ⇒ Object
- #get(url) ⇒ Object
-
#initialize(credentials:) ⇒ Bitbucket
constructor
A new instance of Bitbucket.
- #post(url, body, content_type = "application/json") ⇒ Object
- #pull_requests(repo, source_branch, target_branch, status = %w(OPEN MERGED DECLINED SUPERSEDED))) ⇒ Object
- #tags(repo) ⇒ Object
Constructor Details
#initialize(credentials:) ⇒ Bitbucket
Returns a new instance of Bitbucket.
52 53 54 55 |
# File 'lib/dependabot/clients/bitbucket.rb', line 52 def initialize(credentials:) @credentials = credentials @auth_header = T.let(auth_header_for(credentials&.fetch("token", nil)), T::Hash[String, String]) end |
Class Method Details
.for_source(source:, credentials:) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/dependabot/clients/bitbucket.rb', line 38 def self.for_source(source:, credentials:) credential = credentials .select { |cred| cred["type"] == "git_source" } .find { |cred| cred["host"] == source.hostname } new(credentials: credential) end |
Instance Method Details
#branch(repo, branch_name) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/dependabot/clients/bitbucket.rb', line 132 def branch(repo, branch_name) branch_path = "#{repo}/refs/branches/#{branch_name}" response = get(base_url + branch_path) parse_json_object(response.body, "branch") end |
#commits(repo, branch_name = nil) ⇒ Object
119 120 121 122 123 |
# File 'lib/dependabot/clients/bitbucket.rb', line 119 def commits(repo, branch_name = nil) commits_path = "#{repo}/commits/#{branch_name}?pagelen=100" next_page_url = base_url + commits_path paginate({ "next" => next_page_url }) end |
#compare(repo, previous_tag, new_tag) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/dependabot/clients/bitbucket.rb', line 320 def compare(repo, previous_tag, new_tag) path = "#{repo}/commits/?include=#{new_tag}&exclude=#{previous_tag}" response = get(base_url + path) root = parse_json_object(response.body, "commit comparison") commits = object_array_field(root, "values", "commit comparison") commits.each do |commit| validate_string_paths(commit, "commit comparison", [%w(hash), %w(summary raw), %w(links html href)]) end commits end |
#create_commit(repo, branch_name, base_commit, commit_message, files, author_details) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/dependabot/clients/bitbucket.rb', line 185 def create_commit( repo, branch_name, base_commit, , files, ) parameters = { message: , # TODO: Format markup in commit message author: "#{.fetch(:name)} <#{.fetch(:email)}>", parents: base_commit, branch: branch_name } files.each do |file| parameters[file.path] = file.content end body = encode_form_parameters(parameters) commit_path = "#{repo}/src" post(base_url + commit_path, body, "application/x-www-form-urlencoded") end |
#create_pull_request(repo, pr_name, source_branch, target_branch, pr_description, _labels, _work_item = nil) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/dependabot/clients/bitbucket.rb', line 224 def create_pull_request( repo, pr_name, source_branch, target_branch, pr_description, _labels, _work_item = nil ) reviewers = default_reviewers(repo) content = { title: pr_name, source: { branch: { name: source_branch } }, destination: { branch: { name: target_branch } }, description: pr_description, reviewers: reviewers, close_source_branch: true } pr_path = "#{repo}/pullrequests" post(base_url + pr_path, content.to_json) end |
#current_user ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/dependabot/clients/bitbucket.rb', line 276 def current_user base_url = "https://api.bitbucket.org/2.0/user?fields=uuid" response = get(base_url) root = parse_json_object(response.body, "current user") string_field(root, "uuid", "current user") rescue Unauthorized nil end |
#decline_pull_request(repo, pr_id, comment = nil) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/dependabot/clients/bitbucket.rb', line 258 def decline_pull_request(repo, pr_id, comment = nil) # https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/ decline_path = "#{repo}/pullrequests/#{pr_id}/decline" post(base_url + decline_path, "") comment = "Dependabot declined the pull request." if comment.nil? content = { content: { raw: comment } } comment_path = "#{repo}/pullrequests/#{pr_id}/comments" post(base_url + comment_path, content.to_json) end |
#default_reviewers(repo) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/dependabot/clients/bitbucket.rb', line 286 def default_reviewers(repo) current_uuid = current_user path = "#{repo}/default-reviewers?pagelen=100&fields=values.uuid,next" reviewers_url = base_url + path default_reviewers = paginate({ "next" => reviewers_url }) reviewer_data = [] default_reviewers.each do |reviewer| uuid = string_field(reviewer, "uuid", "default reviewer") reviewer_data.append({ uuid: uuid }) unless current_uuid == uuid end reviewer_data end |
#fetch_commit(repo, branch) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/dependabot/clients/bitbucket.rb', line 58 def fetch_commit(repo, branch) path = "#{repo}/refs/branches/#{branch}" response = get(base_url + path) root = parse_json_object(response.body, "branch") target = object_field(root, "target", "branch") string_field(target, "hash", "branch") end |
#fetch_default_branch(repo) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/dependabot/clients/bitbucket.rb', line 68 def fetch_default_branch(repo) response = get(base_url + repo) root = parse_json_object(response.body, "repository") main_branch = object_field(root, "mainbranch", "repository") string_field(main_branch, "name", "repository") end |
#fetch_file_contents(repo, commit, path) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/dependabot/clients/bitbucket.rb', line 105 def fetch_file_contents(repo, commit, path) path = "#{repo}/src/#{commit}/#{path.gsub(%r{/+$}, '')}" response = get(base_url + path) response.body end |
#fetch_repo_contents(repo, commit = nil, path = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dependabot/clients/bitbucket.rb', line 84 def fetch_repo_contents(repo, commit = nil, path = nil) raise "Commit is required if path provided!" if commit.nil? && path api_path = "#{repo}/src" api_path += "/#{commit}" if commit api_path += "/#{path.gsub(%r{/+$}, '')}" if path api_path += "?pagelen=100" response = get(base_url + api_path) root = parse_json_object(response.body, "repository contents") object_array_field(root, "values", "repository contents") end |
#get(url) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/dependabot/clients/bitbucket.rb', line 333 def get(url) response = Excon.get( URI::RFC2396_PARSER.escape(url), user: credentials&.fetch("username", nil), password: credentials&.fetch("password", nil), # Setting to false to prevent Excon retries, use BitbucketWithRetries for retries. idempotent: false, **Dependabot::SharedHelpers.excon_defaults( headers: auth_header ) ) raise Unauthorized if response.status == 401 raise Forbidden if response.status == 403 raise NotFound if response.status == 404 if response.status >= 400 raise "Unhandled Bitbucket error!\n" \ "Status: #{response.status}\n" \ "Body: #{response.body}" end response end |
#post(url, body, content_type = "application/json") ⇒ Object
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/dependabot/clients/bitbucket.rb', line 365 def post(url, body, content_type = "application/json") headers = auth_header headers = if body.empty? headers.merge({ "Accept" => "application/json" }) else headers.merge({ "Content-Type" => content_type }) end response = Excon.post( url, body: body, user: credentials&.fetch("username", nil), password: credentials&.fetch("password", nil), idempotent: false, **SharedHelpers.excon_defaults( headers: headers ) ) raise Unauthorized if response.status == 401 raise Forbidden if response.status == 403 raise NotFound if response.status == 404 raise TimedOut if response.status == 555 response end |
#pull_requests(repo, source_branch, target_branch, status = %w(OPEN MERGED DECLINED SUPERSEDED))) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/dependabot/clients/bitbucket.rb', line 148 def pull_requests(repo, source_branch, target_branch, status = %w(OPEN MERGED DECLINED SUPERSEDED)) pr_path = "#{repo}/pullrequests?" # Get pull requests with given status status.each { |n| pr_path += "status=#{n}&" } next_page_url = base_url + pr_path pull_requests = paginate({ "next" => next_page_url }) pull_requests unless source_branch && target_branch # rubocop:disable Lint/Void pull_requests.select do |pr| if source_branch.nil? source_branch_matches = true else source = object_field(pr, "source", "pull request") source_branch_details = object_field(source, "branch", "pull request") pr_source_branch = string_field(source_branch_details, "name", "pull request") source_branch_matches = pr_source_branch == source_branch end destination = object_field(pr, "destination", "pull request") target_branch_details = object_field(destination, "branch", "pull request") pr_target_branch = string_field(target_branch_details, "name", "pull request") source_branch_matches && pr_target_branch == target_branch end end |