Class: Codeowners::Cli::InteractiveResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/codeowners/cli/interactive_resolver.rb

Overview

Resolve issues in interactive mode handle_* methods will throw :user_quit if the user chose to save and quit

Instance Method Summary collapse

Constructor Details

#initialize(checker, validate_owners, default_owner) ⇒ InteractiveResolver

Returns a new instance of InteractiveResolver.



14
15
16
17
18
19
20
# File 'lib/codeowners/cli/interactive_resolver.rb', line 14

def initialize(checker, validate_owners, default_owner)
  @checker = checker
  @ignored_owners = []
  @validate_owners = validate_owners
  @default_owner = default_owner
  create_wizards
end

Instance Method Details

#handle(error_type, inconsistencies, meta) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/codeowners/cli/interactive_resolver.rb', line 22

def handle(error_type, inconsistencies, meta)
  case error_type
  when :useless_pattern then handle_useless_pattern(inconsistencies)
  when :missing_ref then handle_new_file(inconsistencies)
  when :invalid_owner then handle_new_owners(inconsistencies, meta)
  when :unrecognized_line then process_parsed_line(inconsistencies)
  else raise ArgumentError, "unknown error_type: #{error_type}"
  end
end

#handle_new_file(file) ⇒ Object

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/codeowners/cli/interactive_resolver.rb', line 32

def handle_new_file(file) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  choice, pattern = @new_file_wizard.suggest_adding(file, @checker.main_group)
  throw :user_quit if choice == :quit
  return unless choice == :add

  validate_owner(pattern, pattern.owner) if @validate_owners
  op, subgroup = @new_file_wizard.select_operation(pattern, @checker.main_group)
  case op
  when :insert_into_subgroup
    subgroup.insert(pattern)
    @made_changes = true
  when :add_to_main_group
    @checker.main_group.add(pattern)
    @made_changes = true
  end
end

#handle_new_owner(line, owner) ⇒ Object

rubocop:disable Metrics/MethodLength



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/codeowners/cli/interactive_resolver.rb', line 53

def handle_new_owner(line, owner) # rubocop:disable Metrics/MethodLength
  return if @ignored_owners.include?(owner)

  choice, new_owner = @new_owner_wizard.suggest_fixing(line, owner)
  case choice
  when :add
    @checker.owners_list << owner
    @made_changes = true
  when :rename
    line.rename_owner(owner, new_owner)
    @checker.owners_list << new_owner
    @made_changes = true
  when :ignore
    @ignored_owners << owner
  when :quit then throw :user_quit
  end
end

#handle_new_owners(line, missing_owners) ⇒ Object



49
50
51
# File 'lib/codeowners/cli/interactive_resolver.rb', line 49

def handle_new_owners(line, missing_owners)
  missing_owners.each { |owner| handle_new_owner(line, owner) }
end

#handle_useless_pattern(line) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/codeowners/cli/interactive_resolver.rb', line 71

def handle_useless_pattern(line)
  choice, new_pattern = @useless_pattern_wizard.suggest_fixing(line)
  case choice
  when :replace
    line.pattern = new_pattern
    @made_changes = true
  when :delete
    line.remove!
    @made_changes = true
  when :quit then throw :user_quit
  end
end

#made_changes?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/codeowners/cli/interactive_resolver.rb', line 108

def made_changes?
  @made_changes
end


99
100
101
102
103
104
105
106
# File 'lib/codeowners/cli/interactive_resolver.rb', line 99

def print_epilogue
  return unless @ignored_owners.any?

  puts 'Ignored owners:'
  @ignored_owners.each do |owner|
    puts " * #{owner}"
  end
end

#process_parsed_line(line) ⇒ Object

rubocop:disable Metrics/MethodLength



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/codeowners/cli/interactive_resolver.rb', line 84

def process_parsed_line(line) # rubocop:disable Metrics/MethodLength
  return line unless line.is_a?(Codeowners::Checker::Group::UnrecognizedLine)

  choice, new_line = @unrecognized_line_wizard.suggest_fixing(line)
  case choice
  when :replace
    @made_changes = true
    new_line
  when :delete
    @made_changes = true
    nil
  when :ignore then line
  end
end