lib/Serge/Sync/Plugin/VCS/gerrit.pm

filter: Statementstmt 17.8% 10 / 56 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 0.0% 0 / 2 totaltotal 15.6% 13 / 83 SCAR 46.6
CC
12
Cov
14%
_push
110.0
_update_message
12.0
name
2.0
CRAP 105.2
« lib/Serge/Engine/Plugin/if.pm ↑ Summary lib/Serge/Engine/Plugin/parse_chrome_json.pm »
1 package Serge::Sync::Plugin::VCS::gerrit;
2 1 ▶use parent Serge::Sync::Plugin::VCS::git;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Config::Neat::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub name {
9 0 ▶ return 'Gerrit sync plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
10 }
11  
12 sub init {
13 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
14  
15 0 $self->SUPER::init(@_);
16  
17 0 $self->merge_schema({
18 user => 'STRING'
19 });
20 }
21  
22 sub _update_message {
23 0 ▶ my ($self, $message) = @_;
Subroutine _update_message: not called CC=3 SCAR=24.8
24  
25 # Gerrit requires a unique 'Change-Id: xxxxxxxxxxxxxxxxxxxxxxxx' line
26 # to be appended to a commit message, otherwise it will reject the commit
27 # Change-Id is a 40-char random hex string, prefixed with 'I'
28  
29 0 my $id = 'I';
30  
31 0 eval('use Crypt::Random qw(makerandom_octet); use Digest::MD5 qw(md5_hex);');
32 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
33 # Crypt::Random not found, using built-in rand() function
34 0 my @hexchars = (0..9, 'a'..'f');
35 0 $id .= $hexchars[rand @hexchars] for 1..40;
36 } else {
37 0 $id .= md5_hex(makerandom_octet(Size => 256, Strength => 1)). # 32 chars
38 substr(md5_hex(makerandom_octet(Size => 256, Strength => 1)), 0, 8); # 8 chars
39 }
40  
41 0 return "$message\nChange-Id: $id";
42 }
43  
44 sub _push {
45 0 ▶ my ($self, $local_path, $remote_path, $message) = @_;
Subroutine _push: not called CC=10 SCAR=47.0
46  
47 0 ($remote_path, my $branch) = $self->split_remote_path_branch($remote_path);
48  
49 0 my $email = $self->{data}->{email};
50 0 my $user = $self->{data}->{user};
51  
52 # these checks are not strictly needed after $self->{schema}->validate() call
53 # and are left here temporarily
54 0 ▶ die "User email not provided\n" unless $email;
Branch: 1 branch
BranchTrueFalseTotal
unless $email 0 0 0
55 0 ▶ die "Username not provided\n" unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 0 0 0
56  
57 # get remote server hostname/port
58  
59 0 my $url = $self->run_in($local_path, 'git config --get remote.origin.url 2>&1', 1); #1:capture
60  
61 0 my ($host, $port);
62 0 ▶ if ($url =~ m|^ssh://((.*?)@)?(.+?)(:(\d+))?/|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^ssh://((.*?)@)?(.+?)(:(\d+))?/]) { } 0 0 0
63 0 $host = $3;
64 0 $port = $5;
65 } else {
66 0 die "Remote 'origin' not set for this repository or not recognized.\nURL: '$url'\n";
67 }
68  
69 0 ▶ $port = 29418 unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 0 0 0
70  
71 # push changes to Gerrit server, then self-verify and self-approve them
72  
73 0 print "Pushing changes to Gerrit into branch '$branch' as user '$user' to reviewer <$email>...\n";
74  
75 0 my $cmd = qq|git push $self->{data}->{push_params} --porcelain --quiet --receive-pack="git receive-pack --reviewer=$email" origin HEAD:refs/for/$branch 2>&1|;
76 0 my $output = $self->run_in($local_path, $cmd, 1); #1:capture
77  
78 0 ▶ if ($output =~ m/\Q[remote rejected] (no new changes)\E/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /\[remote\ rejected\]\ \(no\ new\ changes\)/) 0 0 0
79 0 print "Nothing to push.\n";
80 0 return;
81 }
82  
83 0 my $ok;
84 my @changes;
85  
86 0 ▶ while ($output =~ m|https://\S+/(\d+)|g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ m[https://\S+/(\d+)]g) 0 0 0
87 0 push @changes, $1;
88 0 $ok = 1;
89 }
90  
91 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
92 0 die "Error: can't parse the server response:\n$output\n";
93 }
94  
95 0 my $total = scalar(@changes);
96 0 my $n = 0;
97 0 foreach my $id (@changes) {
98 0 $n++;
99 0 print "Automatically reviewing change $id [$n of $total]...\n";
100  
101 0 my $cmd = qq|ssh -p $port $user\@$host "gerrit review --verified +1 --code-review +2 --submit $id,1" 2>&1|;
102 0 my $output = $self->run_in($local_path, $cmd, 1); #1:capture
103  
104 0 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 0 0 0
105 0 print $output;
106 }
107 }
108 }
109  
110 1 1;
« lib/Serge/Engine/Plugin/if.pm ↑ Summary lib/Serge/Engine/Plugin/parse_chrome_json.pm »