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
27
# 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
  @fix_versions = []
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

#fix_versionsObject (readonly)

Returns the value of attribute fix_versions.



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

def fix_versions
  @fix_versions
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



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

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



255
256
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
# File 'lib/jirametrics/project_config.rb', line 255

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



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/jirametrics/project_config.rb', line 118

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

#aggregate_project_namesObject



101
102
103
104
105
# File 'lib/jirametrics/project_config.rb', line 101

def aggregate_project_names
  return [] unless aggregated_project?

  @aggregate_config.included_projects.filter_map(&:name)
end

#aggregated_project?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/jirametrics/project_config.rb', line 97

def aggregated_project?
  !!@aggregate_config
end

#anonymizeObject



601
602
603
# File 'lib/jirametrics/project_config.rb', line 601

def anonymize
  @anonymizer_needed = true
end

#anonymize_dataObject



605
606
607
# File 'lib/jirametrics/project_config.rb', line 605

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

#atlassian_document_formatObject



416
417
418
419
420
# File 'lib/jirametrics/project_config.rb', line 416

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

#attach_github_prsObject



403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/jirametrics/project_config.rb', line 403

def attach_github_prs
  filename = File.join(@target_path, "#{get_file_prefix}_github_prs.json")
  return unless File.exist?(filename)

  prs_by_issue_key = Hash.new { |h, k| h[k] = [] }
  file_system.load_json(filename).each do |raw|
    pr = PullRequest.new(raw: raw)
    pr.issue_keys.each { |key| prs_by_issue_key[key] << pr }
  end

  @issues.each { |issue| issue.github_prs = prs_by_issue_key[issue.key] }
end

#attach_linked_issues(issue:, all_issues:) ⇒ Object



518
519
520
521
522
523
524
525
# File 'lib/jirametrics/project_config.rb', line 518

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



512
513
514
515
516
# File 'lib/jirametrics/project_config.rb', line 512

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



504
505
506
507
508
509
510
# File 'lib/jirametrics/project_config.rb', line 504

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



130
131
132
133
134
# File 'lib/jirametrics/project_config.rb', line 130

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)


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

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



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# File 'lib/jirametrics/project_config.rb', line 613

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

    status_becomes.each { |status_name| validate_discard_status status_name }

    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.started_stopped_times.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



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

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



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

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

#file(&block) ⇒ Object



114
115
116
# File 'lib/jirametrics/project_config.rb', line 114

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

#file_prefix(prefix) ⇒ Object



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

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



609
610
611
# File 'lib/jirametrics/project_config.rb', line 609

def file_system
  @exporter.file_system
end

#find_board_by_id(board_id = nil) ⇒ Object



445
446
447
448
449
450
451
# File 'lib/jirametrics/project_config.rb', line 445

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



527
528
529
530
531
532
533
534
535
536
# File 'lib/jirametrics/project_config.rb', line 527

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.



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/jirametrics/project_config.rb', line 190

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



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

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.



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
599
# File 'lib/jirametrics/project_config.rb', line 565

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



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/jirametrics/project_config.rb', line 428

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jirametrics/project_config.rb', line 80

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



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/jirametrics/project_config.rb', line 465

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
    attach_github_prs
  end

  @issues
end

#load_all_boardsObject



284
285
286
287
288
289
290
291
# File 'lib/jirametrics/project_config.rb', line 284

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



293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/jirametrics/project_config.rb', line 293

def load_board board_id:, filename:
  raw = file_system.load_json(filename)

  features_filename = File.join(@target_path, "#{get_file_prefix}_board_#{board_id}_features.json")
  features = if file_system.file_exist?(features_filename)
               BoardFeature.from_raw(file_system.load_json(features_filename))
             else
               []
             end

  board = Board.new(raw: raw, possible_statuses: @possible_statuses, features: features)
  board.project_config = self
  @all_boards[board_id] = board
end

#load_dataObject



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

def load_data
  return if @has_loaded_data

  @has_loaded_data = true
  @id = guess_project_id
  
  load_sprints
  load_fix_versions
  load_users
  resolve_blocked_stalled_status_settings
end

#load_fix_versionsObject



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

def load_fix_versions
  filename = File.join(@target_path, "#{get_file_prefix}_fix_versions.json")
  return unless file_system.file_exist?(filename)

  @fix_versions = file_system.load_json(filename).map { |raw| FixVersion.new(raw) }
end

#load_issues_from_issues_directory(path:, timezone_offset:) ⇒ Object



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/jirametrics/project_config.rb', line 538

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



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/jirametrics/project_config.rb', line 367

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



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

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

  if settings['blocked_color']
    file_system.deprecated message: 'blocked color should be set via css now', date: '2024-05-03'
  end
  if settings['stalled_color']
    file_system.deprecated message: 'stalled color should be set via css now', date: '2024-05-03'
  end

  settings['blocked_statuses'] = StatusCollection.new
  settings['stalled_statuses'] = StatusCollection.new

  stringify_keys(settings)
end

#load_sprintsObject



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/jirametrics/project_config.rb', line 334

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

    board_id = $1.to_i
    file_path = File.join(@target_path, file)
    board = @all_boards[board_id]
    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



308
309
310
311
312
313
314
315
316
# File 'lib/jirametrics/project_config.rb', line 308

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



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/jirametrics/project_config.rb', line 318

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



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

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



170
171
172
173
174
175
176
177
178
179
# File 'lib/jirametrics/project_config.rb', line 170

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

#resolve_blocked_stalled_status_settingsObject



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/jirametrics/project_config.rb', line 672

def resolve_blocked_stalled_status_settings
  %w[blocked_statuses stalled_statuses].each do |key|
    next if @settings[key].is_a?(StatusCollection)

    collection = StatusCollection.new
    @settings[key].each do |identifier|
      statuses = @possible_statuses.find_all_by_name(identifier)
      if statuses.empty?
        file_system.warning "Status #{identifier.inspect} in #{key} not found. Ignoring."
      else
        statuses.each { |status| collection << status }
      end
    end
    @settings[key] = collection
  end
end

#run(load_only: false) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jirametrics/project_config.rb', line 49

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

  load_data unless aggregated_project?

  return if load_only

  anonymize_data if @anonymizer_needed

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

#status_category_mapping(status:, category:) ⇒ Object



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
248
249
250
251
252
253
# File 'lib/jirametrics/project_config.rb', line 204

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

#stringify_keys(value) ⇒ Object



664
665
666
667
668
669
670
# File 'lib/jirametrics/project_config.rb', line 664

def stringify_keys value
  case value
  when Hash then value.transform_keys(&:to_s).transform_values { |v| stringify_keys(v) }
  when Array then value.map { |v| stringify_keys(v) }
  else value
  end
end

#to_time(string, end_of_day: false) ⇒ Object



422
423
424
425
426
# File 'lib/jirametrics/project_config.rb', line 422

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

#validate_discard_status(status_name) ⇒ Object



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

def validate_discard_status status_name
  return if status_name == :backlog
  return if possible_statuses.empty? # not yet downloaded; skip validation

  found = possible_statuses.find_all_by_name status_name
  return unless found.empty?

  raise "discard_changes_before: Status #{status_name.inspect} not found. " \
    "Possible statuses are: #{possible_statuses}"
end