Class: Dependabot::Clients::Bitbucket
- Inherits:
-
Object
- Object
- Dependabot::Clients::Bitbucket
- Defined in:
- lib/dependabot/clients/bitbucket.rb
Defined Under Namespace
Classes: Forbidden, NotFound, Unauthorized
Class Method Summary collapse
-
.for_source(source:, credentials:) ⇒ Object
Constructor methods #.
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
rubocop:disable Metrics/ParameterLists.
-
#create_pull_request(repo, pr_name, source_branch, target_branch, pr_description, _labels, _work_item = nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#current_user ⇒ Object
rubocop:enable Metrics/ParameterLists.
- #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
Client #.
- #post(url, body, content_type = "application/json") ⇒ Object
- #pull_requests(repo, source_branch, target_branch) ⇒ Object
- #tags(repo) ⇒ Object
Constructor Details
#initialize(credentials:) ⇒ Bitbucket
Client #
32 33 34 35 |
# File 'lib/dependabot/clients/bitbucket.rb', line 32 def initialize(credentials:) @credentials = credentials @auth_header = auth_header_for(credentials&.fetch("token", nil)) end |
Class Method Details
.for_source(source:, credentials:) ⇒ Object
Constructor methods #
19 20 21 22 23 24 25 26 |
# File 'lib/dependabot/clients/bitbucket.rb', line 19 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
75 76 77 78 79 80 |
# File 'lib/dependabot/clients/bitbucket.rb', line 75 def branch(repo, branch_name) branch_path = "#{repo}/refs/branches/#{branch_name}" response = get(base_url + branch_path) JSON.parse(response.body) end |
#commits(repo, branch_name = nil) ⇒ Object
69 70 71 72 73 |
# File 'lib/dependabot/clients/bitbucket.rb', line 69 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
176 177 178 179 180 181 |
# File 'lib/dependabot/clients/bitbucket.rb', line 176 def compare(repo, previous_tag, new_tag) path = "#{repo}/commits/?include=#{new_tag}&exclude=#{previous_tag}" response = get(base_url + path) JSON.parse(response.body).fetch("values") end |
#create_commit(repo, branch_name, base_commit, commit_message, files, author_details) ⇒ Object
rubocop:disable Metrics/ParameterLists
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/dependabot/clients/bitbucket.rb', line 99 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| absolute_path = file.name.start_with?("/") ? file.name : "/" + file.name parameters[absolute_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
rubocop:disable Metrics/ParameterLists
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/dependabot/clients/bitbucket.rb', line 121 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
rubocop:enable Metrics/ParameterLists
147 148 149 150 151 |
# File 'lib/dependabot/clients/bitbucket.rb', line 147 def current_user base_url = "https://api.bitbucket.org/2.0/user?fields=uuid" response = get(base_url) JSON.parse(response.body).fetch("uuid") end |
#default_reviewers(repo) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/dependabot/clients/bitbucket.rb', line 153 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| reviewer_data.append({ uuid: reviewer.fetch("uuid") }) unless current_uuid == reviewer.fetch("uuid") end reviewer_data end |
#fetch_commit(repo, branch) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/dependabot/clients/bitbucket.rb', line 37 def fetch_commit(repo, branch) path = "#{repo}/refs/branches/#{branch}" response = get(base_url + path) JSON.parse(response.body).fetch("target").fetch("hash") end |
#fetch_default_branch(repo) ⇒ Object
44 45 46 47 48 |
# File 'lib/dependabot/clients/bitbucket.rb', line 44 def fetch_default_branch(repo) response = get(base_url + repo) JSON.parse(response.body).fetch("mainbranch").fetch("name") end |
#fetch_file_contents(repo, commit, path) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/dependabot/clients/bitbucket.rb', line 62 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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/dependabot/clients/bitbucket.rb', line 50 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) JSON.parse(response.body).fetch("values") end |
#get(url) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/dependabot/clients/bitbucket.rb', line 183 def get(url) response = Excon.get( 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
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/dependabot/clients/bitbucket.rb', line 207 def post(url, body, content_type = "application/json") response = Excon.post( url, body: body, user: credentials&.fetch("username", nil), password: credentials&.fetch("password", nil), idempotent: false, **SharedHelpers.excon_defaults( headers: auth_header.merge( { "Content-Type" => content_type } ) ) ) raise Unauthorized if response.status == 401 raise Forbidden if response.status == 403 raise NotFound if response.status == 404 response end |
#pull_requests(repo, source_branch, target_branch) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dependabot/clients/bitbucket.rb', line 82 def pull_requests(repo, source_branch, target_branch) pr_path = "#{repo}/pullrequests" # Get pull requests with any status pr_path += "?status=OPEN&status=MERGED&status=DECLINED&status=SUPERSEDED" next_page_url = base_url + pr_path pull_requests = paginate({ "next" => next_page_url }) pull_requests unless source_branch && target_branch pull_requests.select do |pr| pr_source_branch = pr.fetch("source").fetch("branch").fetch("name") pr_target_branch = pr.fetch("destination").fetch("branch").fetch("name") pr_source_branch == source_branch && pr_target_branch == target_branch end end |
#tags(repo) ⇒ Object
169 170 171 172 173 174 |
# File 'lib/dependabot/clients/bitbucket.rb', line 169 def (repo) path = "#{repo}/refs/tags?pagelen=100" response = get(base_url + path) JSON.parse(response.body).fetch("values") end |