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   7245 use strict;
  1         2  
  1         43  
3 1     1   12 use warnings;
  1         3  
  1         37  
4 1     1   1497 use AnyEvent;
  1         5775  
  1         29  
5 1     1   586 use Bif::Sync::Client;
  0            
  0            
6             use Bif::Mo;
7             use Coro;
8              
9             our $VERSION = '0.1.5_6';
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             my $msg = "[ push: $opts->{path} ($opts->{hub}) ]";
58             if ( $opts->{message} ) {
59             $msg .= "\n\n$opts->{message}\n";
60             }
61              
62             $opts->{change_id} =
63             $self->new_change( parent_id => $pinfo->{first_change_id},
64             );
65              
66             $dbw->xdo(
67             insert_into => 'func_update_project',
68             values => {
69             change_id => $opts->{change_id},
70             id => $pinfo->{id},
71             hub_id => $hinfo->{id},
72             },
73             ) unless $re_push;
74              
75             $self->end_change(
76             id => $opts->{change_id},
77             action_format => "push project $pinfo->{path} "
78             . "(%s) $hinfo->{name}",
79             action_node_id_1 => $pinfo->{id},
80             message => $msg,
81             );
82              
83             $dbw->xdo(
84             update => 'projects',
85             set => { default_hub_id => $hinfo->{id} },
86             where => { id => $pinfo->{id} },
87             );
88              
89             my $status = $client->sync_hub( $hinfo->{id} );
90              
91             unless ( $status eq 'HubSync'
92             or $status eq 'HubMatch' )
93             {
94             $dbw->rollback;
95             $error =
96             "unexpected HubSync/Match status received: $status";
97             return;
98             }
99              
100             if ( $status eq 'HubSync' ) {
101             $status = $client->transfer_hub_changes;
102             if ( $status ne 'TransferHubChanges' ) {
103             $dbw->rollback;
104             $error =
105             "unexpected TransferHubChanges status received: $status";
106             return;
107             }
108             }
109              
110             $status = $client->sync_project( $pinfo->{id} );
111              
112             unless ( $status eq 'ProjectSync'
113             or $status eq 'ProjectMatch' )
114             {
115             $dbw->rollback;
116             $error =
117             "unexpected ProjectSync/Match status received: $status";
118             return;
119             }
120              
121             if ( $status eq 'ProjectSync' ) {
122             $status = $client->transfer_project_related_changes;
123              
124             if ( $status ne 'TransferProjectRelatedChanges' ) {
125             $dbw->rollback;
126             $error =
127             "unexpected TransferProjectRelatedChanges status received: $status";
128             return;
129             }
130             }
131             print "\n";
132             print "Project(s) exported: $opts->{path}\n";
133              
134             return;
135             }
136             );
137             };
138              
139             if ($@) {
140             $error = $@;
141             print "\n";
142             }
143              
144             return $cv->send( !$error );
145             };
146              
147             $cv->recv;
148             $client->disconnect;
149             return $self->err( 'Unknown', $error ) if $error;
150             return $self->ok('PushProject');
151             }
152              
153             1;
154             __END__