File Coverage

blib/lib/App/bif/push/project.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package App::bif::push::project;
2 1     1   7304 use strict;
  1         2  
  1         26  
3 1     1   5 use warnings;
  1         2  
  1         36  
4 1     1   1531 use AnyEvent;
  1         5926  
  1         32  
5 1     1   500 use Bif::Sync::Client;
  0            
  0            
6             use Bif::Mo;
7             use Coro;
8              
9             our $VERSION = '0.1.5_7';
10             extends 'App::bif';
11              
12             sub run {
13             my $self = shift;
14             my $opts = $self->opts;
15             my $dbw = $self->dbw;
16             my $pinfo = $self->get_project( $opts->{path} );
17             my $hinfo = $self->get_hub( $opts->{hub} );
18              
19             my $re_push = $dbw->xval(
20             select => '1',
21             from => 'project_hubs ph',
22             where => {
23             'ph.project_id' => $pinfo->{id},
24             'ph.hub_id' => $hinfo->{id},
25             },
26             );
27             warn "warning: project already pushed: $pinfo->{path} -> $hinfo->{name}\n"
28             if $re_push;
29              
30             $|++; # no buffering
31             my $error;
32             my $cv = AE::cv;
33              
34             my $client = Bif::Sync::Client->new(
35             name => $hinfo->{name},
36             db => $dbw,
37             location => $hinfo->{location},
38             debug => $opts->{debug},
39             debug_bifsync => $opts->{debug_bifsync},
40             on_update => sub {
41             my $client = shift;
42             $self->lprint( $client->name . ': ' . $_[0] );
43             },
44             on_error => sub {
45             $error = shift;
46             $cv->send;
47             },
48             );
49              
50             my $fh = select;
51             my $coro = async {
52             select $fh;
53              
54             eval {
55             $dbw->txn(
56             sub {
57             $self->pause_work;
58             my $start = time;
59              
60             my $msg = "[ push: $opts->{path} ($opts->{hub}) ]";
61             if ( $opts->{message} ) {
62             $msg .= "\n\n$opts->{message}\n";
63             }
64              
65             $opts->{change_id} =
66             $self->new_change( parent_id => $pinfo->{first_change_id},
67             );
68              
69             $dbw->xdo(
70             insert_into => 'func_update_project',
71             values => {
72             change_id => $opts->{change_id},
73             id => $pinfo->{id},
74             hub_id => $hinfo->{id},
75             },
76             ) unless $re_push;
77              
78             $self->save_new_work(
79             change_id => $opts->{change_id},
80             node_id => $pinfo->{id},
81             start => $start,
82             stop => time,
83             comment => "push project",
84             bill => 1,
85             );
86              
87             $self->end_change(
88             id => $opts->{change_id},
89             action_format => "push project $pinfo->{path} "
90             . "(%s) $hinfo->{name}",
91             action_node_id_1 => $pinfo->{id},
92             message => $msg,
93             );
94              
95             $dbw->xdo(
96             update => 'projects',
97             set => { default_hub_id => $hinfo->{id} },
98             where => { id => $pinfo->{id} },
99             );
100              
101             my $status = $client->sync_hub( $hinfo->{id} );
102              
103             unless ( $status eq 'HubSync'
104             or $status eq 'HubMatch' )
105             {
106             $dbw->rollback;
107             $error =
108             "unexpected HubSync/Match status received: $status";
109             return;
110             }
111              
112             if ( $status eq 'HubSync' ) {
113             $status = $client->transfer_hub_changes;
114             if ( $status ne 'TransferHubChanges' ) {
115             $dbw->rollback;
116             $error =
117             "unexpected TransferHubChanges status received: $status";
118             return;
119             }
120             }
121              
122             $status = $client->sync_project( $pinfo->{id} );
123              
124             unless ( $status eq 'ProjectSync'
125             or $status eq 'ProjectMatch' )
126             {
127             $dbw->rollback;
128             $error =
129             "unexpected ProjectSync/Match status received: $status";
130             return;
131             }
132              
133             if ( $status eq 'ProjectSync' ) {
134             $status = $client->transfer_project_related_changes;
135              
136             if ( $status ne 'TransferProjectRelatedChanges' ) {
137             $dbw->rollback;
138             $error =
139             "unexpected TransferProjectRelatedChanges status received: $status";
140             return;
141             }
142             }
143             print "\n";
144             print "Project(s) exported: $opts->{path}\n";
145              
146             $self->resume_work;
147             return;
148             }
149             );
150             };
151              
152             if ($@) {
153             $error = $@;
154             print "\n";
155             }
156              
157             return $cv->send( !$error );
158             };
159              
160             $cv->recv;
161             $client->disconnect;
162             return $self->err( 'Unknown', $error ) if $error;
163             return $self->ok('PushProject');
164             }
165              
166             1;
167             __END__