Class: Apadmi::Grout::JiraBoardService

Inherits:
BoardService show all
Defined in:
lib/apadmi/grout/service/board_service/jira_board_service.rb

Overview

Provides a layer of abstraction on top of the Jira api

Instance Method Summary collapse

Constructor Details

#initialize(username, token, base_url, context_path, project, network_service) ⇒ JiraBoardService

Returns a new instance of JiraBoardService.

Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 15

def initialize(username, token, base_url, context_path, project, network_service)
  @options = {
    username: username,
    password: token,
    site: base_url,
    context_path: context_path,
    auth_type: :basic
  }
  @project = project
  @network_service = network_service
  @jira_client = JIRA::Client.new(@options)
end

Instance Method Details

#add_comment(key, comment) ⇒ Object

Parameters:

  • key (String)
  • comment (String)


94
95
96
97
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 94

def add_comment(key, comment)
  payload = "{\"issueKeys\":[\"#{key}\"],\"commentVisibility\":\"\",\"comment\":\"#{comment}\"}"
  @network_service.do_post("/rest/greenhopper/1.0/xboard/issue/flag/flag.json", payload)
end

#all_versionsArray<JIRA::Resource::Version>

Returns:

  • (Array<JIRA::Resource::Version>)


181
182
183
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 181

def all_versions
  @jira_client.Project.find(@project).versions
end

#assign_fixversions(key, version_strings) ⇒ Object

Parameters:

  • key (String)
  • version_strings (Array<String>)


187
188
189
190
191
192
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 187

def assign_fixversions(key, version_strings)
  versions = create_or_get_versions(version_strings)
  fixversions = versions.map { |v| "{\"id\": \"#{v.id}\"}" }.join(", ")
  payload = "{\"fields\" : {\"fixVersions\": [#{fixversions}] }}"
  @network_service.do_put("/rest/api/2/issue/#{key}", payload)
end

#create_version(release_date, name) ⇒ JIRA::Resource::Version

Parameters:

  • release_date (String)
  • name (String)

Returns:

  • (JIRA::Resource::Version)


162
163
164
165
166
167
168
169
170
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 162

def create_version(release_date, name)
  version = @jira_client.Version.build
  version.save!({
                  "project": @project,
                  "name": name,
                  "releaseDate": release_date
                })
  version
end

#delete_version(version_id, move_version_id) ⇒ Object

Parameters:

  • version_id (int)
  • move_version_id (int)


174
175
176
177
178
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 174

def delete_version(version_id, move_version_id)
  payload = "{\"moveFixIssuesTo\": #{move_version_id}, \"moveAffectedIssuesTo\": #{move_version_id}, "\
            "\"customFieldReplacementList\": []}"
  @network_service.do_post("/rest/api/2/version/#{version_id}/removeAndSwap", payload)
end

#find_issues_by_keys(keys) ⇒ Array<Apadmi::Grout::Issue>

Parameters:

  • keys (String[])

Returns:



30
31
32
33
34
35
36
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 30

def find_issues_by_keys(keys)
  return [] if keys.length <= 0

  jql_search = "project = '#{@project}' AND issue IN (#{keys.join(", ")})"
  issues = @jira_client.Issue.jql(jql_search, { max_results: 1000 }).uniq
  convert(issues)
end

#flag_ticket(key, comment) ⇒ Object

Parameters:

  • key (String)
  • comment (String)


73
74
75
76
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 73

def flag_ticket(key, comment)
  payload = "{\"flag\":true,\"issueKeys\":[\"#{key}\"],\"commentVisibility\":\"\",\"comment\":\"#{comment}\"}"
  @network_service.do_post("/rest/greenhopper/1.0/xboard/issue/flag/flag.json", payload)
end

#flagged?(key) ⇒ Boolean

Returns bool.

Parameters:

  • key (String)

Returns:

  • (Boolean)

    bool



86
87
88
89
90
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 86

def flagged?(key)
  jql_search = "project = '#{@project}' AND issue IN (#{key}) AND Flagged is not EMPTY"
  response = @jira_client.Issue.jql(jql_search, { max_results: 1 }).uniq
  response != []
end

#get_ticket_fixversions(key) ⇒ Array<String>

Parameters:

  • key (String)

Returns:

  • (Array<String>)


196
197
198
199
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 196

def get_ticket_fixversions(key)
  response = @network_service.do_get("/rest/api/2/issue/#{key}")
  JSON.parse(response.body)["fields"]["fixVersions"].map { |v| v["name"] } || []
end

#get_ticket_prs(issue) ⇒ Array<PullRequest>

Parameters:

Returns:



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 136

def get_ticket_prs(issue)
  query_string = "issueId=#{issue.raw_object.id}&applicationType=bitbucket&dataType=pullrequest"
  response = @network_service.do_get("/rest/dev-status/latest/issue/details?#{query_string}")

  parsed_detail = JSON.parse(response.body)["detail"]

  return [] if parsed_detail.empty?
  return [] unless parsed_detail[0].key?("pullRequests")

  parsed_detail[0]["pullRequests"].map do |pr|
    PullRequest.new(pr["id"], pr["name"], pr["status"].to_s == "MERGED", pr["status"].to_s == "DECLINED")
  end
end

#get_ticket_status(key) ⇒ String

Parameters:

  • key (String)

Returns:

  • (String)


129
130
131
132
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 129

def get_ticket_status(key)
  issue = @jira_client.Issue.find(key)
  issue.status.name
end

#get_ticket_subtask(keys, component = nil) ⇒ Array<Apadmi::Grout::Issue>

Parameters:

  • keys (String)
  • component (String) (defaults to: nil)

Returns:



153
154
155
156
157
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 153

def get_ticket_subtask(keys, component = nil)
  jql_search = "project = '#{@project}' AND parent IN #{keys.to_s.gsub("[", "(").gsub("]", ")")}"\
                + (component.nil? ? "" : " AND component IN ('#{component}')")
  convert(@jira_client.Issue.jql(jql_search, { max_results: 1000 }).uniq)
end

#get_tickets_by_component(component_key) ⇒ Array<Apadmi::Grout::Issue>

Parameters:

  • component_key (String)

Returns:



116
117
118
119
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 116

def get_tickets_by_component(component_key)
  jql_search = "project = '#{@project}' AND component IN ('#{component_key}')"
  convert(@jira_client.Issue.jql(jql_search, { max_results: 1000 }).uniq)
end

#get_top_comment(key) ⇒ JIRA::Resource::Comment

Parameters:

  • key (String)

Returns:

  • (JIRA::Resource::Comment)


101
102
103
104
105
106
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 101

def get_top_comment(key)
  jql_search = "project = '#{@project}' AND issue IN (#{key})"
  response = @jira_client.Issue.jql(jql_search, { fields: ["comment"], max_results: 1000 }).uniq
  comments = response[0].comments.reverse
  !comments.empty? ? comments[0] : nil
end

#get_transitions(key) ⇒ Object

Parameters:

  • key (String)


122
123
124
125
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 122

def get_transitions(key)
  issue = @jira_client.Issue.find(key)
  @jira_client.Transition.all(issue: issue)
end

#remove_comment(key, comment_id) ⇒ Object

Parameters:

  • key (String)
  • comment_id (String)


110
111
112
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 110

def remove_comment(key, comment_id)
  @network_service.do_delete("/rest/api/2/issue/#{key}/comment/#{comment_id}")
end

#search_unblocked_issues(component, status, ticket_types = [], options = nil) ⇒ Array<Apadmi::Grout::Issue>

Parameters:

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 43

def search_unblocked_issues(component, status, ticket_types = [], options = nil)
  allow_no_sprint = options&.include_no_sprint_tickets || false
  component_filter = (" AND component = '#{component}' " unless component.empty?) || ""
  status_filter = (" AND status = '#{status}' " unless status.empty?) || ""
  type_filter = ("AND (#{ticket_types.map { |type| "type = #{type}" }.join("OR ")})" unless ticket_types.empty?) || ""
  empty_sprint_condition = ("OR sprint is EMPTY" if allow_no_sprint) || ""

  jql_search = %{
    project = '#{@project}'
    #{status_filter} #{component_filter} #{type_filter}
    AND (sprint in openSprints() #{empty_sprint_condition}) AND (labels not in(Blocked) or labels is EMPTY)
    AND Flagged is EMPTY
  }

  issues = @jira_client.Issue.jql(jql_search, { max_results: 1000 }).uniq
  convert(issues)
end

#transition_issue(issue, state_name) ⇒ Object

Parameters:



63
64
65
66
67
68
69
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 63

def transition_issue(issue, state_name)
  transitions = @jira_client.Transition.all(issue: issue.raw_object)
  transition = transitions.find { |elem| elem.name.downcase == state_name.downcase }

  trans = issue.raw_object.transitions.build
  trans.save!("transition" => { "id" => transition.id })
end

#un_flag_ticket(key) ⇒ Object

Parameters:

  • key (String)


79
80
81
82
# File 'lib/apadmi/grout/service/board_service/jira_board_service.rb', line 79

def un_flag_ticket(key)
  payload = "{\"flag\":false,\"issueKeys\":[\"#{key}\"],\"commentVisibility\":\"\",\"comment\":\"Unflagged by CI\"}"
  @network_service.do_post("/rest/greenhopper/1.0/xboard/issue/flag/flag.json", payload)
end