From 5ca9c0c0b344c3ca50a421f1f9f46e72d0d2f1de Mon Sep 17 00:00:00 2001
From: "Bradley M. Kuhn" <bkuhn@ebb.org>
Date: Sun, 15 Jul 2018 08:27:22 -0700
Subject: [PATCH] Outreachy Payment Next script for RT

The Outreachy tickets have to move in some semi-automated fashion when
Conservancy receives approval from the Outreachy PLC that mentors have
approved Outreachy payments.

This script will likely be too specific that it can't be used outside of
Conservancy, but the details of what it does might be useful to learn how to
glue together RT operations from the RT CLI.

This first pass simply finds the tickets via the file names of the approval
files as sent by Outreachy PLC.
---
 scripts/rt-outreachy-payment-next.plx | 73 +++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100755 scripts/rt-outreachy-payment-next.plx

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=<DIRECTORY>   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);
+    }
+  }
+}
+