Class: Danger::RequestSources::BitbucketServerAPI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, slug, pull_request_id, environment) ⇒ BitbucketServerAPI

Returns a new instance of BitbucketServerAPI.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 11

def initialize(project, slug, pull_request_id, environment)
  @username = environment["DANGER_BITBUCKETSERVER_USERNAME"]
  @password = environment["DANGER_BITBUCKETSERVER_PASSWORD"]
  self.host = environment["DANGER_BITBUCKETSERVER_HOST"]
  self.verify_ssl = environment["DANGER_BITBUCKETSERVER_VERIFY_SSL"] == "false" ? false : true
  if self.host && !(self.host.include? "http://") && !(self.host.include? "https://")
    self.host = "https://" + self.host
  end
  self.key = slug
  self.project = project
  self.pr_api_endpoint = "#{host}/rest/api/1.0/projects/#{project}/repos/#{slug}/pull-requests/#{pull_request_id}"
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 9

def host
  @host
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 9

def key
  @key
end

#pr_api_endpointObject

Returns the value of attribute pr_api_endpoint.



9
10
11
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 9

def pr_api_endpoint
  @pr_api_endpoint
end

#projectObject

Returns the value of attribute project.



9
10
11
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 9

def project
  @project
end

#verify_sslObject

Returns the value of attribute verify_ssl.



9
10
11
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 9

def verify_ssl
  @verify_ssl
end

Instance Method Details

#credentials_given?Boolean

Returns:

  • (Boolean)


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

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

#delete_comment(id, version) ⇒ Object



52
53
54
55
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 52

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

#fetch_last_commentsObject



47
48
49
50
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 47

def fetch_last_comments
  uri = URI("#{pr_api_endpoint}/activities?limit=1000")
  fetch_json(uri)[:values].select { |v| v[:action] == "COMMENTED" }.map { |v| v[:comment] }
end

#fetch_pr_jsonObject



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

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

#inspectObject



24
25
26
27
28
29
30
31
32
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 24

def inspect
  inspected = super

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

  inspected
end

#post_comment(text) ⇒ Object



57
58
59
60
61
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 57

def post_comment(text)
  uri = URI("#{pr_api_endpoint}/comments")
  body = { text: text }.to_json
  post(uri, body)
end

#pull_requestObject



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

def pull_request(*)
  fetch_pr_json
end

#update_pr_build_status(status, changeset, build_job_link, description) ⇒ Object



63
64
65
66
67
# File 'lib/danger/request_sources/bitbucket_server_api.rb', line 63

def update_pr_build_status(status, changeset, build_job_link, description)
   uri = URI("#{self.host}/rest/build-status/1.0/commits/#{changeset}")
   body = build_status_body(status, build_job_link, description)
   post(uri, body)
end