Class: Dri::Commands::Fetch::Failures

Inherits:
Dri::Command show all
Includes:
Utils::Constants::Triage::Labels, Utils::Table
Defined in:
lib/dri/commands/fetch/failures.rb

Constant Summary

Constants included from Utils::Constants::Triage::Labels

Utils::Constants::Triage::Labels::FAILURE, Utils::Constants::Triage::Labels::FAILURE_NEW, Utils::Constants::Triage::Labels::FOUND, Utils::Constants::Triage::Labels::INCIDENT, Utils::Constants::Triage::Labels::QA, Utils::Constants::Triage::Labels::QUALITY, Utils::Constants::Triage::Labels::QUARANTINE, Utils::Constants::Triage::Labels::SERVICE

Instance Method Summary collapse

Methods included from Utils::Table

#print_table

Methods inherited from Dri::Command

#add_color, #api_client, #command, #config, #cursor, #editor, #emoji, #handover_report_path, #logger, #ops_token, #pastel, #profile, #prompt, #spinner, #timezone, #token, #username, #verify_config_exists

Constructor Details

#initialize(options) ⇒ Failures

Returns a new instance of Failures.



15
16
17
18
19
20
# File 'lib/dri/commands/fetch/failures.rb', line 15

def initialize(options)
  @options = options
  @start_date = @options[:start_date] ? Date.parse(@options[:start_date]) : Date.today
  @end_date = @options[:end_date] ? Date.parse(@options[:end_date]) : Date.today
  @cutoff_time = @options[:cutoff] ? Time.parse(options[:cutoff]).utc : nil
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength



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
# File 'lib/dri/commands/fetch/failures.rb', line 22

def execute(input: $stdin, output: $stdout) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
  verify_config_exists

  urgent_environments = %w[canary canary.staging]

  title = add_color('Title', :bright_yellow)
  triaged = add_color('Triaged?', :bright_yellow)
  environment = add_color('Environment', :bright_yellow)
  author = add_color('Author', :bright_yellow)
  url = add_color('URL', :bright_yellow)
  updated_at = add_color('Updated at', :bright_yellow)

  sorted_failures = []
  labels = { title: title, triaged: triaged, environment: environment, author: author, url: url,
             updated_at: updated_at }
  triaged_counter = 0

  if @cutoff_time
    @start_date = Time.new(
      @start_date.year,
      @start_date.month,
      @start_date.day,
      @cutoff_time.hour,
      @cutoff_time.min, 0,
      "+00:00"
    )
  end

  logger.info "Fetching failures from #{@start_date} (UTC) to #{@end_date} (UTC)..."

  spinner.run do # rubocop:disable Metrics/BlockLength
    failures = api_client.fetch_all_new_failures(start_date: @start_date, end_date: @end_date, state: 'opened')

    if failures.empty?
      logger.info "Life is great, there are no new failures between #{@start_date} and #{@end_date}!"
      exit 0
    end

    failures.each do |failure|
      project_id = failure.project_id
      title = failure.title.truncate(60)
      author = failure.to_h.dig('author', 'username').truncate(25)
      url = failure.web_url
      updated_at = failure.updated_at
      triaged = add_color('x', :red)
      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) }

      next if @options[:urgent] && !urgent

      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

      sorted_failures << { title: title, triaged: triaged, environment: envs.first || 'none', author: author,
                           url: url, updated_at: updated_at }
    end

    sorted_failures.sort_by! { |failure| failure[@options[:sort_by]&.to_sym || :environment] }
  end

  msg = if @options[:urgent]
          <<~MSG
            Found: #{sorted_failures.size} urgent failures, occurring in both canary.gitlab.com and canary.staging.gitlab.com.
          MSG
        else
          <<~MSG
            Found: #{sorted_failures.size} failures, of these #{triaged_counter} have been triaged with a #{emoji}.
          MSG
        end

  terminal_width = TTY::Screen.width

  if terminal_width <= 210 # adjust the desired minimum width
    labels.delete(:updated_at)
    sorted_failures.map { |failure| failure.delete(:updated_at) }
  end

  print_table(labels.values, sorted_failures.map(&:values), alignments: [:left, :center, :center, :left, :left])
  output.puts(msg)
end