summaryrefslogtreecommitdiffstats
path: root/meeting/create-meeting-boilerplate.rb
blob: d5f0ee034d992877bb6afcdf268819e66a277744 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#! /usr/bin/env ruby
# create-meeting-boilerplate.rb
# Copyright (C) 2012 Akira TAGOH
#
# Authors:
#   Akira TAGOH  <tagoh@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

#
# To install trac4r
#   gem install -r trac4r mediawiki-gateway
require 'rubygems'
require 'trac4r'
require 'media_wiki'
require 'open-uri'
require 'optparse'
require 'date'

date = nil
options = {:update_bugs => true}

begin
  ARGV.options do |opt|
    opt.banner = sprintf("Usage: %s [yyyy/mm/dd]", File.basename(__FILE__))
    opt.on('-q', '--quiet', 'Do not show any output.') {|v| options[:quiet] = v}
    opt.on('--[no-]update-bugs', 'Do not update bugs.') {|v| options[:update_bugs] = v}
    opt.parse!

    subargv = opt.order(ARGV)
    if subargv.length == 0 then
      subargv << Date.today.strftime('%Y/%m/%d')
    end
    if subargv.length == 0 ||
        subargv[0] !~ /\A([0-9]{4})\/([0-9]{1,2})\/([0-9]{1,2})\Z/ then
      puts opt.help
      exit
    end
    date = Date.new($1.to_i, $2.to_i, $3.to_i)
  end
end

trac = Trac.new("https://fedorahosted.org/i18n/xmlrpc")
newtopic = []
ongoingtopic = []
newtask = []
ongoingtask = []
newother = []
ongoingother = []

STDERR.printf("Retrieving information from trac...") if !options[:quiet]
trac.tickets.list(:include_closed => false).each do |id|
  ticket = trac.tickets.get(id)
  if ticket.status == 'new' then
    case ticket.type
    when 'meeting'
      newtopic << ticket
    when 'task'
      newtask << ticket
    else
      newother << ticket
    end
  else
    case ticket.type
    when 'meeting'
      ongoingtopic << ticket
    when 'task'
      ongoingtask << ticket
    else
      ongoingother << ticket
    end
  end
end
STDERR.printf("Done\n") if !options[:quiet]

doc = ""
header = ["* Upcoming schedule"]
footer = ["* Open Floor"]

header.each do |l|
  doc << sprintf("%s\n", l)
end
unless ongoingtopic.empty? then
  doc << sprintf("* Outstanding topic%s\n", ongoingtopic.length > 1 ? 's' : '')
  ongoingtopic.each do |ticket|
    doc << sprintf("** [https://fedorahosted.org/i18n/ticket/%d #%d: %s]\n",
                   ticket.id, ticket.id, ticket.summary)
  end
end
unless ongoingtask.empty? then
  doc << sprintf("* Outstanding task%s\n", ongoingtask.length > 1 ? 's' : '')
  ongoingtask.each do |ticket|
    doc << sprintf("** [https://fedorahosted.org/i18n/ticket/%d #%d: %s]\n",
                   ticket.id, ticket.id, ticket.summary)
  end
end
unless newtopic.empty? then
  doc << sprintf("* New topic%s\n", newtopic.length > 1 ? 's' : '')
  newtopic.each do |ticket|
    doc << sprintf("** [https://fedorahosted.org/i18n/ticket/%d #%d: %s]\n",
                   ticket.id, ticket.id, ticket.summary)
  end
end
unless newtask.empty? then
  doc << sprintf("* New task%s\n", newtask.length > 1 ? 's' : '')
  newtask.each do |ticket|
    doc << sprintf("** [https://fedorahosted.org/i18n/ticket/%d #%d: %s]\n",
                   ticket.id, ticket.id, ticket.summary)
  end
end

footer.each do |l|
  doc << sprintf("%s\n", l)
end
doc = "" if ongoingtopic.empty? && newtopic.empty? && ongoingtask.empty? && newtask.empty?

STDERR.printf("Retrieving the template from wiki...") if !options[:quiet]
mw = MediaWiki::Gateway.new('https://fedoraproject.org/w/api.php')
d = date
dw = 0
page = ""
name = nil
loop do
  d = d - 7
  name = sprintf("I18N/Meetings/%04d-%02d-%02d", d.year, d.month, d.day)
  page = mw.get(name)
  dw = (date - d).to_i / 7
  break unless page.nil?
end
tmpl = mw.get('Template:I18N/Meeting')
STDERR.printf("Done (based on %s)\n", name) if !options[:quiet]

section = nil
value = nil
bug_stat = {}
bug_latest_stat = {}
bug_ids = {}

page.split("\n").map do |l|
  if l =~ /\A\|(.*)=(.*)/ then
    section = $1
    value = $2
  end
  case section
  when /bug_(.*)_id/
    bug_ids[$1] = value
  end
end

STDERR.printf("Retrieving bugzilla information...") if !options[:quiet]
if options[:update_bugs] then
  tmpl.split("\n").map do |l|
    if l =~ /http/ then
      l.split('||').each do |ll|
        # XXX: could checking conditional variable with recursive-regexp but
        # no idea how to remember the history.
        # so assuming the first one is the latest and what we really want so far.
        if ll =~ %r,\{\{\{(.*)\}\}\}\s+\[(https://bugzilla.redhat.com/[^\s]+)\s+.*\], then
          url = $2
          a = $1
          key = $1.sub(/\|.*/, '').sub(/\Abug_/, '')
          url.gsub!(/\^/, '%5E')
          if url =~ /\{\{\{bug_([^|]+)_id\|/ then
            idkey = $1
            if bug_ids.include?(idkey) then
              url.gsub!(/\{\{\{bug_#{idkey}_id\|.*\}\}\}/, bug_ids[idkey])
            else
              url.gsub!(/\{\{\{bug_#{idkey}_id(?:|[^\d]+)?\|(\d+)\}+/, '\1')
            end
          end
          STDERR.printf("%s", key) if !options[:quiet]
          open(url) do |f|
            contents = f.read
            if contents =~ /No results were found/ then
              bug_latest_stat[key] = 0
            elsif contents =~ /One bug found/ then
              bug_latest_stat[key] = 1
            elsif contents =~ /(\d+)\s+bugs found/ then
              bug_latest_stat[key] = $1.to_i
            else
              raise RuntimeError, "Wrong regexp maybe. unable to find out the number of bugs"
            end
          end
          STDERR.printf("(%d) ", bug_latest_stat[key]) if !options[:quiet]
        end
      end
    end
  end
else
  STDERR.printf("Skipped\n")
end
STDERR.printf("Done\n") if !options[:quiet]

STDERR.printf("Generating the boilerplate...") if !options[:quiet]
page = page.split("\n").map do |l|
  if l =~ /\A\|(.*)=(.*)/ then
    section = $1
    value = $2
  end
  case section
  when "prevoffset"
    l.gsub!(/#{value}/, "#{dw} weeks")
  when "nextoffset"
    l.gsub!(/#{value}/, '')
  when "agenda"
    if page.split("\n").grep(/\Aprevoffset=/).empty? && dw > 1 then
      l = "|prevoffset=#{dw} weeks\n#{l}"
    end

    next if doc.nil?
    unless doc.empty? then
      l << "\n"
      l << doc
    end
    doc = nil
  when /bug_.*_id/
    # nothing to do
  when /bug_last_(.*)/
    key = $1
    if bug_stat.include?("bug_#{key}") then
      l = sprintf("|bug_last_%s=%s\n|bug_%s=%s", key, bug_stat["bug_#{key}"], key, bug_stat["bug_#{key}"].length > 0 ? bug_latest_stat[key] : '')
    else
      bug_stat["bug_last_#{key}"] = value
      l = nil
    end
  when /bug_((?!last).*)/
    key = $1
    if bug_stat.include?("bug_last_#{key}") then
      l = sprintf("|bug_last_%s=%s\n|bug_%s=%s", key, value, key, value.length > 0 ? bug_latest_stat[key] : '')
    else
      bug_stat["bug_#{key}"] = value
      l = nil
    end
  when "minutes", "log"
    l.gsub!(/#{value}/, '')
  end

  l
end.flatten.compact
STDERR.printf("Done\n") if !options[:quiet]

page.each {|l| printf("%s\n", l)}