Project

General

Profile

Patch #35648 » 0001-Update-changelog-generator-script-to-use-the-API-to-.patch

Marius BĂLTEANU, 2024-11-12 20:44

View differences:

bin/changelog.rb
2 2
require 'optparse'
3 3
require 'ostruct'
4 4
require 'date'
5
require 'uri'
6
require 'net/http'
7
require 'json'
8
require 'base64'
5 9

  
6 10
VERSION = '1.0.0'
7 11

  
......
10 14
class OptionsParser
11 15
  def self.parse(args)
12 16
    options = OpenStruct.new
13
    options.version_name = ''
14 17
    options.release_date = ''
15
    options.new_branch = 'auto'
18
    options.api_url = 'https://www.redmine.org'
16 19

  
17 20
    opt_parser = OptionParser.new do |opts|
18 21
      opts.banner = 'Usage: changelog_generator.rb [options]'
......
28 31
      opts.separator ''
29 32
      opts.separator 'Optional specific options:'
30 33

  
31
      opts.on('-n', '--version_name VERSIONNAME',
32
              'Name of the version [string]') do |n|
33
        options.version_name = n
34
      end
35 34
      opts.on('-d', '--release_date RELEASEDATE',
36 35
              'Date of the release [string: YYYY-MM-DD]') do |d|
37 36
        options.release_date = d
38 37
      end
39
      opts.on('-b', '--new_branch NEWBRANCH',
40
              'New release branch indicator [string: true/false/auto (default)]') do |b|
41
        options.new_branch = b
38

  
39
      opts.on('-u', '--api-url URL',
40
              'Redmine API URL for requests. Default is https://www.redmine.org') do |u|
41
        options.api_url = u
42
      end
43

  
44
      opts.on('-a', '--api_key APIKEY',
45
              'Redmine API-Key to authenticate to the API. Default mode is anonymous') do |a|
46
        options.api_key = a
42 47
      end
43 48

  
44 49
      opts.separator ''
......
58 63
    opt_parser.parse!(args)
59 64
    options
60 65
  end
61
end
62 66

  
63
# Gracely handle missing required options
64
begin
65
  options = OptionsParser.parse(ARGV)
66
  required = [:version_id]
67
  missing = required.select{ |param| options[param].nil? }
68
  unless missing.empty?
69
    raise OptionParser::MissingArgument.new(missing.join(', '))
67
  # Gracely handle missing required options
68
  begin
69
    options = OptionsParser.parse(ARGV)
70
    required = [:version_id]
71
    missing = required.select{ |param| options[param].nil? }
72
    unless missing.empty?
73
      raise OptionParser::MissingArgument.new(missing.join(', '))
74
    end
75
  rescue OptionParser::ParseError => e
76
    puts e
77
    exit
70 78
  end
71
rescue OptionParser::ParseError => e
72
  puts e
73
  exit
74
end
75 79

  
76
# Extract options values into global variables
77
$v_id = options[:version_id]
78
$v_name = options[:version_name]
79
$r_date = options[:release_date]
80
$n_branch = options[:new_branch]
80
  # Extract options values into global variables
81
  $v_id = options[:version_id]
82
  $r_date = options[:release_date]
83
  $api_url = options[:api_url]
84
  $api_key = options[:api_key]
85
end
81 86

  
82 87
module Redmine
83 88
  module ChangelogGenerator
84
    require 'nokogiri'
85 89
    require 'open-uri'
86 90

  
87 91
    @v_id = $v_id
88
    @v_name = $v_name
89 92
    @r_date = $r_date
90
    @n_branch = $n_branch
91

  
92
    ISSUES_URL = 'https://www.redmine.org/projects/redmine/issues' +
93
                 '?utf8=%E2%9C%93&set_filter=1' +
94
                 '&f%5B%5D=status_id&op%5Bstatus_id%5D=*' +
95
                 '&f%5B%5D=fixed_version_id&op%5Bfixed_version_id%5D=%3D' +
96
                   '&v%5Bfixed_version_id%5D%5B%5D=' + @v_id +
97
                 '&f%5B%5D=&c%5B%5D=tracker&c%5B%5D=subject' +
98
                 '&c%5B%5D=category&group_by='
99
    VERSIONS_URL = 'https://www.redmine.org/versions/' + @v_id
100

  
101
    PAGINATION_ITEMS_SPAN_SELECTOR = 'div#content span.pagination span.items'
102
    ISSUE_TR_SELECTOR = 'div#content table.list.issues > tbody > tr'
103
    VERSION_DETAILS_SELECTOR = 'div#content'
104
    VERSION_NAME_SELECTOR = 'div#roadmap > h2'
105
    RELEASE_DATE_SELECTOR = 'div#roadmap > div.version-overview p'
106

  
107
    PAGINATION_ITEMS_SPAN_REGEX = %r{(?:[(])([\d]+)(?:-)([\d]+)(?:[\/])([\d]+)(?:[)])}
108
    RELEASE_DATE_REGEX_INCOMPLETE = %r{\((\d{4}-\d{2}-\d{2})\)}
109
    RELEASE_DATE_REGEX_COMPLETE = %r{^(\d{4}-\d{2}-\d{2})}
110
    VERSION_REGEX = %r{^(\d+)(?:\.(\d+))?(?:\.(\d+))?}
93
    @api_url = $api_url
94
    @api_key = $api_key
111 95

  
112 96
    CONNECTION_ERROR_MSG = "Connection error: couldn't retrieve data from " +
113
                           "https://www.redmine.org.\n" +
114
                           "Please try again later..."
97
      "https://www.redmine.org.\n" +
98
      "Please try again later..."
99

  
100
    # Page size (number of issues per request)
101
    PAGE_SIZE = 100
115 102

  
116 103
    class << self
117 104
      def generate
118
        parse_pagination_items_span_content
119
        get_changelog_items(@no_of_pages)
105
        get_changelog_items
120 106
        sort_changelog_items
121
        build_output(@changelog_items, @no_of_issues, version_name, release_date,
122
                     new_branch?, 'packaged_file')
123
        build_output(@changelog_items, @no_of_issues, version_name, release_date,
124
                     new_branch?, 'website')
107

  
108
        build_output(@changelog_items, @no_of_issues, @version_name, release_date, 'packaged_file')
109
        build_output(@changelog_items, @no_of_issues, @version_name, release_date, 'website')
125 110
      end
126 111

  
127
      def parse_pagination_items_span_content
128
        items_span = retrieve_pagination_items_span_content
129
        items_span = items_span.match(PAGINATION_ITEMS_SPAN_REGEX)
112
      def api_issues_get(page)
113
        uri = URI(@api_url + "/issues.json?fixed_version_id=#{@v_id}&status_id=*&limit=#{PAGE_SIZE}&offset=#{page * PAGE_SIZE}")
130 114

  
131
        items_per_page = items_span[2].to_i
132
        @no_of_issues = items_span[3].to_i
115
        https = Net::HTTP.new(uri.host, uri.port)
116
        https.use_ssl = true if @api_url.start_with?("https")
133 117

  
134
        begin
135
          raise if items_per_page == 0 || @no_of_issues == 0
136
        rescue => e
137
          puts "No changelog items to process.\n" +
138
               "Make sure to provide a valid version id as the -i parameter."
118
        request = Net::HTTP::Get.new(uri)
119
        request['Content-Type'] = "application/json"
120
        request['X-Redmine-API-Key'] = @api_key
121

  
122
        response = https.request(request)
123

  
124
        unless response.is_a?(Net::HTTPSuccess)
125
          puts CONNECTION_ERROR_MSG
139 126
          exit
140 127
        end
141 128

  
142
        @no_of_pages = @no_of_issues / items_per_page
143
        @no_of_pages += 1 if @no_of_issues % items_per_page > 0
129
        JSON.parse(response.body)
144 130
      end
145 131

  
146
      def retrieve_pagination_items_span_content
147
        begin
148
          Nokogiri::HTML(URI.open(ISSUES_URL)).css(PAGINATION_ITEMS_SPAN_SELECTOR).text
149
        rescue OpenURI::HTTPError
150
          puts CONNECTION_ERROR_MSG
151
          exit
132
      def retrieve_issues
133
        page = 0
134
        issues_request = api_issues_get(page)
135
        @no_of_issues = issues_request['total_count']
136

  
137
        issues = issues_request['issues']
138
        while issues.length > 0 && @no_of_issues > issues.length
139
          page += 1
140
          new_issues = api_issues_get(page)
141
          issues.concat(new_issues['issues'])
152 142
        end
143

  
144
        issues
153 145
      end
154 146

  
155
      def get_changelog_items(no_of_pages)
147
      def get_changelog_items
156 148
        # Initialize @changelog_items hash
157 149
        #
158 150
        # We'll store categories as hash keys and issues, as nested
......
167 159
        #
168 160
        @changelog_items = Hash.new
169 161

  
170
        (1..no_of_pages).each do |page_number|
171
          page = retrieve_issues_list_page(page_number)
172
          page_trs = page.css(ISSUE_TR_SELECTOR).to_a
173
          store_changelog_items(page_trs)
174
        end
162
        issues = retrieve_issues
163
        store_changelog_items(issues)
175 164
      end
176 165

  
177
      def retrieve_issues_list_page(page_number)
178
        begin
179
          Nokogiri::HTML(URI.open(ISSUES_URL + '&page=' + page_number.to_s))
180
        rescue OpenURI::HTTPError
181
          puts CONNECTION_ERROR_MSG
182
          exit
183
        end
184
      end
166
      def store_changelog_items(issues)
167
        issues.each do |issue|
168
          cat = issue.has_key?('category') ? issue['category']['name'] : "No category"
185 169

  
186
      def store_changelog_items(page_trs)
187
        page_trs.each do |tr|
188
          cat = tr.css('td.category').text
189 170
          unless @changelog_items.keys.include?(cat)
190 171
            @changelog_items.store(cat, [])
191 172
          end
192 173

  
193
          issue_hash = { 'id'       => tr.css('td.id > a').text.to_i,
194
                         'tracker'  => tr.css('td.tracker').text,
195
                         'subject'  => tr.css('td.subject> a').text.strip }
174
          parse_version_name(issue['fixed_version']['name'])
175
          issue_hash = { 'id'       => issue['id'],
176
                         'tracker'  => issue['tracker']['name'],
177
                         'subject'  => issue['subject']
178
          }
179

  
196 180
          @changelog_items[cat].push(issue_hash)
197 181
        end
198 182
      end
......
207 191
        @changelog_items = @changelog_items.sort
208 192
      end
209 193

  
210
      def version_name
211
        @v_name.empty? ? (@version_name || parse_version_name) : @v_name
212
      end
213

  
214
      def parse_version_name
215
        version_details = retrieve_version_details
216
        @version_name = version_details.css(VERSION_NAME_SELECTOR).text
217
      end
218

  
219
      def release_date
220
        @r_date.empty? ? (@release_date || Date.today.strftime("%Y-%m-%d")) : @r_date
221
      end
222

  
223
      def retrieve_version_details
194
      def parse_version_name(version)
224 195
        begin
225
          Nokogiri::HTML(URI.open(VERSIONS_URL)).css(VERSION_DETAILS_SELECTOR)
226
        rescue OpenURI::HTTPError
227
          puts CONNECTION_ERROR_MSG
228
          exit
196
          if !@version_name || Gem::Version.new(version) > Gem::Version.new(@version_name)
197
            @version_name = version
198
          end
199
        rescue
200
          @version_name = version
229 201
        end
230 202
      end
231 203

  
232
      def new_branch?
233
        @new_branch.nil? ? parse_new_branch : @new_branch
234
      end
235

  
236
      def parse_new_branch
237
        @version_name =~ VERSION_REGEX
238
        version = Array.new([$1, $2, $3])
239

  
240
        case @n_branch
241
        when 'auto'
242
          # New branch version detection logic:
243
          #
244
          #   [x.x.0]  => true
245
          #   [x.x.>0] => false
246
          #   [x.x]    => true
247
          #   [x]      => true
248
          #
249
          if (version[2] != nil && version[2] == '0') ||
250
             (version[2] == nil && version[1] != nil) ||
251
             (version[2] == nil && version[1] == nil && version[0] != nil)
252
            new_branch = true
253
          end
254
        when 'true'
255
          new_branch = true
256
        when 'false'
257
          new_branch = false
258
        end
259
        @new_branch = new_branch
204
      def release_date
205
        @r_date.empty? ? (@release_date || Date.today.strftime("%Y-%m-%d")) : @r_date
260 206
      end
261 207

  
262 208
      # Build and write the changelog file
263
      def build_output(items, no_of_issues, v_name, r_date, n_branch, target)
209
      def build_output(items, no_of_issues, v_name, r_date, target)
264 210
        target = target
265 211

  
266 212
        output_filename = v_name + '_changelog_for_' + target + '.txt'
......
276 222
        if target == 'packaged_file'
277 223
          out_file << "== #{r_date} v#{v_name}\n\n"
278 224
        elsif target == 'website'
279
          out_file << "h1. Changelog #{v_name}\n\n" if n_branch == true
280 225
          out_file << "h2. version:#{v_name} (#{r_date})\n\n"
281 226
        end
282 227

  
......
308 253
      def summary(v_name, target, i_cnt, nc_i_cnt, no_of_issues, c_cnt)
309 254
        summary = (('-' * 72) + "\n")
310 255
        summary << "Generation of the #{v_name} changelog for '#{target}' has " +
311
                   "#{result_label(i_cnt, nc_i_cnt, no_of_issues)}:\n"
256
          "#{result_label(i_cnt, nc_i_cnt, no_of_issues)}:\n"
312 257
        summary << "* #{i_cnt} #{issue_label(i_cnt)} within #{c_cnt} issue " +
313
                   "#{category_label(c_cnt)}\n"
258
          "#{category_label(c_cnt)}\n"
314 259
        if nc_i_cnt > 0
315 260
          summary << "* #{nc_i_cnt} #{issue_label(nc_i_cnt)} without issue category\n"
316 261
        end
    (1-1/1)