Commit ffb65878 authored by Robert Ricci's avatar Robert Ricci

Get into shape for sending mail

Not quite done, not calling sendmail yet because I need more cleanup.

Most of the cannibalization this time is from gitmail
parent e8784003
#!/usr/bin/perl -w
#
# gitmaild - simple script to send mail when gitlab issues are posted or
# gitisssued - simple script to send mail when gitlab issues are posted or
# updated
# TODO: Better error handling; try really hard not to die
#
......@@ -13,6 +13,7 @@ use HTTP::Response;
use URI;
use LWP::UserAgent;
use IPC::Open3;
use IPC::Open2;
use Getopt::Long;
use JSON;
use Cwd 'abs_path';
......@@ -35,7 +36,7 @@ use strict;
# just set variables there as you would here, they will override the defaults
#
# The only mandatory option: Token to use when calling gitmail API - should
# The only mandatory option: Token to use when calling gitlab API - should
# belong to an administrator
my $TOKEN = "";
......@@ -64,6 +65,7 @@ my $result = GetOptions("v" => \$verbose, "d" => \$debug, "c:s" => \$configfile)
sub send_message($$);
sub call_gitlab_api($);
sub get_member_addresses($);
sub generate_messageid();
#
# Parse config file if given
......@@ -93,7 +95,7 @@ if ($TOKEN eq "") {
#
my $listen_socket = HTTP::Daemon->new(LocalAddr => 'localhost',
LocalPort => $PORT) || die;
print "gitmaild running at: ", $listen_socket->url, "\n";
print "gitissuedd running at: ", $listen_socket->url, "\n";
# This causes children to be auto-reaped
$SIG{CHLD}="IGNORE";
......@@ -158,23 +160,39 @@ while (my $connection = $listen_socket->accept()) {
}
#
# Actually run gitmail, using the data that we pulled out of the JSON that
# was passed to us
# Actually send a message
#
sub send_message($$) {
my ($data,$get_vars) = @_;
#
# Generate our own messageid - this will be helpful if we are sending
# multiple copies to multiple addesses, so that people's duplicate
# supression can pick up on the fact that they are the same message
#
my $messageid = generate_messageid();
if ($verbose) {
print "Running gitmail\n";
print "Sending message\n";
}
# TODO: Error handling (or at least reporting)
my $attr = $data->{"object_attributes"};
#
# Get information about the user so that we can set, eg., 'from'
# appropriately
#
my $userinfo = call_gitlab_api("/users/" . $data->{"object_attributes"}{"author_id"});
my $authorinfo = call_gitlab_api("/users/" . $attr->{"author_id"});
#
# See if it's assigned
#
my $assigneeinfo;
if ($attr->{"assignee_id"}) {
$assigneeinfo = call_gitlab_api("/users/" . $attr->{"assignee_id"});
}
#
# Get information about the project, so that we know where the repo lives,
......@@ -182,25 +200,59 @@ sub send_message($$) {
# NB: This depends on the Utah patch to gitlab that adds the project ID
# to the data in the hook
#
my $repoinfo = call_gitlab_api("/projects/" . $data->{"object_attributes"}{"project_id"});
my $repoinfo = call_gitlab_api("/projects/" . $attr->{"project_id"});
#
# Build up options that we'll pass to gitmail
#
my %options;
# Who the mail comes from - user doing the push
$options{'mailfrom'} = $userinfo->{name} . " <" . $userinfo->{email} . ">";
# Who the mail comes from
$options{'mailfrom'} = $authorinfo->{name} . " <" . $authorinfo->{email} . ">";
# Name of the repo
$options{'reponame'} = $repoinfo->{path_with_namespace};
my $reponame = $repoinfo->{path_with_namespace};
$options{'alwaysmail'} =
get_member_addresses($data->{"object_attributes"}{"project_id"});
get_member_addresses($attr->{"project_id"});
#
# Actually send the mail
#
if ($debug) {
open(MAIL,">&STDERR");
print MAIL "\n\n";
}
print MAIL "From: " . $options{"mailfrom"} . "\n";
print MAIL "To: " . join(", ",@{$options{'alwaysmail'}}) . "\n";
print MAIL "Subject: gitlab issue: [$reponame] issue #" . $attr->{"iid"} . " " .
$attr->{"action"} . " (" . $attr->{"state"} . ")" . "\n";
print MAIL "Message-Id: $messageid\n";
#
# Add an X-Git-Repo header to help people procmail
#
if (defined($reponame)) {
print MAIL "X-Git-Repo: $reponame\n";
}
#
# Marks end of headers
#
print MAIL "\n";
print MAIL "Issue #" . $attr->{"iid"} . " - " . $attr->{"title"} . "\n";
print MAIL "Action: " . $attr->{"action"} . "\n";
print MAIL "State: " . $attr->{"state"} . "\n";
print MAIL "URL: " . $attr->{"url"} . "\n";
print MAIL "Author: " . $authorinfo->{"name"} . " <" . $authorinfo->{"email"} . ">\n";
if ($assigneeinfo) {
print MAIL "Assigned to: " . $assigneeinfo->{"name"} . " <" . $assigneeinfo->{"email"} . ">\n";
}
print MAIL "Description:\n";
print MAIL $attr->{"description"};
print "\n";
#
# Done!
......@@ -261,3 +313,24 @@ sub get_member_addresses($) {
return \@addresses;
}
#
# Generate a value suitable for use in a Message-Id header
#
sub generate_messageid() {
# First part: current time
my $first = time();
# Second part: some random junk
my $second;
foreach my $i (0 .. 8) {
$second .= sprintf "%02x", int(rand(256));
}
# Third part: hostname
my $hostname = `hostname -f`;
chomp $hostname;
my $msgid = "<$first.$second\@$hostname>";
return $msgid;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment