Class: Dod::BitbucketServerAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/dod/client/bitbucket_server_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#endpointObject

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_idObject

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

Returns:

  • (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

Returns:

  • (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_commentObject



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