Created
November 25, 2009 09:25
-
-
Save mhorbul/242594 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'net/http' | |
| require 'uri' | |
| require 'hpricot' | |
| require 'optparse' | |
| require 'set' | |
| def timed(&block) | |
| s = Time.now | |
| yield | |
| puts "done in #{Time.now - s} seconds." | |
| end | |
| def usage(msg, error = nil) | |
| if error.is_a?(Array) | |
| error = "\n\t- #{error.join("\n\t- ")}\n\n" | |
| end | |
| $stderr.puts "Errors: #{error}" if error | |
| $stderr.puts msg | |
| exit(1) | |
| end | |
| options = {} | |
| parser = OptionParser.new do |p| | |
| p.banner = "Usage: #{File.basename(__FILE__)} [options]" | |
| p.on("-l USERNAME", "--username=USERNAME", "Unfuddle username") do |v| | |
| options[:username] = v | |
| end | |
| p.on("-p PASSWORD", "--password=PASSWORD", "Unfuddle password") do |v| | |
| options[:password] = v | |
| end | |
| p.on("-u URL", "--url=URL", "Unfuddle report URL" ) do |v| | |
| options[:url] = v | |
| end | |
| p.on("-d BASE_DIR", "--base-dir=BASE_DIR", "Projects git clone location" ) do |path| | |
| options[:base_dir] = File.expand_path(path) | |
| end | |
| p.on("-f PROJECTS", "--projects=PROJECTS", "git clone project folder names (comma separated)" ) do |v| | |
| options[:projects] = v | |
| end | |
| p.on("-b BRANCHES", "--branches=BRANCHES", "Branches to match (comma separated)" ) do |v| | |
| options[:branches] = v | |
| end | |
| end | |
| begin | |
| args = parser.parse!(ARGV) | |
| rescue => e | |
| usage(parser, e) | |
| end | |
| errors = [] | |
| errors << "Username can't be blank." if options[:username].nil? | |
| errors << "Password can't be blank." if options[:password].nil? | |
| errors << "URL can't be blank." if options[:url].nil? | |
| if options[:base_dir].nil? | |
| errors << "Base dir can't be blank." | |
| else | |
| unless File.exist?(options[:base_dir]) | |
| errors << "Folder `#{options[:base_dir]}' does not exist." | |
| end | |
| end | |
| begin | |
| uri = URI.parse(options[:url]) unless options[:url].nil? | |
| rescue | |
| errors << "Invalid URL format." | |
| end | |
| projects = (options[:projects].nil?) ? | |
| Dir.glob(File.join(options[:base_dir], "sa-*")). | |
| reduce([]) { |list,p| File.basename(p) } : | |
| options[:projects].to_s.split(",").map { |p| p.strip } | |
| if projects.empty? | |
| errors << "Projects can't be blank." | |
| else | |
| projects.each do |project| | |
| unless File.exist?(File.join(options[:base_dir], project)) | |
| errors << "Project `#{project}' does not exist in `#{options[:base_dir]}'." | |
| end | |
| end | |
| end | |
| branches = options[:branches].to_s.split(",").map { |p| p.strip } | |
| if branches.empty? | |
| errors << "Branches can't be blank." | |
| end | |
| usage(parser, errors) unless errors.empty? | |
| html = "" | |
| http = Net::HTTP.new(uri.host, 80) | |
| http.start do |http| | |
| req = Net::HTTP::Get.new(uri.request_uri) | |
| req.basic_auth(options[:username], options[:password]) | |
| html = http.request(req) | |
| end | |
| doc = Hpricot(html.body) | |
| tickets = [] | |
| doc.search("//ticket").each do |ticket| | |
| tickets << [ | |
| ticket.at('number').inner_html.to_i, | |
| ticket.at('summary').inner_html | |
| ] | |
| end | |
| remotes = branches.reduce(Set.new) do |remotes,branch| | |
| result = branch.split("/") | |
| remotes << (result.size > 1 ? result.first : "upstream") | |
| end | |
| projects.each do |prj| | |
| timed do | |
| print "Updating remotes for project `#{prj}' ... "; $stdout.flush | |
| `cd #{options[:base_dir]}/#{prj}` | |
| `git remote update #{remotes.to_a.join(" ")} 2>&1 > /dev/null` | |
| end | |
| end | |
| tickets.each do |ticket| | |
| puts "*** [##{ticket[0]}] #{ticket[1]} ***" | |
| projects.each do |prj| | |
| branches.each do |branch| | |
| branch = branch.match(/\//) ? branch : "upstream/#{branch}" | |
| out = `cd #{options[:base_dir]}/#{prj}; git log --abbrev-commit --pretty=oneline #{branch} | grep '##{ticket[0]}]'` | |
| if out != "" | |
| puts "\t#{prj}/#{branch}" | |
| puts "\t\t#{out.gsub(/\n/, "\n\t\t")}" | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment