Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support CSV format for get_info and allow including any violation types #110

Merged
merged 8 commits into from
Oct 13, 2023
Prev Previous commit
Next Next commit
Allow including a Date in the get_info report (useful for snapshots)
Including the date as part of the report makes it easy to store the output as snapshots over time to compare progress.
  • Loading branch information
oboxodo committed Oct 12, 2023
commit 9f419a5f17a99da84df2b5778fc19664581d1082
4 changes: 3 additions & 1 deletion lib/packs/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def update
end

desc 'get_info [ packs/my_pack packs/my_other_pack ]', 'Get info about size and violations for packs'
option :include_date, type: :boolean, default: false, aliases: :d, banner: "Include today's date as part of the data (useful to take snapshots over time)"
option :format, type: :string, default: 'detail', aliases: :f, banner: 'Specify the output format (detail, csv)'
option :types, type: :string, default: 'privacy,dependency', aliases: :t, banner: 'List of validation types to include (privacy,dependency,architecture)'
sig { params(pack_names: String).void }
Expand All @@ -140,7 +141,8 @@ def get_info(*pack_names)
Private.get_info(
packs: parse_pack_names(pack_names),
format: options[:format].to_sym,
types: selected_types.map(&:to_sym)
types: selected_types.map(&:to_sym),
include_date: options[:include_date]
)
exit_successfully
end
Expand Down
16 changes: 13 additions & 3 deletions lib/packs/private.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
require 'packs/private/pack_relationship_analyzer'
require 'packs/private/interactive_cli'

require 'date'

module Packs
module Private
extend T::Sig
Expand Down Expand Up @@ -483,12 +485,14 @@ def self.packwerk_package_to_pack(package)
params(
packs: T::Array[Packs::Pack],
format: Symbol,
types: T::Array[Symbol]
types: T::Array[Symbol],
include_date: T::Boolean
).void
end
def self.get_info(packs: Packs.all, format: :detail, types: %i[privacy dependency architecture])
def self.get_info(packs: Packs.all, format: :detail, types: %i[privacy dependency architecture], include_date: false)
require 'csv' if format == :csv

today = Date.today.iso8601
violations = {
inbound: {},
outbound: {}
Expand Down Expand Up @@ -516,12 +520,14 @@ def self.get_info(packs: Packs.all, format: :detail, types: %i[privacy dependenc

case format
when :csv
headers = ['Pack name', 'Owned by', 'Size', 'Public API']
headers = ['Date', 'Pack name', 'Owned by', 'Size', 'Public API']
headers.delete('Date') unless include_date
dir_x_types.each do |direction, type|
headers << "#{direction.capitalize} #{type} violations"
end
puts CSV.generate_line(headers)
else # :detail
puts "Date: #{today}" if include_date
dir_x_types.each do |direction, type|
puts "There are #{all[direction].select { _1.type.to_sym == type }.sum { |v| v.files.count }} total #{direction} #{type} violations"
end
Expand All @@ -531,12 +537,15 @@ def self.get_info(packs: Packs.all, format: :detail, types: %i[privacy dependenc
owner = CodeOwnership.for_package(pack)

row = {
date: today,
pack_name: pack.name,
owner: owner.nil? ? 'No one' : owner.name,
size: pack.relative_path.glob('**/*.rb').count,
public_api: pack.relative_path.join('app/public')
}

row.delete(:date) unless include_date

dir_x_types.each do |direction, type|
key = [direction, type, 'violations'].join('_').to_sym
row[key] = (violations[direction][pack.name] || []).select { _1.type.to_sym == type }.sum { |v| v.files.count }
Expand All @@ -548,6 +557,7 @@ def self.get_info(packs: Packs.all, format: :detail, types: %i[privacy dependenc
else # :detail
puts "\n=========== Info about: #{row[:pack_name]}"

puts "Date: #{row[:date]}" if include_date
puts "Owned by: #{row[:owner]}"
puts "Size: #{row[:size]} ruby files"
puts "Public API: #{row[:public_api]}"
Expand Down
5 changes: 4 additions & 1 deletion lib/packs/private/interactive_cli/use_cases/get_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ def perform!(prompt)
%w[Privacy Dependency Architecture]
)

include_date = !prompt.no?('Should the current date be included in the report?')

puts "You've selected #{selected_packs.count} packs. Wow! Here's all the info."

Private.get_info(
packs: selected_packs,
format: format.downcase.to_sym,
types: types.map(&:downcase).map(&:to_sym)
types: types.map(&:downcase).map(&:to_sym),
include_date: include_date
)
end
end
Expand Down