Class: Jira::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/jira-script/request.rb

Overview

Generic Jira request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, config = {}) ⇒ Request

Returns a new instance of Request.



37
38
39
40
41
42
43
44
45
46
# File 'lib/jira-script/request.rb', line 37

def initialize(type, config = {})
  self.request_type = type
  self.config = config
  self.fields = {}
  self.request_parent = nil
  self.json_data = {}
  self.children = []
  self.key = nil
  init_data_map
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/jira-script/request.rb', line 115

def method_missing(name, *args, &block)
  if data_map.key?(name)
    _set_field(name, args[0])
  else
    super
  end
end

Instance Attribute Details

#childrenObject

request children



29
30
31
# File 'lib/jira-script/request.rb', line 29

def children
  @children
end

#configObject

request config - host, api resource, etc



11
12
13
# File 'lib/jira-script/request.rb', line 11

def config
  @config
end

#data_mapObject

mapping between our dsl keywords and Jira’s json fields



20
21
22
# File 'lib/jira-script/request.rb', line 20

def data_map
  @data_map
end

#fieldsObject

request fields that were set



17
18
19
# File 'lib/jira-script/request.rb', line 17

def fields
  @fields
end

#json_dataObject

request data to send to server



14
15
16
# File 'lib/jira-script/request.rb', line 14

def json_data
  @json_data
end

#keyObject

issue key



35
36
37
# File 'lib/jira-script/request.rb', line 35

def key
  @key
end

#request_parentObject

request parent - used when creating subtasks



23
24
25
# File 'lib/jira-script/request.rb', line 23

def request_parent
  @request_parent
end

#request_typeObject

request type (create, update)



32
33
34
# File 'lib/jira-script/request.rb', line 32

def request_type
  @request_type
end

#responseObject

request response



26
27
28
# File 'lib/jira-script/request.rb', line 26

def response
  @response
end

Instance Method Details

#_error(msg) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/jira-script/request.rb', line 101

def _error(msg)
  if request_type == :create
    raise RequestException, "Error trying to create ticket #{fields[:summary]}: #{msg}"
  elsif request_type == :update
    raise RequestException, "Error trying to update ticket #{key}: #{msg}"
elsif request_type == :search
  raise RequestException, "Error searching for issues: #{msg}"
  end
end

#_set_default_typeObject



72
73
74
75
76
77
78
79
80
# File 'lib/jira-script/request.rb', line 72

def _set_default_type
  # set default type if empty
  return if fields.key?(:type)
  if fields.key?(:parent)
    type config[:default_subtask_type]
  else
    type config[:default_issue_type]
  end
end

#_set_field(name, val) ⇒ Object

Raises:



95
96
97
98
99
# File 'lib/jira-script/request.rb', line 95

def _set_field(name, val)
  raise RequestException, "Invalid request parameter #{name}" unless data_map.key?(name)
  fields[name] = val
  _set_xpath(json_data, data_map[name], val)
end

#_set_xpath(h, xpath, value) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jira-script/request.rb', line 82

def _set_xpath(h, xpath, value)
  len = xpath.length
  pos = xpath.index('/')
  if pos.nil?
    h[xpath.to_sym] = value
  else
    key = xpath[0..pos - 1].to_sym
    h[key] = h[key] || {}
    _set_xpath(h[key], xpath[pos + 1..len], value)
  end
  h
end

#components(*args) ⇒ Object



220
221
222
223
224
225
226
227
# File 'lib/jira-script/request.rb', line 220

def components(*args)
  vals = []
  args.each do |arg|
    vals.push name: arg
  end

  _set_field(:components, vals)
end

#create(summary, &block) ⇒ Object



205
206
207
# File 'lib/jira-script/request.rb', line 205

def create(summary, &block)
  subtask(summary, &block)
end

#init_data_mapObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jira-script/request.rb', line 48

def init_data_map
  self.data_map = {
      project: 'fields/project/key',
      parent: 'fields/parent/key',
      summary: 'fields/summary',
      description: 'fields/description',
      type: 'fields/issuetype/name',
      assignee: 'fields/assignee/name',
      reporter: 'fields/reporter/name',
      estimate: 'fields/timetracking/originalEstimate',
      remaining: 'fields/timetracking/remainingEstimate',
      components: 'fields/components',
      labels: 'fields/labels'
  }
end

#labels(*args) ⇒ Object



229
230
231
# File 'lib/jira-script/request.rb', line 229

def labels(*args)
  _set_field(:labels, [*args])
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/jira-script/request.rb', line 111

def respond_to_missing?(method_name, include_private = false)
  data_map.key?(method_name) || super
end

#runObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/jira-script/request.rb', line 140

def run
  ret = nil
_set_default_type if [:create, :update].include?(request_type)
  url = [config[:host], config[:api_path], config[:resource]].join('/')
json_str = to_json 
puts "url: #{url}" if config[:verbosity] >= 2 
  puts "json: #{json_str}" if config[:verbosity] >= 2

  req = Typhoeus::Request.new(
      url,
      ssl_verifypeer: false,
      method: config[:http_request_type],
      userpwd: "#{config[:user]}:#{config[:password]}",
    body: json_str,
    headers: { 'Content-Type' => 'application/json' }.merge(config[:headers])
  )
  req.on_complete do |response|
    if response.success?
    if !response.body.empty?
       self.response = JSON.parse(response.body, symbolize_names: true)
    else
      self.response = response
    end
   
     if config[:response_node] == nil
      ret = self.response
    else  
      ret = self.response[config[:response_node]]
    end
      self.key = ret if request_type == :create

    if [:create, :update].include?(request_type)
      if config[:verbosity] >= 1
        if fields[:parent].nil?
          prefix = 'Issue'
        else
          prefix = '  - sub-task'
        end
        puts "#{prefix} #{key} updated successfully" if request_type == :update
        puts "#{prefix} #{key}: '#{fields[:summary]}' created successfully" if request_type == :create
      end
    end

      unless children.empty?
        children.each do |child|
          child.parent key
          child.run
        end
      end
    elsif response.timed_out?
      _error('timeout')
    elsif response.code.zero?
      # Could not get an http response, something's wrong.
      _error(response.return_message)
    else
      # Received a non-successful http response.
      _error('HTTP request failed: ' + response.code.to_s + ' / message: ' + response.body)
    end
  end

  req.run

  ret
end

#subtask(summary, &block) ⇒ Object

Raises:



209
210
211
212
213
214
215
216
217
218
# File 'lib/jira-script/request.rb', line 209

def subtask(summary, &block)
  raise RequestException, "Sub-task '#{fields[:summary]}' cannot have other sub-tasks" unless request_parent.nil?
  request = Request.new(:create, config)
  request.config[:http_request_type] = :post
  request.request_parent = self
  children.push request
  request.summary summary
  request.project fields[:project]
  request.instance_eval(&block) if block_given?
end

#to_jsonObject



64
65
66
# File 'lib/jira-script/request.rb', line 64

def to_json
JSON.generate(json_data.merge(config[:body]))
end

#to_sObject



68
69
70
# File 'lib/jira-script/request.rb', line 68

def to_s
json_data.merge(config[:body])
end

#uploadObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/jira-script/request.rb', line 123

def upload
   ret = nil
   url = [config[:host], config[:api_path], config[:resource]].join('/')
  _body = self.json_data.merge(config[:body])
  puts "url: #{url}" if config[:verbosity] >= 2
   puts "body: #{_body}" if config[:verbosity] >= 2

   Typhoeus::Request.post(
       url,
       ssl_verifypeer: false,
       userpwd: "#{config[:user]}:#{config[:password]}",
      body: _body,
      headers: {'Content-Type' => 'application/x-www-form-urlencoded' }.merge(config[:headers])
   )

end