| 1 | package App::SD::Replica::trac; | ||
| 2 | use Any::Moose; | ||
| 3 | extends qw/App::SD::ForeignReplica/; | ||
| 4 | |||
| 5 | use Params::Validate qw(:all); | ||
| 6 | use File::Temp 'tempdir'; | ||
| 7 | use Memoize; | ||
| 8 | use Try::Tiny; | ||
| 9 | |||
| 10 | use constant scheme => 'trac'; | ||
| 11 | use constant pull_encoder => 'App::SD::Replica::trac::PullEncoder'; | ||
| 12 | use constant push_encoder => 'App::SD::Replica::trac::PushEncoder'; | ||
| 13 | |||
| 14 | has trac => ( isa => 'Net::Trac::Connection', is => 'rw'); | ||
| 15 | has remote_url => ( isa => 'Str', is => 'rw'); | ||
| 16 | has query => ( isa => 'Maybe[Str]', is => 'rw'); | ||
| 17 | has foreign_username => ( isa => 'Str', is => 'rw' ); | ||
| 18 | |||
| 19 | sub BUILD { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | # Require rather than use to defer load | ||
| 23 | try { | ||
| 24 | require Net::Trac; | ||
| 25 | } catch { | ||
| 26 | die "SD requires Net::Trac to sync with a Trac server.\n". | ||
| 27 | "'cpan Net::Trac' may sort this out for you.\n"; | ||
| 28 | }; | ||
| 29 | |||
| 30 | my ( $server, $type, $query ) = $self->{url} =~ m/^trac:(.*?)$/ | ||
| 31 | or die | ||
| 32 | "Can't parse Trac server spec. Expected trac:http://example.com"; | ||
| 33 | my $uri = URI->new($server); | ||
| 34 | my ( $username, $password ); | ||
| 35 | if ( my $auth = $uri->userinfo ) { | ||
| 36 | ( $username, $password ) = split /:/, $auth, 2; | ||
| 37 | $uri->userinfo(undef); | ||
| 38 | } | ||
| 39 | $self->remote_url( $uri->as_string ); | ||
| 40 | |||
| 41 | if ( $password ) { | ||
| 42 | try { | ||
| 43 | $self->_trac_login( $username, $password ); | ||
| 44 | } catch { | ||
| 45 | die "Bad username or password specified in URL!\n"; | ||
| 46 | }; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | ($username, $password) = $self->login_loop( | ||
| 50 | uri => $uri, | ||
| 51 | username => $username, | ||
| 52 | login_callback => \&_trac_login, | ||
| 53 | ); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _trac_login { | ||
| 58 | my ($self, $username, $password) = @_; | ||
| 59 | |||
| 60 | $self->trac( | ||
| 61 | Net::Trac::Connection->new( | ||
| 62 | url => $self->remote_url, | ||
| 63 | user => $username, | ||
| 64 | password => $password, | ||
| 65 | ) | ||
| 66 | ); | ||
| 67 | |||
| 68 | # Net::Trac doesn't give us enough information | ||
| 69 | # to give a better diagnostic message, unfortunately | ||
| 70 | die "login failed!\n" unless $self->trac->ensure_logged_in; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub get_txn_list_by_date { | ||
| 74 | my $self = shift; | ||
| 75 | my $ticket = shift; | ||
| 76 | |||
| 77 | my $ticket_obj = Net::Trac::Ticket->new( connection => $self->trac); | ||
| 78 | $ticket_obj->load($ticket); | ||
| 79 | |||
| 80 | my @txns = map { | ||
| 81 | { id => $_->date->epoch, creator => $_->author, | ||
| 82 | created => $_->date->epoch } | ||
| 83 | } sort {$b->date <=> $a->date } @{$ticket_obj->history->entries}; | ||
| 84 | |||
| 85 | return @txns; | ||
| 86 | } | ||
| 87 | |||
| 88 | =head2 uuid | ||
| 89 | |||
| 90 | Return the replica's UUID | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub _uuid_url { | ||
| 95 | my $self = shift; | ||
| 96 | return $self->remote_url; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub remote_uri_path_for_comment { | ||
| 100 | my $self = shift; | ||
| 101 | my $id = shift; | ||
| 102 | return "/comment/".$id; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub remote_uri_path_for_id { | ||
| 106 | my $self = shift; | ||
| 107 | my $id = shift; | ||
| 108 | return "/ticket/".$id; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub database_settings { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | my @resolutions; | ||
| 115 | use Net::Trac::TicketSearch; | ||
| 116 | my $search = Net::Trac::TicketSearch->new( connection => $self->trac ); | ||
| 117 | # find an active ticket to get resolution list | ||
| 118 | $search->limit(1); | ||
| 119 | $search->query( status => [ qw/accepted assigned reopened new/ ] ); | ||
| 120 | my $result = $search->results->[0]; | ||
| 121 | if ( $result ) { | ||
| 122 | $result->_fetch_update_ticket_metadata; | ||
| 123 | @resolutions = @{$result->valid_resolutions}; | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | @resolutions = qw/fixed invalid wontfix duplicate | ||
| 127 | worksforme/; | ||
| 128 | } | ||
| 129 | |||
| 130 | my @active_statuses = qw/new accepted assigned reopened/; | ||
| 131 | return { | ||
| 132 | active_statuses => [@active_statuses], | ||
| 133 | statuses => [ @active_statuses, 'closed', @resolutions ], | ||
| 134 | }; | ||
| 135 | } | ||
| 136 | |||
| 137 | __PACKAGE__->meta->make_immutable; | ||
| 138 | no Any::Moose; | ||
| 139 | |||
| 140 | 1; |