Guest User

Untitled

a guest
Apr 25th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. require 'rubygems'
  2. require 'net/http'
  3. require 'uri'
  4. require 'hpricot'
  5. require 'optparse'
  6. require 'set'
  7.  
  8. def timed(&block)
  9. s = Time.now
  10. yield
  11. puts "done in #{Time.now - s} seconds."
  12. end
  13.  
  14. def usage(msg, error = nil)
  15. if error.is_a?(Array)
  16. error = "\n\t- #{error.join("\n\t- ")}\n\n"
  17. end
  18. $stderr.puts "Errors: #{error}" if error
  19. $stderr.puts msg
  20. exit(1)
  21. end
  22.  
  23. options = {}
  24.  
  25. parser = OptionParser.new do |p|
  26. p.banner = "Usage: #{File.basename(__FILE__)} [options]"
  27.  
  28. p.on("-l USERNAME", "--username=USERNAME", "Unfuddle username") do |v|
  29. options[:username] = v
  30. end
  31.  
  32. p.on("-p PASSWORD", "--password=PASSWORD", "Unfuddle password") do |v|
  33. options[:password] = v
  34. end
  35.  
  36. p.on("-u URL", "--url=URL", "Unfuddle report URL" ) do |v|
  37. options[:url] = v
  38. end
  39.  
  40. p.on("-d BASE_DIR", "--base-dir=BASE_DIR", "Projects git clone location" ) do |path|
  41. options[:base_dir] = File.expand_path(path)
  42. end
  43.  
  44. p.on("-f PROJECTS", "--projects=PROJECTS", "git clone project folder names (comma separated)" ) do |v|
  45. options[:projects] = v
  46. end
  47.  
  48. p.on("-b BRANCHES", "--branches=BRANCHES", "Branches to match (comma separated)" ) do |v|
  49. options[:branches] = v
  50. end
  51.  
  52. end
  53.  
  54. begin
  55. args = parser.parse!(ARGV)
  56. rescue => e
  57. usage(parser, e)
  58. end
  59.  
  60. errors = []
  61.  
  62. errors << "Username can't be blank." if options[:username].nil?
  63. errors << "Password can't be blank." if options[:password].nil?
  64. errors << "URL can't be blank." if options[:url].nil?
  65. if options[:base_dir].nil?
  66. errors << "Base dir can't be blank."
  67. else
  68. unless File.exist?(options[:base_dir])
  69. errors << "Folder `#{options[:base_dir]}' does not exist."
  70. end
  71. end
  72.  
  73. begin
  74. uri = URI.parse(options[:url]) unless options[:url].nil?
  75. rescue
  76. errors << "Invalid URL format."
  77. end
  78.  
  79. projects = (options[:projects].nil?) ?
  80. Dir.glob(File.join(options[:base_dir], "sa-*")).
  81. reduce([]) { |list,p| File.basename(p) } :
  82. options[:projects].to_s.split(",").map { |p| p.strip }
  83.  
  84. if projects.empty?
  85. errors << "Projects can't be blank."
  86. else
  87. projects.each do |project|
  88. unless File.exist?(File.join(options[:base_dir], project))
  89. errors << "Project `#{project}' does not exist in `#{options[:base_dir]}'."
  90. end
  91. end
  92. end
  93.  
  94. branches = options[:branches].to_s.split(",").map { |p| p.strip }
  95. if branches.empty?
  96. errors << "Branches can't be blank."
  97. end
  98.  
  99. usage(parser, errors) unless errors.empty?
  100.  
  101. html = ""
  102. http = Net::HTTP.new(uri.host, 80)
  103. http.start do |http|
  104. req = Net::HTTP::Get.new(uri.request_uri)
  105. req.basic_auth(options[:username], options[:password])
  106. html = http.request(req)
  107. end
  108.  
  109. doc = Hpricot(html.body)
  110. tickets = []
  111. doc.search("//ticket").each do |ticket|
  112. tickets << [
  113. ticket.at('number').inner_html.to_i,
  114. ticket.at('summary').inner_html
  115. ]
  116. end
  117.  
  118. remotes = branches.reduce(Set.new) do |remotes,branch|
  119. result = branch.split("/")
  120. remotes << (result.size > 1 ? result.first : "upstream")
  121. end
  122.  
  123. projects.each do |prj|
  124. timed do
  125. print "Updating remotes for project `#{prj}' ... "; $stdout.flush
  126. `cd #{options[:base_dir]}/#{prj}`
  127. `git remote update #{remotes.to_a.join(" ")} 2>&1 > /dev/null`
  128. end
  129. end
  130.  
  131. tickets.each do |ticket|
  132. puts "*** [##{ticket[0]}] #{ticket[1]} ***"
  133. projects.each do |prj|
  134. branches.each do |branch|
  135. branch = branch.match(/\//) ? branch : "upstream/#{branch}"
  136. out = `cd #{options[:base_dir]}/#{prj}; git log --abbrev-commit --pretty=oneline #{branch} | grep '##{ticket[0]}]'`
  137. if out != ""
  138. puts "\t#{prj}/#{branch}"
  139. puts "\t\t#{out.gsub(/\n/, "\n\t\t")}"
  140. end
  141. end
  142. end
  143. end
Add Comment
Please, Sign In to add comment