Class: Dri::Commands::Publish::Report

Inherits:
Dri::Command show all
Includes:
Utils::Constants::FeatureFlag
Defined in:
lib/dri/commands/publish/report.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary

Constants included from Utils::Constants::FeatureFlag

Utils::Constants::FeatureFlag::TITLE_SUBSTRINGS

Instance Method Summary collapse

Methods inherited from Dri::Command

#add_color, #api_client, #bold, #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) ⇒ Report

Returns a new instance of Report.



19
20
21
22
23
24
25
# File 'lib/dri/commands/publish/report.rb', line 19

def initialize(options)
  @options = options

  @date = Date.today
  @time = Time.now.to_i
  @today_iso_format = Time.now.strftime('%Y-%m-%dT00:00:00Z')
end

Instance Method Details

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

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



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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/dri/commands/publish/report.rb', line 27

def execute(_input: $stdin, output: $stdout) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
  verify_config_exists
  report = Dri::Report.new(config)

  logger.info "Fetching triaged failures and incidents with award emoji #{emoji}..."

  spinner.start

  failures = api_client.fetch_all_triaged_failures(emoji: emoji, state: 'opened')
  incidents = api_client.fetch_triaged_incidents(emoji: emoji)

  spinner.stop

  if failures.empty? && incidents.empty?
    logger.warn "Found no failures nor incidents associated with \"#{emoji}\" emoji. Will exit. Bye 👋"
    exit 1
  end

  logger.info 'Assembling the handover report... '
  # sets each failure on the table
  action_options = [
    'pinged SET',
    'reproduced',
    'transient',
    'quarantined',
    'active investigation',
    'blocking pipelines',
    'awaiting for a fix to merge',
    'notified the team',
    'due to feature flag'
  ]

  spinner.start

  failures.each do |failure|
    actions = []

    if @options[:actions]
      actions = prompt.multi_select(
        "Please mark the actions on #{add_color(failure.title, :yellow)}: ",
        action_options,
        per_page: 9
      )
    end

    report.add_failure(failure, actions)
  end

  unless incidents.empty?
    incidents.each do |issue|
      report.add_incident(issue)
    end
  end

  if @options[:format] == 'list'
    # generates markdown list with failures
    unless report.failures.empty?
      failure_report = Utils::MarkdownLists.make_list(report.labels, report.failures)
    end
    # generates markdown list with incidents
    unless report.incidents.empty?
      incidents_report = Utils::MarkdownLists.make_list(report.labels_incidents, report.incidents)
    end
  else
    # generates markdown table with rows as failures
    unless report.failures.empty?
      failure_report = MarkdownTables.make_table(
        report.labels,
        report.failures,
        is_rows: true, align: %w[l l l l l]
      )
    end
    # generates markdown table with rows as incidents
    unless report.incidents.empty?
      incidents_report = MarkdownTables.make_table(
        report.labels_incidents,
        report.incidents,
        is_rows: true, align: %w[l l l l]
      )
    end
  end

  spinner.stop

  if @options[:feature_flags]
    logger.info 'Fetching today\'s feature flag changes...'

    feature_flag_report = Dri::FeatureFlagReport.new

    spinner.start

    feature_flags = api_client.fetch_feature_flag_logs(@today_iso_format)

    feature_flags.each do |feature_flag|
      next unless TITLE_SUBSTRINGS.any? { |substr| feature_flag.title.include?(substr) }

      feature_flag_report.add_change(feature_flag)
    end

    spinner.stop

    logger.info 'Assembling the feature flags report...'

    spinner.start

    feature_flag_note = "\n\n## Feature Flag Changes"
    feature_flag_changes = ''

    format_type = @options[:format] == 'list' ? :list : :table

    feature_flag_report.get_all_flag_changes.each do |env, changes|
      next if changes.empty?

      feature_flag_changes += format_feature_flag_changes(
        env, changes, feature_flag_report.labels, format_type
      )
    end

    feature_flag_note += if feature_flag_changes.empty?
                           "\n\nNo changes found today"
                         else
                           "\n\n<details><summary>Click to expand</summary>#{feature_flag_changes}</details>"
                         end

    spinner.stop
  end

  report.set_header(timezone, username)
  note = "#{report.header}\n\n#{failure_report}\n\n#{incidents_report}"

  note += feature_flag_note if @options[:feature_flags]

  # creates an .md file with the report locally in /handover_reports
  if @options[:dry_run]
    logger.info 'Downloading the report... '

    spinner.start

    FileUtils.mkdir_p(handover_report_path)
    report_path = "#{handover_report_path.chomp('/')}/report-#{@date}-#{@time}.md"

    File.open(report_path, 'a') do |out_file|
      out_file.puts note
    end

    spinner.stop

    output.puts "Done! ✅\n"
    logger.success "Report is ready at: #{report_path}"
    exit 0
  end

  # sends note to the weekly triage report
  issues = api_client.fetch_current_triage_issue
  current_issue_iid = issues.first.iid

  note_action = 'posted'
  posted_note = nil
  if @options[:update]
    report_file = File.read("handover_reports/.tmp/report-#{@date}.json")
    report_json = JSON.parse(report_file) if report_file
    posted_note = api_client.update_triage_report_note(
      iid: current_issue_iid, note_id: report_json['id'], body: note
    )
    note_action = 'updated'
  else
    posted_note = api_client.post_triage_report_note(iid: current_issue_iid, body: note)

    FileUtils.mkdir_p("#{Dir.pwd}/handover_reports/.tmp")
    report_path = "handover_reports/.tmp/report-#{@date}.json"

    File.write(report_path, posted_note.to_h.to_json)
  end

  output.puts "Done! ✅\n"
  logger.success(<<~MSG)
    Thanks @#{username}, your report was #{note_action} at https://gitlab.com/gitlab-org/quality/pipeline-triage/-/issues/#{current_issue_iid}#note_#{posted_note.id} 🎉
  MSG
end