[Bps-public-commit] r14690 - in sd: trunk/lib/App/SD/CLI/Command/Ticket trunk/lib/App/SD/CLI/Model

spang at bestpractical.com spang at bestpractical.com
Thu Jul 31 07:48:28 EDT 2008


Author: spang
Date: Thu Jul 31 07:48:27 2008
New Revision: 14690

Added:
   sd/trunk/lib/App/SD/CLI/Command/Ticket/Create.pm
Modified:
   sd/   (props changed)
   sd/trunk/lib/App/SD/CLI/Model/Ticket.pm

Log:
 r47465 at loki:  spang | 2008-07-31 12:45:06 +0100
 add new ticket create command which overrides prophet's and allows us to create new tickets with a comment attached in an EDITOR window


Added: sd/trunk/lib/App/SD/CLI/Command/Ticket/Create.pm
==============================================================================
--- (empty file)
+++ sd/trunk/lib/App/SD/CLI/Command/Ticket/Create.pm	Thu Jul 31 07:48:27 2008
@@ -0,0 +1,66 @@
+package App::SD::CLI::Command::Ticket::Create;
+use Moose;
+
+extends 'Prophet::CLI::Command::Create';
+with 'App::SD::CLI::Model::Ticket';
+with 'App::SD::CLI::Command';
+
+# we want to launch an $EDITOR window to grab props and a comment if no
+# props are specified on the commandline
+override run => sub {
+    my $self = shift;
+    my $record = $self->_get_record_class;
+    my @prop_set = $self->prop_set;
+
+    # only invoke editor if no props specified on the commandline
+    if (!@prop_set) {
+        my @props = grep {!/^id$/} $record->props_to_show;
+        my %prefill_props;
+        # these props must exist in the hash, even if they have no value
+        foreach my $prop (@props) { $prefill_props{$prop} = undef; }
+        # set default props
+        $record->default_props(\%prefill_props);
+        # undef values are noisy later when we want to interpolate the hash
+        map { $prefill_props{$_} = '' if !defined($prefill_props{$_}) } @props;
+
+        my $footer = comment_separator();
+
+        my $ticket = $self->get_content(type => 'ticket', default_edit => 1,
+                                        prefill_props => \%prefill_props,
+                                        props_order => \@props,
+                                        footer => $footer);
+
+        die "Aborted.\n"
+            if length($ticket) == 0;
+
+        (my $props_ref, my $comment) = $self->parse_record($ticket);
+
+        foreach my $prop (keys %$props_ref) {
+            $self->set_prop($prop => $props_ref->{$prop})
+                unless $prop eq 'id'; # don't let users create ids
+        }
+
+        super();
+
+        # retrieve the created record from the superclass
+        $record = $self->record();
+
+        if ($comment) {
+            my $props = { content => $comment };
+            my $args = { uuid => $record->uuid() };
+            my @primary_commands = qw/ticket comment create/;
+            $self->cli->run_another_command( type => 'comment',
+                                             props => $props,
+                                             args => $args,
+                                             primary_commands => \@primary_commands,
+                                            );
+        }
+    } else {
+        super();
+    }
+};
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;

Modified: sd/trunk/lib/App/SD/CLI/Model/Ticket.pm
==============================================================================
--- sd/trunk/lib/App/SD/CLI/Model/Ticket.pm	(original)
+++ sd/trunk/lib/App/SD/CLI/Model/Ticket.pm	Thu Jul 31 07:48:27 2008
@@ -3,6 +3,44 @@
 
 use constant record_class => 'App::SD::Model::Ticket';
 
+=head2 comment_separator
+
+Returns a string that separates the prop: value lines from the comment,
+which will be free text to the end of the new ticket. May contain
+arbitrary newlines.
+
+=cut
+
+sub comment_separator { "\n\n=== add ticket comment below ===\n"; }
+
+=head2 parse_record $str
+
+Takes a string containing a ticket record consisting of prop: value pairs
+followed by a separator, followed by an optional comment.
+
+Returns a list of (hashref of prop => value pairs, string contents of comment)
+with props with false values filtered out.
+
+=cut
+
+sub parse_record {
+    my $self = shift;
+    my $ticket = shift;
+
+    my %props;
+    my ($new_props, $comment) = split comment_separator(), $ticket;
+    my @lines = split "\n", $new_props;
+    foreach my $line (@lines) {
+        # match prop: value pairs. whitespace in between is ignored.
+        if ($line =~ m/^(.+):\s*(.*)$/) {
+            my $prop = $1;
+            my $val = $2;
+            $props{$prop} = $val unless !($val);
+        }
+    }
+    return \%props, $comment;
+}
+
 no Moose::Role;
 
 1;



More information about the Bps-public-commit mailing list