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