Class: Dod::BitbucketServerAPI
- Inherits:
-
Object
- Object
- Dod::BitbucketServerAPI
- Defined in:
- lib/dod/client/bitbucket_server_api.rb
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#pull_request_id ⇒ Object
Returns the value of attribute pull_request_id.
Instance Method Summary collapse
- #create_definition_of_done(tasks) ⇒ Object
- #create_tasks_for_comment(comment_id, tasks) ⇒ Object
- #credentials_given? ⇒ Boolean
- #definition_of_done_not_created? ⇒ Boolean
- #fetch_json(uri) ⇒ Object
- #get_pull_request_id(branch) ⇒ Object
-
#initialize(environment, project, repo, branch) ⇒ BitbucketServerAPI
constructor
A new instance of BitbucketServerAPI.
- #post(uri, body) ⇒ Object
- #post_comment ⇒ Object
Constructor Details
#initialize(environment, project, repo, branch) ⇒ BitbucketServerAPI
Returns a new instance of BitbucketServerAPI.
7 8 9 10 11 12 13 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 7 def initialize(environment, project, repo, branch) @username = environment["bamboo_BITBUCKET_USERNAME"] @password = environment["bamboo_BITBUCKET_PASSWORD"] self.endpoint = "https://stash.allegrogroup.com/rest/api/1.0/projects/#{project}/repos/#{repo}" self.pull_request_id = get_pull_request_id(branch) end |
Instance Attribute Details
#endpoint ⇒ Object
Returns the value of attribute endpoint.
5 6 7 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 5 def endpoint @endpoint end |
#pull_request_id ⇒ Object
Returns the value of attribute pull_request_id.
5 6 7 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 5 def pull_request_id @pull_request_id end |
Instance Method Details
#create_definition_of_done(tasks) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 15 def create_definition_of_done(tasks) unless credentials_given? puts "Bitbucket credentials not given. Use BITBUCKET_USERNAME and BITBUCKET_PASSWORD environment variables".red return end unless definition_of_done_not_created? puts "Definition of Done already created.".red return end comment_id = post_comment create_tasks_for_comment(comment_id, tasks) puts "Definition of Done created.".green end |
#create_tasks_for_comment(comment_id, tasks) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 33 def create_tasks_for_comment(comment_id, tasks) uri = URI("https://stash.allegrogroup.com/rest/api/1.0/tasks") tasks.each do |task| body_hash = { anchor: { id: comment_id, type: "COMMENT" }, text: task } body = body_hash.to_json post(uri, body) end end |
#credentials_given? ⇒ Boolean
29 30 31 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 29 def credentials_given? @username && !@username.empty? && @password && !@password.empty? end |
#definition_of_done_not_created? ⇒ Boolean
48 49 50 51 52 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 48 def definition_of_done_not_created? uri = URI("#{endpoint}/pull-requests/#{pull_request_id}/tasks/count") tasks_count = fetch_json(uri) tasks_count[:open] == 0 && tasks_count[:resolved] == 0 end |
#fetch_json(uri) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 59 def fetch_json(uri) req = Net::HTTP::Get.new(uri.request_uri, { "Content-Type" => "application/json" }) req.basic_auth @username, @password res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(req) end JSON.parse(res.body, symbolize_names: true) end |
#get_pull_request_id(branch) ⇒ Object
54 55 56 57 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 54 def get_pull_request_id(branch) uri = URI("#{endpoint}/pull-requests") fetch_json(uri)[:values].select { |v| v[:fromRef][:displayId] == branch }.map { |v| v[:id] }.first end |
#post(uri, body) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 68 def post(uri, body) req = Net::HTTP::Post.new(uri.request_uri, { "Content-Type" => "application/json" }) req.basic_auth @username, @password req.body = body res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| http.request(req) end JSON.parse(res.body, symbolize_names: true) end |
#post_comment ⇒ Object
42 43 44 45 46 |
# File 'lib/dod/client/bitbucket_server_api.rb', line 42 def post_comment uri = URI("#{endpoint}/pull-requests/#{pull_request_id}/comments") body = { text: "Definition of Done." }.to_json post(uri, body)[:id] end |