Module: Roust::Ticket

Included in:
Roust
Defined in:
lib/roust/ticket.rb

Instance Method Summary collapse

Instance Method Details

#ticket_comment(id, attrs) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/roust/ticket.rb', line 85

def ticket_comment(id, attrs)
  attrs['Text'].gsub!(/\n/, "\n ") if attrs['Text'] # insert a space on continuation lines.
  content = compose_content('ticket', id, attrs)

  unless content.match(/Action: (comment|correspond)/i)
    raise "'Action' must be one of 'Comment' or 'Correspond'"
  end

  response = self.class.post(
    "/ticket/#{id}/comment",
    :body => {
      :content => content
    },
  )

    body, _ = explode_response(response)

    case body
    when /^# (Message recorded|Comments added)/
      ticket_show(id)
    when /^# You are not allowed to modify ticket \d+/
      raise Unauthorized, body
    when /^# Syntax error/
      raise SyntaxError, body
    else
      raise UnhandledResponse, body
    end
end

#ticket_create(attrs) ⇒ Object Also known as: create

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/roust/ticket.rb', line 13

def ticket_create(attrs)
  default_attrs = {
    'id' => 'ticket/new'
  }
  attrs = default_attrs.merge(attrs).stringify_keys!

  error = create_invalid?(attrs)
  raise InvalidRecord, error if error

  attrs['Text'].gsub!(/\n/, "\n ") if attrs['Text'] # insert a space on continuation lines.

  # We can't set more than one AdminCc when creating a ticket. WTF RT.
  #
  # Delete it from the ticket we are creating, and we'll update the ticket
  # after we've created.
  key, admincc = attrs.detect {|k,v| k =~ /admincc/i }
  attrs.delete(key)

  content = compose_content('ticket', attrs['id'], attrs)

  response = self.class.post(
    '/ticket/new',
    :body => {
      :content => content
    }
  )

  body, _ = explode_response(response)

  case body
  when /^# Ticket (\d+) created/
    id = $1
    # Add the AdminCc after the ticket is created, because we can't set it
    # on ticket creation.
    ticket_update(id, 'AdminCc' => admincc) if admincc

    # Return the whole ticket, not just the id.
    ticket_show(id)
  when /^# Could not create ticket/
    raise BadRequest, body
  when /^# Syntax error/
    raise SyntaxError, body
  else
    raise UnhandledResponse, body
  end
end

#ticket_history(id, opts = {}) ⇒ Object Also known as: history



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/roust/ticket.rb', line 144

def ticket_history(id, opts = {})
  options = {
    :format   => 'short',
    :comments => false
  }.merge(opts)

  format   = options[:format]
  comments = options[:comments]
  params = {
    :format => format[0]
  }

  response = self.class.get("/ticket/#{id}/history", :query => params)

  body, _ = explode_response(response)

  case format
  when 'short'
    parse_short_history(body, :comments => comments)
  when 'long'
    parse_long_history(body, :comments => comments)
  end
end

Add links on a ticket.

Example attrs:

{
  "RefersTo" => [
    "http://us.example",
    "http://them.example",
  ]
}

Parameters:

  • id (Fixnum)

    the id of the ticket to add links on.

  • attrs (Hash)

    the links to add.

Returns:

  • (Hash)

    all the links on the ticket after the add action.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/roust/ticket.rb', line 199

def ticket_links_add(id, attrs)
  # Get the current links state
  current_links = ticket_links_show(id)
  current_links.delete('id')
  desired_links = Marshal.load(Marshal.dump(current_links))

  # Build up the desired link state
  attrs.each do |k,v|
    desired_links[k] ||= []
    v.each do |link|
      desired_links[k] << link
    end
    desired_links[k].uniq!
  end

  # Remove all links before we add any new ones. Fucking RT API.
  ticket_links_remove(id, current_links)

  # Work out how many times we'll need to make the same request until we
  # get the desired state.
  tries = desired_links.max_by {|k,v| v.size }.last.size

  tries.times do
    content = compose_content('ticket', id, desired_links)

    response = self.class.post(
      "/ticket/#{id}/links",
      :body => {
        :content => content
      }
    )

    body, _ = explode_response(response)

    case body
    when /^# Links for ticket (\d+) updated/
      id = $1
      #ticket_links_show(id)
    when /^# You are not allowed to modify ticket \d+/
      raise Unauthorized, body
    when /^# Syntax error/
      raise SyntaxError, body
    else
      raise UnhandledResponse, body
    end
  end

  ticket_links_show(id)
end

Remove links on a ticket.

Example attrs:

{
  "DependsOn" => [
    "http://us.example",
    "http://them.example",
  ],
  "RefersTo" => [
    "http://others.example",
  ],
}

Parameters:

  • id (Fixnum)

    the id of the ticket to remove links on.

  • attrs (Hash)

    the links to remove.

Returns:

  • (Hash)

    all the links on the ticket after the remove action.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/roust/ticket.rb', line 267

def ticket_links_remove(id, attrs)
  # Get the current links state
  current_links = ticket_links_show(id)
  desired_links = Marshal.load(Marshal.dump(current_links))

  # Build up the desired link state
  attrs.each do |k,v|
    v.each do |link|
      desired_links[k].delete(link) if desired_links[k]
    end
  end

  # Work out how many times we'll need to make the same request until we
  # get the desired state.
  tries = attrs.empty? ? 0 : attrs.max_by {|k,v| v.size }.last.size

  tries.times do
    content = compose_content('ticket', id, desired_links)

    response = self.class.post(
      "/ticket/#{id}/links",
      :body => {
        :content => content
      }
    )

    body, _ = explode_response(response)

    case body
    when /^# Links for ticket (\d+) updated/
      id = $1
    when /^# You are not allowed to modify ticket \d+/
      raise Unauthorized, body
    when /^# Syntax error/
      raise SyntaxError, body
    else
      raise UnhandledResponse, body
    end
  end

  ticket_links_show(id)
end


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/roust/ticket.rb', line 168

def ticket_links_show(id)
  response = self.class.get("/ticket/#{id}/links")
  body, _ = explode_response(response)

  hash = body_to_hash(body)
  id = hash.delete('id').split('/')[1]
  cleaned_hash = hash.map do |k, v|
    ids = v.split(/\s*,\s*/).map do |url|
      url =~ /^fsck\.com\-/ ? url.split('/').last : url
    end
    [ k, ids ]
  end

  Hash[cleaned_hash].merge('id' => id)
end

#ticket_search(attrs) ⇒ Object Also known as: search

Raises:

  • (ArgumentError)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/roust/ticket.rb', line 114

def ticket_search(attrs)
  params = {
    :format  => 's',
    :orderby => '+id'
  }.merge(attrs)

  params[:format] = 'l' if verbose = params.delete(:verbose)

  # FIXME(auxesis): query should be an actual method argument
  raise ArgumentError, ":query not specified" unless params[:query]

  response = self.class.get('/search/ticket', :query => params)

  body, _ = explode_response(response)

  return [] if body =~ /^No matching results\./

  if verbose
    results = body.split("\n--\n\n")
    results.map do |result_body|
      parse_ticket_attributes(result_body)
    end
  else
    body.split("\n").map do |t|
      id, subject = t.split(': ', 2)
      {'id' => id, 'Subject' => subject}
    end
  end
end

#ticket_show(id) ⇒ Object Also known as: show



3
4
5
6
7
8
9
10
11
# File 'lib/roust/ticket.rb', line 3

def ticket_show(id)
  response = self.class.get("/ticket/#{id}/show")

  body, _ = explode_response(response)

  return nil if body =~ /^# (Ticket (\d+) does not exist\.)/

  parse_ticket_attributes(body)
end

#ticket_update(id, attrs) ⇒ Object Also known as: update



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/roust/ticket.rb', line 60

def ticket_update(id, attrs)
  content = compose_content('ticket', id, attrs)

  response = self.class.post(
    "/ticket/#{id}/edit",
    :body => {
      :content => content
    },
  )

    body, _ = explode_response(response)

    case body
    when /^# Ticket (\d+) updated/
      id = $1
      ticket_show(id)
    when /^# You are not allowed to modify ticket \d+/
      raise Unauthorized, body
    when /^# Syntax error/
      raise SyntaxError, body
    else
      raise UnhandledResponse, body
    end
end