Capsis SVN post-commit mail

post-commmit

SVNLOOK=/usr/bin/svnlook

REPOS="$1" 
REV="$2" 
LOG=`$SVNLOOK log -r "$REV" "$REPOS"`
AUTHOR=`$SVNLOOK author -r "$REV" "$REPOS"`
DIRS="`$SVNLOOK dirs-changed -r "$REV" "$REPOS"`" 

MAILS="`perl /var/www/redmine/extra/svn/capsis_svn_mail.pl capsis "$DIRS"`" 

for MAIL in $MAILS
do
    /usr/share/subversion/hook-scripts/commit-email.pl \
    "$REPOS" "$REV" -s "Capsis SVN Commit" --from amap-dev@cirad.fr $MAIL
done

capsis_svn_mail.pl

use strict;
use warnings FATAL => 'all', NONFATAL => 'redefine';

use DBI;
use Shell;

# Usage
# capsis_svn_mail.pl project_id list_of_dirs

my %login_db;
my %mail_db;

#my $project_id = shift @ARGV;
my $project_id = 1; # CAPSIS

my @changed_dirs = @ARGV;

sub get_members { 

  my $dbh = shift;
  my $project_id = shift;

  # Get user (role = Manager, Developer or Modeler)
  my $query = "SELECT 
                 users.id, users.login, users.mail
              FROM members, projects, users
              WHERE 
                projects.id=?
                AND projects.id=members.project_id 
                AND users.id=members.user_id 
                AND users.status=1
                AND (members.role_id=3 OR members.role_id=4 OR members.role_id=6);
                ";

   my $sth = $dbh->prepare($query);
   $sth->execute($project_id);
   my @ret = ();

   # Save data
   while (my ($id, $login, $mail) = $sth->fetchrow_array) {

     $login_db{$id} = $login;
     $mail_db{$id} = $mail;
     push(@ret, $id);
   }

   $sth->finish();
   @ret;
}

sub get_modules { 

  my $dbh = shift;
  my $user_id = shift;

  my $query = "SELECT 
                 value
              FROM custom_values
              WHERE 
                customized_type='User'
                AND customized_id=?
                AND custom_field_id=1;";

   my $sth = $dbh->prepare($query);
   $sth->execute($user_id);
   my $ret = $sth->fetchrow_array;
   $sth->finish();
   $ret;
}

sub get_mails {
    my $dbh = connect_database();
    my @ret = get_members($dbh, $project_id);
    my %mail_to_send;

    my ($id, $val);
    foreach $id (@ret) # for each member in project_id
    {
        $val = get_modules($dbh, $id);

        my @splitted_val = split(/ /, $val);

        foreach my $dir (@changed_dirs) # For each modified directory
        {

           my $ignore = 0;

           foreach my $single_val (@splitted_val) # for each value in user profil
           {
              # remove unnecessary characters
              $single_val =~ s/\n//g;
              $single_val =~ s/^\s+//;
              $single_val =~ s/\s+$//;

              # ignore empty string 
              if(!$single_val) { next; }

              # ignore <> string
              if($single_val =~ m/^</) { $ignore = 1; }
              if($single_val =~ m/>$/) { $ignore = 0; }

              if($ignore) { next; }

              # replace * by .*
              $single_val =~ s/\*/.*/;

              # compare path with string (case insensitive)
              if($dir =~ m/($single_val)/i) # if dir name contained in value
              {
                  $mail_to_send{$id} = $mail_db{$id};
              }
           }
        }
    }

    # Print Result
    my @res = values(%mail_to_send);
    @res = keys %{{ map { $_ => 1 } @res }};

    print "@res\n";

    $dbh->disconnect();

}

sub connect_database {
    return DBI->connect( "DBI:mysql:database=redmine;host=localhost", "******", "****");
}

get_mails();