File Coverage

blib/lib/App/bif/pull/project.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package App::bif::pull::project;
2 1     1   5309 use strict;
  1         2  
  1         28  
3 1     1   6 use warnings;
  1         2  
  1         31  
4 1     1   5 use Bif::Mo;
  1         2  
  1         7  
5 1     1   1463 use AnyEvent;
  1         5767  
  1         33  
6 1     1   515 use Bif::Sync::Client;
  0            
  0            
7             use Coro;
8             use DBIx::ThinSQL qw/qv sq/;
9              
10             our $VERSION = '0.1.5_7';
11             extends 'App::bif';
12              
13             sub run {
14             my $self = shift;
15             my $opts = $self->opts;
16             my $dbw = $self->dbw;
17             my $pinfo = $self->get_project( $opts->{path} );
18              
19             $opts->{hub} //= $dbw->xval(
20             select => 'p.default_hub_id',
21             from => 'nodes_tree nt',
22             inner_join => 'projects p',
23             on => 'p.id = nt.parent AND p.default_hub_id IS NOT NULL',
24             where => { 'nt.child' => $pinfo->{id} },
25             order_by => 'nt.depth DESC',
26             limit => 1,
27             );
28              
29             return $self->err( 'ProjectNoHub', 'project hub not known' )
30             unless $opts->{hub};
31              
32             my $hinfo = $self->get_hub( $opts->{hub} );
33              
34             warn "warning: already imported: $pinfo->{path}\n"
35             if $pinfo->{local};
36              
37             $|++; # no buffering
38             my $error;
39             my $cv = AE::cv;
40              
41             my $client = Bif::Sync::Client->new(
42             name => $hinfo->{name},
43             db => $dbw,
44             location => $hinfo->{location},
45             debug => $opts->{debug},
46             debug_bifsync => $opts->{debug_bifsync},
47             on_update => sub {
48             my $client = shift;
49             $self->lprint( $client->name . ': ' . $_[0] );
50             },
51             on_error => sub {
52             $error = shift;
53             $cv->send;
54             },
55             );
56              
57             my $fh = select;
58             my $coro = async {
59             select $fh;
60              
61             eval {
62             $dbw->txn(
63             sub {
64             $dbw->xdo(
65             update => 'projects',
66             set => 'local = 1',
67             where => { id => $pinfo->{id} },
68             );
69              
70             # Set the default hub here (if this project
71             # is already a hub-level project)
72             $dbw->xdo(
73             update => 'projects',
74             set => { default_hub_id => $hinfo->{id} },
75             where => {
76             'default_hub_id !' => undef,
77             id => $pinfo->{id}
78             },
79             );
80              
81             my $status = $client->sync_hub( $hinfo->{id} );
82              
83             unless ( $status eq 'HubMatch'
84             or $status eq 'HubSync' )
85             {
86             $dbw->rollback;
87             $error =
88             " unexpected HubSync/Match status received : $status ";
89             return;
90              
91             }
92              
93             if ( $status eq 'HubSync' ) {
94             $status = $client->transfer_hub_changes;
95              
96             if ( $status ne 'TransferHubChanges' ) {
97             $dbw->rollback;
98             $error =
99             " unexpected TransferHubChanges status received : $status ";
100             return;
101             }
102             }
103              
104             print "\n";
105             $self->lprint('');
106             $client->name("$hinfo->{name}\[$pinfo->{path}\]");
107              
108             $status = $client->sync_project( $pinfo->{id} );
109              
110             unless ( $status eq 'ProjectSync'
111             or $status eq 'ProjectMatch' )
112             {
113             $dbw->rollback;
114             $error =
115             " unexpected ProjectSync/Match status received : $status ";
116             return;
117             }
118              
119             if ( $status eq 'ProjectSync' ) {
120             $status = $client->transfer_project_related_changes;
121              
122             if ( $status ne 'TransferProjectRelatedChanges' ) {
123             $dbw->rollback;
124             $error =
125             " unexpected TransferProjectRelatedChanges status received : $status ";
126             return;
127             }
128             }
129              
130             $dbw->xdo(
131             insert_or_replace_into =>
132             [ 'bifkv', qw/key change_id change_id2/ ],
133             select =>
134             [ qv('last_sync'), 'MAX(c.id)', 'MAX(c.id)', ],
135             from => 'changes c',
136             );
137              
138             print "\n";
139             return;
140             }
141             );
142             };
143              
144             if ($@) {
145             $error = $@;
146             print " \n ";
147             }
148              
149             return $cv->send;
150             };
151              
152             $cv->recv;
153             $client->disconnect;
154             return $self->err( 'Unknown', $error ) if $error;
155             $dbw->do('ANALYZE');
156             return $self->ok('PullProject');
157              
158             }
159              
160             1;
161             __END__