Commit 0bc07ad858492f8eaf50d99483fe7dcc49aafd2f

Authored by Lucas Kanashiro
1 parent c76b6f4c

Refactor copr_info subroutine

Showing 2 changed files with 37 additions and 38 deletions   Show diff stats
lib/CoprStatus.pm
... ... @@ -7,55 +7,37 @@ use LWP::UserAgent;
7 7  
8 8 $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
9 9  
  10 +# hash with repos data
  11 +our $info = {};
  12 +
10 13 sub copr_monitor_url {
11 14 my ( $user, $repo ) = @_;
12 15 return "http://copr.fedoraproject.org/api/coprs/$user/$repo/monitor/";
13 16 }
14 17  
15 18 sub copr_info {
  19 + my ( $user, $repo, $branch ) = @_;
  20 +
16 21 my $ua = LWP::UserAgent->new;
17 22 $ua->timeout(300);
18 23 $ua->env_proxy;
19 24 $ua->ssl_opts(SSL_verify_mode => 0x00);
20 25  
21   - my $result_v4 = $ua->get(copr_monitor_url("softwarepublico", "v4"));
22   - my $result_v5 = $ua->get(copr_monitor_url("softwarepublico", "v5"));
  26 + my $result = $ua->get(copr_monitor_url($user, $repo));
23 27  
24 28 my $json = JSON->new->allow_nonref;
25 29  
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 = {};
  30 + my $dec_result = $json->decode($result->decoded_content);
29 31  
30   - foreach(@{$dec_result_v4->{'packages'}}) {
  32 + foreach(@{$dec_result->{'packages'}}) {
31 33 my $package = $_->{'pkg_name'};
32 34 my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
33 35 my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'};
34   - $info->{$package}->{'v4_version'} = $version if $status eq "succeeded";
35   - }
36   -
37   - foreach(@{$dec_result_v5->{'packages'}}) {
38   - my $package = $_->{'pkg_name'};
39   - my $status = $_->{'results'}{'epel-7-x86_64'}{'status'};
40   - 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;
  36 + $info->{$package}->{$repo."_version"} = $version if $status eq "succeeded";
55 37 }
56 38  
57 39 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");
  40 + my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/$branch/src/pkg-rpm/$key/$key.spec");
59 41 my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m;
60 42 if($version =~ /%\{version\}/) {
61 43 $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m;
... ... @@ -64,14 +46,14 @@ sub copr_info {
64 46 my $release = 'no_release';
65 47 $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m;
66 48 $version = "$version-$release";
67   - $info->{$key}->{'git_version_stable_4'} = $version;
  49 + $info->{$key}->{'git_version_'.$branch} = $version;
68 50 }
69 51  
70   - return $info;
71 52 }
72 53  
73 54 sub compare_versions {
74   - my $info = copr_info();
  55 + copr_info('softwarepublico', 'v4', 'stable-4.x');
  56 + copr_info('softwarepublico', 'v5', 'master');
75 57 my $match = {};
76 58 foreach my $key (keys %{$info}) {
77 59 if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version_master}) {
... ... @@ -86,12 +68,13 @@ sub compare_versions {
86 68 }
87 69  
88 70 sub info2html {
89   - my $info = copr_info();
  71 + copr_info('softwarepublico', 'v4', 'stable-4.x');
  72 + copr_info('softwarepublico', 'v5', 'master');
90 73 my $table_entries="";
91 74 foreach my $key (keys %{$info}) {
92 75 my $fill_v4_row;
93 76 my $fill_v5_row;
94   - if($info->{$key}->{'v4_version'} eq $info->{$key}->{git_version_stable_4}) {
  77 + if($info->{$key}->{'v4_version'} eq $info->{$key}->{'git_version_stable-4.x'}) {
95 78 $fill_v4_row = "success";
96 79 }
97 80 else {
... ... @@ -107,7 +90,7 @@ sub info2html {
107 90  
108 91 $table_entries .= "<tr>
109 92 <td><b>$key</b></td>
110   - <td>$info->{$key}->{'git_version_stable_4'}</td>
  93 + <td>$info->{$key}->{'git_version_stable-4.x'}</td>
111 94 <td class=\"$fill_v4_row\">$info->{$key}->{'v4_version'}</td>
112 95 <td>$info->{$key}->{'git_version_master'}</td>
113 96 <td class=\"$fill_v5_row\">$info->{$key}->{'v5_version'}</td>
... ... @@ -147,7 +130,8 @@ sub serve_html {
147 130 };
148 131  
149 132 sub serve_json {
150   - my $info = copr_info();
  133 + copr_info('softwarepublico', 'v4', 'stable-4.x');
  134 + copr_info('softwarepublico', 'v5', 'master');
151 135 my $json = JSON->new->allow_nonref;
152 136 my $json_info = $json->encode($info);
153 137 return [
... ...
t/app.t
... ... @@ -4,7 +4,11 @@ use Test::More;
4 4  
5 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 12 ok(ref($info), 'HASH');
9 13 foreach my $key (keys %{$info}) {
10 14 ok(ref($info->{$key}), 'HASH');
... ... @@ -24,10 +28,21 @@ foreach my $key (keys %{$match}) {
24 28  
25 29 my $table = CoprStatus::info2html();
26 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 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 46 my $test_url = "http://copr.fedoraproject.org/api/coprs/foo/bar/monitor/";
32 47 is($monitor_url, $test_url);
33 48  
... ...