Class: Anonymizer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Anonymizer.



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

def initialize project_config:, date_adjustment: -200
  @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



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

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

#anonymize_column_namesObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jirametrics/anonymizer.rb', line 57

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_issue_keys_and_titles(issues: @issues) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jirametrics/anonymizer.rb', line 38

def anonymize_issue_keys_and_titles issues: @issues
  counter = 0
  issues.each do |issue|
    new_key = "ANON-#{counter += 1}"

    issue.raw['key'] = new_key
    issue.raw['fields']['summary'] = random_phrase
    issue.raw['fields']['assignee']['displayName'] = random_name unless issue.raw['fields']['assignee'].nil?

    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_statusesObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jirametrics/anonymizer.rb', line 96

def anonymize_issue_statuses
  @file_system.log 'Anonymizing issue statuses and status categories'
  status_name_hash = build_status_name_hash

  @issues.each do |issue|
    # This is where we create URL's from
    issue.raw['self'] = nil

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

      status_key = change.value
      anonymized_value = status_name_hash[status_key]
      raise "status_name_hash[#{status_key.inspect} is nil" if anonymized_value.nil?

      change.value = anonymized_value

      next if change.old_value.nil?

      status_key = change.old_value
      anonymized_value = status_name_hash[status_key]
      raise "status_name_hash[#{status_key.inspect} is nil" if anonymized_value.nil?

      change.old_value = anonymized_value
    end
  end

  @possible_statuses.each do |status|
    status_key = status.name
    if status_name_hash[status_key].nil?
      raise "Can't find status_key #{status_key.inspect} in #{status_name_hash.inspect}"
    end

    status.name = status_name_hash[status_key] unless status_name_hash[status_key].nil?
  end
end

#build_status_name_hashObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jirametrics/anonymizer.rb', line 69

def build_status_name_hash
  next_status = 'a'
  status_name_hash = {}
  @issues.each do |issue|
    issue.changes.each do |change|
      next unless change.status?

      # TODO: Do old value too
      status_key = change.value
      if status_name_hash[status_key].nil?
        status_name_hash[status_key] = "status-#{next_status}"
        next_status = next_status.next
      end
    end
  end

  @possible_statuses.each do |status|
    status_key = status.name
    if status_name_hash[status_key].nil?
      status_name_hash[status_key] = "status-#{next_status}"
      next_status = next_status.next
    end
  end

  status_name_hash
end

#random_nameObject



147
148
149
150
151
152
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
# File 'lib/jirametrics/anonymizer.rb', line 147

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



27
28
29
30
31
32
33
34
35
36
# File 'lib/jirametrics/anonymizer.rb', line 27

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 We don't care what exception was thrown.
    @file_system.log "Random word blew up on attempt #{i + 1}"
  end
end

#runObject



18
19
20
21
22
23
24
25
# File 'lib/jirametrics/anonymizer.rb', line 18

def run
  anonymize_issue_keys_and_titles
  anonymize_column_names
  # anonymize_issue_statuses
  anonymize_board_names
  shift_all_dates unless @date_adjustment.zero?
  @file_system.log 'Anonymize done'
end

#shift_all_datesObject



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/jirametrics/anonymizer.rb', line 133

def shift_all_dates
  @file_system.log "Shifting all dates by #{@date_adjustment} days"
  @issues.each do |issue|
    issue.changes.each do |change|
      change.time = change.time + @date_adjustment
    end

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

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