Commit 002412274666393573997ca1040009a629f9ce84

Authored by Athos
2 parents c76b6f4c 6b89b456

Merge branch 'refactor' into 'master'

Refactor

See merge request !1
Showing 2 changed files with 60 additions and 49 deletions   Show diff stats
lib/CoprStatus.pm
@@ -7,55 +7,49 @@ use LWP::UserAgent; @@ -7,55 +7,49 @@ use LWP::UserAgent;
7 7
8 $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; 8 $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
9 9
  10 +# hash with repos data
  11 +our $info = {};
  12 +
10 sub copr_monitor_url { 13 sub copr_monitor_url {
11 my ( $user, $repo ) = @_; 14 my ( $user, $repo ) = @_;
12 return "http://copr.fedoraproject.org/api/coprs/$user/$repo/monitor/"; 15 return "http://copr.fedoraproject.org/api/coprs/$user/$repo/monitor/";
13 } 16 }
14 17
  18 +sub git_url {
  19 + my ( $domain, $spec_path, $branch, $package ) = @_;
  20 +
  21 + $spec_path =~ s/<branch>/$branch/;
  22 + $spec_path =~ s/<package>/$package/g;
  23 +
  24 + return "$domain/$spec_path";
  25 +}
  26 +
15 sub copr_info { 27 sub copr_info {
  28 + my ( $user, $repo, $branch ) = @_;
  29 +
16 my $ua = LWP::UserAgent->new; 30 my $ua = LWP::UserAgent->new;
17 $ua->timeout(300); 31 $ua->timeout(300);
18 $ua->env_proxy; 32 $ua->env_proxy;
19 $ua->ssl_opts(SSL_verify_mode => 0x00); 33 $ua->ssl_opts(SSL_verify_mode => 0x00);
20 34
21 - my $result_v4 = $ua->get(copr_monitor_url("softwarepublico", "v4"));  
22 - my $result_v5 = $ua->get(copr_monitor_url("softwarepublico", "v5")); 35 + my $result = $ua->get(copr_monitor_url($user, $repo));
23 36
24 my $json = JSON->new->allow_nonref; 37 my $json = JSON->new->allow_nonref;
25 38
26 - my $dec_result_v4 = $json->decode($result_v4->decoded_content);  
27 - my $dec_result_v5 = $json->decode($result_v5->decoded_content);  
28 - my $info = {};  
29 -  
30 - foreach(@{$dec_result_v4->{'packages'}}) {  
31 - my $package = $_->{'pkg_name'};  
32 - my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};  
33 - my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};  
34 - $info->{$package}->{'v4_version'} = $version if $status eq "succeeded";  
35 - } 39 + my $dec_result = $json->decode($result->decoded_content);
36 40
37 - foreach(@{$dec_result_v5->{'packages'}}) { 41 + foreach(@{$dec_result->{'packages'}}) {
38 my $package = $_->{'pkg_name'}; 42 my $package = $_->{'pkg_name'};
39 my $status = $_->{'results'}{'epel-7-x86_64'}{'status'}; 43 my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
40 my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'}; 44 my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
41 - $info->{$package}->{'v5_version'} = $version if $status eq "succeeded";  
42 - }  
43 -  
44 - foreach my $key (keys %{$info}) {  
45 - my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/master/src/pkg-rpm/$key/$key.spec");  
46 - my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m;  
47 - if($version =~ /%\{version\}/) {  
48 - $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m;  
49 - }  
50 -  
51 - my $release = 'no_release';  
52 - $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m;  
53 - $version = "$version-$release";  
54 - $info->{$key}->{'git_version_master'} = $version; 45 + $info->{$package}->{$repo."_version"} = $version if $status eq "succeeded";
55 } 46 }
56 47
57 - foreach my $key (keys %{$info}) {  
58 - my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/stable-4.x/src/pkg-rpm/$key/$key.spec"); 48 + foreach my $package (keys %{$info}) {
  49 + my $git_url = git_url('http://softwarepublico.gov.br',
  50 + 'gitlab/softwarepublico/softwarepublico/raw/<branch>/src/pkg-rpm/<package>/<package>.spec',
  51 + $branch, $package);
  52 + my $spec = $ua->get($git_url);
59 my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m; 53 my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m;
60 if($version =~ /%\{version\}/) { 54 if($version =~ /%\{version\}/) {
61 $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m; 55 $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m;
@@ -64,21 +58,21 @@ sub copr_info { @@ -64,21 +58,21 @@ sub copr_info {
64 my $release = 'no_release'; 58 my $release = 'no_release';
65 $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m; 59 $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m;
66 $version = "$version-$release"; 60 $version = "$version-$release";
67 - $info->{$key}->{'git_version_stable_4'} = $version; 61 + $info->{$package}->{'git_version_'.$branch} = $version;
68 } 62 }
69 63
70 - return $info;  
71 } 64 }
72 65
73 sub compare_versions { 66 sub compare_versions {
74 - my $info = copr_info(); 67 + copr_info('softwarepublico', 'v4', 'stable-4.x');
  68 + copr_info('softwarepublico', 'v5', 'master');
75 my $match = {}; 69 my $match = {};
76 - foreach my $key (keys %{$info}) {  
77 - if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version_master}) {  
78 - $match->{$key} = 1; 70 + foreach my $package (keys %{$info}) {
  71 + if($info->{$package}->{'v5_version'} eq $info->{$package}->{git_version_master}) {
  72 + $match->{$package} = 1;
79 } 73 }
80 else { 74 else {
81 - $match->{$key} = 0; 75 + $match->{$package} = 0;
82 } 76 }
83 } 77 }
84 78
@@ -86,19 +80,20 @@ sub compare_versions { @@ -86,19 +80,20 @@ sub compare_versions {
86 } 80 }
87 81
88 sub info2html { 82 sub info2html {
89 - my $info = copr_info(); 83 + copr_info('softwarepublico', 'v4', 'stable-4.x');
  84 + copr_info('softwarepublico', 'v5', 'master');
90 my $table_entries=""; 85 my $table_entries="";
91 - foreach my $key (keys %{$info}) { 86 + foreach my $package (keys %{$info}) {
92 my $fill_v4_row; 87 my $fill_v4_row;
93 my $fill_v5_row; 88 my $fill_v5_row;
94 - if($info->{$key}->{'v4_version'} eq $info->{$key}->{git_version_stable_4}) { 89 + if($info->{$package}->{'v4_version'} eq $info->{$package}->{'git_version_stable-4.x'}) {
95 $fill_v4_row = "success"; 90 $fill_v4_row = "success";
96 } 91 }
97 else { 92 else {
98 $fill_v4_row = "danger"; 93 $fill_v4_row = "danger";
99 } 94 }
100 95
101 - if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version_master}) { 96 + if($info->{$package}->{'v5_version'} eq $info->{$package}->{git_version_master}) {
102 $fill_v5_row = "success"; 97 $fill_v5_row = "success";
103 } 98 }
104 else { 99 else {
@@ -106,11 +101,11 @@ sub info2html { @@ -106,11 +101,11 @@ sub info2html {
106 } 101 }
107 102
108 $table_entries .= "<tr> 103 $table_entries .= "<tr>
109 - <td><b>$key</b></td>  
110 - <td>$info->{$key}->{'git_version_stable_4'}</td>  
111 - <td class=\"$fill_v4_row\">$info->{$key}->{'v4_version'}</td>  
112 - <td>$info->{$key}->{'git_version_master'}</td>  
113 - <td class=\"$fill_v5_row\">$info->{$key}->{'v5_version'}</td> 104 + <td><b>$package</b></td>
  105 + <td>$info->{$package}->{'git_version_stable-4.x'}</td>
  106 + <td class=\"$fill_v4_row\">$info->{$package}->{'v4_version'}</td>
  107 + <td>$info->{$package}->{'git_version_master'}</td>
  108 + <td class=\"$fill_v5_row\">$info->{$package}->{'v5_version'}</td>
114 </tr>"; 109 </tr>";
115 } 110 }
116 111
@@ -147,7 +142,8 @@ sub serve_html { @@ -147,7 +142,8 @@ sub serve_html {
147 }; 142 };
148 143
149 sub serve_json { 144 sub serve_json {
150 - my $info = copr_info(); 145 + copr_info('softwarepublico', 'v4', 'stable-4.x');
  146 + copr_info('softwarepublico', 'v5', 'master');
151 my $json = JSON->new->allow_nonref; 147 my $json = JSON->new->allow_nonref;
152 my $json_info = $json->encode($info); 148 my $json_info = $json->encode($info);
153 return [ 149 return [
@@ -4,7 +4,11 @@ use Test::More; @@ -4,7 +4,11 @@ use Test::More;
4 4
5 BEGIN { use_ok('CoprStatus'); } 5 BEGIN { use_ok('CoprStatus'); }
6 6
7 -my $info = CoprStatus::copr_info(); 7 +CoprStatus::copr_info('softwarepubico', 'v4', 'stable-4.x');
  8 +CoprStatus::copr_info('softwarepubico', 'v5', 'master');
  9 +
  10 +my $info = $CoprStatus::info;
  11 +
8 ok(ref($info), 'HASH'); 12 ok(ref($info), 'HASH');
9 foreach my $key (keys %{$info}) { 13 foreach my $key (keys %{$info}) {
10 ok(ref($info->{$key}), 'HASH'); 14 ok(ref($info->{$key}), 'HASH');
@@ -24,10 +28,21 @@ foreach my $key (keys %{$match}) { @@ -24,10 +28,21 @@ foreach my $key (keys %{$match}) {
24 28
25 my $table = CoprStatus::info2html(); 29 my $table = CoprStatus::info2html();
26 like($table, qr/danger|success/m); 30 like($table, qr/danger|success/m);
27 -my $html = CoprStatus::build_html(); 31 +
  32 +my $data = {
  33 + title => "SPB Copr Status",
  34 + table_entries => $table
  35 +};
  36 +
  37 +my $template = Text::Template->new(
  38 + TYPE => 'FILE',
  39 + SOURCE => 'template.html.tt'
  40 +);
  41 +
  42 +my $html = CoprStatus::build_html($data, $template);
28 like($html, qr/SPB Copr Status/m); 43 like($html, qr/SPB Copr Status/m);
29 44
30 -my $monitor_url = copr_monitor_url("foo", "bar"); 45 +my $monitor_url = CoprStatus::copr_monitor_url("foo", "bar");
31 my $test_url = "http://copr.fedoraproject.org/api/coprs/foo/bar/monitor/"; 46 my $test_url = "http://copr.fedoraproject.org/api/coprs/foo/bar/monitor/";
32 is($monitor_url, $test_url); 47 is($monitor_url, $test_url);
33 48