Class: Dri::Commands::Rm::Emoji

Inherits:
Dri::Command show all
Defined in:
lib/dri/commands/rm/emoji.rb

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) ⇒ Emoji

Returns a new instance of Emoji.



9
10
11
# File 'lib/dri/commands/rm/emoji.rb', line 9

def initialize(options)
  @options = options
end

Instance Method Details

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

rubocop:disable Metrics/AbcSize



13
14
15
16
17
18
19
20
21
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
# File 'lib/dri/commands/rm/emoji.rb', line 13

def execute(_input: $stdin, output: $stdout) # rubocop:disable Metrics/AbcSize
  verify_config_exists

  remove = prompt.yes? "Are you sure you want to remove all #{emoji} award emojis from issues?"

  unless remove
    logger.info "Emojis kept in place 👍"
    exit 0
  end

  logger.info "Removing #{emoji} emoji from issues..."

  spinner.start

  failures_with_award_emoji = api_client.fetch_all_triaged_failures(emoji: emoji, state: 'opened')
  incidents_with_award_emoji = api_client.fetch_triaged_incidents(emoji: emoji)

  issues_with_award_emoji = failures_with_award_emoji + incidents_with_award_emoji

  spinner.stop

  cleared_issues_counter = 0
  issues_with_award_emoji.each do |issue|
    logger.info "Removing #{emoji} emoji from #{issue.web_url}"

    response = api_client.fetch_awarded_emojis(issue.iid, project_id: issue.project_id)
    emoji_found = response.find { |e| e.name == emoji && e.to_h.dig('user', 'username') == username }

    if emoji_found.nil?
      logger.error "Emoji #{add_color(emoji, :red)} not found for username: #{add_color(username, :red)}"
      next
    else
      api_client.delete_award_emoji(issue.iid, emoji_id: emoji_found.id, project_id: issue.project_id)
      cleared_issues_counter += 1
    end
  end

  output.puts "Done! ✅"
  logger.success "Removed #{emoji} from #{cleared_issues_counter} issue(s)."
end