diff --git a/scripts/rt-outreachy-payment-next.plx b/scripts/rt-outreachy-payment-next.plx new file mode 100755 index 0000000..417982c --- /dev/null +++ b/scripts/rt-outreachy-payment-next.plx @@ -0,0 +1,73 @@ +#!/usr/bin/perl +# Copyright © 2018, Bradley M. Kuhn +# License: AGPL-3.0-or-later + +use strict; +use warnings; + +use autodie qw(open close opendir); + +use Getopt::Long; +use File::Spec; +use Date::Manip qw(ParseDate UnixDate); + +sub FindUniqueTicket(@) { + my @searchTerms = @_; + open(my $rtLsFH, "-|", "rt", "ls", "-i", 'Queue = outreachy-interns AND ' . + join(" AND ", @searchTerms)); + my $ticketSpec; + while (my $lsLine = <$rtLsFH>) { + chomp $lsLine; + if ($lsLine =~ /ticket/) { + if (defined $ticketSpec) { + $ticketSpec = undef; + last; + } + $ticketSpec = $lsLine; + } + } + close $rtLsFH; + return $ticketSpec; +} +my($PAYMENT_DIR, $VERBOSE); + +GetOptions("paymentDir=s", \$PAYMENT_DIR, "verbose=i", \$VERBOSE); + +unless (defined $PAYMENT_DIR and -d $PAYMENT_DIR) { + print STDERR "usage: $0 --paymentDir= option is required and directory must exist\n"; + exit 1; +} +$VERBOSE = 0 unless defined $VERBOSE; + +opendir(my $dh, $PAYMENT_DIR); + +while (my $file = readdir $dh) { + unless ($file =~ /^\s*(success|faile?d?)-(\S+)\.txt\s*$/i) { + print STDERR "Skipping $file which does not match proper format...\n" if ($VERBOSE >= 2); + next; + } + my($pass, $name) = ($1, $2); + open(my $fh, "<", File::Spec->catfile($PAYMENT_DIR, $file)); + my $date; + while (my $line = <$fh> ) { + if ($line =~ /^\s*Date\s*:\s*(.+)\s*$/) { + $date = UnixDate(ParseDate($1), "%Y-%m-%d"); + next; + } + } + die "Inside $file there is no valid Date:" if (not defined $date); + + my(@nameComponents) = split(/\s*-\s*/, $name); + my(@searchTerms); + foreach my $name (@nameComponents) { + push(@searchTerms, 'Subject LIKE "' . $name . '"'); + } + my $ticket = FindUniqueTicket(@searchTerms); + if (not defined $ticket) { + foreach my $term (@searchTerms) { + $ticket = FindUniqueTicket(($term)); + last if (defined $ticket); + } + } +} +