From b68d3aea40c187d211c621e7583f29815c3fb57a Mon Sep 17 00:00:00 2001 From: Andrew Chilton Date: Sun, 20 Apr 2008 01:07:20 +1200 Subject: [PATCH] Added the 'comment' action --- bin/cil | 70 +++++++++++++++++++++++++++++++++++++++++++++--- lib/CIL/Base.pm | 2 +- lib/CIL/Issue.pm | 52 ++++++++++++++++++++++------------- 3 files changed, 100 insertions(+), 24 deletions(-) diff --git a/bin/cil b/bin/cil index 64077b0..7b104e8 100755 --- 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 = < +Description = <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(); } diff --git a/lib/CIL/Base.pm b/lib/CIL/Base.pm index b0c58cf..3e0cba5 100644 --- a/lib/CIL/Base.pm +++ b/lib/CIL/Base.pm @@ -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 { diff --git a/lib/CIL/Issue.pm b/lib/CIL/Issue.pm index 26fa996..8daad8f 100644 --- a/lib/CIL/Issue.pm +++ b/lib/CIL/Issue.pm @@ -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; ## ---------------------------------------------------------------------------- -- 2.39.5