App-SD
view release on metacpan or search on metacpan
lib/App/SD/Replica/lighthouse.pm view on Meta::CPAN
package App::SD::Replica::lighthouse;
use Any::Moose;
extends qw/App::SD::ForeignReplica/;
use Params::Validate qw(:all);
use Memoize;
use URI;
use Memoize;
use Prophet::ChangeSet;
use constant scheme => 'lighthouse';
use constant pull_encoder => 'App::SD::Replica::lighthouse::PullEncoder';
use constant push_encoder => 'App::SD::Replica::lighthouse::PushEncoder';
has lighthouse => ( isa => 'Net::Lighthouse::Project', is => 'rw' );
has remote_url => ( isa => 'Str', is => 'rw' );
has account => ( isa => 'Str', is => 'rw' );
has project => ( isa => 'Str', is => 'rw' );
has query => ( isa => 'Str', is => 'rw' );
our %PROP_MAP = ( state => 'status', title => 'summary' );
sub BUILD {
my $self = shift;
eval {
require Net::Lighthouse::Project;
require Net::Lighthouse::User;
};
if ($@) {
die "SD requires Net::Lighthouse to sync with a Lighthouse server.\n".
"'cpan Net::Lighthouse' may sort this out for you";
}
my ( $auth, $account, $project, $query ) =
$self->{url} =~ m{^lighthouse:(?:(.*)@)?(.*?)/(.*?)(?:/(.*))?$}
or die
"Can't parse lighthouse server spec. Expected lighthouse:email:password\@account/project/query or lighthouse:token\@account/project/query.";
my $server = "http://$account.lighthouseapp.com";
$self->query( $query || 'all' );
my ( $email, $password, $token );
if ($auth) {
if ( $auth =~ /@/ ) {
( $email, $password ) = split /:/, $auth;
}
else {
$token = $auth;
}
}
unless ( $token || $password ) {
if ($email) {
( undef, $password ) = $self->prompt_for_login(
uri => $server,
username => $email,
);
}
else {
( undef, $token ) = $self->prompt_for_login(
uri => $server,
username => 'not important',
secret_prompt => sub {
"token for $server: ";
}
);
}
}
$self->remote_url($server);
$self->account( $account );
$self->project( $project );
my $lighthouse = Net::Lighthouse::Project->new(
auth => {
$email ? ( email => $email, password => $password ) : (),
$token ? ( token => $token ) : (),
},
account => $account,
);
$lighthouse->load( $project );
$self->lighthouse( $lighthouse );
}
sub get_txn_list_by_date {
my $self = shift;
my $ticket = shift;
my $ticket_obj = $self->lighthouse->ticket;
$ticket_obj->load($ticket);
my $sequence = 0;
my @txns = map {
{
id => $sequence++,
creator => $_->creator_name,
created => $_->created_at->epoch,
}
} $ticket_obj->versions;
if ( $ticket_obj->attachments ) {
my $user = Net::Lighthouse::User->new(
map { $_ => $self->sync_source->lighthouse->$_ }
grep { $self->sync_source->lighthouse->$_ }
qw/account email password token/
);
for my $att ( $ticket_obj->attachments ) {
$user->load( $att->uploader_id );
push @txns,
{
id => $att->id,
creator => $user->name,
created => $att->created_at->epoch,
};
}
@txns = sort { $a->created <=> $b->created } @txns;
}
return @txns;
}
sub foreign_username {
my $self = shift;
my $user =
( run in 0.905 second using v1.01-cache-2.11-cpan-6aa56a78535 )