Class: Dependabot::Clients::Bitbucket

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/clients/bitbucket.rb

Defined Under Namespace

Classes: Forbidden, NotFound, Unauthorized

Instance Method Summary collapse

Constructor Details

#initialize(credentials:) ⇒ Bitbucket

Client #



17
18
19
# File 'lib/dependabot/clients/bitbucket.rb', line 17

def initialize(credentials:)
  @credentials = credentials
end

Instance Method Details

#compare(repo, previous_tag, new_tag) ⇒ Object



60
61
62
63
64
65
# File 'lib/dependabot/clients/bitbucket.rb', line 60

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

#fetch_commit(repo, branch) ⇒ Object



21
22
23
24
25
26
# File 'lib/dependabot/clients/bitbucket.rb', line 21

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



28
29
30
31
32
# File 'lib/dependabot/clients/bitbucket.rb', line 28

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



46
47
48
49
50
51
# File 'lib/dependabot/clients/bitbucket.rb', line 46

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



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

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

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dependabot/clients/bitbucket.rb', line 67

def get(url)
  response = Excon.get(
    url,
    user: credentials&.fetch("username", nil),
    password: credentials&.fetch("password", nil),
    idempotent: true,
    **Dependabot::SharedHelpers.excon_defaults
  )
  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

#tags(repo) ⇒ Object



53
54
55
56
57
58
# File 'lib/dependabot/clients/bitbucket.rb', line 53

def tags(repo)
  path = "#{repo}/refs/tags?pagelen=100"
  response = get(base_url + path)

  JSON.parse(response.body).fetch("values")
end