Created
August 13, 2012 22:10
-
-
Save smujohnson/3344438 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use v5.10.0; | |
use Sjohnson::Database qw(getTBMDSN); | |
use Sjohnson::Shortcuts qw(slurp); | |
use File::Temp qw(tempfile tempdir); | |
use DBI; | |
use Term::ANSIColor qw(:constants); | |
use Text::Trim; | |
use List::MoreUtils qw(uniq); | |
# --- | |
my $tmp_dir; | |
$tmp_dir = (-d '/tmp/tmpfs') ? '/tmp/tmpfs' : '/tmp'; | |
my %rowdata; # stores the actual fields of the insertion row | |
$rowdata{al_name} = determineUser(); | |
# default tag suggestions | |
my @tags_do_want = ('bug', 'fixed'); | |
# all the tags | |
my @tags = getTags( { check_against => \@tags_do_want } ); | |
# start preparing the "before buffer", for vi editing | |
my $buffer_init = prepareCommentedMessages(); | |
my ($tempfile_fh, $tempfile_fn) = tempfile("tmpfileXXXXX", 'DIR', $tmp_dir); | |
print $tempfile_fh $buffer_init; | |
close($tempfile_fh); | |
# end of init state, fire up the editor | |
system($ENV{EDITOR}, $tempfile_fn); | |
# done editing, now load that bad boy up by lines so we can parse the data. | |
# the idea is to grab the Heading and Tags lines and parse them, then yank them out. | |
# What remains is taken as the "activity", and thus whitespace trimmed. | |
my @lines = split("\n", slurp($tempfile_fn)); | |
@lines = grep { ! m/^\s* \#/x } @lines; | |
my @tags_chosen; | |
my $c = -1; | |
foreach (@lines) { | |
++$c; | |
next unless defined; | |
if (m/^Tags: (.+)$/) { | |
@tags_chosen = split(' ', $1); | |
$lines[$c] = undef; | |
next; | |
} | |
if (m/^Heading: (.+)$/) { | |
$lines[$c] = undef; | |
$rowdata{al_heading} = trim($1); | |
next; | |
} | |
} | |
@lines = grep { defined } @lines; | |
if (! @tags_chosen) { | |
gracefulDeath("no tags chosen"); | |
} | |
if (! exists($rowdata{al_heading})) { | |
gracefulDeath("heading not present"); | |
} | |
if (! length($rowdata{al_heading})) { | |
gracefulDeath("blank heading!"); | |
} | |
# if there are any supported tags, exit the program | |
scanTags(\@tags, \@tags_chosen); | |
@tags_chosen = uniq @tags_chosen; | |
# tags in this field are space delimited | |
$rowdata{al_tags} = join ' ', @tags_chosen; | |
$rowdata{al_activity} = trim(join ("\n", @lines)); | |
unless (length($rowdata{al_activity})) { | |
gracefulDeath("No article scooped."); | |
} | |
my $dbi = DBI->connect(getTBMDSN()); | |
my $sth_insert = $dbi->prepare('INSERT INTO `activity_log` (al_datecreated, al_name, al_heading, al_tags, | |
al_activity, al_activitydate) VALUES (NOW(), ?, ?, ?, ?, NOW())'); | |
$sth_insert->execute( @rowdata{"al_name", "al_heading", "al_tags", "al_activity"} ); | |
# --- | |
sub prepareCommentedMessages { | |
my $buffer; | |
$buffer .= "Heading: \n\n\n\n"; | |
$buffer .= "Tags: " . join(' ', @tags_do_want) . "\n"; | |
$buffer .= "\n"; | |
$buffer .= "# Please enter the commit message for your changes. Lines starting\n"; | |
$buffer .= "# with '#' will be ignored.\n"; | |
$buffer .= "#\n"; | |
$buffer .= "# Available tags:\n#\n"; | |
my @tags_copy = @tags; | |
my $c = 0; | |
while (@tags_copy) { | |
++$c; | |
my @five; | |
for (1 .. 5) { | |
last unless @tags_copy; | |
push(@five, shift(@tags_copy)); | |
} | |
$buffer .= "# " . join(' ', @five) . "\n"; | |
} | |
return $buffer; | |
} | |
sub getTags { | |
my $argref = shift; | |
my @tags_from_php = trim split(',' => qx'php -r \'include_once("site/ListFactory.php"); include_once | |
("site/SiteAction/SA_TBMNews.php"); $sa = new SA_TBMNews(); $a = $sa->getTagNames(); print implode (",", | |
$a) . "\n";\''); | |
if (exists($argref->{check_against})) { | |
scanTags(\@tags_from_php, $argref->{check_against}); | |
} | |
return @tags_from_php; | |
} | |
sub scanTags { | |
my $array_ref_main = shift; | |
my $array_ref_keys_to_check = shift; | |
foreach my $tp (@$array_ref_keys_to_check) { | |
if (! grep { $_ eq $tp } @$array_ref_main) { | |
gracefulDeath("requested a tag not found in the source tag list"); | |
} | |
} | |
} | |
sub determineUser { | |
for ($ENV{USER}) { | |
## CENSORED ## | |
## CENSORED ## | |
## CENSORED ## | |
} | |
gracefulDeath("Unknown user!"); | |
} | |
sub gracefulDeath { | |
my $msg = shift; | |
say STDERR "Exiting script, reason given: $msg"; | |
if (defined($rowdata{al_heading}) || defined($rowdata{al_activity})) { | |
print "Dumping input data back to you...\n\n"; | |
if (defined($rowdata{al_heading})) { print "Heading: $rowdata{al_heading}\n\n"; } | |
if (defined($rowdata{al_activity})) { print "Activity: $rowdata{al_activity}\n\n"; } | |
if (@tags_chosen) { print "Tags: @tags_chosen\n"; } | |
} | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment