Class: Codeowners::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/codeowners/cli.rb', line 12

def self.exit_on_failure?
  true
end

Instance Method Details

#find_contributors(feature) ⇒ Object



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
# File 'lib/codeowners/cli.rb', line 29

def find_contributors(feature)
  contributor_finder = ContributorFinder.new(max_commits: options[:max_commits])
  contributors = contributor_finder.find_contributors(feature:)

  if contributors.empty?
    puts "No contributors found for feature '#{feature}'"
    exit(0)
  end

  first_date = contributors.filter_map(&:first_commit_date).min
  last_date = contributors.filter_map(&:last_commit_date).max
  date_range = first_date && last_date ? "#{first_date} to #{last_date}" : 'N/A'
  total_commits = contributors.sum(&:commits)

  puts "Top contributors for feature '#{feature}'"
  puts "Analyzed: last #{options[:max_commits]} commits (#{date_range})"
  puts "Total commits: #{total_commits}"
  puts

  table_format = '%-30s %10s %10s %15s %8s'
  puts format(table_format, 'GitHub Username', 'Additions', 'Deletions', 'Lines Changed', 'Commits')
  puts '-' * 81
  contributors.each do |contributor|
    puts format(
      table_format,
      contributor.username,
      contributor.additions,
      contributor.deletions,
      contributor.lines_changed,
      contributor.commits
    )
  end
end

#find_feature(filepath) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/codeowners/cli.rb', line 151

def find_feature(filepath)
  feature = DefinitionsFile.new.find_feature_for_file(path: filepath)
  if feature
    puts feature
  else
    puts "No feature found for #{filepath}"
    exit(1)
  end
end

#find_features(filepath) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/codeowners/cli.rb', line 163

def find_features(filepath)
  feature_matches = DefinitionsFile.new.find_features_for_file(path: filepath)
  if feature_matches.any?
    feature_matches.each do |match|
      print = []
      print << filepath if options[:show_path]
      print << match.feature
      print << match.path_pattern
      puts print.join(',')
    end
  else
    puts "No feature found for #{filepath}"
    exit(1)
  end
end

#find_files(feature, glob = '*') ⇒ Object



17
18
19
20
# File 'lib/codeowners/cli.rb', line 17

def find_files(feature, glob = '*')
  files = DefinitionsFile.new.feature_paths(feature:).select { |file| file.fnmatch?(glob) }
  puts files.join("\n")
end

#find_owned_files(owner) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/codeowners/cli.rb', line 84

def find_owned_files(owner)
  warn_against_global_glob_pattern if options[:pattern] == '**/*'

  codeowners_file = CodeownersFile.new
  owner_finder = OwnerFinder.new(codeowners_file:)
  owned_files = Dir.glob(options[:pattern]).select do |filepath|
    next unless Pathname.new(filepath).file?
    next unless owner_finder.find_owners_for_file(filepath:).include?(owner)

    filepath
  end

  owner_long_name = Owner.new(owner).long_name
  if owned_files.any?
    puts "#{owned_files.length} #{'file'.pluralize(owned_files.length)} found belonging to owner #{owner_long_name}"
    puts owned_files
  else
    puts "No files found belonging to owner #{owner_long_name}"
  end
end

#find_owner(filepath) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/codeowners/cli.rb', line 65

def find_owner(filepath)
  codeowners_file = CodeownersFile.new
  owner_finder = OwnerFinder.new(codeowners_file:)
  ownership = owner_finder.find_ownership_for_file(filepath:)
  owners = ownership&.owners

  unless owners&.any?
    puts 'No owners found!'
    exit(1)
  end

  output = owners.join(' ')
  output << " #{ownership.glob}" if options[:glob]

  puts output
end

#find_test_files(feature) ⇒ Object



23
24
25
# File 'lib/codeowners/cli.rb', line 23

def find_test_files(feature)
  find_files(feature, '*_test.rb')
end

#find_unowned_filesObject



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
# File 'lib/codeowners/cli.rb', line 109

def find_unowned_files
  warn_against_global_glob_pattern if options[:pattern] == '**/*'

  codeowners_file = CodeownersFile.new
  pattern = options[:pattern]

  git_ls_files_pattern = pattern == '**/*' ? '.' : pattern
  git_tracked_files = `git ls-files #{git_ls_files_pattern} 2>&1`
  unless $CHILD_STATUS.success?
    warn "Failed to run git ls-files: #{git_tracked_files}"
    exit(1)
  end

  filepaths = git_tracked_files.split("\n").select do |filepath|
    filepath_obj = Pathname.new(filepath)
    next if filepath_obj.directory?

    codeowners_file.globs.none? { |glob| glob.match?(filepath) }
  end

  output_stream = options[:output] ? File.open(options[:output], 'w') : $stdout
  use_colors = options[:output].nil?

  begin
    if filepaths.empty?
      output_with_color(output_stream, use_colors, "\e[32m", "No unowned files found in #{options[:pattern]}")
      exit(0)
    else
      truncated_paths = truncate_to_unowned_directories(filepaths)
      output_with_color(output_stream, use_colors, "\e[31m",
                        "The following #{'path'.pluralize(truncated_paths.length)} are not included in the CODEOWNERS file:")
      truncated_paths.each do |filepath|
        output_with_color(output_stream, use_colors, "\e[31m", filepath)
      end
      exit(options[:exit_status_on_match])
    end
  ensure
    output_stream.close if options[:output]
  end
end