2014-05-16 15:49:16 +00:00
#!/usr/bin/perl
use strict;
use Config::Simple;
use DBI;
use File::Basename;
use POSIX qw(strftime);
2015-05-14 04:13:12 +00:00
use SVN::Core;
use SVN::Repos;
use SVN::Fs;
2015-08-13 02:20:26 +00:00
use Net::LDAP;
use Net::LDAP qw(LDAP_SUCCESS);
use URI;
use Mail::Sendmail;
2014-05-16 15:49:16 +00:00
my $CFG_FILE = '@CFGDIR@/codepot.ini';
2015-05-14 04:13:12 +00:00
my $REPOFS = $ARGV[0];
my $REPOBASE = basename($REPOFS);
2014-05-16 15:49:16 +00:00
my $REV = $ARGV[1];
2015-04-29 14:31:15 +00:00
my $QC = '';
2014-05-16 15:49:16 +00:00
sub get_config
{
2014-06-23 14:57:07 +00:00
my $cfg = new Config::Simple();
2014-05-16 15:49:16 +00:00
2014-06-23 14:57:07 +00:00
if (!$cfg->read ($CFG_FILE))
{
return undef;
}
2014-05-16 15:49:16 +00:00
2014-06-23 14:57:07 +00:00
my $config = {
2015-08-13 02:20:26 +00:00
login_model => $cfg->param ('login_model'),
ldap_server_uri => $cfg->param ('ldap_server_uri'),
ldap_server_protocol_version => $cfg->param ('ldap_server_protocol_version'),
ldap_auth_mode => $cfg->param ('ldap_auth_mode'),
ldap_userid_format => $cfg->param ('ldap_userid_format'),
ldap_password_format => $cfg->param ('ldap_password_format'),
ldap_admin_binddn => $cfg->param ('ldap_admin_binddn'),
ldap_admin_password => $cfg->param ('ldap_admin_password'),
ldap_userid_search_base => $cfg->param ('ldap_userid_search_base'),
ldap_userid_search_filter => $cfg->param ('ldap_userid_search_filter'),
ldap_mail_attribute_name => $cfg->param ('ldap_mail_attribute_name'),
2014-06-23 14:57:07 +00:00
database_hostname => $cfg->param ("database_hostname"),
2015-04-28 05:12:06 +00:00
database_port => $cfg->param ("database_port"),
2014-06-23 14:57:07 +00:00
database_username => $cfg->param ("database_username"),
database_password => $cfg->param ("database_password"),
database_name => $cfg->param ("database_name"),
database_driver => $cfg->param ("database_driver"),
2015-08-13 02:20:26 +00:00
database_prefix => $cfg->param ("database_prefix"),
email_sender => $cfg->param ("email_sender")
2014-06-23 14:57:07 +00:00
};
2014-05-16 15:49:16 +00:00
2014-06-23 14:57:07 +00:00
return $config;
2014-05-16 15:49:16 +00:00
}
sub open_database
{
my ($cfg) = @_;
my $dbtype = $cfg->{database_driver};
my $dbname = $cfg->{database_name};
my $dbhost = $cfg->{database_hostname};
2015-04-28 05:12:06 +00:00
my $dbport = $cfg->{database_port};
if ($dbtype eq 'postgre') { $dbtype = 'Pg'; }
2015-04-29 14:31:15 +00:00
elsif ($dbtype eq 'oci8') { $dbtype = 'Oracle'; }
elsif ($dbtype eq 'mysqli') { $dbtype = 'mysql'; }
2015-04-28 05:12:06 +00:00
2015-04-29 14:31:15 +00:00
my $dbstr;
my $dbuser;
my $dbpass;
if ($dbtype eq 'Oracle')
{
$QC = '"';
$dbstr = "DBI:$dbtype:";
$dbuser = $cfg->{database_username} . '/' . $cfg->{database_password} . '@' . $dbhost;
$dbpass = '';
}
else
{
$dbstr = "DBI:$dbtype:database=$dbname;";
if (length($dbhost) > 0) { $dbstr .= "host=$dbhost;"; }
if (length($dbport) > 0) { $dbstr .= "port=$dbport;"; }
$dbuser = $cfg->{database_username};
$dbpass = $cfg->{database_password};
}
2014-05-16 15:49:16 +00:00
my $dbh = DBI->connect(
2015-04-29 14:31:15 +00:00
$dbstr, $dbuser, $dbpass,
2014-05-16 15:49:16 +00:00
{ RaiseError => 0, PrintError => 0, AutoCommit => 0 }
);
return $dbh;
}
sub close_database
{
my ($dbh) = @_;
$dbh->disconnect ();
}
sub write_commit_log
{
my ($dbh, $prefix, $projectid, $revision, $userid) = @_;
#+------+---------+-----------+---------------------------+---------------------+---------------+-----------------+
#| id | type | projectid | message | createdon | action | userid |
#+------+---------+-----------+---------------------------+---------------------+---------------+-----------------+
#| 895 | code | codepot | svn,codepot,72 | 2011-10-10 14:26:43 | commit | hyunghwan.chung |
my $message = "svn,$projectid,$revision";
2015-04-29 14:31:15 +00:00
# the PHP side is executing ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS.FF.
# do i have to do it here or use the database time (CURRENT_TIMESTAMP) instead?
# make sure that you have the same time between the app server and the data server.
# to minize side-effect of using the time of data server.
#my $createdon = strftime ('%Y-%m-%d %H:%M:%S', localtime());
2014-05-16 15:49:16 +00:00
2014-05-16 16:11:33 +00:00
$dbh->begin_work ();
2015-04-29 14:31:15 +00:00
#my $query = $dbh->prepare ("INSERT INTO ${QC}${prefix}log${QC} (${QC}type${QC},${QC}projectid${QC},${QC}message${QC},${QC}createdon${QC},${QC}action${QC},${QC}userid${QC}) VALUES (?,?,?,?,?,?)");
#if (!$query || !$query->execute ('code', $projectid, $message, $createdon, 'commit', $userid))
my $query = $dbh->prepare ("INSERT INTO ${QC}${prefix}log${QC} (${QC}type${QC},${QC}projectid${QC},${QC}message${QC},${QC}createdon${QC},${QC}action${QC},${QC}userid${QC}) VALUES (?,?,?,CURRENT_TIMESTAMP,?,?)");
if (!$query || !$query->execute ('code', $projectid, $message, 'commit', $userid))
2014-05-16 15:49:16 +00:00
{
2014-05-16 16:11:33 +00:00
my $errstr = $dbh->errstr();
$query->finish ();
2014-05-16 15:49:16 +00:00
$dbh->rollback ();
2014-05-16 16:11:33 +00:00
return (-1, $errstr);
2014-05-16 15:49:16 +00:00
}
2014-05-16 16:11:33 +00:00
$query->finish ();
2014-05-16 15:49:16 +00:00
$dbh->commit ();
2014-05-16 16:11:33 +00:00
return (0, undef);
2014-05-16 15:49:16 +00:00
}
2015-05-14 04:13:12 +00:00
sub get_author
{
my $pool = SVN::Pool->new(undef);
my $svn = eval { SVN::Repos::open ($REPOFS, $pool) };
if (!defined($svn))
{
print (STDERR "Cannot open svn - $REPOFS\n");
return undef;
}
my $fs = $svn->fs ();
if (!defined($fs))
{
print (STDERR "Cannot open fs - $REPOFS\n");
return undef;
}
my $author = $fs->revision_prop ($REV, 'svn:author');
return $author;
}
2015-08-13 02:20:26 +00:00
sub format_string
{
my ($fmt, $userid, $password) = @_;
my $out = $fmt;
$out =~ s/\$\{userid\}/$userid/g;
$out =~ s/\$\{password\}/$password/g;
return $out;
}
sub get_author_email_ldap
{
my ($cfg, $userid) = @_;
my $uri = URI->new ($cfg->{ldap_server_uri});
my $ldap = Net::LDAP->new (
$uri->host,
scheme => $uri->scheme,
port => $uri->port,
version => $cfg->{ldap_server_protocol_version}
);
if (!defined($ldap))
{
print (STDERR 'Cannot create LDAP');
return (-1, undef);
}
my $f_rootdn = format_string ($cfg->{ldap_admin_binddn}, $userid, '');
my $f_rootpw = format_string ($cfg->{ldap_admin_password}, $userid, '');
my $res = $ldap->bind ($f_rootdn, password => $f_rootpw);
if ($res->code != LDAP_SUCCESS)
{
print (STDERR "Cannot bind LDAP as $f_rootdn - " . $res->error());
$ldap->unbind();
return (-1, undef);
}
my $f_basedn = format_string ($cfg->{ldap_userid_format}, $userid, '');
my $f_filter = '(' . $cfg->{ldap_mail_attribute_name} . '=*)';
$res = $ldap->search (base => $f_basedn, scope => 'sub', filter => $f_filter);
if ($res->code != LDAP_SUCCESS)
{
$ldap->unbind();
return (0, undef);
}
my $entry = $res->entry(0); # get the first entry only
if (!defined($entry))
{
$ldap->unbind();
return (0, undef);
}
my $xret = 0;
my $email = '';
my @attrs = $entry->attributes ();
foreach my $attr (@attrs)
{
if ($attr eq $cfg->{ldap_mail_attribute_name})
{
$email = $entry->get_value ($attr);
$xret = 1;
last;
}
}
$ldap->unbind();
return ($xret, $email);
}
sub get_author_email_db
{
my ($cfg, $dbh, $prefix, $userid) = @_;
my $query = $dbh->prepare ("SELECT ${QC}email${QC} FROM ${QC}${prefix}user_account${QC} WHERE ${QC}userid${QC}=?");
if (!$query || !$query->execute ($userid))
{
return (-1, $dbh->errstr());
}
if (my @row = $query->fetchrow_array())
{
return (1, @row[0]);
}
return (0, undef);
}
sub email_message_to_project_members
{
my ($cfg, $dbh, $prefix, $projectid, $subject, $message) = @_;
my $query = $dbh->prepare ("SELECT ${QC}userid${QC} FROM ${QC}${prefix}project_membership${QC} WHERE ${QC}projectid${QC}=?");
if (!$query || !$query->execute ($projectid))
{
return (-1, $dbh->errstr());
}
my @members;
while (my @row = $query->fetchrow_array())
{
push (@members, $row[0]);
}
$query->finish ();
my $recipients = '';
foreach my $member (@members)
{
my $xret;
my $email;
if ($cfg->{login_model} eq 'LdapLoginModel')
{
($xret, $email) = get_author_email_ldap ($cfg, $member)
}
elsif ($cfg->{login_model} eq 'DbLoginModel')
{
($xret, $email) = get_author_email_db ($cfg, $dbh, $prefix, $member);
}
else
{
$xret = -2;
$email = '';
}
if ($xret >= 1 && defined($email) && length($email) > 0)
{
if (length($recipients) > 0) { $recipients .= ', '; }
$recipients .= $email;
}
}
if (length($recipients) <= 0) { return (0, undef); }
my %mail = (
To => $recipients,
From => $cfg->{email_sender},
Subject => $subject,
Message => $message
);
Mail::Sendmail::sendmail (%mail);
return (1, undef);
}
2014-05-16 15:49:16 +00:00
#------------------------------------------------------------
# MAIN
#------------------------------------------------------------
2015-05-14 04:13:12 +00:00
my $AUTHOR = get_author();
if (!defined($AUTHOR))
{
print (STDERR "Cannot get author for $REPOBASE $REV\n");
exit (1);
}
2014-05-16 15:49:16 +00:00
chomp ($AUTHOR);
my $cfg = get_config ();
if (!defined($cfg))
{
print (STDERR "Cannot load codepot configuration file\n");
exit (1);
}
my $dbh = open_database ($cfg);
if (!defined($dbh))
{
2015-04-28 05:12:06 +00:00
printf (STDERR "Cannot open database - %s\n", $DBI::errstr);
2014-05-16 15:49:16 +00:00
exit (1);
}
write_commit_log ($dbh, $cfg->{database_prefix}, $REPOBASE, $REV, $AUTHOR);
2015-08-13 02:20:26 +00:00
my $commit_subject = "Commit $REV by $AUTHOR in $REPOBASE";
my $commit_message = $commit_subject; # TODO: compose a proper URL
email_message_to_project_members ($cfg, $dbh, $cfg->{database_prefix}, $REPOBASE, $commit_subject, $commit_message);
2014-05-16 15:49:16 +00:00
close_database ($dbh);
exit (0);