Git-Code-Review
view release on metacpan or search on metacpan
lib/Git/Code/Review/Command/config.pm view on Meta::CPAN
sub _default_file {
my ($file, $cfg) = @_;
my %content = (
'mailhandler.config' => [
"; Mailhandler configuration",
';',
';[global]',
'; server = imap.mailserver.com',
'; port = 993',
'; ssl = 1',
'; credentials-file = /etc/code-review/sox_code_review_email.conf',
'; folder = INBOX',
'; auto-approve = 1',
],
'notification.config' => [
"; Notification Configuration for audit",
"; Valid headers are global and template where template takes a name",
'; ',
';[global]',
"; from = $cfg->{user}",
"; headers.reply-to = $cfg->{user}",
lib/Git/Code/Review/Command/mailhandler.pm view on Meta::CPAN
sub opt_spec {
_init();
my $mail_cfg = $CONFIG{ mailhandler };
return (
['server:s', "IMAP Server", {default=>exists $mail_cfg->{'global.server'} ? $mail_cfg->{'global.server'} : 'localhost' }],
['port:i', "IMAP Server Port", {default=>exists $mail_cfg->{'global.port'} ? $mail_cfg->{'global.port'} : 993 }],
['folder:s', "IMAP Folder to scan", {default=>exists $mail_cfg->{'global.folder'} ? $mail_cfg->{'global.folder'} : 'INBOX' }],
['ssl!', "Use SSL. Use --no-ssl to turn off ssl", {default=>exists $mail_cfg->{'global.ssl'} ? $mail_cfg->{'global.ssl'} : 1 }],
['credentials-file:s', "Location of the Credentials File", {default=>exists $mail_cfg->{'global.credentials-file'} ? $mail_cfg->{'global.credentials-file'} : '/etc/code-review/mailhandler.config' }],
['dry-run', "Test connection, show all configuration variables and exit",],
);
}
sub description {
my $DESC = <<" EOH";
SYNOPSIS
git-code-review mailhandler [options]
DESCRIPTION
Manage replies to the code review mailbox.
EXAMPLES
git-code-review mailhandler
git-code-review mailhandler --dry-run
git-code-review mailhandler --dry-run --server mail.server.com --port 993 --credentials-file ~/mail.config
git-code-review mailhandler --server mail.server.com --port 993 --credentials-file ~/mail.config
git-code-review mailhandler --folder Replies
OPTIONS
EOH
$DESC =~ s/^[ ]{4}//mg;
return $DESC;
}
sub execute {
lib/Git/Code/Review/Command/mailhandler.pm view on Meta::CPAN
die "Too many arguments: " . join( ' ', @$args ) if scalar @$args > 0;
_init();
$CONFIG{ mailhandler }{ 'global.auto-approve' } = 1 unless exists $CONFIG{ mailhandler }{ 'global.auto-approve' }; # not allowed to override via options
my $auto_approve = $CONFIG{ mailhandler }{ 'global.auto-approve' };
debug({color=>'cyan'},"Git::Code::Review Mailhandler Config");
debug_var($opt);
debug({clear => 1}, "Defaults");
debug(Dump $CONFIG{mailhandler});
my @missing_opts = grep { ! exists $opt->{ $_ } } qw( credentials_file server ); # ensure required options are provided
if ( @missing_opts ) {
output( {color=>'red',stderr=>1}, sprintf "Missing required options: %s", join( ', ', map { s/\_/-/g; } @missing_opts ) );
exit 1;
}
if ( ! -f $opt->{ credentials_file } ) {
output({color=>'red',stderr=>1}, sprintf "The credentials file %s does not exist. You can provide the right file with --credentials-file or configure it in global.credentials-file in mailhandler.config file", $opt->{ credentials_file } );
exit 1;
}
# Parse the config file
my $creds = Config::Auto::parse($opt->{credentials_file});
# Try to grab the username/password
my %mapping = (
username => [qw(user userid username)],
password => [qw(pass passwd password)],
);
my %credentials = ();
foreach my $key (keys %mapping) {
foreach my $try (@{ $mapping{$key} }) {
next unless exists $creds->{$try};
$credentials{$key} = $creds->{$try};
last;
}
if(!exists $credentials{$key}) {
output({color=>'red',stderr=>1}, "Unable to find the '$key' in $opt->{credentials_file}");
exit 1;
}
}
if ( $opt->{ dry_run } ) {
# show config details for connection test
output( 'Trying to connect to the mail server with the following settings.' );
output( join( ": ", @$_ ) ) for (
[ Server => $opt->{ server } ],
[ Port => $opt->{ port } ],
[ Ssl => $opt->{ ssl } ],
[ credentials_file => $opt->{ credentials_file } ],
[ User => $credentials{ username } ],
[ Password => '********' ],
[ AutoApprove => ( $auto_approve ? '1' : '0' ) ],
);
}
my $imap = Mail::IMAPClient->new(
Server => $opt->{server},
Port => $opt->{port},
Ssl => $opt->{ssl},
User => $credentials{username},
Password => $credentials{password},
) or die "Unable to connect to $opt->{server}: $@";
$METRICS{ connected } = 1;
verbose({color=>'green'}, sprintf "Successfully connected to %s as %s.", $opt->{server}, $credentials{username});
return if $opt->{ dry_run }; # we were just testing the connection to the mail server
my @folders = $imap->folders;
debug({indent=>1}, "+ Folders discovered: " . join(', ', @folders));
$imap->select($opt->{folder});
my @unseen = $imap->unseen();
verbose({indent=>1}, sprintf "+ Found %d unread messages.", ( scalar @unseen ));
lib/Git/Code/Review/Notify/JIRA.pm view on Meta::CPAN
}
# Parse the config file
my $jira_config = Config::Auto::parse($config{'jira-credential-file'});
# Try to grab the username/password
my %mapping = (
username => [qw(user username)],
password => [qw(pass passwd password)],
);
my %credentials = ();
foreach my $key (keys %mapping) {
foreach my $try (@{ $mapping{$key} }) {
next unless exists $jira_config->{$try};
$credentials{$key} = $jira_config->{$try};
last;
}
if(!exists $credentials{$key}) {
verbose({color=>'red'}, "Notify/JIRA - Unable to find the '$key' in $config{'jira-credential-file'}.");
return;
}
}
# Ticket Creation
verbose({color=>'green'}, "JIRA Parent Ticket: $config{'jira-title'}");
# Should we do the thing?
unless( exists $ENV{GCR_NOTIFY_ENABLED} ) {
output({color=>'cyan',sticky=>1}, "JIRA methods disabled, use --notify to enable.");
return;
}
# Determine the Year to use for the parent ticket
my $jira_client = JIRA::Client->new($config{'jira-url'}, @credentials{qw(username password)}) or die "Cannot create JIRA client: $@";
my %STATUS_ID_NAME = ();
my %STATUS_NAME_ID = ();
foreach my $s (values %{ $jira_client->get_statuses}) {
$STATUS_ID_NAME{$s->{id}} = $s->{name};
$STATUS_NAME_ID{$s->{name}} = $s->{id};
}
# Parent Ticket
my $parent_search = sprintf('project = %s AND summary ~ "%s" AND issuetype in standardIssueTypes() AND status != Closed', @config{qw(jira-project jira-title)});
( run in 0.242 second using v1.01-cache-2.11-cpan-4d50c553e7e )