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   4462 use strict;
  1         3  
  1         24  
3 1     1   5 use warnings;
  1         2  
  1         27  
4 1     1   10 use Bif::Mo;
  1         2  
  1         6  
5 1     1   1454 use AnyEvent;
  1         5919  
  1         38  
6 1     1   499 use Bif::Sync::Client;
  0            
  0            
7             use Coro;
8             use DBIx::ThinSQL qw/qv sq/;
9              
10             our $VERSION = '0.1.5_6';
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             my $start = time;
65              
66             $dbw->xdo(
67             update => 'projects',
68             set => 'local = 1',
69             where => { id => $pinfo->{id} },
70             );
71              
72             # Set the default hub here (if this project
73             # is already a hub-level project)
74             $dbw->xdo(
75             update => 'projects',
76             set => { default_hub_id => $hinfo->{id} },
77             where => {
78             'default_hub_id !' => undef,
79             id => $pinfo->{id}
80             },
81             );
82              
83             $self->start_work(
84             node_id => $pinfo->{id},
85             start => $start,
86             start_comment => 'pull project',
87             stop => time,
88             billable => 0
89             );
90              
91             my $status = $client->sync_hub( $hinfo->{id} );
92              
93             unless ( $status eq 'HubMatch'
94             or $status eq 'HubSync' )
95             {
96             $dbw->rollback;
97             $error =
98             " unexpected HubSync/Match status received : $status ";
99             return;
100              
101             }
102              
103             if ( $status eq 'HubSync' ) {
104             $status = $client->transfer_hub_changes;
105              
106             if ( $status ne 'TransferHubChanges' ) {
107             $dbw->rollback;
108             $error =
109             " unexpected TransferHubChanges status received : $status ";
110             return;
111             }
112             }
113              
114             print "\n";
115             $self->lprint('');
116             $client->name("$hinfo->{name}\[$pinfo->{path}\]");
117              
118             $status = $client->sync_project( $pinfo->{id} );
119              
120             unless ( $status eq 'ProjectSync'
121             or $status eq 'ProjectMatch' )
122             {
123             $dbw->rollback;
124             $error =
125             " unexpected ProjectSync/Match status received : $status ";
126             return;
127             }
128              
129             if ( $status eq 'ProjectSync' ) {
130             $status = $client->transfer_project_related_changes;
131              
132             if ( $status ne 'TransferProjectRelatedChanges' ) {
133             $dbw->rollback;
134             $error =
135             " unexpected TransferProjectRelatedChanges status received : $status ";
136             return;
137             }
138             }
139              
140             $dbw->xdo(
141             insert_or_replace_into =>
142             [ 'bifkv', qw/key change_id change_id2/ ],
143             select =>
144             [ qv('last_sync'), 'MAX(c.id)', 'MAX(c.id)', ],
145             from => 'changes c',
146             );
147              
148             print "\n";
149             return;
150             }
151             );
152             };
153              
154             if ($@) {
155             $error = $@;
156             print " \n ";
157             }
158              
159             return $cv->send;
160             };
161              
162             $cv->recv;
163             $client->disconnect;
164             return $self->err( 'Unknown', $error ) if $error;
165             $dbw->do('ANALYZE');
166             return $self->ok('PullProject');
167              
168             }
169              
170             1;
171             __END__