]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons.rb
4ecd3c4b7e23600c50d8b3c38c5adae4e48b5d56
[gestion-adh.git] / lib / nos_oignons.rb
1 #-*- coding: utf-8 -*-
2
3 require 'nos_oignons/git'
4 require 'nos_oignons/mailman'
5 require 'nos_oignons/member'
6 require 'nos_oignons/reminder'
7 require 'nos_oignons/reminder_db'
8
9 module NosOignons
10   BOARD_EMAIL = 'ca@nos-oignons.net'
11   MEMBER_MAILING_LIST = 'ag'
12
13   # The following class methods are all meant to be called as command-line scripts
14   class << self
15     def list_emails!
16       NosOignons::Member.all.each do |member|
17         if member.up_to_date?
18           puts member.email
19         end
20       end
21     end
22
23     def update_ag_subscribers!
24       list = NosOignons::MEMBER_MAILING_LIST
25
26       current_emails = NosOignons::Mailman.list_members(list)
27       uptodate_emails = NosOignons::Member.all.select(&:up_to_date?).collect(&:email)
28
29       emails_to_add = uptodate_emails - current_emails
30       NosOignons::Mailman.add_members(list, emails_to_add) unless emails_to_add.empty?
31       emails_to_remove = current_emails - uptodate_emails
32       NosOignons::Mailman.remove_members(list, emails_to_remove) unless emails_to_remove.empty?
33     end
34
35     def send_membership_reminders!
36       today = Time.now.to_date
37       NosOignons::Member.all.select(&:up_to_date?).each do |member|
38         anniversary = Time.new(today.year, member.joined_on.month,
39                                member.joined_on.day).to_date
40         next if member.membership_fee_paid_on >= anniversary
41         next if member.reminded_on && member.reminded_on >= today
42         NosOignons::Reminder.all.sort_by(&:days).reverse.each do |reminder|
43           next if anniversary < today + reminder.days
44
45           member.remind(reminder)
46           break
47         end
48       end
49     end
50
51     def pre_commit_hook!
52       if system('git rev-parse --quiet --verify HEAD >/dev/null')
53         against = 'HEAD'
54       else
55         # Initial commit: diff against an empty tree object
56         against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
57       end
58
59       IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io|
60         NosOignons::Git.handle_modified_files(io) do |file|
61           next unless file.start_with?("#{NosOignons::MEMBERS_DB_DIR}/")
62           begin
63             # Use empty ref to get the index
64             NosOignons::Member.read_from_git('', file)
65           rescue ArgumentError, Psych::SyntaxError
66             $stderr.puts "Désolé : #{file} n'a pas le bon format !"
67             exit 1
68           end
69         end
70       end
71     end
72
73     def pre_receive_hook!(stdin)
74       stdin.readlines.each do |ref_line|
75         old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3)
76         IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io|
77           NosOignons::Git.handle_modified_files(io) do |file|
78             next unless file.start_with?("#{NosOignons::MEMBERS_DB_DIR}/")
79             begin
80               NosOignons::Member.read_from_git(new_value, file)
81             rescue ArgumentError, Psych::SyntaxError
82               $stderr.puts "Désolé : #{file} n'a pas le bon format !"
83               exit 1
84             end
85           end
86         end
87       end
88     end
89   end
90 end