Class: ProjectConfig

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exporter:, jira_config:, block:, target_path: '.', name: '', id: nil) ⇒ ProjectConfig

Returns a new instance of ProjectConfig.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jirametrics/project_config.rb', line 12

def initialize exporter:, jira_config:, block:, target_path: '.', name: '', id: nil
  @exporter = exporter
  @block = block
  @file_configs = []
  @download_config = nil
  @target_path = target_path
  @jira_config = jira_config
  @possible_statuses = StatusCollection.new
  @name = name
  @board_configs = []
  @all_boards = {}
  @settings = load_settings
  @id = id
  @has_loaded_data = false
end

Instance Attribute Details

#aggregate_configObject (readonly)

Returns the value of attribute aggregate_config.



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

def aggregate_config
  @aggregate_config
end

#all_boardsObject (readonly)

Returns the value of attribute all_boards.



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

def all_boards
  @all_boards
end

#board_configsObject (readonly)

Returns the value of attribute board_configs.



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

def board_configs
  @board_configs
end

#data_versionObject (readonly)

Returns the value of attribute data_version.



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

def data_version
  @data_version
end

#discarded_changes_dataObject (readonly)

Returns the value of attribute discarded_changes_data.



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

def discarded_changes_data
  @discarded_changes_data
end

#download_configObject (readonly)

Returns the value of attribute download_config.



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

def download_config
  @download_config
end

#exporterObject (readonly)

Returns the value of attribute exporter.



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

def exporter
  @exporter
end

#file_configsObject (readonly)

Returns the value of attribute file_configs.



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

def file_configs
  @file_configs
end

#idObject

Returns the value of attribute id.



10
11
12
# File 'lib/jirametrics/project_config.rb', line 10

def id
  @id
end

#jira_configObject (readonly)

Returns the value of attribute jira_config.



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

def jira_config
  @jira_config
end

#jira_urlObject

Returns the value of attribute jira_url.



10
11
12
# File 'lib/jirametrics/project_config.rb', line 10

def jira_url
  @jira_url
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#possible_statusesObject (readonly)

Returns the value of attribute possible_statuses.



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

def possible_statuses
  @possible_statuses
end

#settingsObject (readonly)

Returns the value of attribute settings.



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

def settings
  @settings
end

#target_pathObject (readonly)

Returns the value of attribute target_path.



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

def target_path
  @target_path
end

#time_rangeObject

Returns the value of attribute time_range.



10
11
12
# File 'lib/jirametrics/project_config.rb', line 10

def time_range
  @time_range
end

#usersObject (readonly)

Returns the value of attribute users.



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

def users
  @users
end

Instance Method Details

#add_issues(issues_list) ⇒ Object

To be used by the aggregate_config only. Not intended to be part of the public API



393
394
395
396
397
398
399
400
401
402
# File 'lib/jirametrics/project_config.rb', line 393

def add_issues issues_list
  @issues = IssueCollection.new if @issues.nil?
  @all_boards = {}

  issues_list.each do |issue|
    @issues << issue
    board = issue.board
    @all_boards[board.id] = board unless @all_boards[board.id]
  end
end

#add_possible_status(status) ⇒ Object



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
248
249
# File 'lib/jirametrics/project_config.rb', line 222

def add_possible_status status
  existing_status = @possible_statuses.find_by_id status.id

  if existing_status && existing_status.name != status.name
    raise "Attempting to redefine the name for status #{status.id} from " \
      "#{existing_status.name.inspect} to #{status.name.inspect}"
  end

  # If it isn't there, add it and go.
  return @possible_statuses << status unless existing_status

  unless status == existing_status
    raise "Redefining status category for status #{status}. " \
      "original: #{existing_status.category}, " \
      "new: #{status.category}"
  end

  # We're registering one we already knew about. This may happen if someone specified a status_category_mapping
  # for something that was already returned from jira.
  #
  # You may be looking at this code and thinking of changing it to spit out a warning since obviously
  # the user has made a mistake. Unfortunately, they may not have made any mistake. Due to inconsistency with the
  # status API, it's possible for two different people to make a request to the same API at the same time and get
  # back a different set of statuses. So that means that some people might need more status/categories mappings than
  # other people for exactly the same instance. See this article for more on that API:
  # https://agiletechnicalexcellence.com/2024/04/12/jira-api-statuses.html
  existing_status
end

#aggregate(&block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/jirametrics/project_config.rb', line 96

def aggregate &block
  raise 'Not allowed to have multiple aggregate blocks in one project' if @aggregate_config
  raise 'Not allowed to have both an aggregate and a download section. Pick only one.' if @download_config

  @aggregate_config = AggregateConfig.new project_config: self, block: block

  # Processing of aggregates should only happen during the export
  return if @exporter.downloading?

  @aggregate_config.evaluate_next_level
end

#aggregated_project?Boolean

Returns:

  • (Boolean)


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

def aggregated_project?
  !!@aggregate_config
end

#anonymizeObject



539
540
541
# File 'lib/jirametrics/project_config.rb', line 539

def anonymize
  @anonymizer_needed = true
end

#anonymize_dataObject



543
544
545
# File 'lib/jirametrics/project_config.rb', line 543

def anonymize_data
  Anonymizer.new(project_config: self).run
end

#atlassian_document_formatObject



355
356
357
358
359
# File 'lib/jirametrics/project_config.rb', line 355

def atlassian_document_format
  @atlassian_document_format ||= AtlassianDocumentFormat.new(
    users: @users, timezone_offset: exporter.timezone_offset
  )
end

#attach_linked_issues(issue:, all_issues:) ⇒ Object



456
457
458
459
460
461
462
463
# File 'lib/jirametrics/project_config.rb', line 456

def attach_linked_issues issue:, all_issues:
  issue.issue_links.each do |link|
    if link.other_issue.artificial?
      other = all_issues.find { |i| i.key == link.other_issue.key }
      link.other_issue = other if other
    end
  end
end

#attach_parent(issue:, all_issues:) ⇒ Object



450
451
452
453
454
# File 'lib/jirametrics/project_config.rb', line 450

def attach_parent issue:, all_issues:
  parent_key = issue.parent_key
  parent = all_issues.find { |i| i.key == parent_key }
  issue.parent = parent if parent
end

#attach_subtasks(issue:, all_issues:) ⇒ Object



442
443
444
445
446
447
448
# File 'lib/jirametrics/project_config.rb', line 442

def attach_subtasks issue:, all_issues:
  issue.raw['fields']['subtasks']&.each do |subtask_element|
    subtask_key = subtask_element['key']
    subtask = all_issues.find { |i| i.key == subtask_key }
    issue.subtasks << subtask if subtask
  end
end

#board(id:, &block) ⇒ Object



108
109
110
111
112
# File 'lib/jirametrics/project_config.rb', line 108

def board id:, &block
  config = BoardConfig.new(id: id, block: block, project_config: self)
  config.run if data_downloaded?
  @board_configs << config
end

#data_downloaded?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/jirametrics/project_config.rb', line 32

def data_downloaded?
  file_system.file_exist? File.join(@target_path, "#{get_file_prefix}_meta.json")
end

#discard_changes_before(status_becomes: nil, &block) ⇒ Object



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/jirametrics/project_config.rb', line 551

def discard_changes_before status_becomes: nil, &block
  cycletimes_touched = Set.new
  if status_becomes
    status_becomes = [status_becomes] unless status_becomes.is_a? Array

    block = lambda do |issue|
      trigger_statuses = status_becomes.collect do |status_name|
        if status_name == :backlog
          issue.board.backlog_statuses
        else
          possible_statuses.find_all_by_name status_name
        end
      end.flatten

      next if trigger_statuses.empty?

      trigger_status_ids = trigger_statuses.collect(&:id)

      time = nil
      issue.status_changes.each do |change|
        time = change.time if trigger_status_ids.include?(change.value_id) # && change.artificial? == false
      end
      time
    end
  end

  issues.each do |issue|
    cutoff_time = block.call(issue)
    next if cutoff_time.nil?

    original_start_time = issue.board.cycletime.started_stopped_times(issue).first
    next if original_start_time.nil?

    issue.discard_changes_before cutoff_time
    cycletimes_touched << issue.board.cycletime

    next unless cutoff_time
    next if original_start_time > cutoff_time # ie the cutoff would have made no difference.

    (@discarded_changes_data ||= []) << {
      cutoff_time: cutoff_time,
      original_start_time: original_start_time,
      issue: issue
    }
  end

  cycletimes_touched.each { |c| c.flush_cache }
end

#download(&block) ⇒ Object



85
86
87
88
89
90
# File 'lib/jirametrics/project_config.rb', line 85

def download &block
  raise 'Not allowed to have multiple download blocks in one project' if @download_config
  raise 'Not allowed to have both an aggregate and a download section. Pick only one.' if @aggregate_config

  @download_config = DownloadConfig.new project_config: self, block: block
end

#evaluate_next_levelObject



28
29
30
# File 'lib/jirametrics/project_config.rb', line 28

def evaluate_next_level
  instance_eval(&@block) if @block
end

#file(&block) ⇒ Object



92
93
94
# File 'lib/jirametrics/project_config.rb', line 92

def file &block
  @file_configs << FileConfig.new(project_config: self, block: block)
end

#file_prefix(prefix) ⇒ Object



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

def file_prefix prefix
  # The file_prefix has to be set before almost everything else. It really should have been an attribute
  # on the project declaration itself. Hindsight is 20/20.

  # There can only be one of these
  if @file_prefix
    raise "file_prefix can only be set once. Was #{@file_prefix.inspect} and now changed to #{prefix.inspect}."
  end

  raise_if_prefix_already_used(prefix)

  @file_prefix = prefix

  # Yes, this is a wierd place to be initializing this. Unfortunately, it has to happen after the file_prefix
  # is set but before anything inside the project block is run. If only we had made file_prefix an attribute
  # on project, we wouldn't have this ugliness. 🤷‍♂️
  load_status_category_mappings
  load_status_history
  load_all_boards

  @file_prefix
end

#file_systemObject



547
548
549
# File 'lib/jirametrics/project_config.rb', line 547

def file_system
  @exporter.file_system
end

#find_board_by_id(board_id = nil) ⇒ Object



384
385
386
387
388
389
390
# File 'lib/jirametrics/project_config.rb', line 384

def find_board_by_id board_id = nil
  board = all_boards[board_id || guess_board_id]

  raise "Unable to find configuration for board_id: #{board_id}" if board.nil?

  board
end

#find_default_boardObject



465
466
467
468
469
470
471
472
473
474
# File 'lib/jirametrics/project_config.rb', line 465

def find_default_board
  default_board = all_boards.values.first
  raise "No boards found for project #{name.inspect}" if all_boards.empty?

  if all_boards.size != 1
    file_system.log "Multiple boards are in use for project #{name.inspect}. " \
      "Picked #{default_board.name.inspect} to attach issues to.", also_write_to_stderr: true
  end
  default_board
end

#find_ids_by_status_name_across_all_issues(name) ⇒ Object

Walk across all the issues and find any status with that name. Return a list of ids that match.



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/jirametrics/project_config.rb', line 157

def find_ids_by_status_name_across_all_issues name
  ids = Set.new

  issues.each do |issue|
    issue.changes.each do |change|
      next unless change.status?

      ids << change.value_id.to_i if change.value == name
      ids << change.old_value_id.to_i if change.old_value == name
    end
  end
  ids.to_a
end

#get_file_prefix(raise_if_not_set: true) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/jirametrics/project_config.rb', line 148

def get_file_prefix raise_if_not_set: true
  if @file_prefix.nil? && raise_if_not_set
    raise 'file_prefix has not been set yet. Move it to the top of the project declaration.'
  end

  @file_prefix
end

#group_filenames_and_board_ids(path:) ⇒ Object

Scan through the issues directory (path), select the filenames to be loaded and map them to board ids. It’s ok if there are multiple files for the same issue. We load the newest one and map all the other board ids appropriately.



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/jirametrics/project_config.rb', line 503

def group_filenames_and_board_ids path:
  hash = {}
  file_system.foreach(path) do |filename|
    # Matches either FAKE-123.json or FAKE-123-456.json
    if /^(?<key>[^-]+-\d+)(?<_>-(?<board_id>\d+))?\.json$/ =~ filename
      (hash[key] ||= []) << [filename, board_id&.to_i || :unknown]
    end
  end

  result = {}
  hash.values.collect do |list|
    if list.size == 1
      filename, board_id = *list.first
      result[filename] = board_id == :unknown ? board_id : [board_id]
    else
      max_time = nil
      max_board_id = nil
      max_filename = nil
      all_board_ids = []

      list.each do |filename, board_id|
        mtime = File.mtime(File.join(path, filename))
        if max_time.nil? || mtime > max_time
          max_time = mtime
          max_board_id = board_id
          max_filename = filename
        end
        all_board_ids << board_id unless board_id == :unknown
      end

      result[max_filename] = all_board_ids
    end
  end
  result
end

#guess_board_idObject



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/jirametrics/project_config.rb', line 367

def guess_board_id
  return nil if aggregated_project?

  unless all_boards&.size == 1
    message = "If the board_id isn't set then we look for all board configurations in the target" \
      ' directory. '
    if all_boards.empty?
      message += ' In this case, we couldn\'t find any configuration files in the target directory.'
    else
      message += 'If there is only one, we use that. In this case we found configurations for' \
        " the following board ids and this is ambiguous: #{all_boards.keys}"
    end
    raise message
  end
  all_boards.keys[0]
end

#guess_project_idObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jirametrics/project_config.rb', line 64

def guess_project_id
  return @id if @id

  previous_id = nil
  @all_boards.each_value do |board|
    project_id = board.project_id

    # If the id is ambiguous then return nil for now. The user will get an error later
    # in the case where we need it to be unambiguous. Sometimes we don't care and there's
    # no point forcing the user to enter a project id if we don't need it.
    return nil if previous_id && project_id && previous_id != project_id

    previous_id = project_id if project_id
  end
  previous_id
end

#issuesObject



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/jirametrics/project_config.rb', line 404

def issues
  unless @issues
    if aggregated_project?
      raise 'This is an aggregated project and issues should have been included with the include_issues_from ' \
        'declaration but none are here. Check your config.'
    end

    return @issues = IssueCollection.new if @exporter.downloading?
    raise 'No data found. Must do a download before an export' unless data_downloaded?

    load_data if all_boards.empty?

    timezone_offset = exporter.timezone_offset

    issues_path = File.join @target_path, "#{get_file_prefix}_issues"
    if File.exist?(issues_path) && File.directory?(issues_path)
      issues = load_issues_from_issues_directory path: issues_path, timezone_offset: timezone_offset
    else
      file_system.log "Can't find directory #{issues_path}. Has a download been done?", also_write_to_stderr: true
      return IssueCollection.new
    end

    # Attach related issues
    issues.each do |i|
      attach_subtasks issue: i, all_issues: issues
      attach_parent issue: i, all_issues: issues
      attach_linked_issues issue: i, all_issues: issues
    end

    # We'll have some issues that are in the list that weren't part of the initial query. Once we've
    # attached them in the appropriate places, remove any that aren't part of that initial set.
    issues.reject! { |i| !i.in_initial_query? } # rubocop:disable Style/InverseMethods
    @issues = issues
  end

  @issues
end

#load_all_boardsObject



251
252
253
254
255
256
257
258
# File 'lib/jirametrics/project_config.rb', line 251

def load_all_boards
  Dir.foreach(@target_path) do |file|
    next unless file =~ /^#{get_file_prefix}_board_(\d+)_configuration\.json$/

    board_id = $1.to_i
    load_board board_id: board_id, filename: "#{@target_path}#{file}"
  end
end

#load_board(board_id:, filename:) ⇒ Object



260
261
262
263
264
265
266
# File 'lib/jirametrics/project_config.rb', line 260

def load_board board_id:, filename:
  board = Board.new(
    raw: file_system.load_json(filename), possible_statuses: @possible_statuses
  )
  board.project_config = self
  @all_boards[board_id] = board
end

#load_dataObject



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

def load_data
  return if @has_loaded_data

  @has_loaded_data = true
  @id = guess_project_id
  
  load_sprints
  load_users
end

#load_issues_from_issues_directory(path:, timezone_offset:) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/jirametrics/project_config.rb', line 476

def load_issues_from_issues_directory path:, timezone_offset:
  issues = IssueCollection.new
  default_board = nil

  group_filenames_and_board_ids(path: path).each do |filename, board_ids|
    content = file_system.load_json(File.join(path, filename))
    if board_ids == :unknown
      boards = [(default_board ||= find_default_board)]
    else
      boards = board_ids.collect { |b| all_boards[b] }
    end

    boards.each do |board|
      if board.cycletime.nil?
        raise "The board declaration for board #{board.id} must come before the " \
          "first usage of 'issues' in the configuration"
      end
      issues << Issue.new(raw: content, timezone_offset: timezone_offset, board: board)
    end
  end

  issues
end

#load_project_metadataObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/jirametrics/project_config.rb', line 319

def 
  filename = File.join @target_path, "#{get_file_prefix}_meta.json"
  json = file_system.load_json(filename)

  @data_version = json['version'] || 1

  start = to_time(json['date_start'] || json['time_start']) # date_start is the current format. Time is the old.
  stop  = to_time(json['date_end'] || json['time_end'], end_of_day: true)

  # If no_earlier_than was set then make sure it's applied here.
  if download_config
    download_config.run
    no_earlier = download_config.no_earlier_than
    if no_earlier
      no_earlier = to_time(no_earlier.to_s)
      start = no_earlier if start < no_earlier
    end
  end

  @time_range = start..stop

  @jira_url = json['jira_url']
rescue Errno::ENOENT
  file_system.log "Can't load #{filename}. Have you done a download?", also_write_to_stderr: true
  raise
end

#load_settingsObject



59
60
61
62
# File 'lib/jirametrics/project_config.rb', line 59

def load_settings
  # This is the weird exception that we don't ever want mocked out so we skip FileSystem entirely.
  JSON.parse(File.read(File.join(__dir__, 'settings.json'), encoding: 'UTF-8'))
end

#load_sprintsObject



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/jirametrics/project_config.rb', line 294

def load_sprints
  file_system.foreach(@target_path) do |file|
    next unless file =~ /^#{get_file_prefix}_board_(\d+)_sprints_\d+.json$/

    file_path = File.join(@target_path, file)
    board = @all_boards[$1.to_i]
    unless board
      @exporter.file_system.log(
        'Found sprint data but can\'t find a matching board in config. ' \
          "File: #{file_path}, Boards: #{@all_boards.keys.sort}"
      )
      next
    end

    timezone_offset = exporter.timezone_offset
    file_system.load_json(file_path)['values']&.each do |json|
      board.sprints << Sprint.new(raw: json, timezone_offset: timezone_offset)
    end
  end

  @all_boards.each_value do |board|
    board.sprints.sort_by!(&:id)
  end
end

#load_status_category_mappingsObject



268
269
270
271
272
273
274
275
276
# File 'lib/jirametrics/project_config.rb', line 268

def load_status_category_mappings
  filename = File.join @target_path, "#{get_file_prefix}_statuses.json"
  return unless file_system.file_exist? filename

  file_system
    .load_json(filename)
    .map { |snippet| Status.from_raw(snippet) }
    .each { |status| add_possible_status status }
end

#load_status_historyObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/jirametrics/project_config.rb', line 278

def load_status_history
  filename = File.join @target_path, "#{get_file_prefix}_status_history.json"
  return unless file_system.file_exist? filename

  file_system.log '  Loading historical statuses', also_write_to_stderr: true
  file_system
    .load_json(filename)
    .map { |snippet| Status.from_raw(snippet) }
    .each { |status| possible_statuses.historical_status_mappings[status.to_s] = status.category }

  possible_statuses
rescue => e # rubocop:disable Style/RescueStandardError
  file_system.warning "Unable to load status history due to #{e.message.inspect}. If this is because of a " \
    'malformed file then it should be fixed on the next download.'
end

#load_usersObject



346
347
348
349
350
351
352
353
# File 'lib/jirametrics/project_config.rb', line 346

def load_users
  @users = []
  filename = File.join @target_path, "#{get_file_prefix}_users.json"
  return unless File.exist? filename

  json = file_system.load_json(filename)
  json.each { |user_data| @users << User.new(raw: user_data) }
end

#raise_if_prefix_already_used(prefix) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/jirametrics/project_config.rb', line 137

def raise_if_prefix_already_used prefix
  @exporter.project_configs.each do |project|
    next unless project.get_file_prefix(raise_if_not_set: false) == prefix && project.target_path == target_path

    raise "Project #{name.inspect} specifies file prefix #{prefix.inspect}, " \
      "but that is already used by project #{project.name.inspect} in the same target path #{target_path.inspect}. " \
      'This is almost guaranteed to be too much copy and paste in your configuration. ' \
      'File prefixes must be unique within a directory.'
  end
end

#run(load_only: false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jirametrics/project_config.rb', line 46

def run load_only: false
  return if @exporter.downloading?

  load_data unless aggregated_project?
  anonymize_data if @anonymizer_needed

  return if load_only

  @file_configs.each do |file_config|
    file_config.run
  end
end

#status_category_mapping(status:, category:) ⇒ Object



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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/jirametrics/project_config.rb', line 171

def status_category_mapping status:, category:
  return if @exporter.downloading?

  status, status_id = possible_statuses.parse_name_id status
  category, category_id = possible_statuses.parse_name_id category

  if status_id.nil?
    guesses = find_ids_by_status_name_across_all_issues status
    if guesses.empty?
      file_system.warning "For status_category_mapping status: #{status.inspect}, category: #{category.inspect}\n" \
        "Cannot guess status id for #{status.inspect} as no statuses found anywhere in the issues " \
        "histories with that name. Since we can't find it, you probably don't need this mapping anymore so we're " \
        "going to ignore it. If you really want it, then you'll need to specify a status id."
      return
    end

    if guesses.size > 1
      raise "Cannot guess status id as there are multiple ids for the name #{status.inspect}. Perhaps it's one " \
        "of #{guesses.to_a.sort.inspect}. If you need this mapping then you must specify the status_id."
    end

    status_id = guesses.first
    file_system.log "status_category_mapping for #{status.inspect} has been mapped to id #{status_id}. " \
      "If that's incorrect then specify the status_id."
  end

  possible_categories = possible_statuses.find_all_categories_by_name category
  if possible_categories.empty?
    all = possible_statuses.find_all_categories.join(', ')
    raise "No status categories found for name #{category.inspect} in [#{all}]. " \
      'Either fix the name or add an ID.'
  elsif possible_categories.size > 1
    # Theoretically impossible and yet we've seen wierder things out of Jira so we're prepared.
    raise "More than one status category found with the name #{category.inspect} in " \
      "[#{possible_categories.join(', ')}]. Either fix the name or add an ID"
  end

  found_category = possible_categories.first

  if category_id && category_id != found_category.id
    raise "ID is incorrect for status category #{category.inspect}. Did you mean #{found_category.id}?"
  end

  add_possible_status(
    Status.new(
      name: status, id: status_id,
      category_name: category, category_id: found_category.id, category_key: found_category.key
    )
  )
end

#to_time(string, end_of_day: false) ⇒ Object



361
362
363
364
365
# File 'lib/jirametrics/project_config.rb', line 361

def to_time string, end_of_day: false
  time = end_of_day ? '23:59:59' : '00:00:00'
  string = "#{string}T#{time}#{exporter.timezone_offset}" if string.match?(/^\d{4}-\d{2}-\d{2}$/)
  Time.parse string
end