Complete script, improving error messages.

This commit is contained in:
Bradley M. Kuhn 2018-07-22 18:49:06 -07:00
parent 421dd769cb
commit 9cb57fe776

View file

@ -12,7 +12,7 @@ use File::Spec;
use Date::Manip qw(ParseDate UnixDate);
my($PAYMENT_DIR, $VERBOSE, $INTERACTIVE, $PAYMENT_NUMBER, $RT_CMD, $INVOICE_LINE, $INTERN_SUCCESS_FILE,
$INTERN_FAIL_FILE);
$INTERN_FAIL_FILE, $LEDGER_ENTRY_DATE, $SVN_CMD);
###############################################################################
sub FindUniqueTicket(@) {
@ -66,6 +66,22 @@ sub LedgerTagFromTicket($$) {
return $tagValue;
}
###############################################################################
sub AllFormattedLedgerTagFromTicket($) {
my($ticketSpec) = @_;
my @tags;
open(my $rtLedgerTagsFH, "-|", "$RT_CMD", "show", "-f", 'CF.{ledger-tags}', $ticketSpec);
my $tagValue;
my $start = 0;
while (my $tagsLine = <$rtLedgerTagsFH>) {
$start = 1 if $tagsLine =~ s/^\s*CF.{ledger-tags}\s*:\s+//;
next unless $start;
$tagsLine =~ s/^\s*//;
push(@tags, " $tagsLine");
}
close $rtLedgerTagsFH;
return @tags;
}
###############################################################################
sub GetLinksForTicket($) {
my($ticketSpec) = @_;
my @linked;
@ -150,12 +166,18 @@ sub GetCustomFieldForTicket($$) {
GetOptions("paymentDir=s" => \$PAYMENT_DIR, "verbose=i" => \$VERBOSE, "interactive" => \$INTERACTIVE,
"paymentNumber=i" => \$PAYMENT_NUMBER, "rtCommand=s" => \$RT_CMD,
"invoiceLine=s" => \$INVOICE_LINE, "internSuccessFile=s", \$INTERN_SUCCESS_FILE,
"internFailFile=s", \$INTERN_FAIL_FILE);
"internFailFile=s", \$INTERN_FAIL_FILE, 'ledgerEntryDate=s' => \$LEDGER_ENTRY_DATE,
"svnCommand=s" => \$SVN_CMD);
$RT_CMD = '/usr/bin/rt' unless defined $RT_CMD;
$SVN_CMD = '/usr/bin/svn' unless defined $SVN_CMD;
$INTERACTIVE = 0 if not defined $INTERACTIVE;
unless (defined $LEDGER_ENTRY_DATE and $LEDGER_ENTRY_DATE =~ /^[\d\-]+$/) {
print STDERR "usage: $0 --ledgerEntryDate=<DATE> option is required and must be in ISO 8601 format\n";
exit 1;
}
unless (defined $INVOICE_LINE and $INVOICE_LINE =~ /^rt.*/) {
print STDERR "usage: $0 --invoiceLine=<RT_SPEC> option is required and must match an RT spec\n";
exit 1;
@ -228,7 +250,7 @@ while (my $file = readdir $dh) {
}
if (not defined $ticket) {
if (not $INTERACTIVE) {
print STDERR "\"$file\": Skipped: unable to to find a matching ticket.\n";
print STDERR "\"$file\": TICKET-NOT-FOUND: Skipped: unable to to find a matching ticket.\n";
next;
} else {
# FIXME: prompt for ticket
@ -237,7 +259,7 @@ while (my $file = readdir $dh) {
}
my $completedInternshipField = GetCustomFieldForTicket($ticket, "completed-internship");
if (not defined $completedInternshipField) {
print STDERR "\"$file\": \"$ticket\": Skipping: cannot determine Entity from ticket.\n" ;
print STDERR "\"$file\": \"$ticket\": FIELD-NOT-FOUND: Skipping: cannot determine Entity from ticket.\n" ;
next;
} elsif ($completedInternshipField eq 'successful') {
# Don't print to STDERR here, just keep a count since these are "old interns"
@ -246,7 +268,7 @@ while (my $file = readdir $dh) {
}
my $entity = LedgerTagFromTicket($ticket, 'Entity');
if (not defined $entity) {
print STDERR "\"$file\": \"$ticket\": Skipping: cannot determine Entity from ticket.\n" ;
print STDERR "\"$file\": \"$ticket\": ENTITY-NOT-FOUND: Skipping: cannot determine Entity from ticket.\n" ;
next;
}
@ -319,6 +341,69 @@ while (my $file = readdir $dh) {
"ticket is in status \"$mainTicketStatus\" instead of \"needs-project-ok\"\n";
next;
}
print STDERR "\"$file\": \"$ticket\": processing to payment $PAYMENT_NUMBER state... ";
my $successString = ($pass) ? "success" : "failed";
my $repositoryFile = File::Spec->catfile($PAYMENT_DIR, $mentorDate . "_" . $entity . '_' . $successString . '-report.mbox');
my $approvalTag = $repositoryFile;
$approvalTag =~ s%^.*(Projects/Outreachy/.*)$%$1%;
$approvalTag = " ;Approval: $approvalTag";
rename(File::Spec->catfile($PAYMENT_DIR, $file), $repositoryFile);
system($SVN_CMD, "add", $repositoryFile);
open(my $rtCorrespondFH, "|-", $RT_CMD, 'correspond', $ticket, '-m', '-');
my @dd;
foreach my $line (@{$internCorrespond{$successString}}) {
$line =~ s/FIXME_PAYMENT_NUMBER/$PAYMENT_NUMBER/g;
$line =~ s/FIXME_MENTOR_DATE/$mentorDate/g;
push(@dd, $line);
}
print $rtCorrespondFH @dd;
close $rtCorrespondFH;
my $invoiceTicket = $INVOICE_LINE;
$invoiceTicket =~ s%^.*ticket/(\d+).*$%$1%;
my $num = $ticket; $num =~ s%^.*ticket/(\d+).*$%$1%;
system($RT_CMD, 'link', $invoiceTicket, 'refersto', $num);
if ($pass) {
open(my $rtCommentFH, "|-", $RT_CMD, 'comment', $ticket, '-m', '-');
print $rtCommentFH " ;Invoice: $INVOICE_LINE\n";
close $rtCommentFH;
system($RT_CMD, "edit", $ticket, 'set', 'CF.{completed-internship}=payment-' . $PAYMENT_NUMBER . '-approved',
'Status=open');
my($leftA, $rightA);
if ($PAYMENT_NUMBER == 1) {
$leftA = ' $-500.00'; $rightA = ' $500.00';
} elsif ($PAYMENT_NUMBER == 2) {
$leftA = '$-2,250.00'; $rightA = '$2,250.00';
} elsif ($PAYMENT_NUMBER == 3) {
$leftA = '$-2,750.00'; $rightA = '$2,750.00';
}
my(@tags) = AllFormattedLedgerTagFromTicket($ticket);
open(my $ledgerEntryFH, ">>", File::Spec->catfile($PAYMENT_DIR, "entry.ledger"));
print $ledgerEntryFH <<LEDGER_ENTRY
$LEDGER_ENTRY_DATE FIXME - Outreachy Internship - Round 2018-05 - Payment 2
@tags ;Invoice: $INVOICE_LINE
$approvalTag
Accrued:Accounts Payable:Outreachy $leftA
Expenses:Outreachy:Internships $rightA
LEDGER_ENTRY
;
close $ledgerEntryFH;
} else {
system($RT_CMD, "edit", $reimbursementTicket, 'Status=open');
system($RT_CMD, "edit", $reimbursementTicket, 'Status=rejected');
if ($PAYMENT_NUMBER == 1) {
system($RT_CMD, "edit", $taxTicketStatus, 'Status=rejected');
system($RT_CMD, "edit", $ticket, 'Status=rejected');
} else {
system($RT_CMD, "edit", $ticket, 'Status=entered');
}
}
print STDERR "...done\n";
print STDERR "Waiting? ";
my $x = <STDIN>;
}
print STDERR "Old Interns, who were marked as successful (likely from previous interns) ignored: $oldInterns\n";
###############################################################################