| 1 | package App::SD::CLI::Model::Ticket; | ||
| 2 | use Any::Moose 'Role'; | ||
| 3 | use Params::Validate qw(:all); | ||
| 4 | use constant record_class => 'App::SD::Model::Ticket'; | ||
| 5 | |||
| 6 | =head2 add_comment content => str, uuid => str | ||
| 7 | |||
| 8 | A convenience method that takes a content string and a ticket uuid and creates | ||
| 9 | a new comment record, for use in other commands (such as ticket create | ||
| 10 | and ticket update). | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | sub add_comment { | ||
| 15 | my $self = shift; | ||
| 16 | validate(@_, { content => 1, uuid => 1 } ); | ||
| 17 | my %args = @_; | ||
| 18 | |||
| 19 | require App::SD::CLI::Command::Ticket::Comment::Create; | ||
| 20 | |||
| 21 | $self->context->mutate_attributes( args => \%args ); | ||
| 22 | my $command = App::SD::CLI::Command::Ticket::Comment::Create->new( | ||
| 23 | uuid => $args{uuid}, | ||
| 24 | cli => $self->cli, | ||
| 25 | context => $self->context, | ||
| 26 | type => 'comment', | ||
| 27 | ); | ||
| 28 | $command->run(); | ||
| 29 | } | ||
| 30 | |||
| 31 | =head2 metadata_separator | ||
| 32 | |||
| 33 | A string of text that goes in the comment denoting the beginning of | ||
| 34 | immutable ticket metadata in a string representing a ticket. | ||
| 35 | |||
| 36 | Immutable ticket metadata includes things such as ticket id and | ||
| 37 | creation date that are useful to display to the user when editing a | ||
| 38 | ticket but are automatically assigned by sd and are not intended to | ||
| 39 | be changed manually. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | use constant metadata_separator => 'required ticket metadata (changes here will not be saved)'; | ||
| 44 | use constant mutable_props_separator => 'edit ticket details below'; | ||
| 45 | use constant comment_separator => 'add new ticket comment below'; | ||
| 46 | |||
| 47 | =head2 create_record_template [ RECORD ] | ||
| 48 | |||
| 49 | Creates a string representing a new record, prefilling default props | ||
| 50 | and props specified on the command line. Intended to be presented to | ||
| 51 | the user for editing using L<Prophet::CLI::TextEditorCommand->try_to_edit> | ||
| 52 | and then parsed using L</parse_record_template>. | ||
| 53 | |||
| 54 | If RECORD is given, then we are updating that record rather than | ||
| 55 | creating a new one, and the ticket string will be created from its | ||
| 56 | props rather than prop defaults. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub create_record_template { | ||
| 61 | my $self = shift; | ||
| 62 | my $record = shift; | ||
| 63 | my $update; | ||
| 64 | |||
| 65 | if ($record) { $update = 1 } | ||
| 66 | else { | ||
| 67 | |||
| 68 | $record = $self->_get_record_object; | ||
| 69 | $update = 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | my @do_not_edit = $record->immutable_props; | ||
| 73 | my ( @metadata_order, @mutable_order ); | ||
| 74 | my ( %immutable_props, %mutable_props ); | ||
| 75 | |||
| 76 | # separate out user-editable props so we can both show all | ||
| 77 | # the props that will be added to the new ticket and prevent | ||
| 78 | # users from being able to break things by changing props | ||
| 79 | # that shouldn't be changed, such as uuid | ||
| 80 | # | ||
| 81 | # filter out props we don't want to present for editing | ||
| 82 | my %do_not_edit = map { $_ => 1 } @do_not_edit; | ||
| 83 | |||
| 84 | for my $prop ( $record->props_to_show( | ||
| 85 | # only call props_to_show with --verbose if we're in an update | ||
| 86 | # because new tickets have no declared props | ||
| 87 | { 'verbose' => ($self->has_arg('all-props') && $update), | ||
| 88 | update => $update } ) ) { | ||
| 89 | if ( $do_not_edit{$prop}) { | ||
| 90 | if ( $prop eq 'id' && $update ) { | ||
| 91 | |||
| 92 | # id isn't a *real* prop, so we have to mess with it some more | ||
| 93 | push @metadata_order, $prop; | ||
| 94 | $immutable_props{$prop} | ||
| 95 | = $record->luid . ' (' . $record->uuid . ")"; | ||
| 96 | } elsif ( !( ( $prop eq 'id' or $prop eq 'created' ) && !$update ) ) | ||
| 97 | { | ||
| 98 | push @metadata_order, $prop; | ||
| 99 | |||
| 100 | # which came first, the chicken or the egg? | ||
| 101 | # | ||
| 102 | # we don't want to display id/created for ticket creates | ||
| 103 | # because they can't by their nature be specified until the | ||
| 104 | # ticket is actually created | ||
| 105 | $immutable_props{$prop} | ||
| 106 | = $update ? $record->prop($prop) : undef; | ||
| 107 | } | ||
| 108 | } else { | ||
| 109 | push @mutable_order, $prop; | ||
| 110 | $mutable_props{$prop} = $update ? $record->prop($prop) : undef; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | # fill in prop defaults if we're creating a new ticket | ||
| 115 | if ( !$update ) { | ||
| 116 | $record->default_props( \%immutable_props ); | ||
| 117 | $record->default_props( \%mutable_props ); | ||
| 118 | } | ||
| 119 | |||
| 120 | # fill in props specified on the commandline (overrides defaults) | ||
| 121 | if ( $self->has_arg('edit') ) { | ||
| 122 | map { $mutable_props{$_} = $self->prop($_) if $self->has_prop($_) } | ||
| 123 | @mutable_order; | ||
| 124 | $self->delete_arg('edit'); | ||
| 125 | } | ||
| 126 | |||
| 127 | my $immutable_props_string = $self->_build_kv_pairs( | ||
| 128 | order => \@metadata_order, | ||
| 129 | data => \%immutable_props, | ||
| 130 | verbose => $self->has_arg('verbose'), | ||
| 131 | record => $record, | ||
| 132 | ); | ||
| 133 | |||
| 134 | my $mutable_props_string = $self->_build_kv_pairs( | ||
| 135 | order => \@mutable_order, | ||
| 136 | data => \%mutable_props, | ||
| 137 | verbose => $self->has_arg('verbose'), | ||
| 138 | record => $record, | ||
| 139 | ); | ||
| 140 | |||
| 141 | # glue all the parts together | ||
| 142 | return join( | ||
| 143 | "\n", | ||
| 144 | |||
| 145 | $self->build_template_section( | ||
| 146 | header => metadata_separator, | ||
| 147 | data => $immutable_props_string | ||
| 148 | ), | ||
| 149 | |||
| 150 | $self->build_template_section( | ||
| 151 | header => mutable_props_separator, | ||
| 152 | data => $mutable_props_string | ||
| 153 | ), | ||
| 154 | $self->build_template_section( | ||
| 155 | header => comment_separator, | ||
| 156 | data => '' | ||
| 157 | ) | ||
| 158 | |||
| 159 | ); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _build_kv_pairs { | ||
| 163 | my $self = shift; | ||
| 164 | my %args = validate (@_, { order => 1, data => 1, | ||
| 165 | verbose => 1, record => 1 }); | ||
| 166 | |||
| 167 | my $string = ''; | ||
| 168 | for my $prop ( @{$args{order}}) { | ||
| 169 | # if called with --verbose, we print descriptions and valid values for | ||
| 170 | # props (if they exist) | ||
| 171 | if ( $args{verbose} ) { | ||
| 172 | if ( my $desc = $self->app_handle->setting( | ||
| 173 | label => 'prop_descriptions' )->get()->[0]->{$prop} ) { | ||
| 174 | $string .= '# '.$desc."\n"; | ||
| 175 | } | ||
| 176 | if ( ($args{record}->recommended_values_for_prop($prop))[0] ) { | ||
| 177 | my @valid_values = | ||
| 178 | $args{record}->recommended_values_for_prop($prop); | ||
| 179 | |||
| 180 | my $valid_vals_header = "# valid values for $prop:"; | ||
| 181 | my $valid_vals_header_len = length $valid_vals_header; | ||
| 182 | my $line_length = $valid_vals_header_len; | ||
| 183 | |||
| 184 | $string .= $valid_vals_header; | ||
| 185 | for my $val (@valid_values) { | ||
| 186 | $line_length += length($val) + 1; # add 1 for space char | ||
| 187 | my $default_line_length | ||
| 188 | = $self->config->get( key => 'core.cli-line-length' ) | ||
| 189 | || $self->cli->LINE_LENGTH; | ||
| 190 | if ( $line_length > $default_line_length ) { | ||
| 191 | $string .= "\n#"; | ||
| 192 | $string .= q{ } x $valid_vals_header_len; | ||
| 193 | $string .= $val; | ||
| 194 | $line_length = $valid_vals_header_len + length($val); | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | $string .= " $val"; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | $string .= "\n"; | ||
| 201 | } | ||
| 202 | } | ||
| 203 | $string .= "$prop: ".($args{data}->{$prop} ||'') ."\n"; | ||
| 204 | } | ||
| 205 | return $string; | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 parse_record_template $str | ||
| 209 | |||
| 210 | Takes a string containing a ticket record consisting of prop: value pairs | ||
| 211 | followed by a separator, followed by an optional comment. | ||
| 212 | |||
| 213 | Returns a list of (hashref of prop => value pairs, string contents of comment) | ||
| 214 | with props with false values filtered out. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub parse_record_template { | ||
| 219 | my $self = shift; | ||
| 220 | my $ticket = shift; | ||
| 221 | |||
| 222 | my @lines = split "\n", $ticket; | ||
| 223 | my $last_seen_sep = ''; | ||
| 224 | my %new_props; | ||
| 225 | my $comment = ''; | ||
| 226 | |||
| 227 | for my $line (@lines) { | ||
| 228 | if ($line =~ $self->separator_pattern) { | ||
| 229 | $last_seen_sep = $1; | ||
| 230 | } elsif ($line =~ $self->comment_pattern) { | ||
| 231 | # skip comments | ||
| 232 | next; | ||
| 233 | } elsif ( $last_seen_sep eq metadata_separator) { | ||
| 234 | # skip unchangeable props | ||
| 235 | next; | ||
| 236 | } elsif ($last_seen_sep eq mutable_props_separator) { | ||
| 237 | # match prop: value pairs. whitespace in between is ignored. | ||
| 238 | if ($line =~ m/^([^:]+):\s*(.*)$/) { | ||
| 239 | my $prop = $1; | ||
| 240 | my $val = $2; | ||
| 241 | $new_props{$prop} = $val unless !($val); | ||
| 242 | } | ||
| 243 | } elsif ($last_seen_sep eq comment_separator) { | ||
| 244 | $comment .= $line . "\n"; | ||
| 245 | } else { | ||
| 246 | # Throw away the section | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | return \%new_props, $comment; | ||
| 251 | } | ||
| 252 | |||
| 253 | no Any::Moose; | ||
| 254 | |||
| 255 | 1; | ||
| 256 |