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.



9
10
11
12
13
14
15
# 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

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



7
8
9
# File 'lib/dod/client/bitbucket_server_api.rb', line 7

def endpoint
  @endpoint
end

#pull_request_idObject

Returns the value of attribute pull_request_id.



7
8
9
# File 'lib/dod/client/bitbucket_server_api.rb', line 7

def pull_request_id
  @pull_request_id
end

Instance Method Details

#create_definition_of_done(tasks) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object



35
36
37
38
39
40
41
42
# 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?Boolean

Returns:

  • (Boolean)


31
32
33
# 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?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# 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) ⇒ Object



61
62
63
64
65
66
67
68
# 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) ⇒ Object



56
57
58
59
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
# 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_commentObject



44
45
46
47
48
# 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