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 11KB

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