Class: Dependabot::Clients::Bitbucket

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

Defined Under Namespace

Classes: Forbidden, NotFound, Unauthorized

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials) ⇒ Bitbucket

Client #



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

def initialize(credentials)
  @credentials = credentials
end

Class Method Details

.for_bitbucket_dot_org(credentials:) ⇒ Object

Constructor methods #



17
18
19
20
21
22
23
24
# File 'lib/dependabot/clients/bitbucket.rb', line 17

def self.for_bitbucket_dot_org(credentials:)
  credential =
    credentials.
    select { |cred| cred["type"] == "git_source" }.
    find { |cred| cred["host"] == "bitbucket.org" }

  new(credential)
end

Instance Method Details

#compare(repo, previous_tag, new_tag) ⇒ Object



73
74
75
76
77
78
# File 'lib/dependabot/clients/bitbucket.rb', line 73

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



34
35
36
37
38
39
# File 'lib/dependabot/clients/bitbucket.rb', line 34

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



41
42
43
44
45
# File 'lib/dependabot/clients/bitbucket.rb', line 41

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



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

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



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dependabot/clients/bitbucket.rb', line 47

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:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dependabot/clients/bitbucket.rb', line 80

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

  response
end

#tags(repo) ⇒ Object



66
67
68
69
70
71
# File 'lib/dependabot/clients/bitbucket.rb', line 66

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

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