Commit 403d44df071ad4458b60687799d736e0a820e631
1 parent
55ad7157
Exists in
master
add Copr::Api to handle api calls
Showing
1 changed file
with
54 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,54 @@ |
1 | +package Copr::Api; | |
2 | +use strict; | |
3 | +use warnings; | |
4 | +use LWP::Simple; | |
5 | +use JSON; | |
6 | +# this package is not available on Debian | |
7 | +use RPM::VersionCompare; | |
8 | + | |
9 | +my $copr_base_url = "https://copr.fedorainfracloud.org"; | |
10 | + | |
11 | +sub get_project_id { | |
12 | + my ( $user, $repo ) = @_; | |
13 | + my $project_json = get("$copr_base_url/api_2/projects?owner=$user&name=$repo"); | |
14 | + my $json = JSON->new->allow_nonref; | |
15 | + my $project_data = $json->decode($project_json); | |
16 | + return $project_data->{projects}[0]->{project}->{id}; | |
17 | +} | |
18 | + | |
19 | +sub get_project_builds { | |
20 | + my ( $user, $repo ) = @_; | |
21 | + my $project_id = get_project_id($user, $repo); | |
22 | + my $builds_json = get("$copr_base_url/api_2/builds?limit=0&project_id=$project_id"); | |
23 | + my $json = JSON->new->allow_nonref; | |
24 | + my $builds_data = $json->decode($builds_json); | |
25 | + return @{$builds_data->{builds}}; | |
26 | +} | |
27 | + | |
28 | +# gets all latest builds of each package | |
29 | +sub get_latest_packages { | |
30 | + my ( $user, $repo ) = @_; | |
31 | + my %latest_packages; | |
32 | + my @builds = get_project_builds($user, $repo); | |
33 | + foreach my $build (@builds) { | |
34 | + next if $build->{build}->{state} ne "succeeded"; | |
35 | + my $package_name = $build->{build}->{package_name}; | |
36 | + my $package_version = $build->{build}->{package_version}; | |
37 | + my $package_submitter = $build->{build}->{submitter}; | |
38 | + if(!(defined $latest_packages{$package_name})) { | |
39 | + $latest_packages{$package_name}{version} = $package_version; | |
40 | + $latest_packages{$package_name}{submitter} = $package_submitter; | |
41 | + } | |
42 | + elsif(RPM::VersionCompare::rpmvercmp($latest_packages{$package_name}{version}, $package_version) == 1) { | |
43 | + next; | |
44 | + } | |
45 | + else { | |
46 | + $latest_packages{$package_name}{version} = $package_version; | |
47 | + $latest_packages{$package_name}{submitter} = $package_submitter; | |
48 | + } | |
49 | + } | |
50 | + | |
51 | + return %latest_packages; | |
52 | +} | |
53 | + | |
54 | +1; | ... | ... |