enhancing etc/post-commit.in with webhooks trigger

This commit is contained in:
hyung-hwan 2021-09-13 10:00:13 +00:00
parent f4139e5d29
commit ac049e7590

View File

@ -15,6 +15,7 @@ use Net::LDAP;
use Net::LDAP qw(LDAP_SUCCESS); use Net::LDAP qw(LDAP_SUCCESS);
use URI; use URI;
use Mail::Sendmail; use Mail::Sendmail;
use LWP::UserAgent;
my $CFG_FILE = '@CFGDIR@/codepot.ini'; my $CFG_FILE = '@CFGDIR@/codepot.ini';
my $REPOFS = $ARGV[0]; my $REPOFS = $ARGV[0];
@ -445,6 +446,48 @@ sub format_commit_url
return $out; return $out;
} }
sub trigger_webhooks
{
my ($cfg, $dbh, $prefix, $projectid) = @_;
my $query = $dbh->prepare("SELECT ${QC}webhooks${QC} FROM ${QC}${prefix}project${QC} WHERE ${QC}projectid${QC}=?");
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 '')
{
## TODO: some formatting on webhook?
my $res = $ua->get($webhook);
if ($res->is_success)
{
print "$res->decoded_content\n";
}
else
{
print "$res->status_line\n";
}
}
}
$query->finish ();
return (0, undef);
}
#------------------------------------------------------------ #------------------------------------------------------------
# MAIN # MAIN
#------------------------------------------------------------ #------------------------------------------------------------
@ -499,6 +542,8 @@ if (lc($cfg->{commit_notification}) eq 'yes')
email_message_to_project_members ($cfg, $dbh, $cfg->{database_prefix}, $REPOBASE, $commit_subject, $commit_message); email_message_to_project_members ($cfg, $dbh, $cfg->{database_prefix}, $REPOBASE, $commit_subject, $commit_message);
} }
trigger_webhooks ($cfg, $dbh, $cfg->{database_prefix}, $REPOBASE);
close_database ($dbh); close_database ($dbh);
exit (0); exit (0);