You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

changelog.rb 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #!/usr/bin/env ruby
  2. require 'optparse'
  3. require 'ostruct'
  4. require 'date'
  5. VERSION = '1.0.0'
  6. ARGV << '-h' if ARGV.empty?
  7. class OptionsParser
  8. def self.parse(args)
  9. options = OpenStruct.new
  10. options.version_name = ''
  11. options.release_date = ''
  12. options.new_branch = 'auto'
  13. opt_parser = OptionParser.new do |opts|
  14. opts.banner = 'Usage: changelog_generator.rb [options]'
  15. opts.separator ''
  16. opts.separator 'Required specific options:'
  17. opts.on('-i', '--version_id VERSIONID',
  18. 'Numerical id of the version [int]') do |i|
  19. options.version_id = i
  20. end
  21. opts.separator ''
  22. opts.separator 'Optional specific options:'
  23. opts.on('-n', '--version_name VERSIONNAME',
  24. 'Name of the version [string]') do |n|
  25. options.version_name = n
  26. end
  27. opts.on('-d', '--release_date RELEASEDATE',
  28. 'Date of the release [string: YYYY-MM-DD]') do |d|
  29. options.release_date = d
  30. end
  31. opts.on('-b', '--new_branch NEWBRANCH',
  32. 'New release branch indicator [string: true/false/auto (default)]') do |b|
  33. options.new_branch = b
  34. end
  35. opts.separator ''
  36. opts.separator 'Common options:'
  37. opts.on_tail('-h', '--help', 'Prints this help') do
  38. puts opts
  39. exit
  40. end
  41. opts.on_tail('-v', '--version', 'Show version') do
  42. puts VERSION
  43. exit
  44. end
  45. end
  46. opt_parser.parse!(args)
  47. options
  48. end
  49. end
  50. # Gracely handle missing required options
  51. begin
  52. options = OptionsParser.parse(ARGV)
  53. required = [:version_id]
  54. missing = required.select{ |param| options[param].nil? }
  55. unless missing.empty?
  56. raise OptionParser::MissingArgument.new(missing.join(', '))
  57. end
  58. rescue OptionParser::ParseError => e
  59. puts e
  60. exit
  61. end
  62. # Extract options values into global variables
  63. $v_id = options[:version_id]
  64. $v_name = options[:version_name]
  65. $r_date = options[:release_date]
  66. $n_branch = options[:new_branch]
  67. module Redmine
  68. module ChangelogGenerator
  69. require 'nokogiri'
  70. require 'open-uri'
  71. @v_id = $v_id
  72. @v_name = $v_name
  73. @r_date = $r_date
  74. @n_branch = $n_branch
  75. ISSUES_URL = 'http://www.redmine.org/projects/redmine/issues' +
  76. '?utf8=%E2%9C%93&set_filter=1' +
  77. '&f%5B%5D=status_id&op%5Bstatus_id%5D=*' +
  78. '&f%5B%5D=fixed_version_id&op%5Bfixed_version_id%5D=%3D' +
  79. '&v%5Bfixed_version_id%5D%5B%5D=' + @v_id +
  80. '&f%5B%5D=&c%5B%5D=tracker&c%5B%5D=subject' +
  81. '&c%5B%5D=category&group_by='
  82. VERSIONS_URL = 'http://www.redmine.org/versions/' + @v_id
  83. PAGINATION_ITEMS_SPAN_SELECTOR = 'div#content p.pagination > span.items'
  84. ISSUE_TR_SELECTOR = 'div#content table.list.issues > tbody > tr'
  85. VERSION_DETAILS_SELECTOR = 'div#content'
  86. VERSION_NAME_SELECTOR = 'div#content > h2'
  87. RELEASE_DATE_SELECTOR = 'div#content > div#roadmap > p'
  88. PAGINATION_ITEMS_SPAN_REGEX = %r{(?:[(])([\d]+)(?:-)([\d]+)(?:[\/])([\d]+)(?:[)])}
  89. RELEASE_DATE_REGEX_INCOMPLETE = %r{\((\d{4}-\d{2}-\d{2})\)}
  90. RELEASE_DATE_REGEX_COMPLETE = %r{^(\d{4}-\d{2}-\d{2})}
  91. VERSION_REGEX = %r{^(\d+)(?:\.(\d+))?(?:\.(\d+))?}
  92. CONNECTION_ERROR_MSG = "Connection error: couldn't retrieve data from " +
  93. "https://www.redmine.org.\n" +
  94. "Please try again later..."
  95. class << self
  96. def generate
  97. parse_pagination_items_span_content
  98. get_changelog_items(@no_of_pages)
  99. sort_changelog_items
  100. build_output(@changelog_items, @no_of_issues, version_name, release_date,
  101. new_branch?, 'packaged_file')
  102. build_output(@changelog_items, @no_of_issues, version_name, release_date,
  103. new_branch?, 'website')
  104. end
  105. def parse_pagination_items_span_content
  106. items_span = retrieve_pagination_items_span_content
  107. items_span = items_span.match(PAGINATION_ITEMS_SPAN_REGEX)
  108. items_per_page = items_span[2].to_i
  109. @no_of_issues = items_span[3].to_i
  110. begin
  111. raise if items_per_page == 0 || @no_of_issues == 0
  112. rescue => e
  113. puts "No changelog items to process.\n" +
  114. "Make sure to provide a valid version id as the -i parameter."
  115. exit
  116. end
  117. @no_of_pages = @no_of_issues / items_per_page
  118. @no_of_pages += 1 if @no_of_issues % items_per_page > 0
  119. end
  120. def retrieve_pagination_items_span_content
  121. begin
  122. Nokogiri::HTML(open(ISSUES_URL)).css(PAGINATION_ITEMS_SPAN_SELECTOR).text
  123. rescue OpenURI::HTTPError
  124. puts CONNECTION_ERROR_MSG
  125. exit
  126. end
  127. end
  128. def get_changelog_items(no_of_pages)
  129. # Initialize @changelog_items hash
  130. #
  131. # We'll store categories as hash keys and issues, as nested
  132. # hashes, in nested arrays as the hash'es values:
  133. #
  134. # {"categoryX"=>
  135. # [{"id"=>1, "tracker"=>"tracker1", "subject"=>"subject1"},
  136. # {"id"=>2, "tracker"=>"tracker2", "subject"=>"subject2"}],
  137. # "categoryY"=>
  138. # [{"id"=>3, "tracker"=>"tracker3", "subject"=>"subject3"},
  139. # {"id"=>4, "tracker"=>"tracker4", "subject"=>"subject4"}]}
  140. #
  141. @changelog_items = Hash.new
  142. (1..no_of_pages).each do |page_number|
  143. page = retrieve_issues_list_page(page_number)
  144. page_trs = page.css(ISSUE_TR_SELECTOR).to_a
  145. store_changelog_items(page_trs)
  146. end
  147. end
  148. def retrieve_issues_list_page(page_number)
  149. begin
  150. Nokogiri::HTML(open(ISSUES_URL + '&page=' + page_number.to_s))
  151. rescue OpenURI::HTTPError
  152. puts CONNECTION_ERROR_MSG
  153. exit
  154. end
  155. end
  156. def store_changelog_items(page_trs)
  157. page_trs.each do |tr|
  158. cat = tr.css('td.category').text
  159. unless @changelog_items.keys.include?(cat)
  160. @changelog_items.store(cat, [])
  161. end
  162. issue_hash = { 'id' => tr.css('td.id > a').text.to_i,
  163. 'tracker' => tr.css('td.tracker').text,
  164. 'subject' => tr.css('td.subject> a').text.strip }
  165. @changelog_items[cat].push(issue_hash)
  166. end
  167. end
  168. # Sort the changelog items hash
  169. def sort_changelog_items
  170. # Sort changelog items hash values; first by tracker, then by id
  171. @changelog_items.each do |key, value|
  172. @changelog_items[key] = value.sort_by{ |a| [a['tracker'], a['id']] }
  173. end
  174. # Sort changelog items hash keys; by category
  175. @changelog_items = @changelog_items.sort
  176. end
  177. def version_name
  178. @v_name.empty? ? (@version_name || parse_version_name) : @v_name
  179. end
  180. def parse_version_name
  181. version_details = retrieve_version_details
  182. @version_name = version_details.css(VERSION_NAME_SELECTOR).text
  183. end
  184. def release_date
  185. @r_date.empty? ? (@release_date || Date.today.strftime("%Y-%m-%d")) : @r_date
  186. end
  187. def retrieve_version_details
  188. begin
  189. Nokogiri::HTML(open(VERSIONS_URL)).css(VERSION_DETAILS_SELECTOR)
  190. rescue OpenURI::HTTPError
  191. puts CONNECTION_ERROR_MSG
  192. exit
  193. end
  194. end
  195. def new_branch?
  196. @new_branch.nil? ? parse_new_branch : @new_branch
  197. end
  198. def parse_new_branch
  199. @version_name =~ VERSION_REGEX
  200. version = Array.new([$1, $2, $3])
  201. case @n_branch
  202. when 'auto'
  203. # New branch version detection logic:
  204. #
  205. # [x.x.0] => true
  206. # [x.x.>0] => false
  207. # [x.x] => true
  208. # [x] => true
  209. #
  210. if (version[2] != nil && version[2] == '0') ||
  211. (version[2] == nil && version[1] != nil) ||
  212. (version[2] == nil && version[1] == nil && version[0] != nil)
  213. new_branch = true
  214. end
  215. when 'true'
  216. new_branch = true
  217. when 'false'
  218. new_branch = false
  219. end
  220. @new_branch = new_branch
  221. end
  222. # Build and write the changelog file
  223. def build_output(items, no_of_issues, v_name, r_date, n_branch, target)
  224. target = target
  225. output_filename = v_name + '_changelog_for_' + target + '.txt'
  226. out_file = File.new(output_filename, 'w')
  227. # Categories counter
  228. c_cnt = 0
  229. # Issues with category counter
  230. i_cnt = 0
  231. # Issues without category counter
  232. nc_i_cnt = 0
  233. if target == 'packaged_file'
  234. out_file << "== #{r_date} v#{v_name}\n\n"
  235. elsif target == 'website'
  236. out_file << "h1. Changelog #{v_name}\n\n" if n_branch == true
  237. out_file << "h2. version:#{v_name} (#{r_date})\n\n"
  238. end
  239. # Print the categories...
  240. items.each do |key, values|
  241. key = key.empty? ? '-none-' : key
  242. if target == 'packaged_file'
  243. out_file << "=== [#{key}]\n"
  244. elsif target == 'website'
  245. out_file << "h3. [#{key}]\n"
  246. end
  247. out_file << "\n"
  248. (c_cnt += 1) unless key == '-none-'
  249. # ...and their associated issues
  250. values.each do |val|
  251. out_file << "* #{val['tracker']} ##{val['id']}: #{val['subject']}\n"
  252. key == '-none-' ? (nc_i_cnt += 1) : (i_cnt += 1)
  253. end
  254. out_file << "\n"
  255. end
  256. summary(v_name, target, i_cnt, nc_i_cnt, no_of_issues, c_cnt)
  257. out_file.close
  258. end
  259. def summary(v_name, target, i_cnt, nc_i_cnt, no_of_issues, c_cnt)
  260. summary = (('-' * 72) + "\n")
  261. summary << "Generation of the #{v_name} changelog for '#{target}' has " +
  262. "#{result_label(i_cnt, nc_i_cnt, no_of_issues)}:\n"
  263. summary << "* #{i_cnt} #{issue_label(i_cnt)} within #{c_cnt} issue " +
  264. "#{category_label(c_cnt)}\n"
  265. if nc_i_cnt > 0
  266. summary << "* #{nc_i_cnt} #{issue_label(nc_i_cnt)} without issue category\n"
  267. end
  268. puts summary
  269. return summary
  270. end
  271. def result_label(i_cnt, nc_i_cnt, no_of_issues)
  272. result = i_cnt + nc_i_cnt == no_of_issues ? 'succeeded' : 'failed'
  273. result.upcase
  274. end
  275. def issue_label(count)
  276. count > 1 ? 'issues' : 'issue'
  277. end
  278. def category_label(count)
  279. count > 1 ? 'categories' : 'category'
  280. end
  281. end
  282. end
  283. end
  284. Redmine::ChangelogGenerator.generate