]> git.mjollnir.org Git - cil.git/commitdiff
Added the 'comment' action
authorAndrew Chilton <andychilton@gmail.com>
Sat, 19 Apr 2008 13:07:20 +0000 (01:07 +1200)
committerAndrew Chilton <andychilton@gmail.com>
Sat, 19 Apr 2008 13:07:20 +0000 (01:07 +1200)
bin/cil
lib/CIL/Base.pm
lib/CIL/Issue.pm

diff --git a/bin/cil b/bin/cil
index 64077b051809749600119b829108b399de0d6abe..7b104e8f0df56d279c7ff303786f82208b092a26 100755 (executable)
--- a/bin/cil
+++ b/bin/cil
@@ -5,6 +5,7 @@ use strict;
 use warnings;
 use Data::Dumper;
 use CIL::Issue;
+use CIL::Comment;
 use Term::CallEditor;
 use File::Touch;
 use File::Glob ':glob';
@@ -16,11 +17,11 @@ my $COMMANDS = {
     add     => 1,
     show    => 1,
     edit    => 1,
+    comment => 1,
 };
 
 my $new_issue_text = <<"EOF";
 [Issue]
-
 Summary     =
 Status      =New
 CreatedBy   =$ENV{GIT_AUTHOR_NAME} <$ENV{GIT_AUTHOR_EMAIL}>
@@ -30,6 +31,14 @@ Description = <<END_OF_DESCRIPTION
 END_OF_DESCRIPTION
 EOF
 
+my $add_comment_text = <<"EOF";
+[Comment]
+CreatedBy   = $ENV{GIT_AUTHOR_NAME} <$ENV{GIT_AUTHOR_EMAIL}>
+Description = <<END_OF_DESCRIPTION
+
+END_OF_DESCRIPTION
+EOF
+
 ## ----------------------------------------------------------------------------
 
 {
@@ -61,6 +70,10 @@ EOF
         my ($issue_name) = @ARGV;
         edit($issue_name);
     }
+    elsif ( $command eq 'comment' ) {
+        my ($issue_name) = @ARGV;
+        comment($issue_name);
+    }
 
     else {
         fatal("program error when trying command '$command'");
@@ -196,7 +209,7 @@ EOF
         fatal("couldn't parse issue: $@");
     }
     unless ( defined $issue_edited ) {
-        fatal("couldn't parse issue (program error)");
+        fatal("couldn't create issue (program error)");
     }
 
     # for the fields we're interested in, set them
@@ -211,6 +224,48 @@ EOF
     }
 }
 
+sub comment {
+    my ($issue_name) = @_;
+
+    my $issue = CIL::Issue->new_load_issue($issue_name);
+    unless ( defined $issue ) {
+        fatal("Couldn't load issue '$issue'");
+    }
+
+    # read in the new issue text
+    my $fh = solicit( $add_comment_text );
+
+    my $cfg;
+    eval {
+        $cfg = Config::IniFiles->new( -file => $fh );
+    };
+    if ( $@ ) {
+        fatal("couldn't parse comment: $@");
+    }
+
+    unless ( defined $cfg ) {
+        fatal("not a valid inifile");
+    }
+
+    my $comment = CIL::Comment->new();
+    foreach my $field ( qw(CreatedBy Description) ) {
+        # modify the data directly, otherwise Updated will kick in
+        my $value = $cfg->val( 'Comment', $field );
+        next unless defined $value;
+
+        $value =~ s/^\s*//;
+        $value =~ s/\s*$//;
+        $comment->set_no_update($field, $value);
+    }
+
+    # tell the comment when it was inserted
+    $comment->inserted;
+
+    # add the comment to the issue, update it's timestamp and save it out
+    $issue->add_comment( $comment );
+    $issue->save();
+}
+
 ## ----------------------------------------------------------------------------
 
 sub check_paths {
@@ -249,8 +304,15 @@ sub display_issue_full {
     field( 'Labels', $issue->Labels() );
     separator();
     text('Description', $issue->Description());
-    separator();
-    title('Comments');
+    my $comments = $issue->comments();
+    foreach my $comment ( @$comments ) {
+        separator();
+        title('Comment');
+        field( 'CreatedBy', $comment->CreatedBy() );
+        field( 'Inserted', $comment->Inserted() );
+        field( 'Updated', $comment->Inserted() );
+        text('Description', $comment->Description());
+    }
     separator();
 }
 
index b0c58cf4edc4855d3cbe17d9462ce08d7aeba885..3e0cba519baadb0cc5aac813de032f78fd8b6d70 100644 (file)
@@ -7,7 +7,7 @@ use Carp;
 use DateTime;
 
 use base qw(Class::Accessor);
-__PACKAGE__->mk_accessors(qw(Description CreatedBy Inserted Updated));
+__PACKAGE__->mk_accessors(qw(CreatedBy Inserted Updated Description));
 
 # override Class::Accessor's set
 sub set {
index 26fa9965046c6c1bd13cf199004562eb8f87c20e..8daad8f09d87f35134dfe14068bdc8ec341b2f59 100644 (file)
@@ -38,23 +38,25 @@ sub new_load_issue {
         croak "filename '$filename' does no exist";
     }
 
-    my $issue = CIL::Issue->new();
-    $issue->{data} = LoadFile( $filename );
-    return $issue;
+    my $data = LoadFile( $filename );
 
-    my $cfg = Config::IniFiles->new( -file => $filename );
-    unless ( defined $cfg ) {
-        croak("not a valid inifile");
-    }
+    my $issue = CIL::Issue->new();
 
-    # my $issue = CIL::Issue->new();
+    # do the issue
     foreach my $field ( qw(Summary Name Description CreatedBy Status Labels Inserted Updated) ) {
         # modify the data directly, otherwise Updated will kick in
-        $issue->{data}{$field} = $cfg->val( 'Issue', $field );
+        $issue->{data}{$field} = $data->{$field};
     }
-    $issue->{data}{Comments}    = [];
 
-    # set the issue Name
+    # now the comments
+    foreach my $c ( @{$data->{comments}} ) {
+        my $comment = CIL::Comment->new();
+        foreach my $field ( qw(CreatedBy Inserted Updated Description) ) {
+            # modify the data directly, otherwise Updated will kick in
+            $comment->set_no_update($field, $c->{$field});
+        }
+        push @{$issue->{comments}}, $comment;
+    }
     $issue->{data}{Name} = $name;
 
     return $issue;
@@ -84,11 +86,30 @@ sub new_parse_issue {
     return $issue;
 }
 
+sub comments {
+    my ($self) = @_;
+    return $self->{comments};
+}
+
+sub add_comment {
+    my ($self, $comment) = @_;
+
+    croak "can only add comments of type CIL::Comment"
+        unless ref $comment eq 'CIL::Comment';
+
+    push @{$self->{comments}}, $comment;
+}
+
 sub save {
     my ($self) = @_;
     my $name = $self->Name;
     my $filename = "issues/$name.yaml";
-    DumpFile($filename, $self->{data});
+    my $data = {};
+    %$data = ( %{$self->{data}});
+    foreach my $comment ( @{$self->{comments}} ) {
+        push @{$data->{comments}}, $comment->{data};
+    }
+    DumpFile($filename, $data);
 }
 
 sub reset {
@@ -99,13 +120,6 @@ sub reset {
     }
 }
 
-sub add_comment {
-    my ($self, $comment_h) = @_;
-
-    my $comment = CIL::Comment->new($comment_h);
-
-}
-
 ## ----------------------------------------------------------------------------
 1;
 ## ----------------------------------------------------------------------------