From 0bc07ad858492f8eaf50d99483fe7dcc49aafd2f Mon Sep 17 00:00:00 2001 From: Lucas Kanashiro Date: Wed, 6 Jan 2016 15:14:10 -0200 Subject: [PATCH] Refactor copr_info subroutine --- lib/CoprStatus.pm | 54 +++++++++++++++++++----------------------------------- t/app.t | 21 ++++++++++++++++++--- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/lib/CoprStatus.pm b/lib/CoprStatus.pm index b971037..7d5704c 100644 --- a/lib/CoprStatus.pm +++ b/lib/CoprStatus.pm @@ -7,55 +7,37 @@ use LWP::UserAgent; $ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0; +# hash with repos data +our $info = {}; + sub copr_monitor_url { my ( $user, $repo ) = @_; return "http://copr.fedoraproject.org/api/coprs/$user/$repo/monitor/"; } sub copr_info { + my ( $user, $repo, $branch ) = @_; + my $ua = LWP::UserAgent->new; $ua->timeout(300); $ua->env_proxy; $ua->ssl_opts(SSL_verify_mode => 0x00); - my $result_v4 = $ua->get(copr_monitor_url("softwarepublico", "v4")); - my $result_v5 = $ua->get(copr_monitor_url("softwarepublico", "v5")); + my $result = $ua->get(copr_monitor_url($user, $repo)); my $json = JSON->new->allow_nonref; - my $dec_result_v4 = $json->decode($result_v4->decoded_content); - my $dec_result_v5 = $json->decode($result_v5->decoded_content); - my $info = {}; + my $dec_result = $json->decode($result->decoded_content); - foreach(@{$dec_result_v4->{'packages'}}) { + foreach(@{$dec_result->{'packages'}}) { my $package = $_->{'pkg_name'}; my $status = $_->{'results'}{'epel-7-x86_64'}{'status'}; my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'}; - $info->{$package}->{'v4_version'} = $version if $status eq "succeeded"; - } - - foreach(@{$dec_result_v5->{'packages'}}) { - my $package = $_->{'pkg_name'}; - my $status = $_->{'results'}{'epel-7-x86_64'}{'status'}; - my $version = $_->{'results'}{'epel-7-x86_64'}{'pkg_version'}; - $info->{$package}->{'v5_version'} = $version if $status eq "succeeded"; - } - - foreach my $key (keys %{$info}) { - my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/master/src/pkg-rpm/$key/$key.spec"); - my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m; - if($version =~ /%\{version\}/) { - $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m; - } - - my $release = 'no_release'; - $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m; - $version = "$version-$release"; - $info->{$key}->{'git_version_master'} = $version; + $info->{$package}->{$repo."_version"} = $version if $status eq "succeeded"; } foreach my $key (keys %{$info}) { - my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/stable-4.x/src/pkg-rpm/$key/$key.spec"); + my $spec = $ua->get("https://softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/raw/$branch/src/pkg-rpm/$key/$key.spec"); my $version = $1 if $spec->decoded_content =~ /^Version:\s*([^\s]+)\s*$/m; if($version =~ /%\{version\}/) { $version = $1 if $spec->decoded_content =~ /define version\s*([^\s]+)\s*$/m; @@ -64,14 +46,14 @@ sub copr_info { my $release = 'no_release'; $release = $1 if $spec->decoded_content =~ /^Release:\s*([^\s]+)\s*$/m; $version = "$version-$release"; - $info->{$key}->{'git_version_stable_4'} = $version; + $info->{$key}->{'git_version_'.$branch} = $version; } - return $info; } sub compare_versions { - my $info = copr_info(); + copr_info('softwarepublico', 'v4', 'stable-4.x'); + copr_info('softwarepublico', 'v5', 'master'); my $match = {}; foreach my $key (keys %{$info}) { if($info->{$key}->{'v5_version'} eq $info->{$key}->{git_version_master}) { @@ -86,12 +68,13 @@ sub compare_versions { } sub info2html { - my $info = copr_info(); + copr_info('softwarepublico', 'v4', 'stable-4.x'); + copr_info('softwarepublico', 'v5', 'master'); my $table_entries=""; foreach my $key (keys %{$info}) { my $fill_v4_row; my $fill_v5_row; - if($info->{$key}->{'v4_version'} eq $info->{$key}->{git_version_stable_4}) { + if($info->{$key}->{'v4_version'} eq $info->{$key}->{'git_version_stable-4.x'}) { $fill_v4_row = "success"; } else { @@ -107,7 +90,7 @@ sub info2html { $table_entries .= " $key - $info->{$key}->{'git_version_stable_4'} + $info->{$key}->{'git_version_stable-4.x'} $info->{$key}->{'v4_version'} $info->{$key}->{'git_version_master'} $info->{$key}->{'v5_version'} @@ -147,7 +130,8 @@ sub serve_html { }; sub serve_json { - my $info = copr_info(); + copr_info('softwarepublico', 'v4', 'stable-4.x'); + copr_info('softwarepublico', 'v5', 'master'); my $json = JSON->new->allow_nonref; my $json_info = $json->encode($info); return [ diff --git a/t/app.t b/t/app.t index 55ba830..e02a661 100644 --- a/t/app.t +++ b/t/app.t @@ -4,7 +4,11 @@ use Test::More; BEGIN { use_ok('CoprStatus'); } -my $info = CoprStatus::copr_info(); +CoprStatus::copr_info('softwarepubico', 'v4', 'stable-4.x'); +CoprStatus::copr_info('softwarepubico', 'v5', 'master'); + +my $info = $CoprStatus::info; + ok(ref($info), 'HASH'); foreach my $key (keys %{$info}) { ok(ref($info->{$key}), 'HASH'); @@ -24,10 +28,21 @@ foreach my $key (keys %{$match}) { my $table = CoprStatus::info2html(); like($table, qr/danger|success/m); -my $html = CoprStatus::build_html(); + +my $data = { + title => "SPB Copr Status", + table_entries => $table +}; + +my $template = Text::Template->new( + TYPE => 'FILE', + SOURCE => 'template.html.tt' +); + +my $html = CoprStatus::build_html($data, $template); like($html, qr/SPB Copr Status/m); -my $monitor_url = copr_monitor_url("foo", "bar"); +my $monitor_url = CoprStatus::copr_monitor_url("foo", "bar"); my $test_url = "http://copr.fedoraproject.org/api/coprs/foo/bar/monitor/"; is($monitor_url, $test_url); -- libgit2 0.21.2