Class: Downloader
- Inherits:
-
Object
- Object
- Downloader
- Defined in:
- lib/jirametrics/downloader.rb
Constant Summary collapse
- CURRENT_METADATA_VERSION =
4
Instance Attribute Summary collapse
-
#board_id_to_filter_id ⇒ Object
readonly
For testing only.
-
#file_system ⇒ Object
readonly
Returns the value of attribute file_system.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#start_date_in_query ⇒ Object
readonly
For testing only.
Instance Method Summary collapse
- #download_board_configuration(board_id:) ⇒ Object
- #download_issues(board:) ⇒ Object
- #download_sprints(board_id:) ⇒ Object
- #download_statuses ⇒ Object
- #download_users ⇒ Object
- #file_prefix ⇒ Object
- #find_board_ids ⇒ Object
- #identify_other_issues_to_be_downloaded(raw_issue:, board:) ⇒ Object
- #init_gateway ⇒ Object
-
#initialize(download_config:, file_system:, jira_gateway:) ⇒ Downloader
constructor
A new instance of Downloader.
- #jira_search_by_jql(jql:, initial_query:, board:, path:) ⇒ Object
- #load_metadata ⇒ Object
- #log(text, both: false) ⇒ Object
- #make_jql(filter_id:, today: Date.today) ⇒ Object
- #metadata_pathname ⇒ Object
- #remove_old_files ⇒ Object
- #run ⇒ Object
- #save_metadata ⇒ Object
- #update_status_history_file ⇒ Object
Constructor Details
#initialize(download_config:, file_system:, jira_gateway:) ⇒ Downloader
Returns a new instance of Downloader.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jirametrics/downloader.rb', line 15 def initialize download_config:, file_system:, jira_gateway: @metadata = {} @download_config = download_config @target_path = @download_config.project_config.target_path @file_system = file_system @jira_gateway = jira_gateway @board_id_to_filter_id = {} @issue_keys_downloaded_in_current_run = [] @issue_keys_pending_download = [] end |
Instance Attribute Details
#board_id_to_filter_id ⇒ Object (readonly)
For testing only
13 14 15 |
# File 'lib/jirametrics/downloader.rb', line 13 def board_id_to_filter_id @board_id_to_filter_id end |
#file_system ⇒ Object (readonly)
Returns the value of attribute file_system.
10 11 12 |
# File 'lib/jirametrics/downloader.rb', line 10 def file_system @file_system end |
#metadata ⇒ Object
Returns the value of attribute metadata.
9 10 11 |
# File 'lib/jirametrics/downloader.rb', line 9 def @metadata end |
#start_date_in_query ⇒ Object (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
#download_board_configuration(board_id:) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/jirametrics/downloader.rb', line 216 def download_board_configuration board_id: log " Downloading board configuration for board #{board_id}", both: true json = @jira_gateway.call_url relative_url: "/rest/agile/1.0/board/#{board_id}/configuration" @file_system.save_json( json: json, filename: File.join(@target_path, "#{file_prefix}_board_#{board_id}_configuration.json") ) # We have a reported bug that blew up on this line. Moved it after the save so we can # actually look at the returned json. @board_id_to_filter_id[board_id] = json['filter']['id'].to_i download_sprints board_id: board_id if json['type'] == 'scrum' # TODO: Should be passing actual statuses, not empty list Board.new raw: json, possible_statuses: StatusCollection.new end |
#download_issues(board:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/jirametrics/downloader.rb', line 69 def download_issues board: log " Downloading primary issues for board #{board.id}", both: true path = File.join(@target_path, "#{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: board, 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: board, path: path) end end |
#download_sprints(board_id:) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/jirametrics/downloader.rb', line 234 def download_sprints board_id: log " Downloading sprints for board #{board_id}", both: true max_results = 100 start_at = 0 is_last = false while is_last == false json = @jira_gateway.call_url relative_url: "/rest/agile/1.0/board/#{board_id}/sprint?" \ "maxResults=#{max_results}&startAt=#{start_at}" @file_system.save_json( json: json, filename: File.join(@target_path, "#{file_prefix}_board_#{board_id}_sprints_#{start_at}.json") ) is_last = json['isLast'] max_results = json['maxResults'] if json['values'] start_at += json['values'].size else log " No sprints found for board #{board_id}" end end end |
#download_statuses ⇒ Object
170 171 172 173 174 175 176 177 178 |
# File 'lib/jirametrics/downloader.rb', line 170 def download_statuses log ' Downloading all statuses', both: true json = @jira_gateway.call_url relative_url: '/rest/api/2/status' @file_system.save_json( json: json, filename: File.join(@target_path, "#{file_prefix}_statuses.json") ) end |
#download_users ⇒ Object
180 181 182 183 184 185 186 187 188 |
# File 'lib/jirametrics/downloader.rb', line 180 def download_users log ' Downloading all users', both: true json = @jira_gateway.call_url relative_url: '/rest/api/2/users' @file_system.save_json( json: json, filename: File.join(@target_path, "#{file_prefix}_users.json") ) end |
#file_prefix ⇒ Object
351 352 353 |
# File 'lib/jirametrics/downloader.rb', line 351 def file_prefix @download_config.project_config.get_file_prefix end |
#find_board_ids ⇒ Object
62 63 64 65 66 67 |
# File 'lib/jirametrics/downloader.rb', line 62 def find_board_ids ids = @download_config.project_config.board_configs.collect(&:id) raise 'Board ids must be specified' if ids.empty? ids end |
#identify_other_issues_to_be_downloaded(raw_issue:, board:) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/jirametrics/downloader.rb', line 156 def identify_other_issues_to_be_downloaded raw_issue:, board: issue = Issue.new raw: raw_issue, board: board @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 end |
#init_gateway ⇒ Object
53 54 55 56 |
# File 'lib/jirametrics/downloader.rb', line 53 def init_gateway @jira_gateway.load_jira_config(@download_config.project_config.jira_config) @jira_gateway.ignore_ssl_errors = @download_config.project_config.settings['ignore_ssl_errors'] end |
#jira_search_by_jql(jql:, initial_query:, board:, path:) ⇒ Object
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 |
# File 'lib/jirametrics/downloader.rb', line 93 def jira_search_by_jql jql:, initial_query:, board:, path: intercept_jql = @download_config.project_config.settings['intercept_jql'] jql = intercept_jql.call jql if intercept_jql log " JQL: #{jql}" escaped_jql = CGI.escape jql if @jira_gateway.cloud? max_results = 5_000 # The maximum allowed by Jira next_page_token = nil issue_count = 0 loop do json = @jira_gateway.call_url relative_url: '/rest/api/3/search/jql' \ "?jql=#{escaped_jql}&maxResults=#{max_results}&" \ "nextPageToken=#{next_page_token}&expand=changelog&fields=*all" next_page_token = json['nextPageToken'] json['issues'].each do |issue_json| issue_json['exporter'] = { 'in_initial_query' => initial_query } identify_other_issues_to_be_downloaded raw_issue: issue_json, board: board file = "#{issue_json['key']}-#{board.id}.json" @file_system.save_json(json: issue_json, filename: File.join(path, file)) issue_count += 1 end = " Downloaded #{issue_count} issues" log , both: true break unless next_page_token end else max_results = 100 start_at = 0 total = 1 while start_at < total json = @jira_gateway.call_url relative_url: '/rest/api/2/search' \ "?jql=#{escaped_jql}&maxResults=#{max_results}&startAt=#{start_at}&expand=changelog&fields=*all" json['issues'].each do |issue_json| issue_json['exporter'] = { 'in_initial_query' => initial_query } identify_other_issues_to_be_downloaded raw_issue: issue_json, board: board file = "#{issue_json['key']}-#{board.id}.json" @file_system.save_json(json: issue_json, filename: File.join(path, file)) end total = json['total'].to_i max_results = json['maxResults'] = " Downloaded #{start_at + 1}-#{[start_at + max_results, total].min} of #{total} issues to #{path} " log , both: true start_at += json['issues'].size end end end |
#load_metadata ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/jirametrics/downloader.rb', line 262 def # If we've never done a download before then this file won't be there. That's ok. hash = file_system.load_json(, fail_on_error: false) return if hash.nil? # 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
58 59 60 |
# File 'lib/jirametrics/downloader.rb', line 58 def log text, both: false @file_system.log text, also_write_to_stderr: both end |
#make_jql(filter_id:, today: Date.today) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/jirametrics/downloader.rb', line 322 def make_jql filter_id:, today: Date.today segments = [] segments << "filter=#{filter_id}" start_date = @download_config.start_date today: today if start_date @download_date_range = start_date..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' # 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") segments << "(#{find_in_range} OR #{catch_all})" end segments.join ' AND ' end |
#metadata_pathname ⇒ Object
258 259 260 |
# File 'lib/jirametrics/downloader.rb', line 258 def File.join(@target_path, "#{file_prefix}_meta.json") end |
#remove_old_files ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/jirametrics/downloader.rb', line 301 def remove_old_files Dir.foreach @target_path do |file| next unless file.match?(/^#{file_prefix}_\d+\.json$/) next if file == "#{file_prefix}_status_history.json" File.unlink File.join(@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.match?(/\.json$/) File.unlink File.join(path, file) end end |
#run ⇒ Object
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 |
# File 'lib/jirametrics/downloader.rb', line 27 def run log '', both: true log @download_config.project_config.name, both: true init_gateway 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 update_status_history_file download_statuses find_board_ids.each do |id| board = download_board_configuration board_id: id download_issues board: board end download_users end |
#save_metadata ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/jirametrics/downloader.rb', line 281 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 @file_system.save_json json: @metadata, filename: end |
#update_status_history_file ⇒ Object
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 |
# File 'lib/jirametrics/downloader.rb', line 190 def update_status_history_file status_filename = File.join(@target_path, "#{file_prefix}_statuses.json") return unless file_system.file_exist? status_filename status_json = file_system.load_json(status_filename) history_filename = File.join(@target_path, "#{file_prefix}_status_history.json") history_json = file_system.load_json(history_filename) if file_system.file_exist? history_filename if history_json file_system.log ' Updating status history file', also_write_to_stderr: true else file_system.log ' Creating status history file', also_write_to_stderr: true history_json = [] end status_json.each do |status_item| id = status_item['id'] history_item = history_json.find { |s| s['id'] == id } history_json.delete(history_item) if history_item history_json << status_item end file_system.save_json(filename: history_filename, json: history_json) end |