You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whimsical.apache.org by se...@apache.org on 2018/07/05 19:15:08 UTC

[whimsy] branch master updated: Add code to email the vote tally

This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new dbcc19f  Add code to email the vote tally
dbcc19f is described below

commit dbcc19fb46ee456c3e29f6a40508f6691cd7bd54
Author: Sebb <se...@apache.org>
AuthorDate: Thu Jul 5 20:15:06 2018 +0100

    Add code to email the vote tally
---
 www/project/icla/views/actions/update.json.rb | 61 +++++++++++++++++++++++++--
 1 file changed, 58 insertions(+), 3 deletions(-)

diff --git a/www/project/icla/views/actions/update.json.rb b/www/project/icla/views/actions/update.json.rb
index 234c40b..5ac2784 100644
--- a/www/project/icla/views/actions/update.json.rb
+++ b/www/project/icla/views/actions/update.json.rb
@@ -18,6 +18,49 @@ HAS_COMMENT=%w{submitComment startVoting invite} # do we update the comments arr
 VALID_PHASES=%w{discuss vote cancelled tallied invite}
 VALID_VOTES=%w{+1 +0 -0 -1}
 
+# Tally the votes and send them
+def sendTally(data, contents)
+  member = data['member'] # currently logged in
+  user_email = "#{member}@apache.org"
+  pmc = ASF::Committee.find(contents['project'])
+  pmc_email = "#{pmc.mail_list}@apache.org"
+  subject = "[RESULT]" + contents['subject']
+  last_votes = Hash.new  
+  votes = contents['votes']
+  votes.each { |v|
+    last_votes[v['member']] = [v['vote'], v['timestamp']]
+  }
+  counts=Hash.new(0)
+  last_votes.each { |k,v|
+    counts[v[0]] += 1
+  }
+  started = Time.parse votes[0]['timestamp']
+  elapsed=(Time.now - started) / (60*60)
+  body_text = <<~EOD
+    Here is the tally of the latest votes from each voter:
+
+    #{last_votes.map{ |k,v| "Member: #{k.ljust(20)} Vote: #{v[0]} Date: #{v[1]}"}.join("\n")}
+
+    Vote  Count
+    #{counts.sort_by{|k,v| VALID_VOTES.index(k) || 99}.map{|k,v| "  #{k}  #{v.to_s.rjust(5)}"}.join("\n")}
+
+    Vote started: #{started.to_s[0..9]} Hours elapsed: #{elapsed.to_i}
+  EOD
+  _body_text body_text # debug
+
+  # create the email to the p(pmc)
+  mail = Mail.new do
+    to pmc_email
+    from user_email
+    cc user_email
+    subject subject
+    text_part do
+      body body_text
+    end
+  end
+  mail.deliver
+end
+
 def update(data)
   # setup and validation
   token = data['token']
@@ -110,6 +153,10 @@ def update(data)
     end
   end
 
+  if action == 'tallyVote'
+    sendTally(data, contents)
+  end
+
   # return the data
   _rewrite rewrite # debug
   contents
@@ -136,9 +183,11 @@ def main(params) # called by CLI which passes params
 end
 
 if __FILE__ == $0 # Allow independent testing
+  require 'whimsy/asf'
+  require 'mail'
   $ret = {}
   # method_missing caused some errors to be overlooked
-  %w{backtrace error contents rewrite}.each do |n|
+  %w{backtrace body_text error contents rewrite}.each do |n|
     define_method("_#{n}") do |a|
       $ret[n] = a
     end
@@ -146,8 +195,14 @@ if __FILE__ == $0 # Allow independent testing
   data = Hash[*ARGV] # cannot combine this with next line as hash doesn't yet exist
   data.each{|k,v| data[k] = v.split(',') if v =~ /,/} # fix up lists
   puts data.inspect
-  main(data)
-  puts JSON.pretty_generate($ret) # output the return data
+  if data['action'] == 'sendTally'
+    contents = JSON.parse(File.read("/srv/icla/#{data['token']}.json"))
+    sendTally(data, contents)
+    puts $ret['body_text']
+  else
+    main(data)
+    puts JSON.pretty_generate($ret) # output the return data
+  end
 else
   embed # Sinatra sets params
 end
\ No newline at end of file