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