Class: Issue

Inherits:
Object show all
Defined in:
lib/jirametrics/issue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw:, board:, timezone_offset: '+00:00') ⇒ Issue

Returns a new instance of Issue.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jirametrics/issue.rb', line 9

def initialize raw:, board:, timezone_offset: '+00:00'
  @raw = raw
  @timezone_offset = timezone_offset
  @subtasks = []
  @changes = []
  @board = board

  return unless @raw['changelog']

  load_history_into_changes

  # If this is an older pull of data then comments may not be there.
  load_comments_into_changes if @raw['fields']['comment']

  # It might appear that Jira already returns these in order but we've found different
  # versions of Server/Cloud return the changelog in different orders so we sort them.
  sort_changes!

  # It's possible to have a ticket created with certain things already set and therefore
  # not showing up in the change log. Create some artificial entries to capture those.
  @changes = [
    fabricate_change(field_name: 'status'),
    fabricate_change(field_name: 'priority')
  ].compact + @changes
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



6
7
8
# File 'lib/jirametrics/issue.rb', line 6

def board
  @board
end

#changesObject (readonly)

Returns the value of attribute changes.



6
7
8
# File 'lib/jirametrics/issue.rb', line 6

def changes
  @changes
end

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'lib/jirametrics/issue.rb', line 7

def parent
  @parent
end

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/jirametrics/issue.rb', line 6

def raw
  @raw
end

#subtasksObject (readonly)

Returns the value of attribute subtasks.



6
7
8
# File 'lib/jirametrics/issue.rb', line 6

def subtasks
  @subtasks
end

Instance Method Details

#<=>(other) ⇒ Object

Sort by key



483
484
485
486
487
488
489
# File 'lib/jirametrics/issue.rb', line 483

def <=> other
  /(?<project_code1>[^-]+)-(?<id1>.+)/ =~ key
  /(?<project_code2>[^-]+)-(?<id2>.+)/ =~ other.key
  comparison = project_code1 <=> project_code2
  comparison = id1 <=> id2 if comparison.zero?
  comparison
end

#all_subtask_activity_timesObject



373
374
375
376
377
378
379
# File 'lib/jirametrics/issue.rb', line 373

def all_subtask_activity_times
  subtask_activity_times = []
  @subtasks.each do |subtask|
    subtask_activity_times += subtask.changes.collect(&:time)
  end
  subtask_activity_times
end

#artificial?Boolean

It’s artificial if it wasn’t downloaded from a Jira instance.

Returns:

  • (Boolean)


478
479
480
# File 'lib/jirametrics/issue.rb', line 478

def artificial?
  @raw['exporter'].nil?
end

#assigned_toObject



224
225
226
# File 'lib/jirametrics/issue.rb', line 224

def assigned_to
  @raw['fields']&.[]('assignee')&.[]('displayName')
end

#authorObject



72
# File 'lib/jirametrics/issue.rb', line 72

def author = @raw['fields']['creator']&.[]('displayName') || ''

#blocked_on_date?(date, end_time:) ⇒ Boolean

Returns:

  • (Boolean)


234
235
236
237
238
239
# File 'lib/jirametrics/issue.rb', line 234

def blocked_on_date? date, end_time:
  blocked_stalled_changes_on_date(date: date, end_time: end_time) do |change|
    return true if change.blocked?
  end
  false
end

#blocked_stalled_changes(end_time:, settings: @board.project_config.settings) ⇒ Object



257
258
259
260
261
262
263
264
265
266
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/jirametrics/issue.rb', line 257

def blocked_stalled_changes end_time:, settings: @board.project_config.settings
  blocked_statuses = settings['blocked_statuses']
  stalled_statuses = settings['stalled_statuses']
  unless blocked_statuses.is_a?(Array) && stalled_statuses.is_a?(Array)
    raise "blocked_statuses(#{blocked_statuses.inspect}) and " \
      "stalled_statuses(#{stalled_statuses.inspect}) must both be arrays"
  end

  blocked_link_texts = settings['blocked_link_text']
  stalled_threshold = settings['stalled_threshold']

  blocking_issue_keys = []

  result = []
  previous_was_active = true
  previous_change_time = created

  blocking_status = nil
  flag = nil

  # This mock change is to force the writing of one last entry at the end of the time range.
  # By doing this, we're able to eliminate a lot of duplicated code in charts.
  mock_change = ChangeItem.new time: end_time, author: '', artificial: true, raw: { 'field' => '' }
  (changes + [mock_change]).each do |change|

    previous_was_active = false if check_for_stalled(
      change_time: change.time,
      previous_change_time: previous_change_time,
      stalled_threshold: stalled_threshold,
      blocking_stalled_changes: result
    )

    if change.flagged?
      flag = change.value
      flag = nil if change.value == ''
    elsif change.status?
      blocking_status = nil
      if blocked_statuses.include?(change.value) || stalled_statuses.include?(change.value)
        blocking_status = change.value
      end
    elsif change.link?
      unless /^This issue (?<link_text>.+) (?<issue_key>.+)$/ =~ (change.value || change.old_value)
        puts "Can't parse link text: #{change.value || change.old_value}"
        next
      end

      if blocked_link_texts.include? link_text
        if change.value
          blocking_issue_keys << issue_key
        else
          blocking_issue_keys.delete issue_key
        end
      end
    end

    new_change = BlockedStalledChange.new(
      flagged: flag,
      status: blocking_status,
      status_is_blocking: blocking_status.nil? || blocked_statuses.include?(blocking_status),
      blocking_issue_keys: (blocking_issue_keys.empty? ? nil : blocking_issue_keys.dup),
      time: change.time
    )

    # We don't want to dump two actives in a row as that would just be noise. Unless this is
    # the mock change, which we always want to dump
    result << new_change if !new_change.active? || !previous_was_active || change == mock_change

    previous_was_active = new_change.active?
    previous_change_time = change.time
  end

  if result.size >= 2
    # The existence of the mock entry will mess with the stalled count as it will wake everything
    # back up. This hack will clean up appropriately.
    hack = result.pop
    result << BlockedStalledChange.new(
      flagged: hack.flag,
      status: hack.status,
      status_is_blocking: hack.status_is_blocking,
      blocking_issue_keys: hack.blocking_issue_keys,
      time: hack.time,
      stalled_days: result[-1].stalled_days
    )
  end
  result
end

#blocked_stalled_changes_on_date(date:, end_time:) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/jirametrics/issue.rb', line 241

def blocked_stalled_changes_on_date date:, end_time:
  next_day_start_time = (date + 1).to_time
  this_day_start_time = date.to_time

  # changes_affecting_date = []
  previous_change_time = nil
  blocked_stalled_changes(end_time: end_time).each do |change|
    if previous_change_time.nil?
      previous_change_time = change.time
      next
    end

    yield change if previous_change_time < next_day_start_time && change.time >= this_day_start_time
  end
end

#check_for_stalled(change_time:, previous_change_time:, stalled_threshold:, blocking_stalled_changes:) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/jirametrics/issue.rb', line 344

def check_for_stalled change_time:, previous_change_time:, stalled_threshold:, blocking_stalled_changes:
  stalled_threshold_seconds = stalled_threshold * 60 * 60 * 24

  # The most common case will be nothing to split so quick escape.
  return false if (change_time - previous_change_time).to_i < stalled_threshold_seconds

  list = [previous_change_time..change_time]
  all_subtask_activity_times.each do |time|
    matching_range = list.find { |range| time >= range.begin && time <= range.end }
    next unless matching_range

    list.delete matching_range
    list << ((matching_range.begin)..time)
    list << (time..(matching_range.end))
  end

  inserted_stalled = false

  list.sort_by(&:begin).each do |range|
    seconds = (range.end - range.begin).to_i
    next if seconds < stalled_threshold_seconds

    an_hour_later = range.begin + (60 * 60)
    blocking_stalled_changes << BlockedStalledChange.new(stalled_days: seconds / (24 * 60 * 60), time: an_hour_later)
    inserted_stalled = true
  end
  inserted_stalled
end

#component_namesObject



85
86
87
# File 'lib/jirametrics/issue.rb', line 85

def component_names
  @raw['fields']['components']&.collect { |component| component['name'] } || []
end

#createdObject



207
208
209
210
# File 'lib/jirametrics/issue.rb', line 207

def created
  # This shouldn't be necessary and yet we've seen one case where it was.
  parse_time @raw['fields']['created'] if @raw['fields']['created']
end

#currently_in_status(*status_names) ⇒ Object

Are we currently in this status? If yes, then return the time of the most recent status change.



166
167
168
169
170
171
# File 'lib/jirametrics/issue.rb', line 166

def currently_in_status *status_names
  change = most_recent_status_change
  return false if change.nil?

  change.time if change.current_status_matches(*status_names)
end

#currently_in_status_category(*category_names) ⇒ Object

Are we currently in this status category? If yes, then return the time of the most recent status change.



174
175
176
177
178
179
180
# File 'lib/jirametrics/issue.rb', line 174

def currently_in_status_category *category_names
  change = most_recent_status_change
  return false if change.nil?

  status = find_status_by_name change.value
  change.time if status && category_names.include?(status.category_name)
end

#expedited?Boolean

Returns:

  • (Boolean)


381
382
383
384
385
386
387
# File 'lib/jirametrics/issue.rb', line 381

def expedited?
  names = @board&.expedited_priority_names
  return false unless names

  current_priority = raw['fields']['priority']&.[]('name')
  names.include? current_priority
end

#expedited_on_date?(date) ⇒ Boolean

Returns:

  • (Boolean)


389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/jirametrics/issue.rb', line 389

def expedited_on_date? date
  expedited_start = nil
  expedited_names = @board&.expedited_priority_names

  changes.each do |change|
    next unless change.priority?

    if expedited_names.include? change.value
      expedited_start = change.time.to_date if expedited_start.nil?
    else
      return true if expedited_start && (expedited_start..change.time.to_date).include?(date)

      expedited_start = nil
    end
  end

  return false if expedited_start.nil?

  expedited_start <= date
end

#fabricate_change(field_name:) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/jirametrics/issue.rb', line 89

def fabricate_change field_name:
  first_status = nil
  first_status_id = nil

  created_time = parse_time @raw['fields']['created']
  first_change = @changes.find { |change| change.field == field_name }
  if first_change.nil?
    # There have been no changes of this type yet so we have to look at the current one
    return nil unless @raw['fields'][field_name]

    first_status = @raw['fields'][field_name]['name']
    first_status_id = @raw['fields'][field_name]['id'].to_i
  else
    # Otherwise, we look at what the first one had changed away from.
    first_status = first_change.old_value
    first_status_id = first_change.old_value_id
  end
  ChangeItem.new time: created_time, artificial: true, author: author, raw: {
    'field' => field_name,
    'to' => first_status_id,
    'toString' => first_status
  }
end

#find_status_by_name(name) ⇒ Object



182
183
184
185
186
187
# File 'lib/jirametrics/issue.rb', line 182

def find_status_by_name name
  status = board.possible_statuses.find_by_name(name)
  return status if status

  raise "Status name #{name.inspect} for issue #{key} not found in #{board.possible_statuses.collect(&:name).inspect}"
end

#first_resolutionObject



216
217
218
# File 'lib/jirametrics/issue.rb', line 216

def first_resolution
  @changes.find { |change| change.resolution? }&.time
end

#first_status_change_after_createdObject



189
190
191
# File 'lib/jirametrics/issue.rb', line 189

def first_status_change_after_created
  @changes.find { |change| change.status? && change.artificial? == false }&.time
end

#first_time_in_or_right_of_column(column_name) ⇒ Object



121
122
123
# File 'lib/jirametrics/issue.rb', line 121

def first_time_in_or_right_of_column column_name
  first_time_in_status(*board.status_ids_in_or_right_of_column(column_name))
end

#first_time_in_status(*status_names) ⇒ Object



113
114
115
# File 'lib/jirametrics/issue.rb', line 113

def first_time_in_status *status_names
  @changes.find { |change| change.current_status_matches(*status_names) }&.time
end

#first_time_in_status_category(*category_names) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/jirametrics/issue.rb', line 193

def first_time_in_status_category *category_names
  @changes.each do |change|
    next unless change.status?

    category = find_status_by_name(change.value).category_name
    return change.time if category_names.include? category
  end
  nil
end

#first_time_not_in_status(*status_names) ⇒ Object



117
118
119
# File 'lib/jirametrics/issue.rb', line 117

def first_time_not_in_status *status_names
  @changes.find { |change| change.status? && status_names.include?(change.value) == false }&.time
end

#fix_versionsObject



435
436
437
438
439
440
441
442
# File 'lib/jirametrics/issue.rb', line 435

def fix_versions
  if @fix_versions.nil?
    @fix_versions = @raw['fields']['fixVersions']&.collect do |fix_version|
      FixVersion.new fix_version
    end || []
  end
  @fix_versions
end

#in_initial_query?Boolean

Returns:

  • (Boolean)


473
474
475
# File 'lib/jirametrics/issue.rb', line 473

def in_initial_query?
  @raw['exporter'].nil? || @raw['exporter']['in_initial_query']
end

#inspectObject

Many test failures are simply unreadable because the default inspect on this class goes on for pages. Shorten it up.



230
231
232
# File 'lib/jirametrics/issue.rb', line 230

def inspect
  "Issue(#{key.inspect})"
end


426
427
428
429
430
431
432
433
# File 'lib/jirametrics/issue.rb', line 426

def issue_links
  if @issue_links.nil?
    @issue_links = @raw['fields']['issuelinks']&.collect do |issue_link|
      IssueLink.new origin: self, raw: issue_link
    end || []
  end
  @issue_links
end

#keyObject



45
# File 'lib/jirametrics/issue.rb', line 45

def key = @raw['key']

#key_as_iObject



81
82
83
# File 'lib/jirametrics/issue.rb', line 81

def key_as_i
  $1.to_i if key =~ /-(\d+)$/
end

#labelsObject



70
# File 'lib/jirametrics/issue.rb', line 70

def labels = @raw['fields']['labels'] || []

#last_activity(now: Time.now) ⇒ Object

Return the last time there was any activity on this ticket. Starting from “now” and going backwards Returns nil if there was no activity before that time.



412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/jirametrics/issue.rb', line 412

def last_activity now: Time.now
  result = @changes.reverse.find { |change| change.time <= now }&.time

  # The only condition where this could be nil is if "now" is before creation
  return nil if result.nil?

  @subtasks.each do |subtask|
    subtask_last_activity = subtask.last_activity now: now
    result = subtask_last_activity if subtask_last_activity && subtask_last_activity > result
  end

  result
end

#last_resolutionObject



220
221
222
# File 'lib/jirametrics/issue.rb', line 220

def last_resolution
  @changes.reverse.find { |change| change.resolution? }&.time
end

#most_recent_status_changeObject



161
162
163
# File 'lib/jirametrics/issue.rb', line 161

def most_recent_status_change
  changes.reverse.find { |change| change.status? }
end

#parent_key(project_config: @board.project_config) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/jirametrics/issue.rb', line 444

def parent_key project_config: @board.project_config
  # Although Atlassian is trying to standardize on one way to determine the parent, today it's a mess.
  # We try a variety of ways to get the parent and hopefully one of them will work. See this link:
  # https://community.developer.atlassian.com/t/deprecation-of-the-epic-link-parent-link-and-other-related-fields-in-rest-apis-and-webhooks/54048

  fields = @raw['fields']

  # At some point in the future, this will be the only way to retrieve the parent so we try this first.
  parent = fields['parent']&.[]('key')

  # The epic field
  parent = fields['epic']&.[]('key') if parent.nil?

  # Otherwise the parent link will be stored in one of the custom fields. We've seen different custom fields
  # used for parent_link vs epic_link so we have to support more than one.
  if parent.nil? && project_config
    custom_field_names = project_config.settings['customfield_parent_links']
    custom_field_names = [custom_field_names] if custom_field_names.is_a? String

    custom_field_names&.each do |field_name|
      parent = fields[field_name]
      # A break would be more appropriate than a return but the runtime caused an error when we do that
      return parent if parent
    end
  end

  parent
end

#parse_time(text) ⇒ Object



203
204
205
# File 'lib/jirametrics/issue.rb', line 203

def parse_time text
  Time.parse(text).getlocal(@timezone_offset)
end

#resolutionObject



74
# File 'lib/jirametrics/issue.rb', line 74

def resolution = @raw['fields']['resolution']&.[]('name')

#sort_changes!Object



35
36
37
38
39
40
41
42
43
# File 'lib/jirametrics/issue.rb', line 35

def sort_changes!
  @changes.sort! do |a, b|
    # It's common that a resolved will happen at the same time as a status change.
    # Put them in a defined order so tests can be deterministic.
    compare = a.time <=> b.time
    compare = 1 if compare.zero? && a.resolution?
    compare
  end
end

#statusObject



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

def status
  raw_status = @raw['fields']['status']
  raw_category = raw_status['statusCategory']

  Status.new(
    name: raw_status['name'],
    id: raw_status['id'].to_i,
    category_name: raw_category['name'],
    category_id: raw_category['id'].to_i
  )
end

#status_idObject



65
66
67
68
# File 'lib/jirametrics/issue.rb', line 65

def status_id
  puts 'DEPRECATED(Issue.status_id) Call Issue.status.id instead'
  status.id
end

#still_in_or_right_of_column(column_name) ⇒ Object



125
126
127
# File 'lib/jirametrics/issue.rb', line 125

def still_in_or_right_of_column column_name
  still_in_status(*board.status_ids_in_or_right_of_column(column_name))
end

#still_in_status(*status_names) ⇒ Object

If it ever entered one of these statuses and it’s still there then what was the last time it entered



147
148
149
150
151
# File 'lib/jirametrics/issue.rb', line 147

def still_in_status *status_names
  still_in do |change|
    status_names.include?(change.value) || status_names.include?(change.value_id)
  end
end

#still_in_status_category(*category_names) ⇒ Object

If it ever entered one of these categories and it’s still there then what was the last time it entered



154
155
156
157
158
159
# File 'lib/jirametrics/issue.rb', line 154

def still_in_status_category *category_names
  still_in do |change|
    status = find_status_by_name change.value
    category_names.include?(status.category_name) || category_names.include?(status.category_id)
  end
end

#summaryObject



51
# File 'lib/jirametrics/issue.rb', line 51

def summary = @raw['fields']['summary']

#typeObject



47
# File 'lib/jirametrics/issue.rb', line 47

def type = @raw['fields']['issuetype']['name']

#type_icon_urlObject



49
# File 'lib/jirametrics/issue.rb', line 49

def type_icon_url = @raw['fields']['issuetype']['iconUrl']

#updatedObject



212
213
214
# File 'lib/jirametrics/issue.rb', line 212

def updated
  parse_time @raw['fields']['updated']
end

#urlObject



76
77
78
79
# File 'lib/jirametrics/issue.rb', line 76

def url
  # Strangely, the URL isn't anywhere in the returned data so we have to fabricate it.
  "#{@board.server_url_prefix}/browse/#{key}"
end