-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathroutes.rb
executable file
·658 lines (553 loc) · 18.5 KB
/
routes.rb
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#
# Server side Sinatra routes
#
require 'whimsy/asf/status'
# redirect root to latest agenda
get '/' do
agenda = dir('board_agenda_*.txt').max
pass unless agenda
redirect "#{request.path}#{agenda[/\d+_\d+_\d+/].gsub('_', '-')}/"
end
# alias for latest agenda
get '/latest/' do
agenda = dir('board_agenda_*.txt').max
pass unless agenda
call env.merge(
'PATH_INFO' => "/#{agenda[/\d+_\d+_\d+/].gsub('_', '-')}/"
)
end
# alias for latest agenda in JSON format
get '/latest.json' do
agenda = dir('board_agenda_*.txt').max
pass unless agenda
call env.merge!(
'PATH_INFO' => "/#{agenda[/\d+_\d+_\d+/].gsub('_', '-')}.json"
)
end
get '/calendar.json' do
_json do
{
nextMeeting: ASF::Board.nextMeeting.iso8601,
calendar: ASF::Board.calendar.map(&:iso8601),
agendas: dir('board_agenda_*.txt').sort,
drafts: dir('board_minutes_*.txt').sort
}
end
end
# icon
get '/whimsy.svg' do
send_file File.expand_path('../../../whimsy.svg', __FILE__),
type: 'image/svg+xml'
end
# Progress Web App Manifest
get '/manifest.json' do
@svgmtime = File.mtime(File.expand_path('../../../whimsy.svg', __FILE__)).to_i
@pngmtime = File.mtime(File.expand_path('../public/whimsy.png', __FILE__)).to_i
# capture all the variable content
hash = {
source: File.read("#{settings.views}/manifest.json.erb"),
svgmtime: @svgmtime
}
# detect if there were any modifications
etag Digest::MD5.hexdigest(JSON.dump(hash))
content_type 'application/json'
erb :"manifest.json"
end
# redirect shepherd to latest agenda
get '/shepherd' do
user = ASF::Person.find(env.user).public_name.split(' ').first
agenda = dir('board_agenda_*.txt').max
pass unless agenda
redirect File.dirname(request.path) +
"/#{agenda[/\d+_\d+_\d+/].gsub('_', '-')}/shepherd/#{user}"
end
# redirect missing to missing page for the latest agenda
get '/missing' do
agenda = dir('board_agenda_*.txt').max
pass unless agenda # this will result in a 404
# Support for sending out reminders before the agenda is created.
# Useful in cases where the agenda creation is delayed due to
# a board election.
if agenda < Date.today.strftime('board_agenda_%Y_%m_%d.txt')
# update in memory cache with a dummy agenda. The only relevant
# part of the agenda that matters for this operation is the list
# of pmcs (@pmcs).
template = File.join(ASF::SVN['foundation_board'], 'templates', 'board_agenda.erb')
@meeting = ASF::Board.nextMeeting
agenda = @meeting.strftime('board_agenda_%Y_%m_%d.txt')
@directors = ['TBD']
@minutes = []
@owner = ASF::Board::ShepherdStream.new
@pmcs = ASF::Board.reporting(@meeting)
contents = Erubis::Eruby.new(IO.read(template)).result(binding)
Agenda.update_cache(agenda, nil, contents, true)
end
response.headers['Location'] =
"#{agenda[/\d+_\d+_\d+/].gsub('_', '-')}/missing"
status 302
end
get '/session.json' do
_json do
{session: Session.user(env.user)}
end
end
# for debugging purposes
get '/env' do
content_type 'text/plain'
asset = {
path: Wunderbar::Asset.path,
root: Wunderbar::Asset.root,
virtual: Wunderbar::Asset.virtual,
scripts: Wunderbar::Asset.scripts.map do |script|
{path: script.path}
end
}
JSON.pretty_generate(env: env, ENV: ENV.to_h, asset: asset)
end
# enable debugging of the agenda cache
get '/cache.json' do
_json Agenda.cache
end
# agenda followup
get %r{/(\d\d\d\d-\d\d-\d\d)/followup\.json} do |date|
pass unless Dir.exist? '/srv/mail/board'
agenda = "board_agenda_#{date.gsub('-', '_')}.txt"
pass unless Agenda.parse agenda, :quick
# select agenda items that have comments
parsed = Agenda[agenda][:parsed]
followup = parsed.reject {|item| item['comments'].to_s.empty?}.
map {|item| [item['title'], {comments: item['comments'],
shepherd: item['shepherd'],
mail_list: item['mail_list'],
count: 0}]
}.to_h
# count number of feedback emails found in the board archive
start = Time.parse(date)
months = Dir['/srv/mail/board/*'].sort[-2..-1]
Dir[*months.map {|month| "#{month}/*"}].each do |file|
next unless File.mtime(file) > start
raw = File.read(file).force_encoding(Encoding::BINARY)
next unless raw =~ /Subject: .*Board feedback on #{date} (.*) report/
followup[$1][:count] += 1 if followup[$1]
end
# return results
_json followup
end
# pending items
get %r{/(\d\d\d\d-\d\d-\d\d)/pending\.json} do
pending = Pending.get(env.user)
_json pending
end
# agenda digest information
get %r{/(\d\d\d\d-\d\d-\d\d)/digest\.json} do |date|
agenda = "board_agenda_#{date.gsub('-', '_')}.txt"
_json(
{
agenda: {
file: agenda,
digest: Agenda[agenda][:digest],
etag: Agenda.uptodate(agenda) ? Agenda[agenda][:etag] : nil
},
reporter: Reporter.digest
}
)
end
# feedback
get %r{/(\d\d\d\d-\d\d-\d\d)/feedback.json} do |date|
@agenda = "board_agenda_#{date.gsub('-', '_')}.txt"
@dryrun = true
_json :'actions/feedback'
end
post %r{/(\d\d\d\d-\d\d-\d\d)/feedback.json} do |date|
unavailable = Status.updates_disallowed_reason # are updates disallowed?
return [503, unavailable] if unavailable
@agenda = "board_agenda_#{date.gsub('-', '_')}.txt"
@dryrun = false
_json :'actions/feedback'
end
def server
if env['REMOTE_USER']
userid = env['REMOTE_USER']
elsif ENV['RACK_ENV'] == 'test'
userid = env['HTTP_REMOTE_USER'] || 'test'
elsif env.respond_to? :user
userid = env.user
else
require 'etc'
userid = Etc.getlogin
end
pending = Pending.get(userid)
# determine who is present
@present = []
@present_mtime = nil
file = File.join(AGENDA_WORK, 'sessions', 'present.yml')
if File.exist?(file) and File.mtime(file) != @present_mtime
@present_mtime = File.mtime(file)
@present = YAML.load_file(file).
reject {|name| name =~ /^board_agenda_[_\d]+$/}
end
if env['SERVER_NAME'] == 'localhost'
websocket = 'ws://localhost:34234/'
else
websocket = (env['rack.url_scheme'].sub('http', 'ws')) + '://' +
env['SERVER_NAME'] + env['SCRIPT_NAME'] + '/websocket/'
end
@server = {
userid: userid,
agendas: dir('board_agenda_*.txt').sort,
drafts: dir('board_minutes_*.txt').sort,
pending: pending,
username: pending['username'],
firstname: pending['firstname'],
initials: pending['initials'],
online: @present,
session: Session.user(userid),
role: pending['role'],
directors: Hash[ASF::Service['board'].members.map {|person|
initials = begin
YAML.load_file(File.join(AGENDA_WORK, "#{person.id}.yml"))['initials']
rescue
person.public_name.gsub(/[^A-Z]/, '').downcase
end
[initials, person.public_name.split(' ').first]
}],
websocket: websocket
}
end
get '/server.json' do
_json server
end
# all agenda pages
get %r{/(\d\d\d\d-\d\d-\d\d)/(.*)} do |date, path|
agenda = "board_agenda_#{date.gsub('-', '_')}.txt"
pass unless Agenda.parse agenda, :quick
@base = "#{env['SCRIPT_NAME']}/#{date}/"
@server = server
@page = {
path: path,
query: params['q'],
agenda: agenda,
parsed: Agenda[agenda][:parsed],
digest: Agenda[agenda][:digest],
etag: Agenda.uptodate(agenda) ? Agenda[agenda][:etag] : nil
}
minutes = AGENDA_WORK + '/' +
agenda.sub('agenda', 'minutes').sub('.txt', '.yml')
@page[:minutes] = YAML.safe_load(File.read(minutes), permitted_classes: [Symbol]) if File.exist? minutes
@cssmtime = File.mtime('public/stylesheets/app.css').to_i
@manmtime = File.mtime("#{settings.views}/manifest.json.erb").to_i
@appmtime = Wunderbar::Asset.convert("#{settings.views}/app.js.rb").mtime.to_i
@server[:swmtime] = File.mtime("#{settings.views}/sw.js.rb").to_i
if path == 'bootstrap.html'
unless env.password
@server[:userid] = nil
@server[:role] = nil
end
@page[:parsed] = [
{title: 'Roll Call', timestamp: @page[:parsed].first['timestamp']}
]
@page[:digest] = nil
@page[:etag] = nil
@server[:session] = nil
# capture all the variable content
hash = {
source: File.read("#{settings.views}/bootstrap.html.erb"),
cssmtime: @cssmtime,
appmtime: @appmtime,
manmtime: @manmtime,
scripts: Wunderbar::Asset.scripts.
map {|script| [script.path, script.mtime.to_i]}.sort,
stylesheets: Wunderbar::Asset.stylesheets.
map {|stylesheet| [stylesheet.path, stylesheet.mtime.to_i]}.sort,
server: @server,
page: @page
}
# detect if there were any modifications
etag Digest::MD5.hexdigest(JSON.dump(hash))
erb :"bootstrap.html"
else
_html :main
end
end
# append slash to agenda page if not present
get %r{/(\d\d\d\d-\d\d-\d\d)} do |date|
redirect to("/#{date}/")
end
# post item support
get '/json/post-data' do
_json :"actions/post-data"
end
# feedback responses
get '/json/responses' do
_json :"actions/responses"
end
# posted reports
get '/json/posted-reports' do
_json :"actions/posted-reports"
end
post '/json/posted-reports' do
unavailable = Status.updates_disallowed_reason # are updates disallowed?
return [503, unavailable] if unavailable
_json :"actions/posted-reports"
end
# podling name searches
get '/json/podlingnamesearch' do
_json ASF::Podling.namesearch
end
# podling name searches
get '/json/reporter' do
_json Reporter.drafts(env)
end
# posted actions
post '/json/:file' do
unavailable = Status.updates_disallowed_reason # are updates disallowed?
return [503, unavailable] if unavailable
_json :"actions/#{params[:file]}"
end
# Raw minutes
get %r{/(\d\d\d\d-\d\d-\d\d).ya?ml} do |file|
minutes = AGENDA_WORK + '/' + "board_minutes_#{file.gsub('-', '_')}.yml"
pass unless File.exist? minutes
_text File.read minutes
end
# updates to agenda data
get %r{/(\d\d\d\d-\d\d-\d\d).json} do |date|
file = "board_agenda_#{date.gsub('-', '_')}.txt"
pass unless Agenda.parse file, :full
begin
_json do
last_modified Agenda[file][:mtime]
minutes_file = AGENDA_WORK + '/' + file.sub('_agenda_', '_minutes_').
sub('.txt', '.yml')
# merge in minutes, if available
if File.exist? minutes_file
minutes = YAML.load_file(minutes_file)
Agenda[file][:parsed].each do |item|
item[:minutes] = minutes[item['title']] if minutes[item['title']]
end
end
agenda = Agenda[file][:parsed]
# filter list for non-PMC chairs and non-officers
user = env.respond_to?(:user) && ASF::Person.find(env.user)
unless !user or user.asf_chair_or_member?
status 206 # Partial Content
committees = user.committees.map(&:display_name)
agenda = agenda.select {|item| committees.include? item['title']}
end
# Add banner (or nil) to the first entry
# must always update the entry as they may be cached
agenda.first['banner'] = Status.banner
agenda
end
ensure
Agenda[file][:etag] = headers['ETag']
end
end
# draft committers report
get %r{/text/summary/(\d\d\d\d-\d\d-\d\d)} do |date|
@date = date.gsub('-', '_')
_text :committers_report
end
# draft minutes
get '/text/minutes/:file' do |file|
file = "board_minutes_#{file.gsub('-', '_')}.txt"
if dir('board_minutes_*.txt').include? file
path = File.join(FOUNDATION_BOARD, file)
elsif not Dir[File.join(ASF::SVN['minutes'], file[/\d+/], file)].empty?
path = File.join(ASF::SVN['minutes'], file[/\d+/], file)
else
pass
end
_text do
last_modified File.mtime(path)
_ File.read(path)
end
end
# jira project info
get '/json/jira' do
uri = URI.parse('https://issues.apache.org/jira/rest/api/2/project')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
_json { JSON.parse(response.body).map {|project| project['key']} }
end
# get list of committers (for use in roll-call)
get '/json/committers' do
_json do
members = ASF.search_one(ASF::Group.base, 'cn=member', 'memberUid').first
members = Hash[members.map {|name| [name, true]}]
ASF.search_one(ASF::Person.base, 'uid=*', ['uid', 'cn']).
map {|person| {id: person['uid'].first,
member: members[person['uid'].first] || false,
name: person['cn'].first.force_encoding('utf-8')}}.
sort_by {|person| person[:name].downcase.unicode_normalize(:nfd)}
end
end
# Secretary post-meeting todos
get '/json/secretary-todos/:date' do
unavailable = Status.updates_disallowed_reason # are updates disallowed?
return [503, unavailable] if unavailable
_json :'actions/todos'
end
post '/json/secretary-todos/:date' do
unavailable = Status.updates_disallowed_reason # are updates disallowed?
return [503, unavailable] if unavailable
_json :'actions/todos'
end
# potential actions
get '/json/potential-actions' do
_json :'actions/potential-actions'
end
get %r{/json/(reminder[12]|non-responsive)} do |reminder|
@reminder = reminder
_json :'actions/reminder-text'
end
# chat log
get %r{/json/chat/(\d\d\d\d_\d\d_\d\d)} do |date|
log = "#{AGENDA_WORK}/board_agenda_#{date}-chat.yml"
if File.exist? log
_json YAML.safe_load(File.read(log), permitted_classes: [Symbol])
else
_json []
end
end
# historical comments, filtered to only include the list of projects which
# the user is a member of the PMC for non-ASF-members and non-officers.
get '/json/historical-comments' do
user = env.respond_to?(:user) && ASF::Person.find(env.user)
comments = HistoricalComments.comments
unless !user or user.asf_chair_or_member?
status 206 # Partial Content
committees = user.committees.map(&:display_name)
comments = comments.select do |project, _list|
committees.include? project
end
end
_json comments.to_h
end
# draft minutes
get '/text/draft/:file' do |file|
agenda = "board_agenda_#{file.gsub('-', '_')}.txt"
minutes = AGENDA_WORK + '/' +
agenda.sub('_agenda_', '_minutes_').sub('.txt', '.yml')
_text do
Dir.chdir(FOUNDATION_BOARD) do
if Dir['board_agenda_*.txt'].include?(agenda)
_ Minutes.draft(agenda, minutes)
else
halt 404
end
end
end
end
# draft new agenda
get '/new' do
# extract time and date for next meeting, month of previous meeting
@meeting = ASF::Board.nextMeeting
localtime = ASF::Board::TIMEZONE.utc_to_local(@meeting)
@tzlink = ASF::Board.tzlink(localtime)
zone = ASF::Board::TIMEZONE.name
@start_time = localtime.strftime('%H:%M') + ' ' + zone
duration = 1.hours
@adjournment = (localtime + duration).strftime('%H:%M') + ' ' + zone
@prev_month = @meeting.to_date.prev_month.strftime('%B')
# retrieve latest committee info
# TODO: this is the workspace copy -- should it be using the copy from SVN instead?
cinfo = File.join(ASF::SVN['board'], 'committee-info.txt')
info = ASF::SVN.getInfo(cinfo, env.user, env.password)
contents = ASF::SVN.svn('cat', cinfo, {env: env})
ASF::Committee.load_committee_info(contents, info)
# extract committees expected to report 'next month'
next_month = contents[/Next month.*?\n\n/m].chomp
@next_month = next_month[/(.*#.*\n)+/] || ''
# get potential actions
begin
actions = JSON.parse(Wunderbar::JsonBuilder.new({}).instance_eval(
File.read("#{settings.views}/actions/potential-actions.json.rb"),
).target!, symbolize_names: true)[:actions]
rescue IOError => e
Wunderbar.warn "#{e}, could not access previous actions, continuing"
actions = nil
end
# Get directors, list of pmcs due to report, and shepherds
@directors = ASF::Board.directors
@pmcs = ASF::Board.reporting(@meeting)
@owner = ASF::Board::ShepherdStream.new(actions)
# Get list of unpublished and unapproved minutes (used by the agenda template)
latest = Dir["#{AGENDA_WORK}/board_minutes*.yml"].max
if latest
draft = YAML.load_file(latest)
else
draft = {} # allow for missing yml file
end
@minutes = dir('board_agenda_*.txt').
map {|file| Date.parse(file[/\d[_\d]+/].gsub('_', '-'))}.
reject {|date| date >= @meeting.to_date}.
reject {|date| draft[date.strftime('%B %d, %Y')] == 'approved'}.
sort
template = File.join(ASF::SVN['foundation_board'], 'templates', 'board_agenda.erb')
@disabled = dir('board_agenda_*.txt').
include? @meeting.strftime('board_agenda_%Y_%m_%d.txt')
begin
@agenda = Erubis::Eruby.new(IO.read(template)).result(binding)
rescue => error
status 500
STDERR.puts error
return "error in #{template} in: #{error}"
end
@cssmtime = File.mtime('public/stylesheets/app.css').to_i
_html :new
end
# post a new agenda
post %r{/(\d\d\d\d-\d\d-\d\d)/} do |date|
unavailable = Status.updates_disallowed_reason # are updates disallowed?
return [503, unavailable] if unavailable
boardurl = ASF::SVN.svnurl('foundation_board')
agenda = "board_agenda_#{date.gsub('-', '_')}.txt"
contents = params[:agenda].gsub("\r\n", "\n")
Dir.mktmpdir do |dir|
ASF::SVN.svn!('checkout', [boardurl, dir], {depth: 'empty', env: env})
agendapath = File.join(dir, agenda)
File.write agendapath, contents
ASF::SVN.svn!('add', agendapath)
currentpath = File.join(dir, 'current.txt')
ASF::SVN.svn!('update', currentpath, {env: env})
if File.symlink? currentpath # Does the symlink exist?
File.unlink currentpath
File.symlink agenda, currentpath
else
Wunderbar.warn 'current.txt link does not exist, creating it'
File.symlink agenda, currentpath
ASF::SVN.svn!('add', currentpath)
end
ASF::SVN.svn!('commit', [agendapath, currentpath], {msg: "Post #{date} agenda", env: env})
Agenda.update_cache agenda, agendapath, contents, false
end
auto_remind(date, agenda)
redirect to("/#{date}/")
end
get %r{/testautoremind/(\d\d\d\d-\d\d-\d\d)/} do |date|
agenda = "board_agenda_#{date.gsub('-', '_')}.txt"
@dryrun = !Status.testnode? # For debug only!
_json auto_remind(date, agenda)
end
# Code to send initial reminders
def auto_remind(date, agenda)
# draft reminder text
@reminder = 'reminder1' # reminder template
@tzlink = ASF::Board.tzlink(ASF::Board::TIMEZONE.utc_to_local(ASF::Board.nextMeeting))
reminder = eval(File.read('views/actions/reminder-text.json.rb'))
# extract data
@subject = reminder[:subject]
@message = reminder[:body]
# send reminders and summary
@summary = 'reminder-summary' # template name
@sendsummary = true
@agenda = agenda
@meeting = date
boardchair = ASF::Committee.officers.select{|h| h.name == 'boardchair'}.first.chairs.first[:name]
@from = "\"#{boardchair}\" <[email protected]>"
eval(File.read('views/actions/send-reminders.json.rb'))
end