]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons.rb
694a87b9649af1b1153d5b700be9618043832e2c
[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
7 module NosOignons
8   # The following class methods are all meant to be called as command-line scripts
9   class << self
10     def list_emails!
11       NosOignons::Member.all.each do |member|
12         if member.up_to_date?
13           puts member.email
14         end
15       end
16     end
17
18     MEMBER_MAILING_LIST = 'ag'
19     def update_ag_subscribers!
20       current_emails = NosOignons::Mailman.list_members(MEMBER_MAILING_LIST)
21       uptodate_emails = NosOignons::Member.all.select(&:up_to_date?).collect(&:email)
22
23       NosOignons::Mailman.add_members(MEMBER_MAILING_LIST, uptodate_emails - current_emails)
24       NosOignons::Mailman.remove_members(MEMBER_MAILING_LIST, current_emails - uptodate_emails)
25     end
26
27     def pre_commit_hook!
28       if system('git rev-parse --quiet --verify HEAD >/dev/null')
29         against = 'HEAD'
30       else
31         # Initial commit: diff against an empty tree object
32         against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
33       end
34
35       IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io|
36         NosOignons::Git.handle_modified_files(io) do |file|
37           next unless file.start_with?("#{NosOignons::MEMBERS_ROOT}/")
38           begin
39             # Use empty ref to get the index
40             NosOignons::Member.read_from_git('', file)
41           rescue ArgumentError
42             $stderr.puts "Désolé : #{file} n'a pas le bon format !"
43             exit 1
44           end
45         end
46       end
47     end
48
49     def pre_receive_hook!(stdin)
50       stdin.readlines.each do |ref_line|
51         old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3)
52         IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io|
53           NosOignons::Git.handle_modified_files(io) do |file|
54             next unless file.start_with?("#{NosOignons::MEMBERS_ROOT}/")
55             begin
56               NosOignons::Member.read_from_git(new_value, file)
57             rescue ArgumentError
58               $stderr.puts "Désolé : #{file} n'a pas le bon format !"
59               exit 1
60             end
61           end
62         end
63       end
64     end
65   end
66 end