# frozen_string_literal: true require_relative '../../command' require_relative '../../utils/table' module Dri module Commands class Fetch class Failures < Dri::Command include Dri::Utils::Table using Refinements SORT_BY_OPTIONS = { title: 0, triaged: 1, author: 2, url: 3 }.freeze def initialize(options) @options = options @today_iso_format = Time.now.strftime('%Y-%m-%dT00:00:00Z') end def execute(input: $stdin, output: $stdout) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength verify_config_exists title = add_color('Title', :bright_yellow) triaged = add_color('Triaged?', :bright_yellow) author = add_color('Author', :bright_yellow) url = add_color('URL', :bright_yellow) failures = [] urgent = [] labels = [title, triaged, 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') if response.empty? logger.info 'Life is great, there are no new failures today!' exit 0 end response.each do |failure| title = failure.title.truncate(60) author = failure.to_h.dig('author', 'username') url = failure.web_url triaged = add_color('x', :red) emoji_awards = api_client.fetch_awarded_emojis(failure.iid).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] labels = failure.labels labels.each do |label| if label.include?('found:canary.gitlab.com' && 'found:canary.staging.gitlab.com') urgent << [title, triaged, author, url] end end end failures << [title, triaged, author, url] failures.sort_by! { |e| e[SORT_BY_OPTIONS[@options[:sort_by].to_sym]] } if @options[:sort_by] end end if @options[:urgent] print_table(labels, urgent, alignments: [:left, :center, :center, :left]) output.puts(<<~MSG) Found: #{urgent.size} urgent failures, occurring in both canary.gitlab.com and canary.staging.gitlab.com. MSG else print_table(labels, failures, alignments: [:left, :center, :center, :left]) output.puts(<<~MSG) Found: #{failures.size} failures, of these #{triaged_counter} have been triaged with a #{emoji}. MSG end end end end end end