Class: Danger::RequestSources::BitbucketCloudAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/request_sources/bitbucket_cloud_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_slug, pull_request_id, branch_name, environment) ⇒ BitbucketCloudAPI

Returns a new instance of BitbucketCloudAPI.



10
11
12
13
14
15
16
17
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 10

def initialize(repo_slug, pull_request_id, branch_name, environment)
  @username = environment["DANGER_BITBUCKETCLOUD_USERNAME"]
  @password = environment["DANGER_BITBUCKETCLOUD_PASSWORD"]
  self.project, self.slug = repo_slug.split("/")
  self.access_token = fetch_access_token(environment)
  self.pull_request_id = pull_request_id || fetch_pr_from_branch(branch_name)
  self.host = "https://bitbucket.org/"
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



8
9
10
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 8

def access_token
  @access_token
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 8

def host
  @host
end

#projectObject

Returns the value of attribute project.



8
9
10
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 8

def project
  @project
end

#pull_request_idObject

Returns the value of attribute pull_request_id.



8
9
10
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 8

def pull_request_id
  @pull_request_id
end

#slugObject

Returns the value of attribute slug.



8
9
10
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 8

def slug
  @slug
end

Instance Method Details

#credentials_given?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 29

def credentials_given?
  @username && !@username.empty? && @password && !@password.empty?
end

#delete_comment(id) ⇒ Object



59
60
61
62
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 59

def delete_comment(id)
  uri = URI("#{pr_api_endpoint}/comments/#{id}")
  delete(uri)
end

#fetch_commentsObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 47

def fetch_comments
  values = []
  # TODO: use a url parts encoder to encode the query
  uri = "#{pr_api_endpoint}/comments?pagelen=100&q=deleted+%7E+false+AND+user.username+%7E+%22#{@username}%22"
  while(uri)
    json = fetch_json(URI(uri))
    values += json[:values]
    uri = json[:next]
    values
  end
end

#fetch_pr_jsonObject



42
43
44
45
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 42

def fetch_pr_json
  uri = URI(pr_api_endpoint)
  fetch_json(uri)
end

#inspectObject



19
20
21
22
23
24
25
26
27
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 19

def inspect
  inspected = super

  if @password
    inspected = inspected.sub! @password, "********".freeze
  end

  inspected
end

#my_uuidObject



33
34
35
36
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 33

def my_uuid
  uri = URI("https://api.bitbucket.org/2.0/users/#{@username}")
  @my_uuid ||= fetch_json(uri)[:uuid]
end

#post_comment(text, file: nil, line: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 64

def post_comment(text, file: nil, line: nil)
  uri = URI("#{pr_api_endpoint}/comments")
  body = {
    content: {
      raw: text
    }
  }
  body.merge!(inline: { path: file, to: line }) if file && line

  post(uri, body.to_json)
end

#pull_requestObject



38
39
40
# File 'lib/danger/request_sources/bitbucket_cloud_api.rb', line 38

def pull_request(*)
  fetch_pr_json
end