summaryrefslogtreecommitdiff
path: root/lib/CIL
diff options
context:
space:
mode:
Diffstat (limited to 'lib/CIL')
-rw-r--r--lib/CIL/Base.pm47
-rw-r--r--lib/CIL/Comment.pm8
-rw-r--r--lib/CIL/Issue.pm63
-rw-r--r--lib/CIL/Utils.pm23
4 files changed, 133 insertions, 8 deletions
diff --git a/lib/CIL/Base.pm b/lib/CIL/Base.pm
index ed5c3a8..f9b932f 100644
--- a/lib/CIL/Base.pm
+++ b/lib/CIL/Base.pm
@@ -25,6 +25,7 @@ use strict;
use warnings;
use Carp;
use DateTime;
+use CIL::Utils;
use base qw(Class::Accessor);
__PACKAGE__->mk_accessors(qw(CreatedBy Inserted Updated));
@@ -87,6 +88,30 @@ sub new_from_fh {
return $class->new_from_data( $name, $data );
}
+sub set_data {
+ my ($self, $data) = @_;
+
+ # loop through all the allowed fields
+ my $fields = $self->fields();
+ my $array_fields = $self->array_fields();
+
+ # save each field
+ foreach my $field ( @$fields ) {
+ next unless defined $data->{$field};
+
+ # make it an array if it should be one
+ if ( exists $array_fields->{$field} and ref $data->{$field} ne 'ARRAY' ) {
+ $data->{$field} = [ $data->{$field} ];
+ }
+
+ # modify the data directly, otherwise Updated will kick in
+ $self->set_no_update($field, $data->{$field});
+ }
+ $self->set_no_update('Changed', 1);
+
+ $self->{data} = $data;
+}
+
sub save {
my ($self, $cil) = @_;
@@ -96,12 +121,18 @@ sub save {
CIL::Utils->write_cil_file( $filename, $self->{data}, @$fields );
}
+sub as_output {
+ my ($self) = @_;
+ my $fields = $self->fields();
+ return CIL::Utils->format_data_as_output( $self->{data}, @$fields );
+}
+
sub create_filename {
my ($class, $cil, $name) = @_;
# create the filename from it's parts
my $prefix = $class->prefix();
- my $issue_dir = $cil->issue_dir;
+ my $issue_dir = $cil->IssueDir;
my $filename = "${issue_dir}/${prefix}_${name}.cil";
return $filename;
@@ -125,7 +156,7 @@ sub set {
# finish if both are defined and they're the same
if ( defined $orig and defined $value ) {
- return if eval { $orig eq $value };
+ return if $orig eq $value;
}
# finish if neither are defined
@@ -147,7 +178,7 @@ sub set_no_update {
sub set_inserted_now {
my ($self) = @_;
- my $time = DateTime->now;
+ my $time = DateTime->now->iso8601;
$self->{data}{Inserted} = $time;
$self->{data}{Updated} = $time;
$self->{Changed} = 1;
@@ -155,7 +186,7 @@ sub set_inserted_now {
sub set_updated_now {
my ($self) = @_;
- my $time = DateTime->now;
+ my $time = DateTime->now->iso8601;
$self->{data}{Updated} = $time;
$self->{Changed} = 1;
}
@@ -184,6 +215,14 @@ sub name {
return $self->{name};
}
+sub errors {
+ my $self = shift;
+ if( @_ ) {
+ $self->{errors} = $_[0];
+ }
+ return $self->{errors};
+}
+
## ----------------------------------------------------------------------------
1;
## ----------------------------------------------------------------------------
diff --git a/lib/CIL/Comment.pm b/lib/CIL/Comment.pm
index 9d0398c..362f094 100644
--- a/lib/CIL/Comment.pm
+++ b/lib/CIL/Comment.pm
@@ -75,6 +75,14 @@ sub last_field {
return 'Description';
}
+sub is_valid {
+ # ToDo:
+ # * check that the issue is valid
+ # * Inserted and Updated are valid
+ # * Description has something in it
+ return 1;
+}
+
## ----------------------------------------------------------------------------
1;
## ----------------------------------------------------------------------------
diff --git a/lib/CIL/Issue.pm b/lib/CIL/Issue.pm
index 0dfaf53..d44626e 100644
--- a/lib/CIL/Issue.pm
+++ b/lib/CIL/Issue.pm
@@ -90,6 +90,37 @@ sub last_field {
return 'Description';
}
+sub is_valid {
+ my ($self, $cil) = @_;
+
+ my @errors;
+
+ # issues should have a Summary
+ unless ( defined defined $self->Summary and length $self->Summary ) {
+ push @errors, 'Issue does not have a summary';
+ }
+
+ # see if we only allow certain Statuses
+ if ( $cil->StatusStrict ) {
+ unless ( exists $cil->StatusAllowed()->{$self->Status} ) {
+ push @errors, "StatusStrict is turned on but this issue has an invalid status '" . $self->Status . "'";
+ }
+ }
+
+ # see if we only allow certain Labels
+ if ( $cil->LabelStrict ) {
+ my @labels = @{$self->Labels};
+ foreach my $label ( @labels ) {
+ unless ( exists $cil->LabelAllowed()->{$label} ) {
+ push @errors, "LabelStrict is turned on but this issue has an invalid label '$label'";
+ }
+ }
+ }
+
+ $self->errors( \@errors );
+ return @errors ? 0 : 1;
+}
+
sub add_label {
my ($self, $label) = @_;
@@ -124,9 +155,9 @@ sub add_attachment {
$self->flag_as_updated();
}
-sub as_output {
+sub Labels {
my ($self) = @_;
- return CIL::Utils->format_data_as_output( $self->{data}, @FIELDS );
+ return $self->{data}{Label};
}
sub Comments {
@@ -139,6 +170,34 @@ sub Attachments {
return $self->{data}{Attachment};
}
+sub is_open {
+ my ($self, $cil) = @_;
+
+ # check against the list of Open Statuses
+ my $open = $cil->StatusOpen();
+ return exists $open->{$self->Status};
+}
+
+sub is_closed {
+ my ($self, $cil) = @_;
+
+ # check against the list of Closed Statuses
+ my $closed = $cil->StatusClosed();
+ return exists $closed->{$self->Status};
+}
+
+sub assigned_to_email {
+ my ($self) = @_;
+
+ return CIL::Utils->extract_email_address( $self->AssignedTo );
+}
+
+sub created_by_email {
+ my ($self) = @_;
+
+ return CIL::Utils->extract_email_address( $self->CreatedBy );
+}
+
## ----------------------------------------------------------------------------
1;
## ----------------------------------------------------------------------------
diff --git a/lib/CIL/Utils.pm b/lib/CIL/Utils.pm
index a0e165c..e9611cf 100644
--- a/lib/CIL/Utils.pm
+++ b/lib/CIL/Utils.pm
@@ -26,6 +26,7 @@ use warnings;
use Carp;
use File::Slurp;
use File::Temp qw(tempfile);
+use Email::Find;
use POSIX qw(getpgrp tcgetpgrp);
use Fcntl qw(:DEFAULT :flock);
@@ -76,8 +77,9 @@ sub parse_from_lines {
}
}
- # now read everything that's left into the $last_field field
- $data->{$last_field} = join("\n", @lines);
+ # now read everything that's left into the $last_field field (if there is one)
+ $data->{$last_field} = join("\n", @lines)
+ if defined $last_field;
return $data;
}
@@ -128,6 +130,8 @@ sub write_cil_file {
sub solicit {
my ($class, $message) = @_;
+ $message = join('', @$message) if ref $message eq 'ARRAY';
+
# when calling this, assume we're already interactive
File::Temp->safe_level(File::Temp::HIGH);
@@ -179,6 +183,21 @@ sub ensure_interactive {
return;
}
+sub extract_email_address {
+ my ($class, $text) = @_;
+
+ my $email_address;
+ my $num_found = find_emails(
+ $text,
+ sub {
+ my ($mail_address, $text_email) = @_;
+ $email_address = $text_email;
+ }
+ );
+
+ return $email_address;
+}
+
## ----------------------------------------------------------------------------
1;
## ----------------------------------------------------------------------------