class Dod::BitbucketServerAPI
Attributes
endpoint[RW]
pull_request_id[RW]
Public Class Methods
new(environment, project, repo, branch)
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 9 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
Public Instance Methods
create_definition_of_done(tasks)
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 17 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)
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 35 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?()
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 31 def credentials_given? @username && !@username.empty? && @password && !@password.empty? end
definition_of_done_not_created?()
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 50 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)
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 61 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)
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 56 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)
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 70 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()
click to toggle source
# File lib/dod/client/bitbucket_server_api.rb, line 44 def post_comment uri = URI("#{endpoint}/pull-requests/#{pull_request_id}/comments") body = { text: "Definition of Done." }.to_json post(uri, body)[:id] end