Class: Anonymizer

Inherits:
ChartBase show all
Defined in:
lib/jirametrics/anonymizer.rb

Constant Summary

Constants inherited from ChartBase

ChartBase::LABEL_POSITIONS, ChartBase::OKABE_ITO_PALETTE

Instance Attribute Summary collapse

Attributes inherited from ChartBase

#aggregated_project, #all_boards, #atlassian_document_format, #board_id, #canvas_height, #canvas_width, #data_quality, #date_range, #file_system, #fix_versions, #holiday_dates, #settings, #time_range, #timezone_offset, #x_axis_title, #y_axis_title

Instance Method Summary collapse

Methods inherited from ChartBase

#aggregated_project?, #before_run, #call_before_run, #canvas, #canvas_responsive?, #chart_format, #collapsible_issues_panel, #color_block, #color_for, #completed_issues_in_range, #current_board, #cycletime, #cycletime_for_issue, #daily_chart_dataset, #date_annotation, #describe_non_working_days, #description_text, #format_integer, #format_status, #header_text, #holidays, #html_directory, #icon_span, #link_to_issue, #next_id, #non_working_day?, #normalize_annotation_datetime, #not_visible_icon, #not_visible_text, #random_color, #render, #render_axis_title, #render_top_text, #resolve_status, #stagger_label_positions, #status_category_color, #to_human_readable, #working_days_annotation, #wrap_and_render

Constructor Details

#initialize(project_config:, date_adjustment: -200) ⇒ Anonymizer

Returns a new instance of Anonymizer.



9
10
11
12
13
14
15
16
17
# File 'lib/jirametrics/anonymizer.rb', line 9

def initialize project_config:, date_adjustment: -200
  super()
  @project_config = project_config
  @issues = @project_config.issues
  @all_boards = @project_config.all_boards
  @possible_statuses = @project_config.possible_statuses
  @date_adjustment = date_adjustment
  @file_system = project_config.exporter.file_system
end

Instance Attribute Details

#issuesObject (readonly)

needed for testing



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

def issues
  @issues
end

#project_configObject (readonly)

needed for testing



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

def project_config
  @project_config
end

Instance Method Details

#anonymize_board_namesObject



188
189
190
191
192
# File 'lib/jirametrics/anonymizer.rb', line 188

def anonymize_board_names
  @all_boards.each_value do |board|
    board.raw['name'] = "#{random_phrase} board"
  end
end

#anonymize_change_content(issue, seen_author_raws) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/jirametrics/anonymizer.rb', line 73

def anonymize_change_content issue, seen_author_raws
  issue.changes.each do |change|
    anonymize_author_raw change.author_raw, seen_author_raws
    next unless change.comment? || change.description?

    change.value = nil
    change.old_value = nil
  end
end

#anonymize_column_namesObject



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/jirametrics/anonymizer.rb', line 126

def anonymize_column_names
  @all_boards.each_key do |board_id|
    @file_system.log "Anonymizing column names for board #{board_id}"

    column_name = 'Column-A'
    @all_boards[board_id].visible_columns.each do |column|
      column.name = column_name
      column_name = column_name.next
    end
  end
end

#anonymize_fix_versionsObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jirametrics/anonymizer.rb', line 105

def anonymize_fix_versions
  version_counter = 0
  version_name_map = {}
  @issues.each do |issue|
    issue.raw['fields']['fixVersions']&.each do |fix_version|
      name = fix_version['name']
      unless version_name_map[name]
        version_counter += 1
        version_name_map[name] = "Version-#{version_counter}"
      end
      fix_version['name'] = version_name_map[name]
    end
  end
end

#anonymize_issue_keys_and_titles(issues: @issues) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jirametrics/anonymizer.rb', line 46

def anonymize_issue_keys_and_titles issues: @issues
  counter = 0
  seen_author_raws = {}.compare_by_identity
  issues.each do |issue|
    issue.raw['key'] = "ANON-#{counter += 1}"
    anonymize_issue_summary_fields issue
    anonymize_author_raw issue.raw['fields']['creator'], seen_author_raws
    anonymize_change_content issue, seen_author_raws

    issue.issue_links.each do |link|
      other_issue = link.other_issue
      next if other_issue.key.match?(/^ANON-\d+$/) # Already anonymized?

      other_issue.raw['key'] = "ANON-#{counter += 1}"
      other_issue.raw['fields']['summary'] = random_phrase
    end
  end
end

#anonymize_issue_summary_fields(issue) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/jirametrics/anonymizer.rb', line 65

def anonymize_issue_summary_fields issue
  issue.raw['fields']['summary'] = random_phrase
  issue.raw['fields']['description'] = nil
  return if issue.raw['fields']['assignee'].nil?

  issue.raw['fields']['assignee']['displayName'] = random_name
end

#anonymize_labels_and_componentsObject



83
84
85
86
87
88
# File 'lib/jirametrics/anonymizer.rb', line 83

def anonymize_labels_and_components
  @issues.each do |issue|
    issue.raw['fields']['labels'] = []
    issue.raw['fields']['components'] = []
  end
end

#anonymize_server_urlObject



120
121
122
123
124
# File 'lib/jirametrics/anonymizer.rb', line 120

def anonymize_server_url
  @all_boards.each_value do |board|
    board.raw['self'] = board.raw['self']&.sub(%r{^https?://[^/]+}, 'https://anon.example.com')
  end
end

#anonymize_sprintsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/jirametrics/anonymizer.rb', line 90

def anonymize_sprints
  sprint_counter = 0
  sprint_name_map = {}
  @all_boards.each_value do |board|
    board.sprints.each do |sprint|
      name = sprint.raw['name']
      unless sprint_name_map[name]
        sprint_counter += 1
        sprint_name_map[name] = "Sprint-#{sprint_counter}"
      end
      sprint.raw['name'] = sprint_name_map[name]
    end
  end
end

#random_nameObject



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
# File 'lib/jirametrics/anonymizer.rb', line 153

def random_name
  # Names generated from https://www.random-name-generator.com
  [
    'Benjamin Pelletier',
    'Levi Scott',
    'Emilia Leblanc',
    'Victoria Singh',
    'Theodore King',
    'Amelia Kelly',
    'Samuel Jones',
    'Lucy Kelly',
    'Oliver Fortin',
    'Riley Murphy',
    'Elijah Stewart',
    'Elizabeth Murphy',
    'Declan Simard',
    'Myles Singh',
    'Jayden Smith',
    'Sophie Richard',
    'Levi Mitchell',
    'Alexander Davis',
    'Sebastian Thompson',
    'Logan Robinson',
    'Madison Girard',
    'Ellie King',
    'Aiden Miller',
    'Ethan Anderson',
    'Scarlett Murray',
    'Audrey Moore',
    'Emmett Reid',
    'Jacob Poirier',
    'Violet MacDonald'
  ].sample
end

#random_phraseObject



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

def random_phrase
  # RandomWord periodically blows up for no reason we can determine. If it throws an exception then
  # just try again. In every case we've seen, it's worked on the second attempt, but we'll be
  # cautious and try five times.
  5.times do |i|
    return RandomWord.phrases.next.tr('_', ' ')
  rescue # rubocop:disable Style/RescueStandardError
    @file_system.log "Random word blew up on attempt #{i + 1}"
  end
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jirametrics/anonymizer.rb', line 19

def run
  anonymize_issue_keys_and_titles
  anonymize_column_names
  # Status names are deliberately left alone. They show up throughout the reports (hover text, aging
  # tables, the board columns themselves), so scrubbing them to tokens would make those displays
  # useless -- you'd lose the workflow story that makes the report worth reading. If a client's status
  # vocabulary is itself confidential, rename the statuses in Jira rather than anonymizing here.
  anonymize_board_names
  anonymize_labels_and_components
  anonymize_sprints
  anonymize_fix_versions
  anonymize_server_url
  shift_all_dates unless @date_adjustment.zero?
  @file_system.log 'Anonymize done'
end

#shift_all_dates(date_adjustment: @date_adjustment) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/jirametrics/anonymizer.rb', line 138

def shift_all_dates date_adjustment: @date_adjustment
  adjustment_in_seconds = 60 * 60 * 24 * date_adjustment
  @file_system.log "Shifting all dates by #{label_days date_adjustment}"
  @issues.each do |issue|
    issue.changes.each do |change|
      change.time = change.time + adjustment_in_seconds
    end

    issue.raw['fields']['updated'] = (issue.updated + adjustment_in_seconds).to_s
  end

  range = @project_config.time_range
  @project_config.time_range = (range.begin + adjustment_in_seconds)..(range.end + adjustment_in_seconds)
end