lib/dri/commands/fetch/failures.rb in dri-0.7.0 vs lib/dri/commands/fetch/failures.rb in dri-0.8.0

- old
+ new

@@ -1,15 +1,17 @@ # frozen_string_literal: true require_relative '../../command' require_relative '../../utils/table' +require_relative '../../utils/constants' module Dri module Commands class Fetch class Failures < Dri::Command include Dri::Utils::Table + include Dri::Utils::Constants::Triage::Labels using Refinements SORT_BY_OPTIONS = { title: 0, triaged: 1, @@ -32,65 +34,66 @@ triaged = add_color('Triaged?', :bright_yellow) environment = add_color('Environment', :bright_yellow) author = add_color('Author', :bright_yellow) url = add_color('URL', :bright_yellow) - failures = [] + sorted_failures = [] labels = [title, triaged, environment, author, url] triaged_counter = 0 logger.info "Fetching today's failures..." spinner.run do # rubocop:disable Metrics/BlockLength - response = api_client.fetch_failures(date: @today_iso_format, state: 'opened') + failures = api_client.fetch_all_new_failures(date: @today_iso_format, state: 'opened') - if response.empty? + if failures.empty? logger.info 'Life is great, there are no new failures today!' exit 0 end - response.each do |failure| + failures.each do |failure| + project_id = failure.project_id title = failure.title.truncate(60) author = failure.to_h.dig('author', 'username') url = failure.web_url triaged = add_color('x', :red) - envs = failure.labels.select { |l| l.include?('found:') }.map do |l| + envs = failure.labels.select { |l| l.include?(FOUND) }.map do |l| env = l.split(':').last.gsub('.gitlab.com', '') env == 'gitlab.com' ? 'production' : env end urgent = urgent_environments.all? { |env| envs.include?(env) } - emoji_awards = api_client.fetch_awarded_emojis(failure.iid).find do |e| + emoji_awards = api_client.fetch_awarded_emojis(failure.iid, project_id: project_id).find do |e| e.name == emoji && e.to_h.dig('user', 'username') == username end if emoji_awards triaged = add_color('✓', :green) triaged_counter += 1 end if @options[:urgent] - failures << [title, triaged, envs.first, author, url] if urgent + sorted_failures << [title, triaged, envs.first, author, url] if urgent else - failures << [title, triaged, envs.first, author, url] + sorted_failures << [title, triaged, envs.first, author, url] end end - failures.sort_by! { |failure| failure[SORT_BY_OPTIONS[@options[:sort_by]&.to_sym || :environment]] } + sorted_failures.sort_by! { |failure| failure[SORT_BY_OPTIONS[@options[:sort_by]&.to_sym || :environment]] } end msg = if @options[:urgent] <<~MSG - Found: #{failures.size} urgent failures, occurring in both canary.gitlab.com and canary.staging.gitlab.com. + Found: #{sorted_failures.size} urgent failures, occurring in both canary.gitlab.com and canary.staging.gitlab.com. MSG else <<~MSG - Found: #{failures.size} failures, of these #{triaged_counter} have been triaged with a #{emoji}. + Found: #{sorted_failures.size} failures, of these #{triaged_counter} have been triaged with a #{emoji}. MSG end - print_table(labels, failures, alignments: [:left, :center, :center, :left]) + print_table(labels, sorted_failures, alignments: [:left, :center, :center, :left]) output.puts(msg) end end end end