| File: | lib/App/TimeTracker/Command/RT.pm |
| Coverage: | 62.7% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | package App::TimeTracker::Command::RT; | ||||||
| 2 | 2 2 2 | 1355 5 43 | use strict; | ||||
| 3 | 2 2 2 | 9 5 58 | use warnings; | ||||
| 4 | 2 2 2 2 2 2 | 73 98 27 7 5 24 | use 5.010; | ||||
| 5 | |||||||
| 6 | # ABSTRACT: TimeTracker RT plugin | ||||||
| 7 | |||||||
| 8 | 2 2 2 | 10 5 22 | use Moose::Role; | ||||
| 9 | 2 2 2 | 6901 55871 106 | use RT::Client::REST; | ||||
| 10 | 2 2 2 | 13 5 1108 | use Try::Tiny; | ||||
| 11 | |||||||
| 12 | has 'rt' => (is=>'ro',isa=>'TT::RT',coerce=>1,); | ||||||
| 13 | has 'rt_client' => (is=>'ro',isa=>'RT::Client::REST',lazy_build=>1); | ||||||
| 14 | sub _build_rt_client { | ||||||
| 15 | 0 | my $self = shift; | |||||
| 16 | 0 | my $config = $self->config->{rt}; | |||||
| 17 | 0 | unless ($config) { | |||||
| 18 | 0 | say "Please configure RT in your TimeTracker config"; | |||||
| 19 | 0 | exit; | |||||
| 20 | } | ||||||
| 21 | |||||||
| 22 | 0 | my $client = RT::Client::REST->new( | |||||
| 23 | server => $config->{server}, | ||||||
| 24 | timeout => $config->{timeout}, | ||||||
| 25 | ); | ||||||
| 26 | |||||||
| 27 | 0 | $client->login( username => $config->{username}, password => $config->{password} ); | |||||
| 28 | 0 | return $client; | |||||
| 29 | } | ||||||
| 30 | |||||||
| 31 | before 'cmd_start' => sub { | ||||||
| 32 | my $self = shift; | ||||||
| 33 | return unless my $rt = $self->rt; | ||||||
| 34 | my $ticketname='RT'.$rt; | ||||||
| 35 | |||||||
| 36 | $self->insert_tag($ticketname); | ||||||
| 37 | |||||||
| 38 | if ($self->meta->does_role('App::TimeTracker::Command::Git')) { | ||||||
| 39 | my $ticket=$self->rt_client->show(type => 'ticket', id => $rt); | ||||||
| 40 | my $subject = $ticket->{Subject}; | ||||||
| 41 | $subject=~s/\W/_/g; | ||||||
| 42 | $subject=~s/_+/_/g; | ||||||
| 43 | $subject=~s/^_//; | ||||||
| 44 | $subject=~s/_$//; | ||||||
| 45 | |||||||
| 46 | $self->branch($ticketname.'_'.$subject) unless $self->branch; | ||||||
| 47 | } | ||||||
| 48 | }; | ||||||
| 49 | |||||||
| 50 | after 'cmd_start' => sub { | ||||||
| 51 | my $self = shift; | ||||||
| 52 | return unless $self->config->{rt}{set_owner_to}; | ||||||
| 53 | |||||||
| 54 | my $task = $self->_current_task; | ||||||
| 55 | return unless $task; | ||||||
| 56 | my $ticket_id = $task->rt_id; | ||||||
| 57 | unless ($ticket_id) { | ||||||
| 58 | say "No RT ticket id found, cannot take ticket"; | ||||||
| 59 | return; | ||||||
| 60 | } | ||||||
| 61 | |||||||
| 62 | try { | ||||||
| 63 | $self->rt_client->edit( type => 'ticket', id => $ticket_id, set=>{ | ||||||
| 64 | Status=>'open', | ||||||
| 65 | Owner=>$self->config->{rt}{set_owner_to}, | ||||||
| 66 | }); | ||||||
| 67 | } | ||||||
| 68 | catch { | ||||||
| 69 | say $_; | ||||||
| 70 | }; | ||||||
| 71 | }; | ||||||
| 72 | |||||||
| 73 | after 'cmd_stop' => sub { | ||||||
| 74 | my $self = shift; | ||||||
| 75 | |||||||
| 76 | return unless $self->config->{rt}{update_time_worked}; | ||||||
| 77 | |||||||
| 78 | my $task = $self->_previous_task; | ||||||
| 79 | return unless $task; | ||||||
| 80 | my $ticket_id = $task->rt_id; | ||||||
| 81 | unless ($ticket_id) { | ||||||
| 82 | say "No RT ticket id found, cannot update TimeWorked"; | ||||||
| 83 | return; | ||||||
| 84 | } | ||||||
| 85 | |||||||
| 86 | my $old = $self->rt_client->show(type => 'ticket', id => $ticket_id); | ||||||
| 87 | unless ($old) { | ||||||
| 88 | say "Cannot find ticket $ticket_id in RT"; | ||||||
| 89 | return; | ||||||
| 90 | } | ||||||
| 91 | |||||||
| 92 | my $worked = $old->{TimeWorked} || 0; | ||||||
| 93 | $worked =~s/\D//g; | ||||||
| 94 | |||||||
| 95 | $self->rt_client->edit( type => 'ticket', id => $ticket_id, set=>{ | ||||||
| 96 | TimeWorked=> $worked + $task->rounded_minutes | ||||||
| 97 | }); | ||||||
| 98 | }; | ||||||
| 99 | |||||||
| 100 | sub App::TimeTracker::Data::Task::rt_id { | ||||||
| 101 | 0 | my $self = shift; | |||||
| 102 | 0 0 | foreach my $tag (@{$self->tags}) { | |||||
| 103 | 0 | next unless $tag =~ /^RT(\d+)/; | |||||
| 104 | 0 | return $1; | |||||
| 105 | } | ||||||
| 106 | } | ||||||
| 107 | |||||||
| 108 | 2 2 2 | 11 5 28 | no Moose::Role; | ||||
| 109 | 1; | ||||||
| 110 | |||||||