-
Notifications
You must be signed in to change notification settings - Fork 1
/
21-jira_update_epics.rb
276 lines (241 loc) · 9.78 KB
/
21-jira_update_epics.rb
1
2
3
4
5
6
7
8
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
95
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# frozen_string_literal: true
### EXPERIMENTAL
load './lib/common.rb'
### --- Jira tickets --- ###
# jira-tickets.csv: result,retries,message,jira_ticket_id,jira_ticket_key,project_id,summary,issue_type_id,
# issue_type_name, assignee_name,reporter_name,priority_name,status_name,labels,description,assembla_ticket_id,
# assembla_ticket_number,custom_field,milestone_name,story_rank
tickets_jira_csv = "#{OUTPUT_DIR_JIRA}/jira-tickets.csv"
@tickets_jira = csv_to_array(tickets_jira_csv).select { |ticket| ticket['result'] == 'OK' }
puts "\nTotal Jira tickets: #{@tickets_jira.length}"
@is_ticket_id = {}
@tickets_jira.each do |ticket|
@is_ticket_id[ticket['assembla_ticket_id']] = true
end
### --- Assembla tickets --- ###
# tickets.csv: id,number,summary,description,priority,completed_date,component_id,created_on,permission_type,
# importance,is_story,milestone_id,notification_list,space_id,state,status,story_importance,updated_at,working_hours,
# estimate,total_estimate,total_invested_hours,total_working_hours,assigned_to_id,reporter_id,custom_fields,
# hierarchy_type,due_date,assigned_to_name,picture_url
tickets_assembla_csv = "#{OUTPUT_DIR_ASSEMBLA}/tickets.csv"
@tickets_assembla = csv_to_array(tickets_assembla_csv)
@a_id_to_a_nr = {}
@a_id_to_a_is = {}
@a_id_to_a_ht = {}
@tickets_assembla.each do |ticket|
id = ticket['id'].to_i
@a_id_to_a_nr[id] = ticket['number']
@a_id_to_a_is[id] = ticket['is_story']
@a_id_to_a_ht[id] = ticket['hierarchy_type']
end
# ticket-associations.csv: id,ticket1_id,ticket2_id,relationship,created_at,ticket_id,ticket_number,relationship_name
associations_assembla_csv = "#{OUTPUT_DIR_ASSEMBLA}/ticket-associations.csv"
@asociations_assembla = csv_to_array(associations_assembla_csv)
# --- Filter by date if TICKET_CREATED_ON is defined --- #
tickets_created_on = get_tickets_created_on
if tickets_created_on
puts "Filter newer than: #{tickets_created_on}"
tickets_initial = @tickets_assembla.length
@tickets_assembla.select! { |item| item_newer_than?(item, tickets_created_on) }
puts "Total Assembla tickets: #{tickets_initial} => #{@tickets_assembla.length} ∆#{tickets_initial - @tickets_assembla.length}"
else
puts "\nTotal Assembla tickets: #{@tickets_assembla.length}"
end
@tickets_assembla.select! { |item| @is_ticket_id[item['id']] }
puts "Total Assembla tickets after: #{@tickets_assembla.length}"
# hierarchy_type => epic
@tickets_assembla_epic_h = @tickets_assembla.select do |epic_h|
hierarchy_type_epic(epic_h['hierarchy_type']) &&
epic_h['summary'] !~ /^(spike|bug)/i
end
@tickets_assembla_epic_s = @tickets_assembla.select do |epic_s|
hierarchy_type_epic(epic_s['hierarchy_type']) &&
!@tickets_assembla_epic_h.detect { |epic_h| epic_h['id'].to_i == epic_s['id'].to_i }
end
puts "\nTotal Assembla epics: #{@tickets_assembla_epic_h.length} + #{@tickets_assembla_epic_s.length} = " \
"#{@tickets_assembla_epic_h.length + @tickets_assembla_epic_s.length}"
@tickets_jira_epics = @tickets_jira.select do |jira_ticket|
@tickets_assembla_epic_h.detect { |assembla_ticket| assembla_ticket['id'].to_i == jira_ticket['assembla_ticket_id'].to_i } ||
@tickets_assembla_epic_s.detect { |assembla_ticket| assembla_ticket['id'].to_i == jira_ticket['assembla_ticket_id'].to_i }
end
puts "\nTotal Jira epics: #{@tickets_jira_epics.length}"
@a_id_to_j_id = {}
@a_nr_to_j_key = {}
@tickets_jira.each do |ticket|
id = ticket['assembla_ticket_id'].to_i
nr = ticket['assembla_ticket_number'].to_i
@a_id_to_j_id[id] = ticket['jira_ticket_id']
@a_nr_to_j_key[nr] = ticket['jira_ticket_key']
end
@epics_with_stories = []
def epic_overview(epic)
epic_with_stories = nil
epic_id = epic['id'].to_i
epic_nr = epic['number'].to_i
children = @asociations_assembla.select do |association|
epic['id'].to_i == association['ticket_id'].to_i && association['relationship_name'] == 'parent'
end
nr = children.length
plural = nr == 1 ? 'story' : 'stories'
jira_key = @a_nr_to_j_key[epic_nr] || 'unknown'
if nr.positive?
epic_with_stories = {
epic_id: epic_id,
epic_nr: epic_nr,
jira_key: jira_key,
stories: []
}
end
summary = epic['summary']
summary = "#{summary [0..50]}..." if summary.length > 53
puts "Epic #{epic_id}|#{epic_nr}|#{jira_key} has #{children.length} #{plural} '#{summary}'"
children.each do |child|
ticket_id = child['ticket1_id'].to_i
ticket_nr = (@a_id_to_a_nr[ticket_id] || 0).to_i
story_id = (@a_id_to_j_id[ticket_id] || 0).to_i
story_key = @a_nr_to_j_key[ticket_nr] || 'unknown'
epic_with_stories[:stories] << {
ticket_id: ticket_id,
ticket_nr: ticket_nr,
story_id: story_id,
story_key: story_key
}
puts "* #{ticket_id}|#{ticket_nr}|#{story_id}|#{story_key}"
end
@epics_with_stories << epic_with_stories if epic_with_stories
end
puts "\nHierarchy (#{@tickets_assembla_epic_h.length}) => OK"
@tickets_assembla_epic_h.each do |epic|
epic_overview(epic)
end
puts "\nSummary (#{@tickets_assembla_epic_s.length}) => OK"
@tickets_assembla_epic_s.each do |epic|
epic_overview(epic)
end
@epics_with_stories.sort! { |x, y| x[:epic_id].to_i <=> y[:epic_id].to_i }
puts "\nTotal Jira epics with stories: #{@epics_with_stories.length}"
@epics_with_stories.each do |epic|
nr = epic[:stories].length
unique = @epics_with_stories.select { |epic2| epic2[:epic_id].to_i == epic[:epic_id].to_i }.length == 1
puts "* #{epic[:epic_id]}|#{epic[:epic_nr]}|#{epic[:jira_key]} => #{nr}#{unique ? '' : ' NOT UNIQUE!'}"
end
puts
# GET /rest/agile/1.0/board/{boardId}/epic
def jira_get_epics(board)
board_id = board['id']
start_at = 0
max_results = 50
is_last = false
epics = []
until is_last
url = "#{URL_JIRA_BOARDS}/#{board_id}/epic?startAt=#{start_at}&maxResults=#{max_results}"
begin
response = RestClient::Request.execute(method: :get, url: url, headers: JIRA_HEADERS_ADMIN)
json = JSON.parse(response)
is_last = json['isLast']
values = json['values']
count = values.length
if count.positive?
values.each do |epic|
epics << epic
end
end
start_at += max_results
puts "GET #{url} => ok (#{count})"
rescue RestClient::ExceptionWithResponse => e
rest_client_exception(e, 'GET', url)
rescue => e
puts "GET #{url} => nok (#{e.message})"
end
end
epics
end
# POST /rest/agile/1.0/epic/{epicIdOrKey}/issue
# The maximum number of issues that can be moved in one operation is 50.
def jira_move_stories_to_epic(epic, story_keys, counter, total)
goodbye("Number of stories (#{story_keys.length}) must be less than or equal to 50") if story_keys.length > 50
goodbye("Number of stories (#{story_keys.length}) must be greater than 0") if story_keys.length.zero?
result = {
result: false,
error: ''
}
epic_key = epic[:jira_key]
url = "#{URL_JIRA_EPICS}/#{epic_key}/issue"
payload = {
issues: story_keys
}.to_json
list = story_keys.map { |story| story.sub(/^[^\-]+\-/, '').to_i }
begin
# For a dry-run, comment out the following line
RestClient::Request.execute(method: :post, url: url, payload: payload, headers: JIRA_HEADERS_ADMIN)
percentage = ((counter * 100) / total).round.to_s.rjust(3)
puts "#{percentage}% [#{counter}|#{total}] POST #{url} #{list} => OK"
result[:result] = true
rescue RestClient::ExceptionWithResponse => e
result[:error] = rest_client_exception(e, 'POST', url)
rescue => e
puts "#{percentage}% [#{counter}|#{total}] POST #{url} #{list} => NOK (#{e.message})"
result[:error] = e.message
end
result
end
@board = jira_get_board_by_project_name(JIRA_API_PROJECT_NAME)
goodbye('Cannot find board name') unless @board
puts
@remote_epics = jira_get_epics(@board)
puts "\nTotal remote epics: #{@remote_epics.length}"
# Sanity check that all epics have been created.
@remote_epics_found = []
@remote_epics_not_found = []
@remote_epics.each do |remote_epic|
key = remote_epic['key']
if @tickets_jira_epics.detect { |epic| key == epic['jira_ticket_key'] }
@remote_epics_found << remote_epic
else
@remote_epics_not_found << remote_epic
end
end
puts "* Epics real (#{@remote_epics_found.length}) => #{@remote_epics_found.map { |epic| epic['key'] }}"
puts "* Epics converted (#{@remote_epics_not_found.length}) => #{@remote_epics_not_found.map { |epic| epic['key'] }}"
@local_epics_not_found = []
@tickets_jira_epics.each do |epic|
key = epic['jira_ticket_key']
unless @remote_epics_found.detect { |remote_epic| key == remote_epic['key'] }
@local_epics_not_found << epic
end
end
# Another sanity check just in case
if @local_epics_not_found.length.positive?
puts "\nEpics not found: #{@local_epics_not_found.length}"
@local_epics_not_found.each do |epic|
puts "* #{epic['jira_ticket_key']} '#{epic['summary']}'"
end
puts
end
@total_updated = 0
@updated_epics_jira_csv = "#{OUTPUT_DIR_JIRA}/jira-update-epics.csv"
puts "\nMoving stories to epics"
@total_epics_with_stories = @epics_with_stories.length
@updated_epics = []
@epics_with_stories.each_with_index do |epic, index|
counter = index + 1
stories_nonzero = epic[:stories].select { |story| story[:story_id].positive? }
story_keys = stories_nonzero.map { |story| story[:story_key] }
while story_keys.length.positive?
story_keys_slice = story_keys.slice!(0, 50)
result = jira_move_stories_to_epic(epic, story_keys_slice, counter, @total_epics_with_stories)
@total_updated += 1 if result[:result]
updated_epic = {
result: result[:result] ? 'OK' : 'NOK',
epic_nr: epic[:epic_nr],
jira_key: epic[:jira_key],
stories: story_keys_slice.length,
story_keys: "[#{story_keys_slice.join(',')}]",
error: result[:error]
}
# For a dry-run, comment out the following line
write_csv_file_append(@updated_epics_jira_csv, [updated_epic], counter == 1)
end
end
puts "Total updated: #{@total_updated}"
puts @updated_epics_jira_csv