Class: Downloader

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

Constant Summary collapse

CURRENT_METADATA_VERSION =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(download_config:, json_file_loader: JsonFileLoader.new) ⇒ Downloader

Returns a new instance of Downloader.



15
16
17
18
19
20
21
22
23
24
# File 'lib/jirametrics/downloader.rb', line 15

def initialize download_config:, json_file_loader: JsonFileLoader.new
  @metadata = {}
  @download_config = download_config
  @target_path = @download_config.project_config.target_path
  @json_file_loader = json_file_loader
  @board_id_to_filter_id = {}

  @issue_keys_downloaded_in_current_run = []
  @issue_keys_pending_download = []
end

Instance Attribute Details

#logfileObject

Returns the value of attribute logfile.



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

def logfile
  @logfile
end

#logfile_nameObject

Returns the value of attribute logfile_name.



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

def logfile_name
  @logfile_name
end

#metadataObject

Returns the value of attribute metadata.



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

def 
  @metadata
end

#quiet_modeObject

Returns the value of attribute quiet_mode.



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

def quiet_mode
  @quiet_mode
end

#start_date_in_queryObject (readonly)

For testing only



13
14
15
# File 'lib/jirametrics/downloader.rb', line 13

def start_date_in_query
  @start_date_in_query
end

Instance Method Details

#call_command(command) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/jirametrics/downloader.rb', line 81

def call_command command
  log "  #{command.gsub(/\s+/, ' ')}"
  result = `#{command}`
  log result unless $CHILD_STATUS.success?
  return result if $CHILD_STATUS.success?

  log "Failed call with exit status #{$CHILD_STATUS.exitstatus}. See #{@logfile_name} for details", both: true
  exit $CHILD_STATUS.exitstatus
end

#download_board_configuration(board_id:) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/jirametrics/downloader.rb', line 202

def download_board_configuration board_id:
  log "  Downloading board configuration for board #{board_id}", both: true
  command = make_curl_command url: "#{@jira_url}/rest/agile/1.0/board/#{board_id}/configuration"

  json = JSON.parse call_command(command)
  exit_if_call_failed json

  @board_id_to_filter_id[board_id] = json['filter']['id'].to_i
  # @board_configuration = json if @download_config.board_ids.size == 1

  file_prefix = @download_config.project_config.file_prefix
  write_json json, "#{@target_path}#{file_prefix}_board_#{board_id}_configuration.json"

  download_sprints board_id: board_id if json['type'] == 'scrum'
end

#download_issues(board_id:) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/jirametrics/downloader.rb', line 104

def download_issues board_id:
  log "  Downloading primary issues for board #{board_id}", both: true
  path = "#{@target_path}#{@download_config.project_config.file_prefix}_issues/"
  unless Dir.exist?(path)
    log "  Creating path #{path}"
    Dir.mkdir(path)
  end

  filter_id = @board_id_to_filter_id[board_id]
  jql = make_jql(filter_id: filter_id)
  jira_search_by_jql(jql: jql, initial_query: true, board_id: board_id, path: path)

  log "  Downloading linked issues for board #{board_id}", both: true
  loop do
    @issue_keys_pending_download.reject! { |key| @issue_keys_downloaded_in_current_run.include? key }
    break if @issue_keys_pending_download.empty?

    keys_to_request = @issue_keys_pending_download[0..99]
    @issue_keys_pending_download.reject! { |key| keys_to_request.include? key }
    jql = "key in (#{keys_to_request.join(', ')})"
    jira_search_by_jql(jql: jql, initial_query: false, board_id: board_id, path: path)
  end
end

#download_sprints(board_id:) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/jirametrics/downloader.rb', line 218

def download_sprints board_id:
  log "  Downloading sprints for board #{board_id}", both: true
  file_prefix = @download_config.project_config.file_prefix
  max_results = 100
  start_at = 0
  is_last = false

  while is_last == false
    command = make_curl_command url: "#{@jira_url}/rest/agile/1.0/board/#{board_id}/sprint?" \
      "maxResults=#{max_results}&startAt=#{start_at}"
    json = JSON.parse call_command(command)
    exit_if_call_failed json

    write_json json, "#{@target_path}#{file_prefix}_board_#{board_id}_sprints_#{start_at}.json"
    is_last = json['isLast']
    max_results = json['maxResults']
    start_at += json['values'].size
  end
end

#download_statusesObject



194
195
196
197
198
199
200
# File 'lib/jirametrics/downloader.rb', line 194

def download_statuses
  log '  Downloading all statuses', both: true
  command = make_curl_command url: "\"#{@jira_url}/rest/api/2/status\""
  json = JSON.parse call_command(command)

  write_json json, "#{@target_path}#{@download_config.project_config.file_prefix}_statuses.json"
end

#exit_if_call_failed(json) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/jirametrics/downloader.rb', line 185

def exit_if_call_failed json
  # Sometimes Jira returns the singular form of errorMessage and sometimes the plural. Consistency FTW.
  return unless json['errorMessages'] || json['errorMessage']

  log "Download failed. See #{@logfile_name} for details.", both: true
  log "  #{JSON.pretty_generate(json)}"
  exit 1
end

#find_board_idsObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/jirametrics/downloader.rb', line 55

def find_board_ids
  ids = @download_config.project_config.board_configs.collect(&:id)
  if ids.empty?
    deprecated message: 'board_ids in the download block have been deprecated. See https://github.com/mikebowler/jira-export/wiki/Deprecated'
    ids = @download_config.board_ids
  end
  raise 'Board ids must be specified' if ids.empty?

  ids
end

#identify_other_issues_to_be_downloaded(raw_issue) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/jirametrics/downloader.rb', line 164

def identify_other_issues_to_be_downloaded raw_issue
  issue = Issue.new raw: raw_issue, board: nil
  @issue_keys_downloaded_in_current_run << issue.key

  # Parent
  parent_key = issue.parent_key(project_config: @download_config.project_config)
  @issue_keys_pending_download << parent_key if parent_key

  # Sub-tasks
  issue.raw['fields']['subtasks'].each do |raw_subtask|
    @issue_keys_pending_download << raw_subtask['key']
  end

  # Links
  # We shouldn't blindly follow links as some, like cloners, aren't valuable and are just wasting time/effort
  # to download
  # issue.raw['fields']['issuelinks'].each do |raw_link|
  #   @issue_keys_pending_download << IssueLink(raw: raw_link).other_issue.key
  # end
end

#jira_search_by_jql(jql:, initial_query:, board_id:, path:) ⇒ Object



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

def jira_search_by_jql jql:, initial_query:, board_id:, path:
  intercept_jql = @download_config.project_config.settings['intercept_jql']
  jql = intercept_jql.call jql if intercept_jql

  log "  #{jql}"
  escaped_jql = CGI.escape jql

  max_results = 100
  start_at = 0
  total = 1
  while start_at < total
    command = make_curl_command url: "#{@jira_url}/rest/api/2/search" \
      "?jql=#{escaped_jql}&maxResults=#{max_results}&startAt=#{start_at}&expand=changelog&fields=*all"

    json = JSON.parse call_command(command)
    exit_if_call_failed json

    json['issues'].each do |issue_json|
      issue_json['exporter'] = {
        'in_initial_query' => initial_query
      }
      identify_other_issues_to_be_downloaded issue_json
      file = "#{issue_json['key']}-#{board_id}.json"
      write_json(issue_json, File.join(path, file))
    end

    total = json['total'].to_i
    max_results = json['maxResults']

    message = "    Downloaded #{start_at + 1}-#{[start_at + max_results, total].min} of #{total} issues to #{path} "
    log message, both: true

    start_at += json['issues'].size
  end
end

#load_jira_config(jira_config) ⇒ Object



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

def load_jira_config jira_config
  @jira_url = jira_config['url']
  @jira_email = jira_config['email']
  @jira_api_token = jira_config['api_token']
  @jira_personal_access_token = jira_config['personal_access_token']

  raise 'When specifying an api-token, you must also specify email' if @jira_api_token && !@jira_email

  if @jira_api_token && @jira_personal_access_token
    raise "You can't specify both an api-token and a personal-access-token. They don't work together."
  end

  @cookies = (jira_config['cookies'] || []).collect { |key, value| "#{key}=#{value}" }.join(';')
end

#load_metadataObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/jirametrics/downloader.rb', line 249

def 
  # If we've never done a download before then this file won't be there. That's ok.
  return unless File.exist? 

  hash = JSON.parse(File.read )

  # Only use the saved metadata if the version number is the same one that we're currently using.
  # If the cached data is in an older format then we're going to throw most of it away.
  @cached_data_format_is_current = (hash['version'] || 0) == CURRENT_METADATA_VERSION
  if @cached_data_format_is_current
    hash.each do |key, value|
      value = Date.parse(value) if value.is_a?(String) && value =~ /^\d{4}-\d{2}-\d{2}$/
      @metadata[key] = value
    end
  end

  # Even if this is the old format, we want to obey this one tag
  @metadata['no-download'] = hash['no-download'] if hash['no-download']
end

#log(text, both: false) ⇒ Object



50
51
52
53
# File 'lib/jirametrics/downloader.rb', line 50

def log text, both: false
  @logfile&.puts text
  puts text if both
end

#make_curl_command(url:) ⇒ Object



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

def make_curl_command url:
  command = 'curl'
  command += ' -s'
  command += ' -k' if @download_config.project_config.settings['ignore_ssl_errors']
  command += " --cookie #{@cookies.inspect}" unless @cookies.empty?
  command += " --user #{@jira_email}:#{@jira_api_token}" if @jira_api_token
  command += " -H \"Authorization: Bearer #{@jira_personal_access_token}\"" if @jira_personal_access_token
  command += ' --request GET'
  command += ' --header "Accept: application/json"'
  command += " --url \"#{url}\""
  command
end

#make_jql(filter_id:, today: Date.today) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/jirametrics/downloader.rb', line 310

def make_jql filter_id:, today: Date.today
  segments = []
  segments << "filter=#{filter_id}"

  unless @download_config.rolling_date_count.nil?
    @download_date_range = (today.to_date - @download_config.rolling_date_count)..today.to_date

    # For an incremental download, we want to query from the end of the previous one, not from the
    # beginning of the full range.
    @start_date_in_query = ['date_end'] || @download_date_range.begin
    log "    Incremental download only. Pulling from #{@start_date_in_query}", both: true if ['date_end']

    # Catch-all to pick up anything that's been around since before the range started but hasn't
    # had an update during the range.
    catch_all = '((status changed OR Sprint is not EMPTY) AND statusCategory != Done)'

    # Pick up any issues that had a status change in the range
    start_date_text = @start_date_in_query.strftime '%Y-%m-%d'
    end_date_text = today.strftime '%Y-%m-%d'
    # find_in_range = %((status changed DURING ("#{start_date_text} 00:00","#{end_date_text} 23:59")))
    find_in_range = %((updated >= "#{start_date_text} 00:00" AND updated <= "#{end_date_text} 23:59"))

    segments << "(#{find_in_range} OR #{catch_all})"
  end

  segments.join ' AND '
end

#metadata_pathnameObject



245
246
247
# File 'lib/jirametrics/downloader.rb', line 245

def 
  "#{@target_path}#{@download_config.project_config.file_prefix}_meta.json"
end

#remove_old_filesObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/jirametrics/downloader.rb', line 289

def remove_old_files
  file_prefix = @download_config.project_config.file_prefix
  Dir.foreach @target_path do |file|
    next unless file =~ /^#{file_prefix}_\d+\.json$/

    File.unlink "#{@target_path}#{file}"
  end

  return if @cached_data_format_is_current

  # Also throw away all the previously downloaded issues.
  path = File.join @target_path, "#{file_prefix}_issues"
  return unless File.exist? path

  Dir.foreach path do |file|
    next unless file =~ /\.json$/

    File.unlink File.join(path, file)
  end
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jirametrics/downloader.rb', line 26

def run
  log '', both: true
  log @download_config.project_config.name, both: true

  load_jira_config(@download_config.project_config.jira_config)
  

  if @metadata['no-download']
    log '  Skipping download. Found no-download in meta file', both: true
    return
  end

  # board_ids = @download_config.board_ids

  remove_old_files
  download_statuses
  find_board_ids.each do |id|
    download_board_configuration board_id: id
    download_issues board_id: id
  end

  
end

#save_metadataObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/jirametrics/downloader.rb', line 269

def 
  @metadata['version'] = CURRENT_METADATA_VERSION
  @metadata['date_start_from_last_query'] = @start_date_in_query if @start_date_in_query

  if @download_date_range.nil?
    log "Making up a date range in meta since one wasn't specified. You'll want to change that.", both: true
    today = Date.today
    @download_date_range = (today - 7)..today
  end

  @metadata['earliest_date_start'] = @download_date_range.begin if @metadata['earliest_date_start'].nil?

  @metadata['date_start'] = @download_date_range.begin
  @metadata['date_end'] = @download_date_range.end

  @metadata['jira_url'] = @jira_url

  write_json @metadata, 
end

#write_json(json, filename) ⇒ Object



238
239
240
241
242
243
# File 'lib/jirametrics/downloader.rb', line 238

def write_json json, filename
  file_path = File.dirname(filename)
  FileUtils.mkdir_p file_path unless File.exist?(file_path)

  File.write(filename, JSON.pretty_generate(json))
end