Merge pull request #14642 from issyl0/contributions-csv
dev-cmd/contributions: CSV output of queried repos; shorter sentence
This commit is contained in:
		
						commit
						d7d775a4ef
					
				@ -335,6 +335,9 @@ module Homebrew
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      sig { returns(T.nilable(T::Boolean)) }
 | 
					      sig { returns(T.nilable(T::Boolean)) }
 | 
				
			||||||
      def force_auto_update?; end
 | 
					      def force_auto_update?; end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      sig { returns(T::Boolean) }
 | 
				
			||||||
 | 
					      def csv?; end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@
 | 
				
			|||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require "cli/parser"
 | 
					require "cli/parser"
 | 
				
			||||||
 | 
					require "csv"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module Homebrew
 | 
					module Homebrew
 | 
				
			||||||
  extend T::Sig
 | 
					  extend T::Sig
 | 
				
			||||||
@ -17,7 +18,7 @@ module Homebrew
 | 
				
			|||||||
  sig { returns(CLI::Parser) }
 | 
					  sig { returns(CLI::Parser) }
 | 
				
			||||||
  def contributions_args
 | 
					  def contributions_args
 | 
				
			||||||
    Homebrew::CLI::Parser.new do
 | 
					    Homebrew::CLI::Parser.new do
 | 
				
			||||||
      usage_banner "`contributions` <email|name> [<--repositories>`=`]"
 | 
					      usage_banner "`contributions` <email|name> [<--repositories>`=`] [<--csv>]"
 | 
				
			||||||
      description <<~EOS
 | 
					      description <<~EOS
 | 
				
			||||||
        Contributions to Homebrew repos for a user.
 | 
					        Contributions to Homebrew repos for a user.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -34,6 +35,9 @@ module Homebrew
 | 
				
			|||||||
      flag "--to=",
 | 
					      flag "--to=",
 | 
				
			||||||
           description: "Date (ISO-8601 format) to stop searching contributions."
 | 
					           description: "Date (ISO-8601 format) to stop searching contributions."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      switch "--csv",
 | 
				
			||||||
 | 
					             description: "Print a CSV of a user's contributions across repositories over the time period."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      named_args number: 1
 | 
					      named_args number: 1
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
@ -42,9 +46,7 @@ module Homebrew
 | 
				
			|||||||
  def contributions
 | 
					  def contributions
 | 
				
			||||||
    args = contributions_args.parse
 | 
					    args = contributions_args.parse
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    commits = 0
 | 
					    results = {}
 | 
				
			||||||
    coauthorships = 0
 | 
					 | 
				
			||||||
    signoffs = 0
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    all_repos = args.repositories.nil? || args.repositories.include?("all")
 | 
					    all_repos = args.repositories.nil? || args.repositories.include?("all")
 | 
				
			||||||
    repos = all_repos ? SUPPORTED_REPOS : args.repositories
 | 
					    repos = all_repos ? SUPPORTED_REPOS : args.repositories
 | 
				
			||||||
@ -56,21 +58,31 @@ module Homebrew
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      repo_path = find_repo_path_for_repo(repo)
 | 
					      repo_path = find_repo_path_for_repo(repo)
 | 
				
			||||||
      unless repo_path.exist?
 | 
					      unless repo_path.exist?
 | 
				
			||||||
 | 
					 | 
				
			||||||
        opoo "Repository #{repo} not yet tapped! Tapping it now..."
 | 
					        opoo "Repository #{repo} not yet tapped! Tapping it now..."
 | 
				
			||||||
        Tap.fetch("homebrew", repo).install
 | 
					        Tap.fetch("homebrew", repo).install
 | 
				
			||||||
      end
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      commits += git_log_author_cmd(T.must(repo_path), args)
 | 
					      results[repo] = {
 | 
				
			||||||
      coauthorships += git_log_trailers_cmd(T.must(repo_path), "Co-authored-by", args)
 | 
					        commits:       git_log_author_cmd(T.must(repo_path), args),
 | 
				
			||||||
      signoffs += git_log_trailers_cmd(T.must(repo_path), "Signed-off-by", args)
 | 
					        coauthorships: git_log_trailers_cmd(T.must(repo_path), "Co-authored-by", args),
 | 
				
			||||||
 | 
					        signoffs:      git_log_trailers_cmd(T.must(repo_path), "Signed-off-by", args),
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sentence = "#{args.named.first} directly authored #{commits} commits" \
 | 
					    puts "The user #{args.named.first} has made #{total(results)} contributions #{time_period(args)}."
 | 
				
			||||||
               ", co-authored #{coauthorships} commits" \
 | 
					    puts generate_csv(args.named.first, results) if args.csv?
 | 
				
			||||||
               ", and signed-off #{signoffs} commits " \
 | 
					  end
 | 
				
			||||||
               "across #{all_repos ? "all Homebrew repos" : repos.to_sentence}"
 | 
					
 | 
				
			||||||
    sentence += if args.from && args.to
 | 
					  sig { params(repo: String).returns(Pathname) }
 | 
				
			||||||
 | 
					  def find_repo_path_for_repo(repo)
 | 
				
			||||||
 | 
					    return HOMEBREW_REPOSITORY if repo == "brew"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Tap.fetch("homebrew", repo).path
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  sig { params(args: Homebrew::CLI::Args).returns(String) }
 | 
				
			||||||
 | 
					  def time_period(args)
 | 
				
			||||||
 | 
					    if args.from && args.to
 | 
				
			||||||
      "between #{args.from} and #{args.to}"
 | 
					      "between #{args.from} and #{args.to}"
 | 
				
			||||||
    elsif args.from
 | 
					    elsif args.from
 | 
				
			||||||
      "after #{args.from}"
 | 
					      "after #{args.from}"
 | 
				
			||||||
@ -79,16 +91,32 @@ module Homebrew
 | 
				
			|||||||
    else
 | 
					    else
 | 
				
			||||||
      "in all time"
 | 
					      "in all time"
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    sentence += ". Total: #{commits + coauthorships + signoffs}."
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    puts sentence
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  sig { params(repo: String).returns(Pathname) }
 | 
					  sig { params(user: String, results: Hash).returns(String) }
 | 
				
			||||||
  def find_repo_path_for_repo(repo)
 | 
					  def generate_csv(user, results)
 | 
				
			||||||
    return HOMEBREW_REPOSITORY if repo == "brew"
 | 
					    CSV.generate do |csv|
 | 
				
			||||||
 | 
					      csv << %w[user repo commits coauthorships signoffs total]
 | 
				
			||||||
 | 
					      results.each do |repo, counts|
 | 
				
			||||||
 | 
					        csv << [
 | 
				
			||||||
 | 
					          user,
 | 
				
			||||||
 | 
					          repo,
 | 
				
			||||||
 | 
					          counts[:commits],
 | 
				
			||||||
 | 
					          counts[:coauthorships],
 | 
				
			||||||
 | 
					          counts[:signoffs],
 | 
				
			||||||
 | 
					          counts.values.sum,
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					      csv << [user, "*", "*", "*", "*", total(results)]
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Tap.fetch("homebrew", repo).path
 | 
					  sig { params(results: Hash).returns(Integer) }
 | 
				
			||||||
 | 
					  def total(results)
 | 
				
			||||||
 | 
					    results
 | 
				
			||||||
 | 
					      .values # [{:commits=>1, :coauthorships=>0, :signoffs=>3}, {:commits=>500, :coauthorships=>2, :signoffs=>450}]
 | 
				
			||||||
 | 
					      .map(&:values) # [[1, 0, 3], [500, 2, 450]]
 | 
				
			||||||
 | 
					      .sum(&:sum) # 956
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  sig { params(repo_path: Pathname, args: Homebrew::CLI::Args).returns(Integer) }
 | 
					  sig { params(repo_path: Pathname, args: Homebrew::CLI::Args).returns(Integer) }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user