2014-05-16 15:49:16 +00:00
#!/usr/bin/perl
use strict;
use Config::Simple;
use DBI;
use File::Basename;
2016-12-17 11:05:47 +00:00
use POSIX;
2014-05-16 15:49:16 +00:00
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;
2021-09-13 10:00:13 +00:00
use LWP::UserAgent;
2015-08-13 02:20:26 +00:00
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
2021-09-13 10:00:13 +00:00
if (!$cfg->read($CFG_FILE))
2014-06-23 14:57:07 +00:00
{
return undef;
}
2014-05-16 15:49:16 +00:00
2014-06-23 14:57:07 +00:00
my $config = {
2021-09-13 10:00:13 +00:00
login_model => $cfg->param('login_model'),
2015-08-13 02:20:26 +00:00
2021-09-13 10:00:13 +00:00
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'),
database_hostname => $cfg->param("database_hostname"),
database_port => $cfg->param("database_port"),
database_username => $cfg->param("database_username"),
database_password => $cfg->param("database_password"),
database_name => $cfg->param("database_name"),
database_driver => $cfg->param("database_driver"),
database_prefix => $cfg->param("database_prefix"),
database_store_gmt => $cfg->param("database_store_gmt"),
email_sender => $cfg->param("email_sender"),
commit_notification => $cfg->param("commit_notification"),
commit_notification_url => $cfg->param("commit_notification_url")
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'; }
2021-09-05 14:47:49 +00:00
elsif ($dbtype eq 'sqlite') { $dbtype = 'SQLite'; }
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 = '';
}
2021-09-05 14:47:49 +00:00
elsif ($dbtype eq 'SQLite')
{
$dbstr = "DBI:$dbtype:database=$dbhost;";
$dbuser = $cfg->{database_username};
$dbpass = $cfg->{database_password};
}
2015-04-29 14:31:15 +00:00
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 ();
}
2016-01-22 18:12:06 +00:00
sub find_issue_reference_in_commit_message
{
my ($dbh, $prefix, $projectid, $revision, $commit_message) = @_;
# find [[#IXXXX]]
my @issue_ids = ($commit_message =~ /\[\[#I(\d+)\]\]/g);
2016-01-23 06:01:04 +00:00
# find #XXXX
my @issue_ids2 = ($commit_message =~ /(^|[^#])#(\d+)(\D|$)/g);
2016-01-22 18:12:06 +00:00
# find unique issue ids in the findings.
my %tmp;
2016-01-23 06:01:04 +00:00
@tmp{@issue_ids} = 1;
for (my $i = 0; $i < scalar(@issue_ids2); $i += 3)
{
my $id = @issue_ids2[$i + 1];
@tmp{$id} = 1;
}
my @unique_issue_ids = keys(%tmp);
2016-01-22 18:12:06 +00:00
$dbh->begin_work ();
2021-09-13 10:00:13 +00:00
my $query = $dbh->prepare("DELETE FROM ${QC}${prefix}issue_coderev${QC} WHERE ${QC}codeproid${QC}=? AND ${QC}coderev${QC}=?");
if (!$query || !$query->execute($projectid, $revision))
2016-01-22 18:12:06 +00:00
{
my $errstr = $dbh->errstr();
if ($query) { $query->finish (); }
$dbh->rollback ();
return (-1, $errstr);
}
$query->finish ();
for my $issue_id(@unique_issue_ids)
{
2021-09-13 10:00:13 +00:00
my $query = $dbh->prepare("INSERT INTO ${QC}${prefix}issue_coderev${QC} (${QC}projectid${QC},${QC}issueid${QC},${QC}codeproid${QC},${QC}coderev${QC}) VALUES (?,?,?,?)");
2016-01-22 18:12:06 +00:00
if ($query)
{
# ignore errors
$query->execute ($projectid, $issue_id, $projectid, $revision);
$query->finish ();
}
}
$dbh->commit ();
return (0, undef);
}
2014-05-16 15:49:16 +00:00
sub write_commit_log
{
2016-12-17 11:05:47 +00:00
my ($dbh, $prefix, $projectid, $revision, $userid, $store_gmt) = @_;
2014-05-16 15:49:16 +00:00
#+------+---------+-----------+---------------------------+---------------------+---------------+-----------------+
#| 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";
2016-12-17 11:05:47 +00:00
my $timestamp;
2015-04-29 14:31:15 +00:00
2016-12-17 11:05:47 +00:00
if (($store_gmt =~ /^\d+?$/ && int($store_gmt) != 0) || (lc($store_gmt) eq 'yes'))
{
2021-09-13 10:00:13 +00:00
$timestamp = POSIX::strftime('%Y-%m-%d %H:%M:%S', gmtime());
2016-12-17 11:05:47 +00:00
}
else
{
2021-09-13 10:00:13 +00:00
$timestamp = POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime());
2016-12-17 11:05:47 +00:00
}
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.
2016-12-17 11:05:47 +00:00
#my $createdon = POSIX::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 (?,?,?,?,?,?)");
2021-09-15 12:08:13 +00:00
#if (!$query || !$query->execute('code', $projectid, $message, $createdon, 'commit', $userid))
2015-04-29 14:31:15 +00:00
2016-12-17 11:05:47 +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 (?,?,?,?,?,?)");
2021-09-15 12:08:13 +00:00
if (!$query || !$query->execute('code', $projectid, $message, $timestamp, 'commit', $userid))
2014-05-16 15:49:16 +00:00
{
2014-05-16 16:11:33 +00:00
my $errstr = $dbh->errstr();
2016-01-22 18:12:06 +00:00
if ($query) { $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;
}
2021-09-13 10:00:13 +00:00
my $fs = $svn->fs();
2015-05-14 04:13:12 +00:00
if (!defined($fs))
{
print (STDERR "Cannot open fs - $REPOFS\n");
return undef;
}
my $author = $fs->revision_prop ($REV, 'svn:author');
return $author;
}
2015-08-28 02:06:26 +00:00
sub get_commit_message
{
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;
}
2021-09-13 10:00:13 +00:00
my $fs = $svn->fs();
2015-08-28 02:06:26 +00:00
if (!defined($fs))
{
print (STDERR "Cannot open fs - $REPOFS\n");
return undef;
}
2021-09-13 10:00:13 +00:00
my $logmsg = $fs->revision_prop($REV, 'svn:log');
2016-01-22 18:12:06 +00:00
return $logmsg;
2015-08-28 02:06:26 +00:00
}
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);
}
2021-09-14 13:21:54 +00:00
my $f_rootdn = format_string($cfg->{ldap_admin_binddn}, $userid, '');
my $f_rootpw = format_string($cfg->{ldap_admin_password}, $userid, '');
2015-08-13 02:20:26 +00:00
2021-09-14 13:21:54 +00:00
my $res = $ldap->bind($f_rootdn, password => $f_rootpw);
2015-08-13 02:20:26 +00:00
if ($res->code != LDAP_SUCCESS)
{
print (STDERR "Cannot bind LDAP as $f_rootdn - " . $res->error());
$ldap->unbind();
return (-1, undef);
}
2015-08-13 02:56:58 +00:00
my $f_basedn = '';
my $f_filter = '';
if ($cfg->{ldap_auth_mode} == 2)
{
2021-09-14 13:21:54 +00:00
$f_basedn = format_string($cfg->{ldap_userid_search_base}, $userid, '');
$f_filter = format_string($cfg->{ldap_userid_search_filter}, $userid, '');
2015-08-13 02:56:58 +00:00
2021-09-14 13:21:54 +00:00
$res = $ldap->search(base => $f_basedn, scope => 'sub', filter => $f_filter);
2015-08-13 02:56:58 +00:00
if ($res->code != LDAP_SUCCESS)
{
$ldap->unbind();
return (-1, undef);
}
my $entry = $res->entry(0); # get the first entry only
if (!defined($entry))
{
$ldap->unbind();
return (0, undef);
}
2021-09-14 13:21:54 +00:00
$f_basedn = $entry->dn();
2015-08-13 02:56:58 +00:00
}
else
{
2021-09-14 13:21:54 +00:00
$f_basedn = format_string($cfg->{ldap_userid_format}, $userid, '');
2015-08-13 02:56:58 +00:00
}
$f_filter = '(' . $cfg->{ldap_mail_attribute_name} . '=*)';
2015-08-13 02:20:26 +00:00
2021-09-14 13:21:54 +00:00
$res = $ldap->search(base => $f_basedn, scope => 'sub', filter => $f_filter);
2015-08-13 02:20:26 +00:00
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) = @_;
2021-09-15 12:08:13 +00:00
my $query = $dbh->prepare("SELECT ${QC}email${QC} FROM ${QC}${prefix}user_account${QC} WHERE ${QC}userid${QC}=?");
if (!$query || !$query->execute($userid))
2015-08-13 02:20:26 +00:00
{
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) = @_;
2021-09-15 12:08:13 +00:00
my $query = $dbh->prepare("SELECT ${QC}userid${QC} FROM ${QC}${prefix}project_membership${QC} WHERE ${QC}projectid${QC}=?");
if (!$query || !$query->execute($projectid))
2015-08-13 02:20:26 +00:00
{
2016-01-22 18:12:06 +00:00
if ($query) { $query->finish (); }
2015-08-13 02:20:26 +00:00
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')
{
2021-09-15 12:08:13 +00:00
($xret, $email) = get_author_email_ldap($cfg, $member)
2015-08-13 02:20:26 +00:00
}
elsif ($cfg->{login_model} eq 'DbLoginModel')
{
2021-09-15 12:08:13 +00:00
($xret, $email) = get_author_email_db($cfg, $dbh, $prefix, $member);
2015-08-13 02:20:26 +00:00
}
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,
Subject => $subject,
Message => $message
);
2015-08-16 11:06:39 +00:00
if (length($cfg->{email_sender}) > 0)
{
$mail{From} .= $cfg->{email_sender};
}
2015-08-13 02:20:26 +00:00
Mail::Sendmail::sendmail (%mail);
return (1, undef);
}
2015-08-13 05:33:23 +00:00
sub format_commit_url
{
my ($fmt, $projectid, $author, $rev) = @_;
my $out = $fmt;
$out =~ s/\$\{PROJECTID\}/$projectid/g;
$out =~ s/\$\{AUTHOR\}/$author/g;
$out =~ s/\$\{REV\}/$rev/g;
return $out;
}
2021-09-13 10:00:13 +00:00
sub trigger_webhooks
{
2021-09-14 13:21:54 +00:00
my ($cfg, $dbh, $prefix, $projectid, $commit_message) = @_;
2021-09-15 12:06:29 +00:00
# find [skip ci], [no ci] or something similar
my @skip = ();
while ($commit_message =~ /\[(skip|no)[[:space:]]+([[:alpha:]]+)\]/g)
2021-09-14 13:21:54 +00:00
{
2021-09-15 12:06:29 +00:00
push (@skip, $2);
2021-09-14 13:21:54 +00:00
}
2021-09-13 10:00:13 +00:00
2021-09-13 13:05:51 +00:00
my $query = $dbh->prepare("SELECT ${QC}webhooks${QC} FROM ${QC}${prefix}project${QC} WHERE ${QC}id${QC}=?");
2021-09-13 10:00:13 +00:00
if (!$query || !$query->execute($projectid))
{
if ($query) { $query->finish (); }
return (-1, $dbh->errstr());
}
my $webhooks = '';
if (my @row = $query->fetchrow_array())
{
$webhooks = $row[0];
}
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
$ua->timeout (5);
foreach my $webhook (split(/\n/ ,$webhooks))
{
$webhook =~ s/^\s+|\s+$//g;
if ($webhook ne '')
{
2021-09-15 12:06:29 +00:00
my @tmp = split(/[[:space:]]+/, $webhook);
my $type = 'ci';
my $url = '';
if (scalar(@tmp) == 1) { $url = @tmp[0]; }
elsif (scalar(@tmp) == 2) { $type = @tmp[0]; $url = @tmp[1]; }
else { next; }
if (grep(/^$type$/, @skip)) { next; }
## TODO: some formatting on url?
my $res = $ua->get($url);
2021-09-13 10:00:13 +00:00
if ($res->is_success)
{
2021-09-13 13:05:51 +00:00
##print $res->decoded_content . "\n";
2021-09-13 10:00:13 +00:00
}
else
{
2021-09-13 13:05:51 +00:00
##print $res->status_line . "\n";
2021-09-13 10:00:13 +00:00
}
}
}
$query->finish ();
return (0, 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);
2021-09-14 13:21:54 +00:00
my $cfg = get_config();
2014-05-16 15:49:16 +00:00
if (!defined($cfg))
{
print (STDERR "Cannot load codepot configuration file\n");
exit (1);
}
2021-09-14 13:21:54 +00:00
my $dbh = open_database($cfg);
2014-05-16 15:49:16 +00:00
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);
}
2016-01-22 18:12:06 +00:00
my $raw_commit_message = get_commit_message();
find_issue_reference_in_commit_message ($dbh, $cfg->{database_prefix}, $REPOBASE, $REV, $raw_commit_message);
2016-12-17 11:05:47 +00:00
write_commit_log ($dbh, $cfg->{database_prefix}, $REPOBASE, $REV, $AUTHOR, $cfg->{database_store_gmt});
2015-08-13 02:20:26 +00:00
2015-08-16 11:06:39 +00:00
if (lc($cfg->{commit_notification}) eq 'yes')
2015-08-13 05:33:23 +00:00
{
2015-08-16 11:06:39 +00:00
my $commit_subject = "Commit r$REV by $AUTHOR in $REPOBASE";
my $commit_message = '';
2015-08-13 02:20:26 +00:00
2015-08-16 11:06:39 +00:00
if ($cfg->{commit_notification_url} eq '')
{
$commit_message = $commit_subject;
}
else
{
2015-08-28 02:06:26 +00:00
$commit_message = format_commit_url($cfg->{commit_notification_url}, $REPOBASE, $AUTHOR, $REV);
2015-08-16 11:06:39 +00:00
}
2016-01-22 18:12:06 +00:00
if (defined($raw_commit_message))
2015-08-28 02:06:26 +00:00
{
2016-01-22 18:12:06 +00:00
$commit_message = $commit_message . "\n" . $raw_commit_message;
2015-08-28 02:06:26 +00:00
}
2015-08-16 11:06:39 +00:00
email_message_to_project_members ($cfg, $dbh, $cfg->{database_prefix}, $REPOBASE, $commit_subject, $commit_message);
}
2015-08-13 02:20:26 +00:00
2021-09-14 13:21:54 +00:00
trigger_webhooks ($cfg, $dbh, $cfg->{database_prefix}, $REPOBASE, $raw_commit_message);
2021-09-13 10:00:13 +00:00
2014-05-16 15:49:16 +00:00
close_database ($dbh);
exit (0);