Class: JiraRestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/jira_rest_client.rb

Overview

#

Jira Rest Module # —————————————————————————–# The REST module currently supports the 6.0.8 version of the Jira API as # well as a rest client which supports both HTTP and HTTPS #

#

Instance Method Summary collapse

Constructor Details

#initialize(integration_settings = BrpmAuto.integration_settings) ⇒ JiraRestClient

Returns a new instance of JiraRestClient.



12
13
14
15
16
17
18
# File 'lib/jira_rest_client.rb', line 12

def initialize(integration_settings = BrpmAuto.integration_settings)
  @url = integration_settings.dns
  @username = integration_settings.username
  @password = integration_settings.password

  @api_url = "#{@url}/rest/api/2"
end

Instance Method Details

#add_comment(issue_id, comment_body = 'Dummy Comment') ⇒ Object

POST /rest/api/2/issue/issueIdOrKey/comment



21
22
23
24
25
26
27
28
29
30
# File 'lib/jira_rest_client.rb', line 21

def add_comment(issue_id, comment_body = 'Dummy Comment')
  cmmnt = {:body => comment_body}
  result = Rest.post("#{@api_url}/issue/#{issue_id}/comment", cmmnt, { :username => @username, :password => @password })

  unless result["status"] == "success"
    raise "Could not add the comment: #{result["error_message"]}"
  end

  result["response"]
end

#create_option_for_dropdown_custom_field(custom_field_id, option_value) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/jira_rest_client.rb', line 176

def create_option_for_dropdown_custom_field(custom_field_id, option_value)
  # NOTE: this method assumes that the "Customfield Editor Plugin" is installed on the JIRA instance and that permission was granted for the custom field

  custom_field_option = get_option_for_dropdown_custom_field(custom_field_id, option_value)

  if custom_field_option
    BrpmAuto.log "The option already exists, nothing to do."
    return custom_field_option
  end

  url = "#{@url}/rest/jiracustomfieldeditorplugin/1.1/user/customfieldoption/customfield_#{custom_field_id}"
  data = {:optionvalue => option_value }

  result = Rest.post(url, data, { :username => @username, :password => @password })

  if result["status"] == "success"
    result["response"]
  else
    raise "Could not create option: #{result["error_message"]}"
  end
end

#delete_option_for_dropdown_custom_field(custom_field_id, option_value) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/jira_rest_client.rb', line 220

def delete_option_for_dropdown_custom_field(custom_field_id, option_value)
  # NOTE: this method assumes that the "Customfield Editor Plugin" is installed on the JIRA instance and that permission was granted for the custom field

  custom_field_option_to_delete = get_option_for_dropdown_custom_field(custom_field_id, option_value)

  if custom_field_option_to_delete
    url = "#{@url}/rest/jiracustomfieldeditorplugin/1.1/user/customfieldoption/customfield_#{custom_field_id}/#{custom_field_option_to_delete["id"]}"

    result = Rest.delete(url, { :username => @username, :password => @password })

    if result["status"] == "success"
      return result["response"]
    else
      raise "Could not delete option: #{result["error_message"]}"
    end
  else
    BrpmAuto.log "The option doesn't exist, nothing to do."
  end
end

#get_issue(issue_id, fields = '', expand = '') ⇒ Object

GET /rest/api/2/issue/issueIdOrKey



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/jira_rest_client.rb', line 135

def get_issue(issue_id, fields = '', expand = '')
  added = false
  url = "#{@api_url}/issue/#{issue_id}"
  if not fields.eql? ''
    url = "#{url}?fields=#{fields}"
    added = true
  end
  if not expand.eql? ''
    if added
      url = "#{url}&expand=#{expand}"
    else
      url = "#{url}?expand=#{expand}"
    end
  end
  result = Rest.get(url, { :username => @username, :password => @password })

  if result["status"] == "success"
    result["response"]
  else
    raise "Error getting the issue: #{result["error_message"]}"
  end
end

#get_issue_transition(issue_id, transition_id, expand_transition = false) ⇒ Object

GET /rest/api/2/issue/issueIdOrKey/transitions?transitionId=transistion_id



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jira_rest_client.rb', line 52

def get_issue_transition(issue_id, transition_id, expand_transition = false)
  url = "#{@api_url}/issue/#{issue_id}/transitions?transitionId=#{transition_id}"
  if expand_transition
      url = "#{url}&expand=transitions.fields"
  end
  result = Rest.get(url, { :username => @username, :password => @password })

  if result["status"] == "success"
    result["response"]
  else
    raise "Error getting the issue transition: #{result["error_message"]}"
  end
end

#get_issue_transitions(issue_id, expand_transition = false) ⇒ Object

GET /rest/api/2/issue/issueIdOrKey/transitions



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jira_rest_client.rb', line 33

def get_issue_transitions(issue_id, expand_transition = false)
  url = "#{@api_url}/issue/#{issue_id}/transitions"
  if expand_transition
    url = "#{url}?expand=transitions.fields"
  end
  result = Rest.get(url, { :username => @username, :password => @password })

  if result["status"] == "success"
    result["response"]["transitions"]
  else
    if result["code"] == 404
      {}
    else
      raise "Error getting the issue transitions: #{result["error_message"]}"
    end
  end
end

#get_option_for_dropdown_custom_field(custom_field_id, option_value) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/jira_rest_client.rb', line 158

def get_option_for_dropdown_custom_field(custom_field_id, option_value)
  # NOTE: this method assumes that the "Customfield Editor Plugin" is installed on the JIRA instance and that permission was granted for the custom field

  url = "#{@url}/rest/jiracustomfieldeditorplugin/1.1/user/customfieldoptions/customfield_#{custom_field_id}"
  result = Rest.get(url, { :username => @username, :password => @password })

  if result["status"] == "success"
    custom_field_options = result["response"]
    custom_field_options.find { |custom_field_option| custom_field_option["optionvalue"] == option_value }
  else
    if result["code"] == 404
      return nil
    else
      raise "Error getting option: #{result["error_message"]}"
    end
  end
end

#get_projectsObject

GET /rest/api/2/project



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jira_rest_client.rb', line 84

def get_projects()
  result = Rest.get("#{@api_url}/project", { :username => @username, :password => @password })

  if result["status"] == "success"
    result["response"]
  else
    if result["code"] == 404
      {}
    else
      raise "Error getting the projects: #{result["error_message"]}"
    end
  end
end

#post_issue_transition(issue_id, transition_id, comment = 'simple comment', expand_transition = false) ⇒ Object

POST /rest/api/2/issue/issueIdOrKey/transitions



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jira_rest_client.rb', line 67

def post_issue_transition(issue_id, transition_id, comment = 'simple comment', expand_transition = false)
  url = "#{@api_url}/issue/#{issue_id}/transitions"
  if expand_transition
    url = "#{url}?expand=transitions.fields"
  end
  transition = {:update=>{:comment =>[{:add => {:body => "#{comment}"}}]}, :transition => {:id => "#{transition_id}"}}
  #Simple post as only return code is returned
  result = Rest.post(url, transition, { :username => @username, :password => @password })

  unless result["status"] == "success"
    raise "Could not add the comment: #{result["error_message"]}"
  end

  result["response"]
end

#search(jql, start_at = 0, max_results = 50, fields = '', expand = '') ⇒ Object

GET /rest/api/2/search?jql=[Some Jira Query Language Search]



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jira_rest_client.rb', line 114

def search(jql, start_at = 0, max_results = 50, fields = '', expand = '')
  url = "#{@api_url}/search?jql=#{jql}"
  url = "#{url}&startAt=#{start_at}" unless start_at == 0
  url = "#{url}&maxResults=#{max_results}" unless max_results == 50
  url = "#{url}&fields=#{fields}" unless fields == ''
  url = "#{url}&expand=#{expand}" unless expand == ''

  Rest.get(url, { :username => @username, :password => @password })

  if result["status"] == "success"
    result["response"]
  else
    if result["code"] == 404
      {}
    else
      raise "Error doing the search: #{result["error_message"]}"
    end
  end
end

#set_issue_to_status(issue_id, status) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jira_rest_client.rb', line 98

def set_issue_to_status(issue_id, status)
  BrpmAuto.log "Getting the possible transitions for issue #{issue_id}..."
  transitions = get_issue_transitions(issue_id)

  transition = transitions.find { |transition| transition["to"]["name"] == status }

  if transition
    BrpmAuto.log "Issuing transition #{transition["name"]} to update the status of the issue to #{status}..."
    post_issue_transition(issue_id, transition["id"])
  else
    BrpmAuto.log "This issue does not have a transition to status #{status} currently. Leaving it in its current state."
    nil
  end
end

#update_option_for_dropdown_custom_field(custom_field_id, old_option_value, new_option_value) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/jira_rest_client.rb', line 198

def update_option_for_dropdown_custom_field(custom_field_id, old_option_value, new_option_value)
  # NOTE: this method assumes that the "Customfield Editor Plugin" is installed on the JIRA instance and that permission was granted for the custom field

  custom_field_option_to_update = get_option_for_dropdown_custom_field(custom_field_id, old_option_value)

  if custom_field_option_to_update
    url = "#{@url}/rest/jiracustomfieldeditorplugin/1.1/user/customfieldoption/customfield_#{custom_field_id}/#{custom_field_option_to_update["id"]}"
    data = {:optionvalue => new_option_value }

    result = Rest.put(url, data, { :username => @username, :password => @password })

    if result["status"] == "success"
      return result["response"]
    else
      raise "Could not update option: #{result["error_message"]}"
    end
  else
    BrpmAuto.log "The option doesn't exist yet, creating it instead of updating..."
    create_option_for_dropdown_custom_field(custom_field_id, new_option_value)
  end
end